Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

can someone help me withthe following lc3 program that lc3 program that takes in an input from the user, and translates that input into ether

can someone help me withthe following lc3 program that lc3 program that takes in an input from the user, and translates that input into ether a Decimal, Binary, Octal, or even a Hex.

this is my code and its almost finished

.ORIG x3000
LOOP
LEA R0, PROMPT ;Display the input prompt
TRAP x22
JSR GETDEC ;Input an unsigned (decimal) integer
ADD R0, R0, #0 ;Exit if the input integer is 0
BRZ EXIT
JSR NEWLN ;Move cursor to the start of a new line
AND R1, R1, #0 ;R1 = 10 (output base 10 DECIMAL)
ADD R1, R1, #10
JSR PRINT ;Print the integer in decimal
JSR NEWLN ;Move cursor to the start of a new line
AND R1, R1, #0 ;R1 = 2 (output base 2 BINARY)
ADD R1, R1, #2
JSR PRINT ;Print the integer in binary
JSR NEWLN ;Move cursor to the start of a new line
AND R1, R1, #0 ;R1 = 8 (output base 8 OCTAL)
ADD R1, R1, #8
JSR PRINT ;Print the integer in octal
JSR NEWLN ;Move cursor to the start of a new line
AND R1, R1, #0 ;R1 = 16 (output base 16 HEXADECIMAL)
ADD R1, R1, #15
ADD R1, R1, #1
JSR PRINT ;Print the integer in hexadecimal
JSR NEWLN ;Move cursor to the start of a new line
BRNZP LOOP
EXIT ;Loop exit point
TRAP x25 ;HALT program execution
PROMPT .STRINGZ "Enter an unsigned integer (0 to quit)>"
;Subroutine NEWLN*************************************************************
;Advances the console cursor to the start of a new line
NEWLN
ST R7, NEW7 ;Save working registers
ST R0, NEW0
LD R0, NL ;Output a newline character
TRAP x21
LD R0, NEW0 ;Restore working registers
LD R7, NEW7
RET ;Return
;Data
NL .FILL x000A ;Newline character x0A
NEW0 .BLKW 1 ;Save area - R0
NEW7 .BLKW 1 ;Save area - R7
;Subroutine GETDEC************************************************************
;Inputs an unsigned integer typed at the console in decimal format
;The input value is returned in R0
GETDEC
ST R7, GET7 ;Save the working Registers
ST R6, GET6 ;Save the working Registers
ST R5, GET5 ;Save the working Registers
ST R4, GET4 ;Save the working Registers
ST R3, GET3 ;Save the working Registers
ST R2, GET2 ;Save the working Registers
ST R1, GET1 ;Save the working Registers
;ST R0, GET0 Save the working Registers (Taken out)
AND R7 R7 0 ; clear everything
AND R6 R6 0 ; clear everything
AND R5 R5 0 ; clear everything
AND R4 R4 0 ; clear everything
AND R3 R3 0 ; clear everything
AND R1 R1 0 ; clear everything
AND R0 R0 0 ; clear everything
LD R3 28 ; LOADS R3 WITH x0030
NOT R3 R3
ADD R3 R3 1 ; R3 IS NOW -30
AND R2 R2 0 ; clear everything
ADD R2 R2 -10 ; asci code for enter is x0A so i negated it
BRNZP 5 ; do while loop
ADD R4 R4 R4 ; starting to add the number to itself 4X
ADD R7 R4 0 ; Stores the previous value into R7
ADD R4 R4 R4 ; Adds R4 with itself again
ADD R4 R4 R4 ; ADDS R4 Withitself again
ADD R4 R7 R4 ; Adds the value from R4 with the value in R7
TRAP x20 ; Reads a single character but no echo
ADD R5 R2 R0 ; CHECKS TO SEE IF THE KEYBOARD INPUT IS X0A
BRZ 5 ; BRANCHES IF THE RESULT IS A ZERO
ADD R0 R3 R0 ; RO CONTAINS WHATEVER NUMBER MINUS 30
ADD R0 R0 R4 ; Adds c
ADD R4 R0 0 ; ADDS R0 to R4
ADD R1 R0 0 ; Copies the value into R1
BRNZP -13 ; Branches up to multiply R4 10x
AND R0 R0 0 ; Clears R0
ADD R0 R1 0 ; Adds R4 into R0
LD R7, GET7 ; Loads back up the registers
LD R6, GET6
LD R5, GET5
LD R4, GET4
LD R3, GET3
LD R2, GET2
LD R1, GET1
RET ;Return
;Data
.FILL x0030
GET7 .BLKW 1 ;Save Area - R7
GET6 .BLKW 1 ;Save Area - R6
GET5 .BLKW 1 ;Save Area - R5
GET4 .BLKW 1 ;Save Area - R4
GET3 .BLKW 1 ;Save Area - R3
GET2 .BLKW 1 ;Save Area - R2
GET1 .BLKW 1 ;Save Area - R1
;Subroutine PRINT*************************************************************
;Displays an unsigned integer in any base up to 16, e.g. binary, octal, decimal
;Parameters - R0: the integer - R1: the base
PRINT
ST R7, PRT7 ;Save the working Registers
ST R6, PRT6 ;Save the working Registers
ST R5, PRT5 ;Save the working Registers
ST R4, PRT4 ;Save the working Registers
ST R3, PRT3 ;Save the working Registers
ST R2, PRT2 ;Save the working Registers
AND R7 R7 0 ; Clears the register
AND R6 R6 0 ; Clears the register
AND R5 R5 0 ; Clears the register
AND R4 R4 0 ; Clears the register
AND R3 R3 0 ; Clears the register
AND R2 R2 0 ; Clears the register
; save current value
ST R0 CURRENT ; Load
;checker for decimal
LEA R6 STORE ; Loads R5 with area we will
LEA R7 ASCI ; Loads R7 with x0030
LDR R4 R7 0 ; Gets the value inside of the memory location
ADD R2 R2 -15 ; Checker
ADD R2 R2 -1 ; makes it -16
ADD R2 R1 R2 ; checks if the value is zero
BRZ HEX ; offsets to the next place
AND R2 R2 0 ; Clears R2,
BRNZP 3 ; skips the first part
AND R5 R5 0 ; Clears R5
ADD R5 R5 R2 ; Checks to see if R0 is empty
BRZ DISPLAY ; offsets to the end of program
JSR DIVIDE ; Divides the current value
ADD R3 R3 R4 ; Adds the value with R7
STR R3 R6 0 ; Stores the content of R6 into that memo location
ADD R6 R6 1 ; increments the memory location of R6\
ADD R0 R2 0 ; Stores the new value from R2 into R0
BRNZP -9 ; loop the program
HEX
; checker for Hex
; Still working on it
AND R2 R2 0 ; Clears r2
LEA R5 DIGITS ; Gets the first value of that location
BRNZP 2
JSR DIVIDE ; Divides the current value
ADD R6 R3 0 ; Checks if the value is zero
BRZ END ; branches to the end of the program
ADD R3 R5 R3 ; ADDS the memory location of that item
LDR R0 R3 0 ;
TRAP x21 ; Prints out the values
AND R3 R3 0 ; clears r3
BRNZP -8 ; offsets
DISPLAY
AND R7 R7 0
AND R5 R5 0
ADD R6 R6 -1
LDR R5 R6 0 ; Loads the content of R6 into R5
ADD R7 R5 0 ; Check to see if the content at R6 is zero
BRZ 4 ; Offsets to the end of the program
ADD R0 R5 0 ; Adds the item into this spot
TRAP x21 ; Out puts the char to the console
ADD R6 R6 -1 ; decrements the location
BRNZP -7 ; loops the program
END
AND R0 R0 0 ; Clears R0
LD R0 CURRENT ; loads RO with the mmemory location of current val
LD R7, PRT7
LD R6, PRT6
LD R5, PRT5
LD R4, PRT4
LD R3, PRT3
LD R2, PRT2
RET ;Return
;Data
CURRENT .FILL x0000 ; place to store the current value
PRT7 .BLKW 1 ;Save Area R7
PRT6 .BLKW 1 ;Save Area R6
PRT5 .BLKW 1 ;Save Area R5
PRT4 .BLKW 1 ;Save Area R4
PRT3 .BLKW 1 ;Save Area R3
PRT2 .BLKW 1 ;Save Area R2
ASCI .FILL x0030 ; Saves the asci addition
DIGITS .STRINGZ "0123456789ABCDEF" ;Digits
BUFFER .FILL x0000 ;0-marker for the end of the buffer
STORE .BLKW 18 ;Output Buffer
;Subroutine DIVIDE************************************************************
;Perform integer division to obtain Quotient and Remainder
;Parameters (IN) : R0 - Numerator, R1 - Divisor
;Parameters (OUT) : R2 - Quotient, R3 - Remainder
DIVIDE
ST R7, DIV7 ;Save the working Registers
ST R6, DIV6 ;Save the working Registers
ST R5, DIV5 ;Save the working Registers
ST R4, DIV4 ;Save the working Registers
AND R7 R7 0 ;Clears R7
AND R6 R6 0 ;Clears R6
AND R5 R5 0 ;Clears R5
AND R4 R4 0 ;Clears R4
AND R3 R3 0 ;Clears R3
AND R2 R2 0 ;Clears R2
ADD R3 R0 0 ; copies the numerator into the remainder
ADD R5 R1 0 ; Copies the value in divisor over into R5
NOT R5 R5 ; negates R5
ADD R5 R5 1 ; Adds 1 to R5
ADD R6 R3 R5
; checks to see if the remainder when subtracted by the divisor
; if that value is still greater than or equal to the divisor
BRN 3 ; exits the loop
ADD R2 R2 1 ; increment the value in R2
ADD R3 R3 R5 ; Minus the diviser from the remainder
BRNZP -5 ; ofsets the program to loop over
LD R7, DIV7 ; loads back up registers
LD R6, DIV6 ; loads back up registers
LD R5, DIV5 ; loads back up registers
LD R4, DIV4 ; loads back up registers
RET ;Return
;Data
DIV7 .BLKW 1 ; Save area R7
DIV6 .BLKW 1 ; Save area R6
DIV5 .BLKW 1 ; Save area R5
DIV4 .BLKW 1 ; Save area R4

.END

so far my code works to translate into decimal binary and octal, however im having trouble translating to hex. could someone please review my code and help make the appropriate changes so that the hex conversion works please

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing Fundamentals Design

Authors: Marion Donnie Dutton Don F. Seaman

14th Edition Globel Edition

1292107634, 978-1292107639

More Books

Students also viewed these Databases questions

Question

Explain the concept of shear force and bending moment in beams.

Answered: 1 week ago