This document describes SPIM, a MIPS simulator. It provides an overview of MIPS assembly programming, including memory usage, registers, directives, instruction sets, and system calls. It also gives examples of assembly code, such as a "Hello World" program and an interactive version that takes user input.
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Spim Mips Simulator 08 02
1. SPIM: a MIPS simulator
Michele Chinosi
michele.chinosi@uninsubria.it
University of Insubria - Varese (IT)
13.04.2007
Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 1 / 26
2. Outline
1 Assemply Programming
The Assembly Language
2 Program Structure
Some introductory notions
Memory Usage
CPU Registers
SPIM Directives
MIPS Instructions Set
Assembly Program Elements
SPIM System Calls
Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 2 / 26
3. The Assembly Language
Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 3 / 26
4. High-level Language Program example
Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 4 / 26
6. Why to Use Assembly Language
Why to use assembly language
Speed
Size
Embedded computers (car’s brakes computer)
Predictable execution time
Time-critical systems
Ability to exploit specialized instructions
No high-level languages availability on particular computers
Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 6 / 26
7. Why NOT to Use Assembly Language
Why NOT to use assembly language
Assembly programs are machine-specific
An assembly language program remains tightly bound to its original
architecture, even after the computer is eclipsed by new, faster, and more
cost-effective machines
Assembly programs are longer
Assembly language programs are longer than the equivalent programs written
in a high-level language
Assembly programs are more difficult to read
Longer programs are more difficult to read and understand and they contain
more bugs. Assembly language exacerbates the problem because of its
complete lack of structure. The resulting programs are hard to read because
the reader must reconstruct every higher-level construct from its pieces and
each instance of a statement may be slightly different.
Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 7 / 26
8. Some introductory notions
Comments
Comments in assembler files begins with a sharp-sign (#). Everything from the
sharp-sign to the end of the line is ignored.
Identifiers
Identifiers are a sequence of alphanumeric characters, underbars ( ), and dots (.)
that do not begin with a number. Opcode for insctructions are reserved words
that are not valid identifiers.
Labels
Labels are declared by putting them at the very beginning of a line followed by a
colon (:).
Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 8 / 26
9. Some introductory notions
Strings
Strings are enclosed in double-quotes (quot;).
Special characters
Special characters in strings follow the C convention:
newline → n
tab → t
quote → quot;
Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 9 / 26
10. MIPS Memory Usage
Memory Usage
Systems based on MIPS processors typically divide memory into three parts.
The first part, near the bottom of the address space (starting at 4000000hex )
is the text segment – program’s instructions
The second part is the data segment, which is divided in two parts:
Static data, starting from 10000000hex -- objects whose size is known
and lifetime same as program execution
Dynamic Data, allocated by the program as it executes
The third part is the stack segment and resides at the top of the virtual
address space (starting at address 7fffffffhex ). Like dynamic data, the
maximum size of a program’s stack is not known in advance.
Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 10 / 26
12. CPU Registers
SPIM Registers
the MIPS/SPIM CPU contains 32 general purpose 32-bit registers (0-31)
register n is designed with $n
register $0 always contains the value 0
Registers Conventions
MIPS has estabilished a set of conventions as to how registers should be used
these suggestons are guidelines
a program that violated them will not work properly with other sw
Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 12 / 26
13. CPU Registers in details
$at (1), $k0 (26), $k1 (27): reserved for assembler and OS
$a0...$a3 (4-7): to pass the first 4 arguments to routines
$v0,$v1 (2,3): to return values from functions
$t0...$t9 (8-15,24,25): caller-saved registers used for temporary quantities
(not preserved across calls)
$s0...$s7 (16-23): callee-saved registers that hold long-lived values
(preserved across calls)
$sp (29): stack pointer (last location in use)
$fp (30): frame pointer
$ra (31): return address for a call (written by a jal)
$gp (28): global pointer, points to the middle of a 64K block of memory in
the heap
Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 13 / 26
14. CPU Registers Summary
Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 14 / 26
15. SPIM Directives
SPIM supports a subset of the assembler directives provided by MIPS assembler.
All the directive must be written preceding them with a single dot (.).
.align n
align the next datum on a 2n byte boudary.
.ascii str
store the string in memory, not null-terminated
.asciiz str
store the string in memory, null-terminated
.byte b1, ..., bn
store the n values in successive bytes of memory
.data <addr>
the following data items should be stored in the data segment
.double d1, ..., dn
store the n FP double precision numbers in successive memory locations
.extern sym size
the datum stored at sym is size byte large and is a global symbol
Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 15 / 26
16. SPIM Directives
.float f1, ..., fn
store the n FP single precision numbers in successive memory locations
.globl sym
declare that symbol sym is global and can be referenced from other files
.half h1, ..., hn
store the n 16-bit quantities in successive memory halfwords
.kdata <addr>
the following data items should be stored in the kernel data segment
.ktext <addr>
the next items (instructions / words) are put in the kernel text segment
.space n
allocate n bytes of space in the data segment
.text <addr>
the next items (instructions / words) are put in the user text segment
.word w1, ..., wn
store the n 32-bit quantities in successive memory words
Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 16 / 26
17. MIPS Instructions Set (1)
Three different instructions types: R, I, J
Some examples:
R : add, addu, and, jr, slt, mfhi, mult
I : addi, beq, bne, lw, ori, sb, sw
J : j, jal
Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 17 / 26
18. MIPS Instructions Set (2)
Instructions Families
All intructions can be grouped in different families depending on their application
domain:
Arithmetic and Logical Instructions
abs, add, and, div, mult, neg, nor, not, or, sll, xor, sub, ...
Constant-Manipulating Instructions
lui, li
Comparison Instructions
slt, seq, sge, sgt, sle, sne, ...
Branch Instructions
b, beq, bgez, bgtz, bltz, bne, beqz, bge, bgt, ble, blt, ...
Jump Instructions
j, jal, jr, ...
Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 18 / 26
19. MIPS Instructions Set (3)
Instructions Families
Load Instructions
la, lb, lbu, lh, lw, ld, ll, ...
Store Instructions
sb, sh, sw, swl, sd, sc, ...
Data Movement Instructions
move, mfhi, mflo, mthi, mtlo, ...
Floating-point Instructions
Exception and Interrupt Instructions
syscall, break, nop, ...
Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 19 / 26
20. MIPS Instructions Set (4)
Pseudoinstructions
The whole instructions set can be divided into two different subsets:
Core instructions set
Pseudoinstructions set
Pseudoinstructions are composed by multiple instructions identified with a name.
They are interpreted by MIPS and substituted by the original instructions
sequence. Some examples are:
Branch Less Then: blt if(R[rs]R[rt]) PC = Label
Branch Greater Then: bgt if(R[rs]R[rt]) PC = Label
Branch Less Then or Equal: ble if(R[rs] ≤ R[rt]) PC = Label
Branch Greater Then or Equal: bge if(R[rs] ≥ R[rt]) PC = Label
Load Immediate: li R[rd] = Immediate
Move: move R[rd] = R[rs]
Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 20 / 26
21. Assembly Program Elements
.data # directive
foo: .asciiz quot;Hello!nquot; # string null-terminated
n: .word 1,4,0 # array
res: .word 0 # .word 0 = memory allocation
.text # directive
.globl main # main becomes global
main: lw $a0, foo # $a0,$a1,$t2,... = registers
loop: li $a1, 1 # main, loop, store = labels
lw $a2, n($t2) #
add $a2, $a2, $a1 #
beq $a2, $t3, store #
j loop # jump to label ‘loop’
store: sw $a2, res # res = identifier
...
Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 21 / 26
22. SPIM System Calls
SPIM provides a small set of operating-system-like services through the system
call (syscall) instruction. To request a service, a program loads the system call
code into register $v0 and the arguments into registers $a0...$a3/$f12. System
calls that return values put their result in register $v0/$f0.
.data
str: .asciiz quot;the answer is quot;
.text
.globl main
main: li $v0, 4 # system call code for print_str
la $a0, str # address of string to print
syscall # print the string
li $v0, 1 # system call code for print_int
li $a0, 5 # integer to print
syscall # print it
This code print: the answer is 5
Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 22 / 26
24. Hello, World!
This is a very common simple test program...
.data
str: .asciiz quot;Hello, World!quot;
.text
.globl main
main: li $v0, 4 # system call code for print_str
la $a0, str # address of string to print
syscall # print the string
Exercise: write an interactive version of “Hello, World!” program
Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 24 / 26
25. Hello, name!
One interactive version of “Hello, World!” program
.data
str: .asciiz quot;Hello, quot;
iname: .asciiz quot;Name: quot;
name: .space 256
.text
.globl main
main:
la $a0, iname # print the prompt
li $v0, 4
syscall
la $a0, name # read the string
li $a1, 256
li $v0, 8
syscall
li $v0, 4 # print quot;Hello, quot;
la $a0, str
syscall
li $v0, 4 # print the value inserted
la $a0, name
syscall
li $v0, 10 # exit
syscall
Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 25 / 26