HELP WITH THIS LC3 PROGRAM. HOW TO DO FOLLOWING BELOW IN LC3 CODE? Read in the ‘
ID: 3794701 • Letter: H
Question
HELP WITH THIS LC3 PROGRAM. HOW TO DO FOLLOWING BELOW IN LC3 CODE?
Read in the ‘+’ or ‘-‘. If the character is a ‘-‘, remember to make the final result negative (i.e. take the 2’s complement of R5 at the end). If the result is positive then ... don’t.
Convert the string of characters input by the user into the binary number they represent (see examples). To do this, you can follow this algorithm:
Initialize R5 to 0 (DO NOT do this by LD'ing a 0 from memory! There is a much simpler & faster way!)
Convert each digit to binary as it is typed in, and add it to R5; if another digit is entered, multiply R5 by 10, and repeat. Stop when you detect the ENTER (x0A):
For example, if the user types ‘2’, then R5 will contain #2 == b0000 0000 0000 0010
If the user then types a ‘3’, making the string now read “23”, then R5 will contain 2 x 10 + 3 == #23 == b0000 0000 0001 0111
If the user then types ‘4’, making the string read “234”, then R5 will contain 23 x 10 + 4 == #234 == b0000 0000 1110 1010 You must also perform input character validation with this assignment – i.e. reject any non-numeric input character. That is, if the user enters “+23g”, your program should "choke" on the ‘g’, print an error message (see sample output), and start over at the beginning with the initial prompt. However, you do not have to detect overflow in this assignment – we will only test your code with inputs in the range [-32768, +32767].
Expected/ Sample output Output
Prompt
Input a positive or negative decimal number (max 5 digits), followed by ENTER
Newline terminated
Error Message
ERROR INVALID INPUT
Newline terminated Example If the user enters “+7246”, your program should read the ‘+’, ‘7’, ‘2’, ‘4’, ‘6’ and end up with the value b0001 1100 0100 1110 in R5 (which corresponds to the number #7246, or x1C4E). If the users enters “-14237”, your program should read the ‘-‘, ‘1’, ‘4’, ‘2’, ‘3’, ‘7’ and end up with the value #-14237 == xC863 == b11001000 01100011 in R5.
Note: You must echo the digits as they are input (no "ghost writing").
You do not have to output the converted binary number. It should “simply” be sitting happily in R5, where you can check it in simpl.
What should happen when an error occurs?
Output "ERROR INVALID INPUT" and start over, prompting the user for input
Other Errors (output "ERROR INVALID INPUT" and start over):
Nothing entered before ENTER
only sign is entered before ENTER
first character entered is neither a sign nor a digit
REMEMBER: all outputs must be newline terminated
Explanation / Answer
The code is as below:
.ORIG x3000
LEA R0, WELCOME ;Load WELCOME Pointer
PUTS ;Print WELCOME Pointer
LOOP LEA R0, RECUR ;Load RECUR Pointer
PUTS ;Print RECUR Pointer
AND R0, R0, 0 ;CHAR STORE: R0 = 0
AND R1, R1, 0 ;NEGATIVE: R1 = 0
AND R2, R2, 0 ;TEMP VAR: R2 = 0
AND R3, R3, 0 ;CHAR COUNT: R3 = 0
AND R4, R4, 0 ;TEMP VAR: R4 = 0
AND R5, R5, 0 ;TEMP VAR: R5 = 0
AND R6, R6, 0 ;SUM OF INPUT: R6 = 0
AND R7, R7, 0 ;TEMP VAR: R7 = 0
TAKE GETC ;Print the welcome message.
OUT ;Print CHAR IN R0
RESETVARS
AND R2, R2, 0 ;Reset TEMPS
AND R5, R5, 0 ;Reset TEMPS
AND R4, R4, 0 ;Reset TEMPS
AND R7, R7, 0 ;Reset TEMPS
CHECKFIRST
ADD R3, R3, 1 ;Increment CHAR count
ADD R4, R3, -1 ;Subtract ONE from R3 and store in R4
BRz FIRST ;Go to FIRST CHAR special
AFTERFIRST
ADD R2, R2, R0 ;R2 = R0 (COPY CHAR TO R2)
ADD R2, R2, -10 ;R2 -= 10 (SEE IF R2 CHAR IS NEWLINE CHAR)
BRz DONE ;if NEWLINE then stop.
CHECKEND
AND R2, R2, 0 ;Reset R2
ADD R2, R2, R0 ;R2 = R0 (copy CHAR to R2)
ADD R2, R2, -16 ;R2 -= 16
ADD R2, R2, -16 ;R2 -= 16
ADD R2, R2, -16 ;R2 -= 16
ADD R2, R2, -16 ;R2 -= 16
ADD R2, R2, -16 ;R2 -= 16
ADD R2, R2, -8 ;R2 -= 8
BRz GOODBYE
GETDIGIT
ADD R7, R7, R0 ;copy CHAR into R7 = R0
ADD R7, R7, -16 ;R7 -= 16
ADD R7, R7, -16 ;R7 -= 16
ADD R7, R7, -16 ;R7 -= 16
BRnzp SETUPMULTIPLY ;SUM * 10 + DIGIT
AFTERMULTIPLY
ADD R6, R6, R7 ;SUM += R7 (digit)
BRnzp TAKE ;Go back to get Next characters
FIRST
ADD R5, R5, R0 ;Store TEMP CHAR IN R5 = R2
ADD R5, R5, -16 ;R5 =- 16
ADD R5, R5, -16 ;R5 =- 16
ADD R5, R5, -13 ;R5 =- 13
BRz NEGATIVE ;Goto NEGATIVE
BRnzp AFTERFIRST ;Goto AFTERFIRST
NEGATIVE
ADD R1, R1, 1 ;Set NEGATIVE to TRUE
BRnzp TAKE ;Goto AFTERFIRST
FLIP
NOT R6, R6 ;Invert SUM
ADD R6, R6, 1 ;Add one to INVERTED SUM
BRnzp AFTERFLIP ;GO BACK TO AFTERFLIP
SETUPMULTIPLY
AND R4, R4, 0 ;Reset TEMPS
AND R2, R2, 0 ;Reset TEMPS
ADD R2, R2, R6 ;set R2 = R6 TEMP
ADD R4, R4, 9 ;COUNTER R4 = 9 (LOOP 10 TIMES)
BRnzp MULTIPLY ;goto MULTIPLY
MULTIPLY
ADD R6, R6, R2 ;Reset TEMPS
ADD R4, R4, -1 ;Decrement our counter
BRp MULTIPLY ;Continue until the 2nd num is 0
BRnzp AFTERMULTIPLY ;Goto AFTERMULTIPLY
DONE
LEA R0, SUCCESS ;Load SUCCESS Pointer
PUTS ;Print SUCCESS Pointer
ADD R1, R1, 0 ;GET R1 INTO CC (CONDITION CODE)
BRp FLIP ;IF NEGATIVE FLIP NUMBER
AFTERFLIP
AND R2, R2, 0 ;Reset TEMPS
AND R4, R4, 0 ;Reset TEMPS
AND R3, R3, 0 ;Reset TEMPS
MASKLOOP
LEA R4, MASK ;load ADDRESS OF first BINARY: NUM 1000 0000 0000 0000
ADD R4, R4, R2 ;shift Pointer TO THE BASE + COUNT (R4 = R4 + R2)
LDR R4, R4, 0 ;load VALUE AT ADDRESS R4 = (BASE + COUNTER)
AND R5, R5, 0 ;Reset TEMPS
AND R0, R0, 0 ;Reset TEMPS
AND R5, R4, R6 ;SUM AND MASK
BRnp PRINTONE ;Print BINARY ONE
BRz PRINTZERO ;Print BINARY ZERO
AFTERPRINT
ADD R2, R2, 1 ;Add ONE TO COUNTER
ADD R3, R2, -16 ;Put LOOP CONDITION IN CONDITION CODE
BRn MASKLOOP ;If still in LOOP goback to Top of the loop.
AND R0, R0, 0 ;Reset R0
ADD R0, R0, 10 ;R0 = 10
OUT ;Print CHAR IN R0
BRzp LOOP ;Goback to OUTERMOST LOOP (RESTART PROGRAM)
PRINTONE
AND R0, R0, 0 ;Print ONE IN BINARY
ADD R0, R0, 15 ;R0 += 15
ADD R0, R0, 15 ;R0 += 15
ADD R0, R0, 15 ;R0 += 15
ADD R0, R0, 4 ;R0 += 3
OUT ;Print CHAR IN R0
BRnzp AFTERPRINT ;GO BACK TO AFTERPRINT
PRINTZERO
AND R0, R0, 0 ;Print ZERO IN BINARY
ADD R0, R0, 15 ;R0 += 15
ADD R0, R0, 15 ;R0 += 15
ADD R0, R0, 15 ;R0 += 15
ADD R0, R0, 3 ;R0 += 3
OUT ;Print CHAR IN R0
BRnzp AFTERPRINT ;Goback to AFTERPRINT
GOODBYE
LEA R0, LEAVE ;Load LEAVE Pointer
PUTS ;Print LEAVE Pointer
BRnzp ENDPROGRAM
ENDPROGRAM
HALT ;Close the Program
WELCOME .STRINGZ "Welcome to the conversion program "
RECUR .STRINGZ "Enter a decimal number or X to quit: "
SUCCESS .STRINGZ "The corresponding Binary number is "
LEAVE .STRINGZ " Thank You. "
MASK .FILL x8000
.FILL x4000
.FILL x2000
.FILL x1000
.FILL x0800
.FILL x0400
.FILL x0200
.FILL x0100
.FILL x0080
.FILL x0040
.FILL x0020
.FILL x0010
.FILL x0008
.FILL x0004
.FILL x0002
.FILL x0001
.END
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.