Spim Mips Simulator 08 02
-
Upload
michele-chinosi -
Category
Technology
-
view
7.202 -
download
2
description
Transcript of Spim Mips Simulator 08 02
SPIM: a MIPS simulator
Michele [email protected]
University of Insubria - Varese (IT)
13.04.2007
Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 1 / 26
Outline
1 Assemply ProgrammingThe Assembly Language
2 Program StructureSome introductory notionsMemory UsageCPU RegistersSPIM DirectivesMIPS Instructions SetAssembly Program ElementsSPIM System Calls
Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 2 / 26
The Assembly Language
Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 3 / 26
High-level Language Program example
Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 4 / 26
Assembly Language Example
Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 5 / 26
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
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 originalarchitecture, even after the computer is eclipsed by new, faster, and morecost-effective machines
Assembly programs are longer
Assembly language programs are longer than the equivalent programs writtenin a high-level language
Assembly programs are more difficult to read
Longer programs are more difficult to read and understand and they containmore bugs. Assembly language exacerbates the problem because of itscomplete lack of structure. The resulting programs are hard to read becausethe reader must reconstruct every higher-level construct from its pieces andeach instance of a statement may be slightly different.
Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 7 / 26
Some introductory notions
Comments
Comments in assembler files begins with a sharp-sign (#). Everything from thesharp-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 wordsthat are not valid identifiers.
LabelsLabels are declared by putting them at the very beginning of a line followed by acolon (:).
Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 8 / 26
Some introductory notions
Strings
Strings are enclosed in double-quotes (").
Special characters
Special characters in strings follow the C convention:newline → \ntab → \tquote → \"
Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 9 / 26
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 virtualaddress space (starting at address 7fffffffhex). Like dynamic data, themaximum size of a program’s stack is not known in advance.
Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 10 / 26
Memory Usage
Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 11 / 26
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
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 inthe heap
Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 13 / 26
CPU Registers Summary
Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 14 / 26
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 nalign the next datum on a 2n byte boudary.
.ascii strstore the string in memory, not null-terminated
.asciiz strstore the string in memory, null-terminated
.byte b1, ..., bnstore the n values in successive bytes of memory
.data <addr>the following data items should be stored in the data segment
.double d1, ..., dnstore the n FP double precision numbers in successive memory locations
.extern sym sizethe 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
SPIM Directives
.float f1, ..., fnstore the n FP single precision numbers in successive memory locations
.globl symdeclare that symbol sym is global and can be referenced from other files
.half h1, ..., hnstore 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 nallocate n bytes of space in the data segment
.text <addr>the next items (instructions / words) are put in the user text segment
.word w1, ..., wnstore the n 32-bit quantities in successive memory words
Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 16 / 26
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
MIPS Instructions Set (2)
Instructions FamiliesAll intructions can be grouped in different families depending on their applicationdomain:
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
MIPS Instructions Set (3)
Instructions FamiliesLoad 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
MIPS Instructions Set (4)
PseudoinstructionsThe 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 instructionssequence. 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
Assembly Program Elements
.data # directivefoo: .asciiz "Hello!\n" # string null-terminatedn: .word 1,4,0 # arrayres: .word 0 # .word 0 = memory allocation
.text # directive
.globl main # main becomes global
main: lw $a0, foo # $a0,$a1,$t2,... = registersloop: 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
SPIM System Calls
SPIM provides a small set of operating-system-like services through the systemcall (syscall) instruction. To request a service, a program loads the system callcode into register $v0 and the arguments into registers $a0...$a3/$f12. Systemcalls that return values put their result in register $v0/$f0.
.datastr: .asciiz "the answer is "
.text
.globl mainmain: li $v0, 4 # system call code for print_str
la $a0, str # address of string to printsyscall # print the string
li $v0, 1 # system call code for print_intli $a0, 5 # integer to printsyscall # print it
This code print: the answer is 5
Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 22 / 26
SPIM System Call Table
Service Syscall Code Args Result
print int 1 $a0 = intprint float 2 $f12 = floatprint double 3 $f12 = doubleprint string 4 $a0 = stringread int 5 $v0 = intread float 6 $f0 = floatread double 7 $f0 = doubleread string 8 $a0 = buffer, $a1 = lengthsbrk 9 $a0 = amount $v0 = addressexit 10
Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 23 / 26
Hello, World!
This is a very common simple test program...
.datastr: .asciiz "Hello, World!"
.text
.globl mainmain: li $v0, 4 # system call code for print_str
la $a0, str # address of string to printsyscall # 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
Hello, name!One interactive version of “Hello, World!” program
.datastr: .asciiz "Hello, "iname: .asciiz "Name: "name: .space 256
.text
.globl mainmain:
la $a0, iname # print the promptli $v0, 4syscall
la $a0, name # read the stringli $a1, 256li $v0, 8syscall
li $v0, 4 # print "Hello, "la $a0, strsyscall
li $v0, 4 # print the value insertedla $a0, namesyscall
li $v0, 10 # exitsyscall
Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 25 / 26
Conclusions
ExercisesExamples and Exercises
Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 26 / 26