QtSPIM Tutorial
QtSPIM Tutorial
Simulators
Learning MIPS & SPIM
• MIPS assembly is a low-level programming language
• The best way to learn any programming language is to write
code
• We will get you started by going through a few example
programs and explaining the key concepts
• Tip: Start by copying existing programs and modifying them
incrementally making sure you understand the behavior at
each step
• Tip: The best way to understand and remember a construct or
keyword is to experiment with it in code, not by reading
about it
MIPS Assembly Code Layout
• Typical Program Layout
0x7fffffff reserved
0x7fffeffc
stack segment
data segment
0x10010000
text segment
(instructions)
0x00400000
reserved
0x00000000
MIPS Assembler Directives
• Top-level Directives:
• .text
• indicates that following items are stored in the user text segment, typically instructions
• .data
• indicates that following data items are stored in the data segment
• .globl sym
• declare that symbol sym is global and can be referenced from other files
MIPS Assembler Directives
• Common Data Definitions:
• .word w1, …, wn
• store n 32-bit quantities in successive memory words
• .half h1, …, hn
• store n 16-bit quantities in successive memory halfwords
• .byte b1, …, bn
• store n 8-bit quantities in successive memory bytes
• .ascii str
• store the string in memory but do not null-terminate it
• strings are represented in double-quotes “str”
• special characters, eg. \n, \t, follow C convention
• .asciiz str
• store the string in memory and null-terminate it
MIPS Assembler Directives
• Common Data Definitions:
• .float f1, …, fn
• store n floating point single precision numbers in successive memory locations
• .double d1, …, dn
• store n floating point double precision numbers in successive memory locations
• .space n
• reserves n successive bytes of space
• .align n
• align the next datum on a 2n byte boundary.
• For example, .align 2 aligns next value on a word boundary.
• .align 0 turns off automatic alignment of .half, .word, etc. till next .data directive
MIPS: Software Conventions
for Registers
0 zero constant 0 16 s0 callee saves
1 at reserved for assembler ...
2 v0 results from callee 23 s7
3 v1 returned to caller 24 t8 temporary (cont’d)
4 a0 arguments to callee 25 t9
5 a1 from caller: caller saves 26 k0 reserved for OS kernel
6 a2 27 k1
7 a3 28 gp pointer to global area
8 t0 temporary 29 sp stack pointer
... 30 fp frame pointer
15 t7 31 ra return Address
caller saves
Pseudoinstructions
• where small means a quantity that can be represented using 16 bits, and
big means a 32 bit quantity. upper( big ) is the upper 16 bits of a 32 bit
quantity. lower( big ) is the lower 16 bits of the 32 bit quantity.
• upper( big ) and lower(big) are not real instructions. If you were to do the
translation, you'd have to break it up yourself to figure out those quantities.
Pseudoinstructions
• As you look through the branch instructions,
you see beq and bne, but not bge (branch on
greater than or equal), bgt (branch on greater
than), ble (branch on less than or equal), blt
(branch on less than). There are no branch
instructions for relational operators!
Pseudoinstructions
• Here's the table for translating pseudoinstructions.
• bge $t0, $s0, LABEL slt $at, $t0, $s0
beq $at, $zero, LABEL
• bgt $t0, $s0, LABEL slt $at, $s0, $t0
bne $at, $zero, LABEL
• ble $t0, $s0, LABEL slt $at, $s0, $t0
beq $at, $zero, LABEL
• blt $t0, $s0, LABEL slt $at, $t0, $s0
bne $at, $zero, LABEL
System Calls
• System Calls (syscall)
• OS-like services
• Method
• Load system call code into register $v0
• Load arguments into registers $a0…$a3
• call system with SPIM instruction syscall
• After call, return value is in register $v0
• Frequently used system calls
System Call Codes
• A Simple Program
#sample example 'add two numbers’
main:
ori $8,$0,0xA # load “10" into register 8
ori $9,$0,0xB # load “11" into register 9
add $10,$8,$9 # add registers 8 and 9, put result
# in register 10
QtSPIM Example Program: swap2memoryWords.asm
## Program to swap two memory words
.text
.globl main
main:
lui $s0, 0x1001 # load data area start address 0x10010000
lw $s1, 0($s0)
lw $s2, 4($s0)
sw $s2, 0($s0)
sw $s1, 4($s0)
QtSPIM Example Program: procCallsProg2.asm
## Procedure call to swap two array words
.text # {
.globl main # int temp;
main: # temp = v[k];
# v[k] = v[k+1];
la $a0, array # v[k+1] = temp;
addi $a1, $0, 0 # }
# swap contents of elements $a1
load para-
meters for # and $a1 + 1 of the array that
swap addi $sp, $sp, -4 # starts at $a0
save return sw $ra, 0($sp) swap: add $t1, $a1, $a1
address $ra add $t1, $t1, $t1
in stack add $t1, $a0, $t1
jump and jal swap lw $t0, 0($t1)
link to swap lw $t2, 4($t1)
lw $ra, 0($sp) sw $t2, 0($t1)
restore
addi $sp, $sp, 4 sw $t0, 4($t1)
return
address jr $ra
jr $ra
jump to $ra .data
# equivalent C code:
array: .word 5, 4, 3, 2, 1
# swap(int v[], int k)
QtSPIM Example Program: systemCalls.asm
lw $t1, 0($t0)
lw $t2, 4($t0)
add $t3, $t1, $t2
## Enter two integers in
## console window sw $t3, 8($t0)
## Sum is displayed system call code
for print_string
.text li $v0, 4
.globl main la $a0, msg1
syscall
main: argument to print_string call
la $t0, value li $v0, 1
system call code
move $a0, $t3 system call code
for read_int syscall for print_int
li $v0, 5
syscall argument to print_int call
li $v0, 10
sw $v0, 0($t0)
syscall system call code
result returned by call for exit
li $v0, 5 .data
syscall value: .word 0, 0, 0
sw $v0, 4($t0) msg1: .asciiz “Sum = “
MARS
Conclusion & More
• The code presented so far should get you started in
writing your own MIPS assembly
• Remember the only way to master the MIPS
assembly language – in fact, any computer language
– is to write lots and lots of code
• For anyone aspiring to understand modern computer
architecture it is extremely important to master MIPS
assembly as all modern computers (since the
mid-80’s) have been inspired by, if not based fully or
partly on the MIPS instruction set architecture