Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
26 views6 pages

MIPS Assembly Language Procedure Calls and Using The Stack

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 6

CS-212 computer organization & AL LAB 5 WEEK 10

CS-212 Computer organization & AL- LAB 5


Date: 18/march/2023 LAB section: 2021
Instructor: Amna k
Student Roll Number: CS-21026

MIPS Assembly Language


Procedure Calls and Using the Stack
TASK 1 - PROCEDURE CALLS
Exercise 1:
Let’s say we have a ‘check511’ function that returns 1 if the value passed to it is greater
than 511 and 0 otherwise, as described in the following C++ program. Let us convert this
code to MIPS Assembly Language. CIN and COUT can be decoded using SYSCALLs for
reading and printing an integer.
C++ Code:
int check511(int z);
int main()
{
int x,y;
cin>>x;
y=check511(x);
cout<<y;
}
//the function
int check511(int z)
{
int result;
if(z>511)
{
result=1;
}
else
{
result=0;
}
return result;
}
For the MIPS Assembly Language, the template for the program that uses check511
procedure (function) is provided on the next page. In the main part of the code, each blank
line stands for exactly one instruction. The purpose of each instruction is defined in terms

GCWUS CS department fall semester 2021 2023


CS-212 computer organization & AL LAB 5 WEEK 10

of corresponding comments (which follow the # sign). The template shows how to call a
procedure in terms of comments. In addition, it shows how to read from the keyboard and
write the answer to the screen using SPIM system calls, again in terms of comments. You
are required to fill in the blanks with the necessary MIPS Assembly Language instructions.
Be sure to use the correct registers required for system and procedure calls as discussed
in this lab earlier. The check511 procedure follows the label check511. You are required
to develop the code for this procedure and write it down as well (in the space provided).
# Using a procedure in main
.text
.globl main
main:

# get input --- the cin part


addi $v0, $zero, 5 #use system call code 5
syscall #invoke system call
add $a0,$zero,$v0 # put the input integer into $a0 which is one of the

# argument registers for parameter passing

jal check511 # call check511 procedure

# cout y -- print output


add $a0, $zero, $v0 # put return value into $a0

addi $v0, $zero, 1 # Use system call 1 (print integer)

syscall # Invoke system call

Exit:

addi $v0, $zero, 10 # Use system call 10 (exit)

syscall # Invoke system call

check511:
# Write your code for the function check511 here. Make sure the return value
is in the register $v0. The use of Pseudo instructions is not allowed within the
Procedure.

addi $v0, $zero, 0 #Place 0 in the register $v0

addi $t0, $zero, 511 #Place the number 511 in the register $t0

GCWUS CS department fall semester 2021 2023


CS-212 computer organization & AL LAB 5 WEEK 10

slt $v0, $t0, $a0 #Perform the check 511 < (Value in $a0) which

jr $ra # Last instruction of check511

# go back to the caller: main (check511 ends here)

What value is stored in register $ra when jal check511 is executed: 0x00400034, ,

Why When the jal instruction is executed, it jumps to an address


where the required procedure is stored. This instruction also updates
the value of $ra with the return address for the procedure being
called. At the end of the called procedure / function the instruction jr
$ra is used which means an unconditional jump is made to the
address stored in the register $ra.

Show the execution of your program to the Instructor during the lab time and get your
handout marked.

TASK 3 – USING THE STACK IN PCSPIM


Exercise 5:
Use the following instructions to load values in the registers.

.data

My Numbs: .word -100, 200, 300, -300

GCWUS CS department fall semester 2021 2023


CS-212 computer organization & AL LAB 5 WEEK 10

.text

.globl main

main:

la $s0, My Numbs #load base address of array

lw $t0, 0($s0) #load first number


lw $t1, 4($s0)

lw $t2, 8($s0)

lw $t3, 12($s0)

What is the value of the stack pointer at this time? 0x7fffeffc

Now write the code to push all the 4 register values on to the stack and fill the table
below. First push $t0, then $t1, then $t2 and then $t3.

Code: (given for the first push – write your code for all push instances below)
addi $sp, $sp, -4
sw $t0, 0($sp)

Memory Stack Address Value


0x7fffeffc 0x00000000
0x7fffeff8 0xffffff9c
0x7fffeff4 0x000000c8
0x7fffeff0 0x0000012c
0x7fffefec 0xfffffed4
Stop and make a picture of the processor memory in your mind. You have code in the memory
starting at address: The text segment starts at 0x00400000 while your own
code starts at 0x00400024 You have data in the memory starting at address The
data segment starts at 0x10000000 while the array that you
initialized starts at 0x10010000.You have data pushed in the stack area of the
memory starting at address The stack pointer points towards the top of the
stack. In this case the final value for $sp for this program is
0x7fffefec and ending at address The stack ends at the highest address
allocated to the stack space which is 0x7fffeffc Draw the memory map of the
processor on the next page.
Memory Map of the Processor:

GCWUS CS department fall semester 2021 2023


CS-212 computer organization & AL LAB 5 WEEK 10

Write the following code to change/mix all the registers:

add $t3, $t2, $t3


add $t0, $t1, $s0
add $t2, $s0, $t3
add $t1, $t2, $t0

Now pop all values from the stack to bring the same values in the registers as loaded in the start. Be
careful about the order. Write down the code for all pop instances below, (Code of pop discussed
in class):
lw $t3, 0($sp)
addi $sp, $sp, 4

lw $t2, 0($sp)

addi $sp, $sp, 4

lw $t1, 0($sp)
addi $sp, $sp, 4

lw $t0, 0($sp)

addi $sp, $sp, 4

Note how the Stack Pointer (SP) changes with each push and pop.

After the last pop, the original values of registers $t0, $t1, $t2 and $t3
have been restored.

What is the value of the stack pointer after the last pop: It is 0x7fffeffc
Is the value of the stack pointer same as before data was pushed on to the stack? Yes it is

GCWUS CS department fall semester 2021 2023


CS-212 computer organization & AL LAB 5 WEEK 10

What happens if you try to go beyond your space and pop more values? You will see that
the stack pointer will go beyond the space meant for the stack and
values will be loaded into registers. Thus the stack pointer has
intruded into the space meant for some other segment. This leads
to severe problems in the operation of an actual computer and
such a situation must be avoided at all costs

GCWUS CS department fall semester 2021 2023

You might also like