Spim Mips Simulator 08 02
Upcoming SlideShare
Loading in...5
×
 

Spim Mips Simulator 08 02

on

  • 11,103 views

A lecture about the first approach for programming using SPIM, the MIPS architecture simulator

A lecture about the first approach for programming using SPIM, the MIPS architecture simulator

Statistics

Views

Total Views
11,103
Slideshare-icon Views on SlideShare
11,081
Embed Views
22

Actions

Likes
2
Downloads
121
Comments
1

2 Embeds 22

http://www.slideshare.net 21
http://webcache.googleusercontent.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

CC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

11 of 1

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Spim Mips Simulator 08 02 Spim Mips Simulator 08 02 Presentation Transcript

    • 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
    • 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
    • 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 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
    • 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
    • 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
    • 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
    • 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 in the 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 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
    • 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
    • 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 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
    • 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
    • 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
    • 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
    • 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
    • SPIM System Call Table Service Syscall Code Args Result $a0 = int $f12 = float print int 1 $f12 = double print float 2 $a0 = string print double 3 $v0 = int print string 4 $f0 = float read int 5 $f0 = double read float 6 $a0 = buffer, $a1 = length read double 7 $a0 = amount $v0 = address read string 8 sbrk 9 exit 10 Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 23 / 26
    • 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
    • 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
    • Conclusions Exercises Examples and Exercises Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 26 / 26