What needs to save the following registers to the stack, when calling a function in MIPS. Answer “caller" for the procedure making a function call, “callee" for the function being called, or “N.A" for neither.
$0 |
$v* |
$a* |
$t* |
$s* |
$sp |
$ra |
There are only two instructions necessary for creating and calling functions: jal and jr.
Suppose If we follow the register conventions when calling functions, we will be able to write much simpler and cleaner MIPS code.
Here we have to save the registers given in the problem. First draw the table with function call for each register as per the question:
$0 |
$v* |
$a* |
$t* |
$s* |
$sp |
$ra |
N.A. |
caller |
caller |
caller |
callee |
N.A. |
caller |
Now assume a function foo that calls another function bar, which is known to call some other functions.
foo can takes one argument at a time and then will modify and use $t0 and $s0.
bar takes two arguments, and returns an integer, and uses $t0-$t2 and $s0-$s1.
Now create a table or boxes and then just before bar calls a function draw a possible ordering of the stack.
The top left box is the address of $sp. When foo function is first called, and then the stack goes downwards, continuing at each next column. Here we will Add ‘(f)’ if the register is stored by foo and ‘(b)’ if the register is stored by bar. The first one is written in the following ways:
1 |
$ra(f) |
2 |
$s0 (f) |
3 |
$v0 (f) |
4 |
$a0 (f) |
5 |
$t0 (f) |
6 |
$ra (b) |
7 |
$s0 (b) |
8 |
$s1 (b) |
9 |
$v0 (b) |
10 |
$a0 (b) |
11 |
$a1 (b) |
12 |
$t0 (b) |
13 |
$t1 (b) |
14 |
$t2(b) |
15 |
|
16 |
|
The following function is used to write a function foo to save the register to the stack
FunctionFoo:
# PROLOGUE
# begin by reserving space on the stack
addiu $sp, $sp, -FrameSize
# now, store needed registers
sw $ra, 0($sp)
sw $s0, 4($sp)
...
# BODY
...
# EPILOGUE
# restore registers
lw $s0 4($sp)
lw $ra 0($sp)
# release stack spaces
addiu $sp, $sp, FrameSize
# return to normal execution
jr $ra
What needs to save the following registers to the stack, when calling a function in MIPS....
The following MIPS assembly code contains a mistake that violates the MIPS convention in terms of using stack for storing/protecting registers. What is the mistake and how should it be fixed? Correct the corresponding lines in the code. For the corrected code, sketch the stack frame contents at the time when the instruction ‘move $s1, $a1’ is being executed. f: addi $sp, $sp, 12 sw $ra, 8($sp) sw $s1, 4($sp) sw $s0, 0($sp) move $s0, $a0 move $s1, $a1 #...
a) Write the following C function in Assembly. You must follow the System V 64-bit calling convention and use AT&T Syntax notation. long fibonacci (long n) { if (n == 0) return 0; else if (n == 1) return 1; else return (fibonacci (n - 1) + fibonacci (n - 2)); } b) The Windows x86-64 calling convention passes function parameters in the registers RCX, RDX, R8 and R9 and returns values on register RAX. Caller saved registers are: RAX,...
Please answer the following Assembly x86 Questions with either TRUE or FALSE. 1. The PUSHAD instruction pushes all the 32-bit general-purpose registers on the stack. 2. The SS register points to the last value pushed on the stack. 3. The POP instruction copies a value from the stack to an operand, then it increments the stack pointer 4. When a macro is invoked, both CALL and RET instructions are needed. 5. When the instruction CALL runs, ESP always changes value....
LC-3 Programming Help!! The Stack Protocol The following outline is the protocol for passing arguments to a function and returning values. Everything is stored on the runtime stack so that space is used only when the function is executing. As a result the actual address of arguments and locals may change from call to call. However, the layout of the stack frame (activation record) is constant. Thus, the offests from the frame pointer (FP) to the parameters/locals are constant. All...
a) Write the following C function in Assembly. You must follow
the System V 64-bit calling convention and use AT&T Syntax
notation. Note: You cannot change the algorithm in any way so your
assembly function must still be recursive. (20 points) long Catalan(long n) { long sum
= 0; if (n == 0) return 1; for (int i = 0; i < n; i++) { sum +=
Catalan(i) * Catalan(n - i - 1); } return sum; } b) The...
Subroutines in MIPS Determines the minimum of two integers Functions within the MIPS slides describe how one can use subroutines (also called procedures, functions, and methods) in MIPS. Because of the importance of subroutines in modern programming, most hardware designers include mechanisms to help programmers. In a high-level language like C or Java, most of the details of subroutine calling are hidden from the programmer. MIPS has special registers to send information to and from a subroutine. The registers $a0,...
ASSEMBLY LANGUAGE (Mars MIPS)
Starting code:
Factorial: #Factorial Recursive function
subu $sp, $sp, 4
sw $ra, 4($sp) # save the return address on stack
beqz $a0, terminate # test for termination
subu $sp, $sp, 4 # do not terminate yet
sw $a0, 4($sp) # save the parameter
sub $a0, $a0, 1 # will call with a smaller argument
jal Factorial
# after the termination condition is reached these lines
# will be executed
lw $t0, 4($sp) # the argument I...
Need to write a MIPS assembly program that finds the minimum and maximum and sum of a stored array. It also finds the locations of the minimum and maximum. The interaction between the main program and the function is solely through the stack. The function stores $ra immediately after being called and restores $ra before returning to main. The main program reserves a static C like array (index starts from 0) of 10 elements and initializes it. The maximum should...
Write a MIPS program with the following specifications: Use the my_mul function in question 1 to create a function which computes factorials: n! = n. (n - 1). … 3 .2 . 1 Note: You can only use my_mul function, and not use other functions). Each function must save all variables it modifies. below is my_mul my_mul : #multiply $a0 with $a1 #does not handle negative $a1 ! #Note : This is an inefficient way to multiply ! addi $sp,...
1. [2 points] Write a MIPS assembly language program of the following C function and the code to call the function: int leaf_example (int g, h, i, j) { int f; f = (g + h) - (i + j); return f; مهه Arguments g, h, i, and j are passed to the function in registers $a0, $al, Şa2, and $a3, respectively while f in $50 (hence, need to save $50 on stack), and the result is to be stored...