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

  • Thread starter Thread starter TheMathNoob
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around a programming issue in LC3, specifically regarding a program that performs a binary AND operation on user input and a test input. Participants are exploring the behavior of registers and the effects of trap instructions in the context of assembly language programming.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • One participant describes an issue where register R0 does not change as expected during the AND operation, preserving the value X7FFF.
  • Another participant suggests checking the documentation for the LC3 instruction set, particularly regarding input methods, as the current implementation may be using character input instead of integer input.
  • A participant expresses concern about unexpected behavior in LC3, noting that running certain code appears to replicate document folders, raising questions about the potential effects of assembly language on a PC.
  • In response, another participant acknowledges that assembly code can affect the operating system, citing a personal experience where buggy assembly code deleted data from a floppy disk.

Areas of Agreement / Disagreement

Participants express differing levels of familiarity with LC3 and assembly language, leading to varied suggestions and concerns. There is no consensus on the cause of the issues being experienced, and multiple viewpoints regarding the implications of assembly language execution are present.

Contextual Notes

Participants have not reached a resolution regarding the specific problem with R0 or the unexpected behavior of the code. The discussion includes assumptions about input methods and the potential risks of running assembly code without proper understanding.

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