MIPS Assembly Language System Calls

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

CS-212 Computer Organization & A.L.

LAB 4 Week-09

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


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

MIPS Assembly Language


System Calls

TASK 1 – SYSTEM CALLS

System call is used to communicate with the system for reading from keyboard or writing
to the screen. A System call requires some parameter to be passed in a particular register and a
request/function number (or service code) to be passed in register $v0.

Steps for using System Calls:


1. Load service code into register $v0. Codes (the values to be loaded in $v0) and their
respective functions are given in the table below.
2. Load arguments (if any) into registers such as $a0, $a1 according to the table.
3. Use syscall.
4. Results are returned in registers such as $v0 according to the table.
5. All programs should terminate with $v0 = 10 and syscall to Exit. This helps in running very
long programs (which are run using the GO option in simulation menu in PCSPIM)

GCWUS CS Department Fall Semester 2022 Page 1 of 4


CS-212 Computer Organization & A.L. LAB 4 Week-09

Table 1: Some System Services


Exercise 1: Printing your Name using SYSCALL
Use the following code to print your name on an output screen.

.data
mesg1: .asciiz “My Name is \n“ # fill the blank with your name
.text
.globl main
main:
li $v0, 4 # Load immediate $v0 with value 4
la $a0, mesg1 # $a0 points to base address of string array mesg1
syscall
li $v0, 10 # prepare to exit
syscall # Exit to OS

Run this program by pressing GO (F5). Show the execution of your program to the Instructor
and get your handout marked.
What do you see in the CONSOLE window?
My Name is Sidra Malik

Now explain the following:

1. In the above code, what is the purpose of \n?


\n is used for the purpose of starting a new line.

GCWUS CS Department Fall Semester 2022 Page 2 of 4


CS-212 Computer Organization & A.L. LAB 4 Week-09

2. What is meant by asciiz (different from ascii)?


Both .asciiz and .ascii store a string in memory. However, .ascii does not null terminate the string
while .asciiz null terminates it.

3. What does the first SYSCALL do (see table)?


The first syscall finds 4 in $v0 and thus, it prints the string whose base address is found in the register
$a0.

4. What argument is contained in $a0 before first SYSCALL


$a0 contains the base address of the string to be printed on the syscall.

5. What is purpose of second SYSCALL (see table)


$a0 contains the base address of the string to be printed on the syscall.

Exercise 2: Printing an Integer using SYSCALL


Modify the above program to print an integer (your roll number such as 15100900). Write your
code below:

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

For printing an Integer, answer the following questions:


1. What should be the value in register a0 before you make the syscall: $a0 should contain the
integer to be printed.
2. What should be the value in register v0 before you make the syscall: $v0 should have the
value 1.

Exercise 3: Sum of Integers


Write a code that asks the user to enter a positive integer N and returns the Sum of all
integers less than or equal to N. The sample code is given on the next page. Note that, now you
can run for N = 100, N= 200, N= 1000 by pressing GO, watching the console window, entering the
value and hitting the Enter key. What is the answer in each case?
For N=100, Sum: 5050 , For N=200, Sum: 20100 , For N=1000, Sum: 500500 ,
What happens if you give a very large N, say 100000? Can you explain why this happens? For a
very large value of N like 100000, an exception occurs due to arithmetic overflow. For a large
value of N, the corresponding sum is an even larger value and a time comes when the intermediate
sum overflows into the signed bit of the register in which it is stored. Since positive signed
numbers are being added, the sum itself cannot be negative and hence an exception occurs due to
the arithmetic overflow into the signed bit. The instruction which is responsible is  add $t0, $t0,
GCWUS CS Department Fall Semester 2022 Page 3 of 4
CS-212 Computer Organization & A.L. LAB 4 Week-09

$v0.

Since we have provided the code for Exercise 3 to you, the grading of this exercise will be done
by testing your understanding of the given program through a viva. You are also required to
show your .asm/.s code and its PSCPIM execution to the Instructor during the lab and get
your handout marked.

Sample Code:

(Taken from the book: “MIPS Assembly Language Programming” by Robert L. Britton)

#######################################################################
# Cross References:
# v0: N, t0: Sum
#######################################################################

.data
Prompt: .asciiz “\n Please Input a value for N = (−1 to end): “
Result: .asciiz “\n The sum of the integers from 1 to N is: ”
Bye: .asciiz “\n **** Good bye – Have a good day****”
.text
.globl main
main:
Li $v0, 4 # system call code for print string
la $a0, Prompt # load address of string Prompt into $a0
syscall # print the prompt message
li $v0, 5 # system call code for Read Integer
syscall # reads the value of N into $v0
ble $v0, $zero, End # branch to end if $v0 <= 0
Li $t0, 0 # clear register $t0 to 0
Loop:
add $t0, $t0, $v0 # sum of integers in register $t0
addi $v0, $v0, −1 # summing integers in reverse order
bne $v0, $zero, Loop # branch to loop if $v0 is != 0

li $v0, 4 # system call code for Print String


la $a0, Result # load address of string Result into $a0
syscall # print the string

li $v0, 1 # another system call to Print Integer


move $a0, $t0 # move value to be printed to $a0
syscall # print sum of integers
j main # go for next number

End: li $v0, 4 # system call code for Print String


la $a0, Bye # load address of string Bye into $a0
syscall # print the string
li $v0, 10 # system call code for exit
syscall # terminate the program & return control

GCWUS CS Department Fall Semester 2022 Page 4 of 4


CS-212 Computer Organization & A.L. LAB 4 Week-09

GCWUS CS Department Fall Semester 2022 Page 5 of 4

You might also like