Professional Documents
Culture Documents
Mohamed Hashi COS Assignment 2
Mohamed Hashi COS Assignment 2
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 :
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