0% found this document useful (0 votes)
48 views4 pages

MIPS Assembly Language Procedure Calls and Using The Stack

This document describes a lab assignment on procedure calls and using the stack in MIPS assembly language. Students are asked to convert a C++ program that checks if a value is greater than 511 into MIPS assembly. A template is provided that shows how to call the check511 procedure, read input using a system call, call check511, print output using a system call, and exit. Students need to fill in the blanks with the correct MIPS instructions. They also need to provide the code for the check511 procedure. The document also asks students to use instructions to load values into registers, push those registers onto the stack, draw a memory map, pop the values back, and observe how the stack pointer changes.

Uploaded by

Sidra Malik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views4 pages

MIPS Assembly Language Procedure Calls and Using The Stack

This document describes a lab assignment on procedure calls and using the stack in MIPS assembly language. Students are asked to convert a C++ program that checks if a value is greater than 511 into MIPS assembly. A template is provided that shows how to call the check511 procedure, read input using a system call, call check511, print output using a system call, and exit. Students need to fill in the blanks with the correct MIPS instructions. They also need to provide the code for the check511 procedure. The document also asks students to use instructions to load values into registers, push those registers onto the stack, draw a memory map, pop the values back, and observe how the stack pointer changes.

Uploaded by

Sidra Malik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

CS-212 Computer Organization & A.L.

LAB 5 Week-10

CS-212 - Computer Organization & A.L – LAB 4


Date: ; Lab Section:
Instructor: Amna K.
Student Roll Number: Total Marks: 100

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 of
corresponding comments (which follow the # sign). The template shows how to call a procedure

GCWUS CS Department Fall Semester 2022 Page 1 of 4


CS-212 Computer Organization & A.L. LAB 5 Week-10

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
# Use system call code 5 (read integer – See Table 1)
syscall # Invoke system call (int is returned in $v0)
# put the input integer into $a0 which is one of the
# argument registers for parameter passing
# call check511 procedure
# cout y -- print output
# put return value into $a0
# Use system call 1 (print integer)
syscall # Invoke system call
# Use system call 10 (exit)
# 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.

# Last instruction of check511


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

GCWUS CS Department Fall Semester 2022 Page 2 of 4


CS-212 Computer Organization & A.L. LAB 5 Week-10

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


Why
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
.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?


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

Table 1: The state of the stack while you push data

Stop and make a picture of the processor memory in your mind. You have code in the memory
starting at address: . You have data in the memory starting at address .
You have data pushed in the stack area of the memory starting at address and ending at
address . Draw the memory map of the processor on the next page.

GCWUS CS Department Fall Semester 2022 Page 3 of 4


CS-212 Computer Organization & A.L. LAB 5 Week-10

Memory Map of the Processor:

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):

Note how the Stack Pointer (SP) changes with each push and pop.
What is the value of the stack pointer after the last pop:
Is the value of the stack pointer same as before data was pushed on to the stack?
What happens if you try to go beyond your space and pop more values?

GCWUS CS Department Fall Semester 2022 Page 4 of 4

You might also like