Write and test a MIPS program consisting of four functions. INSTRUCTIONS. 1. You
ID: 3861744 • Letter: W
Question
Write and test a MIPS program consisting of four functions. INSTRUCTIONS.
1. Your main function must do the following. print your name
set parameter for repeat prompt and call getinput repeat input number of times
DO NOT USE ANY PSEUDO-
set parameter for first prompt and call getinput set parameter for second prompt and call getinput set parameters and call multiply
print the product
set parameters and call divide
print the quotient and remainder
end repeat end program
2. getinput
$a0 – address of prompt to print $v0 – input value
This function should prompt the user for input by printing the string parameter and read and return the input
3. multiply
$a0 – first integer
$a1 – second integer
$v0 – first integer * second integer
This function is to calculate and return the result of multiplying the arguments together. Your solution must implement the multiplication algorithm covered in the text on page 185. Assume that the parameter values can be stored in 16 bits so the product will fit into one 32 bit register. You must handle both positive and negative numbers plus zero. Using a version of the mult instruction is NOT ACCEPTABLE.
4. divide
$a0 – first integer
$a1 – second integer
$v0 – first integer / second integer $v1 – first integer % second integer
This function is to calculate and return the result of dividing the arguments. Your solution must implement the division algorithm covered in the text on page 191. You must handle both positive and negative numbers plus zero. If the second parameter is zero, set both $v0 and $v1 to zero. Using any version of the div instruction is NOT ACCEPTABLE.
General:
You may add other functions if desired.
Use strings to label output.
Test your program!
Do not use pseudo-instructions.
All calls to the functions must be done through jal and returns through jr.
Your code must be documented for each function. Describe which registers are used in
the function and how they are used.
Submit your file with the complete program.
Explanation / Answer
MIPS x86 code:
main: # @main
push RBP
mov RBP, RSP
sub RSP, 48
lea RDI, QWORD PTR [.L.str]
mov AL, 0
call printf
lea RDI, QWORD PTR [.L.str1]
lea RSI, QWORD PTR [RBP - 4]
lea RDX, QWORD PTR [RBP - 8]
mov DWORD PTR [RBP - 28], EAX # 4-byte Spill
mov AL, 0
call scanf
mov ECX, 0
mov EDI, DWORD PTR [RBP - 4]
mov ESI, DWORD PTR [RBP - 8]
mov DWORD PTR [RBP - 32], EAX # 4-byte Spill
mov DWORD PTR [RBP - 36], ECX # 4-byte Spill
call mult(int, int)
mov EDI, DWORD PTR [RBP - 4]
mov ESI, DWORD PTR [RBP - 8]
call div(int, int)
mov EAX, DWORD PTR [RBP - 36] # 4-byte Reload
add RSP, 48
pop RBP
ret
mult(int, int): # @mult(int, int)
push RBP
mov RBP, RSP
sub RSP, 16
lea RAX, QWORD PTR [.L.str2]
mov DWORD PTR [RBP - 4], EDI
mov DWORD PTR [RBP - 8], ESI
mov ESI, DWORD PTR [RBP - 4]
imul ESI, DWORD PTR [RBP - 8]
mov DWORD PTR [RBP - 12], ESI
mov ESI, DWORD PTR [RBP - 12]
mov RDI, RAX
mov AL, 0
call printf
mov ESI, 0
mov DWORD PTR [RBP - 16], EAX # 4-byte Spill
mov EAX, ESI
add RSP, 16
pop RBP
ret
div(int, int): # @div(int, int)
push RBP
mov RBP, RSP
sub RSP, 32
lea RAX, QWORD PTR [.L.str3]
mov DWORD PTR [RBP - 4], EDI
mov DWORD PTR [RBP - 8], ESI
mov EDI, DWORD PTR [RBP - 4]
mov QWORD PTR [RBP - 24], RAX # 8-byte Spill
mov EAX, EDI
cdq
idiv ESI
mov DWORD PTR [RBP - 12], EAX
mov EAX, DWORD PTR [RBP - 12]
mov RDI, QWORD PTR [RBP - 24] # 8-byte Reload
mov DWORD PTR [RBP - 28], ESI # 4-byte Spill
mov ESI, EAX
mov AL, 0
call printf
mov EDX, 0
mov DWORD PTR [RBP - 32], EAX # 4-byte Spill
mov EAX, EDX
add RSP, 32
pop RBP
ret
.L.str:
.asciz "Enter Two Values :"
.L.str1:
.asciz "%d%d"
.L.str2:
.asciz "Multiply : %d "
.L.str3:
.asciz "Div : %d "
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.