Write a MIPS assembly program to calculate A divided by B using the right approa
ID: 1715316 • Letter: W
Question
Write a MIPS assembly program to calculate A divided by B using the right approach.Assume the values are 6 bit unsigned numbers.
j main
# =================================================================================================
# div function - compute a / b
# -------------------------------------------------------------------------------------------------
# a0 -> 'a'
# a1 -> 'b'
# ra has return address
# -------------------------------------------------------------------------------------------------
# v0 gets output (quotient in the least significant 6 bits, remainder in the next 6 bits)
# =================================================================================================
div:
addi $v0, $zero, -1 # prepare the return value in case of errors
# TODO: put your logic in here
jr $ra
# =================================================================================================
# =================================================================================================
# main function
# =================================================================================================
main:
# set up call to div procedure: which should work fine
addi $a0, $zero, 36 # Set argument 'a'
addi $a1, $zero, 6 # Set argument 'b'
jal div # jump and link to div
andi $s0, $v0, 0x3F # mask off quotient into s0
srl $s1, $v0, 6 # set s1 to remainder
# print out the quotient
addi $a0, $s0, 0
addi $v0, $zero, 1 # set up the print int syscall
syscall
# print out 'R'
addi $v0, $zero, 11 # print character syscall
addi $a0, $zero, 'R'
syscall
# print out the remainder
addi $a0, $s1, 0
addi $v0, $zero, 1 # set up the print int syscall
syscall
addi $v0, $zero, 11 # print character syscall
addi $a0, $zero, 10 # newline
syscall
# test for one of the arguments out of range, should be able to swap arguments and get the same
addi $a0, $zero, 129 # set the dividend to 129
addi $a1, $zero, 1 # set the divisor to 1
jal div # jump and link to div
addi $s0, $v0, 0 # move return value into $s0
# print out the quotient
addi $a0, $s0, 0
addi $v0, $zero, 1 # set up the print int syscall
syscall
# exit cleanly
addi $v0, $zero, 10 # set up the exit syscall
syscall # make the syscall
Explanation / Answer
We can use MOV instruction to transfer the product into a register. The syntax of the instruction will depend upon the type of processor we are using. Suppose if we are using Accumlator based processor say Microcontroller 8051. In these type of microprocessors and microcontrollers, the product of say N numbers will be stored in accumlator. Lets denote accumlator by A. So the instruction that transfers product from accumlator to Register will be of the form MOV Register, Accumlator. Say register is D, so it will be MOV D,A. MOV intsruction is of the format MOV destination, Source.
If the processor is not accumlator based for e.g INTEL 8086, we can use Register to Register mode, or memory to registers, based on where the product is present. Suppose product is present register BX, and we want to transfer it to say AX, we use instruction MOV AX,BX.
Now say product is stored in a memory location whose address in a segment is at an offset of 4000H. We can use the instruction MOV BX, [4000H] to transfer the contents i.e product present at 4000H to register BX. MOV instruction is a very powerful and simple instruction for transfer of data between register to register, register to memory, memory to register.
Apart from MOV we can use store accumlator (STA) instruction in accumlator based processors.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.