Mohamed Hashi COS Assignment 2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Mohamed Hashi COS assignment 2

mohamed.hashiu g24
October 2023

1 Question 3
The tables bellow demonstrates the stack frames created during the recursive
calls of the factorial function. Each time a recursive call is made, a new stack
frame is created. The stack frame contains the return address of the caller,
the arguments to the function, and the local variables of the function. When
a function returns, its stack frame is popped off the stack. The stack frames
are shown in the reverse order of their creation, with the most recently created
stack frame at the top.

1
After the call to factorial(5), the stack contains the following values:
Stack Value
Top Return address of factorial(5)
5
fp
Return address of factorial(4)
4
ra
Return address of factorial(3)
3
fp
Return address of factorial(2)
2
ra
Return address of factorial(1)
1
fp
Bottom

After the call to factorial(4), the stack contains the following values:
Stack Value
Top Return address of factorial(4)
4
fp
Return address of factorial(3)
3
ra
Return address of factorial(2)
2
fp
Return address of factorial(1)
1
fp
Bottom

2
After the call to factorial(3), the stack contains the following values:
Stack Value
Top Return address of factorial(3)
3
fp
Return address of factorial(2)
2
ra
Return address of factorial(1)
1
fp
Bottom

After the call to factorial(2), the stack contains the following values:
Stack Value
Top Return address of factorial(2)
2
fp
Return address of factorial(1)
1
ra
fp
Bottom

After the call to factorial(1), the stack contains the following values:
Stack Value
Top Return address of factorial(1)
1
fp
Bottom

After the call to factorial(0), the stack contains the following values:
Stack Value
Top Return address of factorial(0)
fp
Bottom

3
October 15, 2023

5. Fifth Question
32-bit integers can be handled as immediate operands in MIPS using the ‘lui‘
and ‘ori‘ instructions. The ‘lui‘ instruction loads a 16-bit immediate value into
the upper 16 bits of a register, and the ‘ori‘ instruction ORs a 16-bit immediate
value with the lower 16 bits of a register. For example, the following code loads
the 32-bit integer 0x12345678 into register t0 :

lui $t0, 0x1234


ori $t0, $t0, 0x5678

32-bit addresses can be handled when making a jump or branch using the ‘j‘
and ‘beq‘ instructions. The ‘j‘ instruction jumps to a 32-bit address, and the
‘beq‘ instruction branches to a 32-bit address if the two registers specified are
equal. For example, the following code jumps to the address 0x12345678:

j 0x12345678

The following code branches to the address 0x12345678 if the registers

$t0 and $t1 are equal:

beq $t0, $t1, 0x12345678

0.1 Citations in APA


I worked with Abdifatah on this assignment and particularly for the listed ques-
tions.

Mohamed Hashi, Abdifatah Mohamed. (15/10/2023). Assignment2 questions


1, 2, and 4

You might also like