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

Consider the following code that uses nested function calls for a-c below: fun A

ID: 3794117 • Letter: C

Question

Consider the following code that uses nested function calls for a-c below: fun A: addi $a0, $0, 15 jal funB addi $v0, $v0, 1 jr $ra funB: jal funC add $v0, $a0, $v0 jr $ra funC addi $v0, $0, 4 jr $ra Describe what this code does as it is currently written when funA is called from main. Be specific. Describe the proper way to call funB and funC - write an example of the additional code required, and explain where in the code above you would need to add that code. Describe what the code does when calling the functions in the proper way when funA is called from main. Be specific.

Explanation / Answer

a.

General purpose registers (GPRs) are indicated with a dollar sign ($)

Step:1 funA: addi $a0, $0, 15

ADDI -- Add immediate

Addi-Adds a register and a sign-extended immediate value and stores the result in a register($a0=$0+15 i.e $a0=0+15)

Step-2: jal funB

JAL -- Jump and link

Copies the address of the next instruction into the register $ra (register 31) and then jumps to the funB.

Step-3: funB: jal funC

Copies the address of the next instruction into the register $ra (register 31) and then jumps to the funC.

Step:4 funC addi $v0, $0, 4

Addi-Adds a register and a sign-extended immediate value and stores the result in a register($v0=$0+4 i.e $v0=0+4)

Step:5 jr $ra

JR -- Jump register

Jump to the address contained in register $ra ( the address of the next instruction the register $ra-register 31) refer step-3.

Step:6 funB add $v0, $a0, $v0

ADD – Add

Adds two registers and stores the result in a register ( $v0=$a0+$v0 i.e $v0=15+4) i,e $v0=19

Step:7 jr $ra

JR -- Jump register

Jump to the address contained in register $ra

This is the standard problem with assembly language. Registers are shared by all subroutines. $ra contains address of next instruction in function funB i.e add $v0, $a0, $v0 which will loop continuously.

b. Proper way to call funB and funC

The solution? Before you call a subroutine, save the return address and any registers you need on the stack.

If the subroutine is a leaf procedure, i.e., it doesn't have a jal call, then it's easy. You don't need to save any registers on the stack.

If the subroutine has a jal instruction, then make a list of any registers you are using in the subroutine. You will have to, at the very least, save $ra, since jal will overwrite that.

So, save return address to stack as follows:

sw $ra, 4($sp);

The proper way to call funB and funC is:

In funA and funB use "sw $ra, 4($sp)" to store the return address in stack, and when in funB and funC return the value from the stack using temporary register and call jr.

Ex. sw $t0, 4($sp)

jr $to;

c. Modified code:

funA: addi $a0, $0, 15

jal funB

sw $t0, 4($sp)

addi $v0, $v0, 1

jr $ra

funB: jal funC

sw t0, 4($sp)

add $v0, $a0, $v0

sw $t0, 4($sp)

jr $to

funC: addi $v0, $0, 4

sw $t0, 4($s0)

jr $t0

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