For LC-3 I need to ask the user to input two numbers and add them, then print ou
ID: 653787 • Letter: F
Question
For LC-3 I need to ask the user to input two numbers and add them, then print out the sum. Can you tell me where I need to put the code to add and how to do it. Is it ADD R2,R1,R0?
; This program echoes a user's input.
; The code will begin in memory at the address
; specified by .orig .
.ORIG x3000
START:
; clear all registers that we may use
AND R0, R0, 0
AND R1, R0, 0
AND R2, R0, 0
AND R3, R0, 0
AND R4, R0, 0
AND R5, R0, 0
AND R6, R0, 0
AND R7, R0, 0
; print out a greeting
LEA R0, GREETING1
PUTS
; get a user-entered character (result in R0)
; echo it back right away (otherwise it isn't visible)
GETC
PUTC
; store entered string (otherwise it may be overwritten)
ST R0, USERINPUT
; print out a newline and some other stuff
LEA R0, NEWLINE1
PUTS
; print out the user's input
LD R0, USERINPUT
PUTC
; print out the rest of the sentance
LEA R0, USERSTRING1
PUTS
; print out a greeting
LEA R1, GREETING2
PUTS
; get a user-entered character (result in R1)
; echo it back right away (otherwise it isn't visible)
GETC
PUTC
; store entered string (otherwise it may be overwritten)
ST R1, USERINPUT
; print out a newline and some other stuff
LEA R1, NEWLINE2
PUTS
; print out the user's input
LD R1, USERINPUT
PUTC
; print out the rest of the sentance
LEA R1, USERSTRING2
PUTS
;ADD
ADD R2,R1,R0
PUTS
; echo it back right away (otherwise it isn't visible)
GETC
PUTC
; stop the processor
HALT
; data declarations follow below
; strings
GREETING1: .STRINGZ "Welcome to the sample program. Please Enter the first number: "
USERSTRING1: .STRINGZ " <-- is the character you entered. "
NEWLINE1: .STRINGZ " --> ";
GREETING2: .STRINGZ "Enter the second number. "
USERSTRING2: .STRINGZ " <-- is the character you entered. "
NEWLINE2: .STRINGZ " --> ";
; variables
USERINPUT: .FILL 0
; end of code
.END
Explanation / Answer
.model small .stack 100h .data prompt db 13, 10, 'First number:','$' prompt db 13,10, 'Second number:', '$' result db 13, 10, 'Sum','$' ;Variables num1 db ? num2 db ? sum db ? .code main proc mov ax,@data ;get data segment address mov ds,ax ;initialize ds ;Display Prompt mov ah,9 ;print string function mov dx,offset prompt ;ds:dx points to string int 21h mov ah,1 ;input function int 21h xor ah,ah ;clear the top 8 bits of ax mov num1, ax ;save the value from input mov ah,9 lea dx, prompt2 ;print prompt int 21h mov ah,2 ;input second function int 21h xor ah, ah ;clear the top 8 bits of ax mov num2, ax ;save the value from second input ;Addition mov ax,num1 ;move num1 into ax add ax,num2 ;add first and second numbers together mov sum,ax ;move the total sum of numbers in sum ;Print Sum mov ah,9 lea dx, result ; print result int 21h mov ah,2 mov dl,bl int 21h mov dl,'+' ;display + sign int 21h mov dl,bh int 21h mov dl,'=' ;display = sign int 21h mov dl,bh int 21h mov ah,4ch int 21h main endp end main
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.