In this project, you will implement operations to input and output multi-digit n
ID: 3686492 • Letter: I
Question
In this project, you will implement operations to input and output multi-digit numbers. Since the LC-3 is a 16-bit word computer, the range of integers handled will be -215 to (215 - 1), i.e, from -32768 to +32767. To handle these input and output operations, you will also need to implement the multiply and divide arithmetic operations which are not available on LC-3.
The project entails:
Implementing multiply (Mult) and division (Div) subroutines
Implementing an input (Input) subroutine to read numbers in the range
-32768 to 32767.
Implementing an output (Output) subroutine to display numbers in the range
-32768 to 32767.
Testing. Write a program that uses these operations.
Prompts for and reads the numbers 7276 and 2948.
Converts these numbers to integers internally. Displays them on the console.
Adds these numbers and displays the sum
Subtracts 2948 from 7276 and displays the difference
Deliverables:
Submit the .asm files and a screen shot of the outputs
Calling convention and passing parameters
Make sure the program is properly documented. Every subroutine must document in the header the inputs and outputs – registers used for input and output, and the algorithm used. Document the name of the Author of the subroutine.
By convention, R0 returns the output to the caller. R7 is the linkage register. When calling a subroutine, use registers R1 and R2 (if needed) to pass inputs to the subroutine.
Make sure to save and restore registers in the called subroutine
Since we have not implemented a stack, DO NOT use nested calls.
Explanation / Answer
multi-digit numbers Multiplying multiple digit numbers. # Given positive integers a and b, output a/b and a%b. .data str1: .asciiz "Enter a: " str2: .asciiz "Enter b: " str3: .asciiz "a/b = " str4: .asciiz "a%b = " str5: .asciiz "a*b = " newline: .asciiz " " .text main: li $v0, 4 # system call code for print_string la $a0, str1 # address of str1 syscall # print str1 #get the first number from user, put it into $s0 li $v0, 5 # system call code for read_int syscall # read an integer into $v0 from console add $s0, $v0, $zero # copy $v0 into $s0 (a) #read print_string for str2 li $v0, 4 # system call code for print_string la $a0, str2 # address of str1 syscall # print str1 # get second number from user, put it into $t1 li $v0, 5 #load syscall for read_int syscall #make the syscall move $s1, $v0 #move the number read into $s1(b) #DO THE CALCULATIONS................................................ div $s0, $s1 #diving $s0 by $s1 mflo $t0 #storing value of lo(quotient) in #register $t0 mfhi $t1 #storing value of hi(remainder) in #register $t1 mult $s0, $s1 mflo $t2 li $v0,1 move $a0, $t2 syscall li $v0,4 la $a0, str5 syscall #read print_string for str3 li $v0, 4 # system call code for print_string la $a0, str3 # address of str1 syscall # print str1 #print a/b li $v0, 1 #load syscall print_int into $v0 move $a0, $t0 #move the number to print into $t2 syscall # read print string for str4 li $v0, 4 la $a0, str4 syscall # print remainder li $v0, 1 move $a0, $t1 syscall #end of program li $v0, 10 #system call code for exit syscall PUSH BX ; push BX onto the STACK PUSH CX ; push CX onto the STACK PUSH DX ; push DX onto the STACK CMP AX, 0 ; compare AX with 0 JGE @START ; jump to label @START if AX>=0 PUSH AX ; push AX onto the STACK MOV AH, 2 ; set output function MOV DL, "-" ; set DL='-' INT 21H ; print the character POP AX ; pop a value from STACK into AX NEG AX ; take 2's complement of AX @START: ; jump label XOR CX, CX ; clear CX MOV BX, 10 ; set BX=10 @OUTPUT: ; loop label XOR DX, DX ; clear DX DIV BX ; divide AX by BX PUSH DX ; push DX onto the STACK INC CX ; increment CX OR AX, AX ; take OR of Ax with AX JNE @OUTPUT ; jump to label @OUTPUT if ZF=0 MOV AH, 2 ; set output function @DISPLAY: ; loop label POP DX ; pop a value from STACK to DX OR DL, 30H ; convert decimal to ascii code INT 21H ; print a character LOOP @DISPLAY ; jump to label @DISPLAY if CX!=0 POP DX ; pop a value from STACK into DX POP CX ; pop a value from STACK into CX POP BX ; pop a value from STACK into BX RET ; return control to the calling procedure OUTDEC ENDP
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.