Some Samples of MIPS Assembly Language
Some Samples of MIPS Assembly Language
System Calls
Almost all programs, user and
system, are written to be run on a
machine with a fully functional
operating system.
The operating system provides a
number of services to other
programs.
We shall use a number of SPIM
system calls to facilitate input and
output.
.data
str: asciiz The answer is
.text
li $v0, 4
# Code for print string
la $a0, str # Address of string
syscall
# Print the string
li $v0, 1
# Code for print integer
li $a0, 5
# Value to print
syscall
li $v0, 10 # Code for exit
syscall
.space 80
.word 1, 2, 3
.word 1
.word 2
.word 3
# At address W1 + 4
# At address W1 + 8
Data Alignment
The assembler will normally align data as
required by the data type.
The .align N directive aligns the next entry
on an address that is a multiple of 2 N.
Character data can be stored anywhere.
Byte data must be stored at an address that
is a multiple of 2. Use .align 1
Word data must be stored at an address that
is a multiple of 4. Use .align 2
savea0
savea1
.ktext 0x80000180
sw $a0, savea0
sw $a1, savea1
More code goes here.
.kdata
.word 0
.word 0
Compute Fibonacci
Numbers
The Fibonacci sequence was
introduced in 1202 by the Italian
mathematician Leonardo of Pisa,
known as Fibonacci.
The sequence is defined for N 0 as
follows:
F0 = 1, F1 = 1, and FN = FN-1 + FN-2 for
N 2.
This is often used as an example of
recursion.
Fibonacci
This is a non-recursive program to calculate Fibonacci
numbers, defined as follows: F(0) = 1, F(1) = 1,
and F(N) = F(N - 1) + F(N - 2), for N >= 2.
This runs under both MARS and SPIM.
Written by Edward Bosworth, Ph.D.
Associate Professor of Computer Science
Columbus State University
Columbus, GA 31907-5645
E-Mail
bosworth_edward@ColumbusState.edu
Date first written: July
Date last revised: July
4, 2012
4, 2012.
main:
li $v0, 4
# Print string
la $a0, str0 # Address of title string
syscall
# Print the title
#
getN:
li $v0, 4
# Print string
la $a0, str1 # Address of input prompt
syscall
# Print the prompt.
#
li $v0, 5
syscall
# Read an integer
# Return the value in $v0
# Do we stop?
# F(1) is trivial.
# Is N > 1?
li $v0, 4
la $a0, str2
syscall
j
getN
#
#
#
#
$t1, 1
$t2, 1
# Initialization
# code for loop.
$v1,
$t0,
$t1,
$t2,
#
#
#
#
$v1, 1
$t1
$t2
$t0, $t1
Bump $v1
New F(N - 2)
New F(N - 1)
New F(N),
# $v1 contains N
# Are we done?
$v0, 4
$a0, str4
$v0, 1
$a0, $t2
getN
Examples of Exceptions
External interrupts include:
Input the device has data ready to transfer
Output the device is ready to receive data
Timer the CPU timer raises an interrupt,
used to run the CPU clock.
Exceptions include
Arithmetic overflow an arithmetic error
Page fault a reference has been made to
an invalid virtual memory address.
Handling Exceptions
Depending on the exception type, the control unit
will begin execution of kernel code at
one of two locations.
0x80000000 for undefined instructions
0x80000180 for other exceptions
For that reason, the standard exception handler
begins as follows:
.ktext 0x80000180
.set noat
move $k1, $at # This at 0x80000180
.set at
The Coprocessors
CP0 for exceptions; CP1 for floating point
Coprocessor Registers
Coprocessor 0 has a number of
registers to control exceptions and
manage memory.
Here are some of the registers used
by SPIM
$12 the interrupt mask and enable
bits
$13 the exception type and pending
interrupt bits
$14 EPC: address of the instruction
.ktext 0x80000180
sw
$a0, savea0
sw
$a1, savea1
sw
$v0, savev0
#
# Now determine the
#
mfc0
$t0, $13
srl
$t0, $t0,
andi
$t0, $t0,
# More code here to
la
$a0, L4
mtc0
$a0, $14
mtc0
mfc0
andi
ori
mtc0
$0,
$a1,
$a1,
$a1,
$a1,
#
#
#
#
#
lw
lw
lw
$v0, savev0
$a1, savea1
$a0, savea0
#
$13
$12
0xFFFD
0x1
$12
#
eret