In: Computer Science
Part (c): An implementation of division
You are familiar with integer division operations. In some processors there is no
division instruction. One way to implement division is via repeated subtractions.
NB: This is integer division, NOT floating point division.
For example, consider the expression M / N where M = 370 and N = 120 . If we
repeatedly subtract until we have a value less than N , then the number of times we
do the subtraction is the result. Ie:
● 370 – 120 => 250 (subtraction #1)
● 250 – 120 => 130 (subtraction #2)
● 130 – 120 => 10 (subtraction #3)
When we have the result 10 , we have performed 3 subtractions. Therefore, 370 /
120 = 3
Your task for part (a) is to complete the code in division.asm that has been
provided for you. In that file there are some comments giving several simplifying
assumptions.
For example, the value M will always be a positive two’s-complement number; the
value N will always be a two’s complement positive number less than 128. For
greater certainty, N will never be 0 . Your code will not be tested with inputs that do
not meet these requirements.
Some test cases are provided to you.
# Compute M / N, where M must be in $8, N must be in $9, # and M / N must be in $15. # N will never be 0 .text start: lw $8, testcase3_M lw $9, testcase3_N # STUDENTS MAY MODIFY CODE BELOW # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv nop addi $15, $0, -10 # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # STUDENTS MAY MODIFY CODE ABOVE exit: add $2, $0, 10 syscall .data # testcase1: 370 / 120 = 3 # testcase1_M: .word 370 testcase1_N: .word 120 # testcase2: 24156 / 77 = 313 # testcase2_M: .word 24156 testcase2_N: .word 77 # testcase3: 33 / 120 = 0 # testcase3_M: .word 33 testcase3_N: .word 120
Greetings!!
Code:
.data
prompt1: .asciiz "Please enter Divident"
prompt2: .asciiz "Please enter Divisor"
output: .asciiz "Quotient = "
.text
#display prompt1
la $a0,prompt1 #load the address of prompt1 message
li $v0,4 #parameter for syscall for display string
syscall #display
#read divident
li $v0,5 #parameter for syscall for read divident
syscall #read
move $t0,$v0 #store dividend in t0
#display prompt2
la $a0,prompt2 #load the address of prompt2 message
li $v0,4 #parameter for syscall for display string
syscall #display
#read divisor
li $v0,5 #parameter for syscall for read divisor
syscall #read
move $t1,$v0 #store divisor in t1
#multiplication
loop:
blt $t0,$t1,end #if the divisor is less than dividend then go to end
sub $t0,$t0,$t1 #otherwise subtract divisor from the dividend and update divident
addi $t2,$t2,1 #increment the quotient value after one subtraction
j loop #repeat
end:
#display output
la $a0,output #load the address of the output message
li $v0,4 #parameter for syscall for display output message
syscall #display
#display quotient
move $a0,$t2 #load the quotient into the register a0 for display
li $v0,1 #parameter for display the quotient
syscall #display
#standard termination
li $v0,10 #parameter for syscall for termination
syscall #stop execution
Output screenshots:
Testcase 1:
Testcase 2:
Testcase 3:
Hope this helps
Thank You