hello i have a problem doing a homework question on LC3 assembly languge. the qu
ID: 644738 • Letter: H
Question
hello
i have a problem doing a homework question on LC3 assembly languge. the question says: write a program to add 2-digit decimal numbers, read from the consle.
i really have no idea how to start and if anybody can help me doing this programm i will be really thankful
i did this following programming and it suppose to give me a hint :
;; find the largest number and print it;;
.orig x3000
LD R0, length
LEA R1, data
largest LDR R2, R1, #0
NOT R3,R2
ADD R3, R3, #1
NEXT ADD R0, R0, #-1
BRz end
ADD R1, R1, #1
LDR R4, R1, #0
ADD R5, R3, R4
BRnz next
BR largest
end LEA R0, msg
PUTS
halt
length .FILL #6
msg .stringz "The largest is "
data .FILL X0010
.FILL x0034
.FILL x0024
.FILL x001B
.FILL X000C
.FILL X0011
.end
Explanation / Answer
Ans : Program :
; Program to add two 2 digit decimal numbers read from the console
; R1 & R2 are working registers to load 2 digit number
; R3 is first number
; R4 is second number
; R5 is the sum
; R6 is conversion offset
.orig x3600
LEA R0, MESSAGE ; print message
PUTS
; Get first number
LD R0, NEWLINE ; print PROMPT1
OUT
OUT
LEA R0, PROMPT1
PUTS
GETC ; get first character
OUT
LD R6, M30 ; convert char to hex
ADD R0, R0, R6
ADD R1, R0, R0 ; R1 = 2xR0
ADD R2, R1, #0 ; copy R1 into R2
ADD R2, R2, R2 ; R2 = 4xR0
ADD R2, R2, R2 ; R2 = 8xR0
ADD R2, R2, R1 ; R2 = 10xR0
GETC ; get second character
OUT
ADD R0, R0, R6 ; convert to hex
ADD R3, R2, R0 ; R3 = first decimal number
; Get second number
LEA R0, PROMPT2 ; get first character
PUTS
GETC
OUT
ADD R0, R0, R6 ; convert char to hex
ADD R1, R0, R0 ; R1 = 2xR0
ADD R2, R1, #0 ; copy R1 into R2
ADD R2, R2, R2 ; R2 = 4xR0
ADD R2, R2, R2 ; R2 = 8xR0
ADD R2, R2, R1 ; R2 = 10xR0
GETC ; get second character
OUT
ADD R0, R0, R6 ; convert to hex
ADD R4, R2, R0 ; R4 = first decimal number
; Add the numbers and print results
ADD R5, R4, R3 ; R5 = R3 + R4
LEA R0, SUM ; prepare to print results
PUTS
LD R4, P100 ; find 1st digit
LD R3, M100
AND R0, R0, #0
LOOP1 ADD R0, R0, #1
ADD R5, R5, R3 ; subtract 100 until negative
BRZP LOOP1
ADD R5, R5, R4
ADD R0, R0, #-1
LD R6, P30 ; convert to ascii & print
ADD R0, R0, R6
OUT
AND R0, R0, #0 ; find 2nd digit
LOOP2 ADD R0, R0, #1
ADD R5, R5, #-10 ; subtract 10 until negative
BRZP LOOP2
ADD R5, R5, #10
ADD R0, R0, #-1
LD R6, P30 ; convert to ascii & print
ADD R0, R0, R6
OUT
ADD R0, R5, R6 ; convert and print 3rd digit
OUT
LD R0, NEWLINE
OUT
HALT
MESSAGE .STRINGZ "Enter two 2-digit decimal numbers:"
NEWLINE .FILL x000A
PROMPT1 .STRINGZ " The sum of "
PROMPT2 .STRINGZ " and "
SUM .STRINGZ " is "
M30 .FILL xFFD0 ;-x30
P30 .FILL X0030 ; x30
M100 .FILL xFF9C ;-100
P100 .FILL x0064 ; 100
.END
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.