Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question 2.7: Convert following c code to MIPS assembly code ###################

ID: 3725316 • Letter: Q

Question

Question 2.7: Convert following c code to MIPS assembly code

#################################

int multiply(int multiplier, int multiplicand){

return multiplier*multiplicand;

}

int power(int base,int exponent){

int result=1;

for(int i=0;i<exponent;i++){

result=multiply(base,result):

}

return result;

}

int main(){

int p=power(2,16);

return 0;

}

#################################

Given Instructions in MIPS Code:

initialize:

   li $s0, 2           # provide some initial value for base

   li $s1, 3           # provide some initial value for exponent

main:

   #

   # Insert your call to power here

  

   #

   j exitProgram           # jump to exit label to terminate program

multiply:

   # insert your multiply procedure here

   mul $v0,$a0,$a1

   jr $ra

power:

# Adjust stack as necessary and store preserved registers

# initialize local variables

# loop to compute result

# Restore preserved registers and restore stack point

# return to caller

##################################

registers $s0 and $s1 are populated with the base and exponent values respectively. Change these values as necessary to test your code

multiply procedure must return value into registers $v0-$v1 to pass the value back to the caller. Implement the “power” procedure, doing all multiplication by calling the “multiple” procedure.

Do NOT use any $tX registers in your “power” procedure or in your “multiply” procedure. In your “power” procedure, use register $s0 to store the result variable. Use register $s1 to store the iteration variable i. You may need additional $sX registers. Be sure to preserve any and all $sX registers that you use.

After your “power” procedure returns to “main”, move the final return value into the $s2 register.

Explanation / Answer

multiply(int, int):

push rbp

mov rbp,rsp

mov DWORD PTR [rbp-0x4],edi

mov DWORD PTR [rbp-0x8],esi

mov eax,DWORD PTR [rbp-0x4]

imul eax,DWORD PTR [rbp-0x8]

pop rbp

ret

power(int, int):

push rbp

mov rbp,rsp

sub rsp,0x18

mov DWORD PTR [rbp-0x14],edi

mov DWORD PTR [rbp-0x18],esi

mov DWORD PTR [rbp-0x4],0x1

mov DWORD PTR [rbp-0x8],0x0

mov eax,DWORD PTR [rbp-0x8]

cmp eax,DWORD PTR [rbp-0x18]

jge 400526 <power(int, int)+0x3c>

mov edx,DWORD PTR [rbp-0x4]

mov eax,DWORD PTR [rbp-0x14]

mov esi,edx

mov edi,eax

call 4004d7 <multiply(int, int)>

mov DWORD PTR [rbp-0x4],eax

add DWORD PTR [rbp-0x8],0x1

jmp 400506 <power(int, int)+0x1c>

mov eax,DWORD PTR [rbp-0x4]

leave

ret

main:

push rbp

mov rbp,rsp

sub rsp,0x10

mov esi,0x10

mov edi,0x2

call 4004ea <power(int, int)>

mov DWORD PTR [rbp-0x4],eax

mov eax,0x0

leave

ret

nop DWORD PTR [rax+0x0]

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote