Question: So I have to write code in LC-3 that takes in user input as a binary (b1010101010101010 - needs the b at the beginning) and

So I have to write code in LC-3 that takes in user input as a binary (b1010101010101010 - needs the b at the beginning) and converts it to Hex values, so the binary in the paranthesis would be xAAAA and would be stored into Register 2. I have this code written up, but I can't find why, where, and how it's converting from binary to Hex. Please help!

____________________________________________________________________________________________________________________________________________________

.orig x3000

;-------------

; Instructions

;-------------

LD R1, SUB_R ; Load R1 with the address of the first subroutine

JSRR R1 ; Transfer control

LD R5, SUB_P ; Load R5 with the address of the second subroutine

JSRR R5 ; Transfer control

HALT

;-----------

; Local Data

;-----------

COUNT1 .FILL #16

ENDL1 .STRINGZ " "

ZERO1 .FILL #0

SUB_P .FILL x3200

SUB_R .FILL x3400

; =======================================================================

; Subroutine: Sub_P

; Parameter: R1 is a parameter

; Postcondition:

; Return Value:

.orig x3200

; =======================================================================

;-------------

; Instructions

;-------------

; BACKING UP ALL THE REGISTERS

ST R1, BACK1

ST R2, BACK2

ST R4, BACK4

ST R5, BACK5

ST R6, BACK6

ST R7, BACK7

LD R4, DEC_16 ; Load R4 with Dec_16

LD R5, DEC_4 ; Load R5 with Dec_4

LD R0, ASCII_b ; Load the b character into R0

OUT ; Print the b character

SIGN_CHECK

ADD R2, R2, #0 ; Used for the next check

BRZP POS ; If number is zero or positive jump to loop

BRN NEG ; If it's negative jump to another loop

NEG ; NEG loop

LD R0, ASCII_1 ; Load 1 into R0

OUT ; Print out 1

ADD R2,R2,R2 ; Add R2 and R2 and store into R2, left shift

ADD R4,R4,#-1 ; Decrease R4 by 1

BRZ ENDLINE ; If number gets to 0, then jump to endline loop

ADD R5,R5,#-1 ; Decrease R5 by 1

BRZ SPACE ; If zero, jump to space loop

BR SIGN_CHECK ; Now go back to beginning of loop

POS ; POS loop

LD R0, ASCII_0 ; Load 0 into R0

OUT ; Print out 0

ADD R2,R2,R2 ; Add R2 and R2 and store into R2, left shift

ADD R4,R4,#-1 ; Decrease R4 by 1

BRZ ENDLINE ; If zero then jump to this loop

ADD R5,R5,#-1 ; Decrease R5 by 1

BRZ SPACE ; If zero jump to this loop

BR SIGN_CHECK ; Jump to the end of the loop

SPACE ; SPACE loop

LD R0, ASCII_SPACE1 ; Load 1 into R0

OUT ; Print 1 to console

ADD R5,R5, #4 ; Add 4 to R5 to reset the counter

BR SIGN_CHECK ; Jump to the beginning of the loop

ENDLINE ; ENDLINE loop

LD R0, NEWLINE ; Load NEWLINE into R0

OUT ; Print a newline

; BACKUP THE REGISTERS

LD R1, BACK1

LD R2, BACK2

LD R4, BACK4

LD R5, BACK5

LD R6, BACK6

LD R7, BACK7

RET

;--------------------

; Local Data for Subs

;--------------------

BACK1 .BLKW #1

BACK2 .BLKW #1

BACK4 .BLKW #1

BACK5 .BLKW #1

BACK6 .BLKW #1

BACK7 .BLKW #1

DEC_16 .FILL #16

DEC_4 .FILL #4

ASCII_1 .FILL #49

ASCII_0 .FILL #48

ASCII_SPACE1 .FILL #32

ASCII_b .FILL x62

NEWLINE .FILL x0A

; =======================================================================

; Subroutine: Sub_R

; Parameter: R5 is a parameter

; Postcondition:

; Return Value:

.orig x3400

; =======================================================================

; BACKING UP THE REGISTERS

ST R1, BACK_1

ST R3, BACK_3

ST R4, BACK_4

ST R5, BACK_5

ST R7, BACK_7

LD R1, COUNT ; Load count into R1

LD R2, ZERO ; Load zero into R2

LD R3, NEG1 ; Load NEG1 into R3

LD R4, ASCII_B ; Load b into R4

LD R5, ASCII_SPACE ; Load a space into R5

CHECK_IF_B

GETC ; Read first character

OUT ; Print first character

ADD R0, R0, R4 ; Add R4 and R0, if input is b, then it should be zero

BRZ END_CHECK ; Check if it's zero, if it is, end the check

LEA R0, ERROR_M ; Load error message into R0

PUTS ; Print error message

BR CHECK_IF_B ; Go to beginning of loop

END_CHECK

INPUT_LOOP

GETC ; Read the first binary integer

OUT ; Print out the first binary integer

ADD R4,R0,R5 ; Add R5 and R0, if it's zero then it's a valid input

BRZ INPUT_LOOP ; Check if R4 is zero

ADD R4,R4,#-16 ; Now subtract 16 from R4

BRZ LAST_LOOP ; If it's zero, jump to the LAST_LOOP

ADD R4,R4,#-1 ; Subtract 1 from R4

BRZ LAST_LOOP ; If it's zero. jump to LAST_LOOP

LEA R0, ERROR_M ; If it's not zero, then the input was not a 1 or 0

PUTS ; Print out the error message

BR INPUT_LOOP ; Loop back to input loop

LAST_LOOP

ADD R2,R2,R2 ; Add R2 and R2 and store into R2

ADD R0,R0,R3 ; Subtract 48 in order to read as 0 or 1

ADD R2,R0,R2 ; Add R0 and R2 and store into R2

ADD R1,R1,#-1 ; Subtract 1 from counter

BRP INPUT_LOOP ; If R1 is still positive, loop through again

LD R0, ENDL ; Unless, load an endline

OUT ; Print out the endline

; Backup the Data

LD R1, BACK_1

LD R3, BACK_3

LD R4, BACK_4

LD R5, BACK_5

LD R7, BACK_7

RET

;--------------------

; Local Data for Subs

;--------------------

BACK_1 .BLKW #1

BACK_3 .BLKW #1

BACK_4 .BLKW #1

BACK_5 .BLKW #1

BACK_7 .BLKW #1

ZERO .FILL #0

COUNT .FILL #16

ENDL .FILL x0A

ASCII_SPACE .FILL #-32

NEG1 .FILL #-48

ASCII_B .FILL #-98

ERROR_M .STRINGZ " Error: Please input a valid character. "

.end

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!