LC3 AND Program: Perform Binary Conversion and Display User Input on Screen

  • Thread starter Thread starter TheMathNoob
  • Start date Start date
Click For Summary
The discussion revolves around a user attempting to write an LC3 program that performs a binary AND operation on user input but is facing issues with register R0 not changing as expected. The user is trying to AND R0 with a specific value to display the input in binary, yet R0 retains the value X7FFF. Suggestions include checking the LC3 instruction set documentation for proper input handling, as the user may be using character input instead of integer input. Additionally, concerns are raised about the potential for assembly code to affect the computer's file system if not executed correctly, with an anecdote about past experiences with buggy assembly code. The conversation highlights the importance of understanding assembly language operations and their implications on system behavior.
TheMathNoob
Messages
189
Reaction score
4
Mod note: Edited the post to add code tags, and moved the code to the attempt section.

Homework Statement


Hi I am trying to write a program in LC3 which performs the operation AND on the input given by the user and the test input highlighted on the next code . The problem that I am having is that the register R0 doesn't seem to change in the console. I try to AND R0 with 0 to see if it reacts and still doesn't work. It preserves the value X7FFF. The reason why I am ANDing the input given by the user and values like this 1000 0000 0000 0000 is because I am trying to display on the screen the number given by the user in binary.

Homework Equations


The Attempt at a Solution


Code:
.ORIG x3000
LD R0,ZERO
LD R2,ZERO ;INT
LD R3,ZERO  ;INT
LD R4,ZERO  ;DIGIT
LD R5,ZERO ;LF
LD R6,FORTYFIVE ;45 48 AND COUNTER

LOOP TRAP x20
     TRAP x21
     AND R5,R5,#0
     ADD R5,R0,xFFF6
     BRz NEXT3
     ADD R3,R0,R6
     BRnp NEXT
     AND R3,R3,#0
     ADD R3,R3,#1
     BRnzp LOOP

NEXT AND R6,R6,#0
     LD R6,FORTYEIGHT
     ADD R4,R0,R6
     AND R5,R5,#0
     ADD R5,R5,R2
     AND R6,R6,#0
     ADD R6,R6,#9

DIGIT BRz NEXT2
      ADD R2,R2,R5
      ADD R6,R6,#-1
      BRnzp DIGIT

NEXT2 ADD R2,R2,R4
      BRnzp LOOP

NEXT3 AND R4,R4,#0
      AND R6,R6,#0
      LD R4,x0050   ------>Test Input
      LD R6,FORTYEIGHT_
      AND R0,R2,R4
      BRnp GO
      AND R0,R0,#0
      LD R0,ZERO
      ADD R0,R0,R6
GO    AND R0,R0,#0
      LD R0,ZERO
      ADD R6,R6,#1
      ADD R0,R0,R6
       OUT
HALT
ENTER .STRINGZ "ENTER A NUMBER OR PRESS X TO EXIT: "
ZERO .FILL #0
NINE .FILL #9
FORTYFIVE .FILL #-45
FORTYEIGHT .FILL #-48
FORTYEIGHT_ .FILL #48
.END
 
Last edited by a moderator:
Physics news on Phys.org
TheMathNoob said:
Mod note: Edited the post to add code tags, and moved the code to the attempt section.

Homework Statement


Hi I am trying to write a program in LC3 which performs the operation AND on the input given by the user and the test input highlighted on the next code . The problem that I am having is that the register R0 doesn't seem to change in the console. I try to AND R0 with 0 to see if it reacts and still doesn't work. It preserves the value X7FFF. The reason why I am ANDing the input given by the user and values like this 1000 0000 0000 0000 is because I am trying to display on the screen the number given by the user in binary.
I've never heard of LC3, but I have pretty good knowledge of several kinds of assembly language.

Is the input being done in one of the trap instructions? The best advice I can give you is to look at the documentation for the LC3 instruction set, particularly the instructions that are used for input. Possibly the instruction you're using is character input instead of integer input. It would be helpful if you can provide a link to the instruction set.
TheMathNoob said:

Homework Equations


The Attempt at a Solution


Code:
.ORIG x3000
LD R0,ZERO
LD R2,ZERO ;INT
LD R3,ZERO  ;INT
LD R4,ZERO  ;DIGIT
LD R5,ZERO ;LF
LD R6,FORTYFIVE ;45 48 AND COUNTER

LOOP TRAP x20
     TRAP x21
     AND R5,R5,#0
     ADD R5,R0,xFFF6
     BRz NEXT3
     ADD R3,R0,R6
     BRnp NEXT
     AND R3,R3,#0
     ADD R3,R3,#1
     BRnzp LOOP

NEXT AND R6,R6,#0
     LD R6,FORTYEIGHT
     ADD R4,R0,R6
     AND R5,R5,#0
     ADD R5,R5,R2
     AND R6,R6,#0
     ADD R6,R6,#9

DIGIT BRz NEXT2
      ADD R2,R2,R5
      ADD R6,R6,#-1
      BRnzp DIGIT

NEXT2 ADD R2,R2,R4
      BRnzp LOOP

NEXT3 AND R4,R4,#0
      AND R6,R6,#0
      LD R4,x0050   ------>Test Input
      LD R6,FORTYEIGHT_
      AND R0,R2,R4
      BRnp GO
      AND R0,R0,#0
      LD R0,ZERO
      ADD R0,R0,R6
GO    AND R0,R0,#0
      LD R0,ZERO
      ADD R6,R6,#1
      ADD R0,R0,R6
       OUT
HALT
ENTER .STRINGZ "ENTER A NUMBER OR PRESS X TO EXIT: "
ZERO .FILL #0
NINE .FILL #9
FORTYFIVE .FILL #-45
FORTYEIGHT .FILL #-48
FORTYEIGHT_ .FILL #48
.END
 
Mark44 said:
I've never heard of LC3, but I have pretty good knowledge of several kinds of assembly language.

Is the input being done in one of the trap instructions? The best advice I can give you is to look at the documentation for the LC3 instruction set, particularly the instructions that are used for input. Possibly the instruction you're using is character input instead of integer input. It would be helpful if you can provide a link to the instruction set.
Hello, thanks!, now I am having a weird problem. It looks like when I run a certain code in LC3, it replicates document folders in a mysterious way. Can assembly language code affect my pc if I don't use it properly?
 
TheMathNoob said:
Hello, thanks!, now I am having a weird problem. It looks like when I run a certain code in LC3, it replicates document folders in a mysterious way. Can assembly language code affect my pc if I don't use it properly?
It can if one of the trap instructions calls into the operating system to create a new directory.

I wrote some x86 assembly code years ago that was buggy. Instead of doing what I intended, my code deleted the contents of a floppy disk. Luckily it was only a floppy disk that was affected, not the hard drive.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
Replies
10
Views
10K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
1
Views
3K