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

  • Thread starter TheMathNoob
  • Start date
In summary, LC3 is a computer architecture commonly used for educational purposes to teach low-level programming concepts. Programs in LC3 are written in assembly language and translated into binary code for execution. Binary conversion is important because computers only understand binary code, and in an LC3 program, it can be performed using the instruction set or built-in conversion functions. To display user input on the screen in an LC3 program, the TRAP x20 (GETC) and TRAP x21 (OUT) functions can be used.
  • #1
TheMathNoob
189
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
  • #2
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
 
  • #3
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?
 
  • #4
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.
 

1. What is LC3 and how is it used?

LC3 stands for "Little Computer 3" and is a type of computer architecture used in the design and development of computer systems. It is commonly used for educational purposes to teach low-level programming concepts.

2. What is a program and how does it relate to LC3?

A program is a set of instructions that tells a computer what to do. In the context of LC3, a program is written in assembly language and translated into binary code, which is then executed by the LC3 processor.

3. What is binary conversion and why is it important?

Binary conversion is the process of converting data from one number system to another, specifically from decimal (base 10) to binary (base 2). It is important in programming because computers only understand binary code, so all data and instructions must be converted into binary before being executed.

4. How do I perform binary conversion in an LC3 program?

To perform binary conversion in an LC3 program, you can use the LC3 instruction set, which includes commands for arithmetic operations, logical operations, and data manipulation. You can also use the built-in conversion functions such as TRAP x21 (OUT) to display the converted binary number on the screen.

5. How can I display user input on the screen in an LC3 program?

To display user input on the screen in an LC3 program, you can use the TRAP x20 (GETC) function to read a character input from the user and store it in a register. Then, you can use the TRAP x21 (OUT) function to display the input character on the screen. This process can be repeated to display multiple characters or a string of characters on the screen.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
9K
  • Programming and Computer Science
Replies
4
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top