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

int my_global = 0; main(){ int x = 10; int y = 20; int z; z = myFunc(x,y); } int

ID: 664575 • Letter: I

Question

int my_global = 0; main(){ int x = 10; int y = 20; int z; z = myFunc(x,y); } int myFunc(int x, int y){ return x – y + my_global; }

Translate the above C code to MIPS assembly code using the following assumptions. Also show the contents of the stack and the heap during and after the function call. The stack and the static data segments are empty to begin with. The stack and the global pointers start at addresses 0x7FFF FFFC and 0x1000 8000, respectively. All function inputs are passed using argument registers and returned using return registers. The functions can only use the $s registers starting from the $s0.

Explanation / Answer

int my_global = 0;
main()
{
int x = 10;
int y = 20;
int z;
z = myFunc(x,y);
}
int myFunc(int x, int y)
{
return x – y + my_global;
}    


.globl main
.globl myfunction
.type main, @function
.type myfunction, @function
.globl my_global
.data
.align 4
.type my_global, @object
.size my_global, 4
my_global:
.long 0
.text

main:
pushq %rbp
movq %rsp, %rbp
movl $10, -8(%rbp) // moving x value
movl $20, -4(%rbp) // moving y value to rbp register -4
otherway :
movl $10,%eax
movl $20,%ebx
movl $0, %esp // initialize z and move 0 to it
call myfunction (%eax,%ebx)
movl (%eax,%ebx),%esp
movl $0, %eax
call myfunction (%eax,%ebx)
leave
ret
%eax - %ebx + my_global // or first subtract x and y and store it in another register then return it

The l at the end of each instruction in the above program tells that using opcode that works with 32-bit operands. The registers are
indicated by % in front and -4(%ebp) for example, indicates a reference to ebp-4 location.


Hope it helps!