LearningMaterial ICT4 v6 0 Week7
LearningMaterial ICT4 v6 0 Week7
Laboratory Exercise 7
Procedure calls, stack and parameters
Goals
After this laboratory exercise, you should understand how to invoke a procedure
and the mechanism of stack. In addition, you will be able to write some procedures
that use stack to pass parameters and return results also.
Literature
Behrooz Parhami (CAMS): Section 6.1
Preparation
Before you start the exercise, you should review the textbook, section 6.1 and
read this laboratory carefully.
Procedure calls
A procedure is a subprogram that when called (initiated, invoked) performs a
specific task, perhaps leading to one or more results, based on the input parameters
(arguments) with which it is provided and returns to the point of call, having
perturbed nothing else. In assembly language, a procedure is associated with a
symbolic name that denotes its starting address. The jal instruction in MIPS is
inteded specifically for procedure calls: it performs the control transfer
(unconditional jump) to the starting address of the procedure, while also saving the
return address in register $ra.
Home Assignment 1
The following program uses abs procedure. This procedure is used to find the
absolute value in an integer. This program uses two registers, $a0 for input
argument and $v0 for result. You should read this example carefully, pay attention
to how to write and invoke a procedure.
Home Assignment 2
In this example, procedure max has the function to find the largest elements in
three of integers. These intergers are passed to max procedure through $a0, $a1
and $a2 registers. Read this example carefully and try to explain each line of code.
Home Assignment 3
The following program demonstrates the push and pop operations with stack. The
value of two register $s0 and $s1 will be swap using stack. Read this example
carefully, pay attention to adjust operation and the order of push and pop (sw and
lw instructions).
#Laboratory Exercise 7, Home Assignment 3
.text
push: addi $sp,$sp,-8 #adjust the stack pointer
sw $s0,4($sp) #push $s0 to stack
sw $s1,0($sp) #push $s1 to stack
work: nop
nop
nop
pop: lw $s0,0($sp) #pop from stack to $s0
lw $s1,4($sp) #pop from stack to $s1
addi $sp,$sp,8 #adjust the stack pointer
Home Assignment 4
The following program is a recursive procedure for computing n!. Read this
program carefully and pay attention to register $sp and $fp at the start and the end
of each recursive step.
.text
main: jal WARP
#---------------------------------------------------------------------
-
#Procedure WARP: assign value and call FACT
#---------------------------------------------------------------------
-
WARP: sw $fp,-4($sp) #save frame pointer (1)
addi $fp,$sp,0 #new frame pointer point to the top (2)
addi $sp,$sp,-8 #adjust stack pointer (3)
sw $ra,0($sp) #save return address (4)
#---------------------------------------------------------------------
-
#Procedure FACT: compute N!
#param[in] $a0 integer N
#return $v0 the largest value
#---------------------------------------------------------------------
-
FACT: sw $fp,-4($sp) #save frame pointer
addi $fp,$sp,0 #new frame pointer point to stack’s
top
addi $sp,$sp,-12 #allocate space for $fp,$ra,$a0 in
stack
sw $ra,4($sp) #save return address
sw $a0,0($sp) #save $a0 register
At the begin of WRAP, $sp = 7fffeffec At the end of WRAP, $sp = 7fffeff4
Assignment 1
Create a new project to implement the program in Home Assignment 1. Compile
and upload to simulator. Change input parameters and observe the memory when
run the program step by step. Pay attention to register $pc, $ra to clarify invoking
procedure process.
Assignment 2
Create a new project to implement the program in Home Assignment 2. Compile
and upload to simulator. Change input parameters (register $a0, $a1, $a2) and
observe the memory when run the program step by step. Pay attention to register
$pc, $ra to clarify invoking procedure process.
Assignment 3
Create a new project to implement the program in Home Assignment 3. Compile
and upload to simulator. Pass test value to registers $s0 and $s1, observe run
process, pay attention to stack pointer. Goto memory space that pointed by $sp
register to view push and pop operations in detail.
Assignment 4
Create a new project to implement the program in Home Assignment 4. Compile
and upload to simulator. Pass test input through register $a0, run this program and
test result in register $v0. Run this program in step-by-step mode, observe the
changing of register $pc, $ra, $sp and $fp. Draw the stack through this recursive
program in case of n=3 (compute 3!).
Assignment 5
Write a procedure to find the largest, the smallest and these positions in a list of 8
elements that are stored in regsiters $s0 through $s7. For example:
Largest: 9,3 => The largest element is stored in $s3, largest value is 9
Smallest: -3,6 => The smallest element is stored in $s6, smallest value is -3
Tips: using stack to pass arguments and return results.
Conclusions
Before you pass the laboratory exercise, think about the questions below: