On Textbook CD: SPIM Jen-Chang Liu, 2006
Simulation of a virtual machine Virtual machine (simulator) Differences between SPIM and real MIPS? No delayed branch SPIM simulates the richer virtual machine SPIM: MIPS simulator MIPS code Real machine: Window, Linux, … VMware: Window, Linux OS Real machine: Window, Linux, … user
Setup SPIM Extract files from (uninstall version) files: pcspim.exe, pcspim.ico, hello.s, exceptions.s You can get the full version from my homepage Setup environment Modify it to your current directory
MIPS CPU Floating point
Assembly Variables: Registers (1/4) Unlike HLL, assembly cannot use variables Why not? Keep Hardware Simple Assembly Operands are registers limited number of special locations built directly into the hardware operations can only be performed on these! Benefit: Since registers are directly in hardware, they are very fast
Assembly Variables: Registers (2/4) Drawback: Since registers are in hardware, there are a predetermined number of them Solution: MIPS code must be very carefully put together to efficiently use registers 32 registers in MIPS Why 32? Smaller is faster Each MIPS register is 32 bits wide Groups of 32 bits called a word in MIPS
Assembly Variables: Registers (3/4) Registers are numbered from 0 to 31 Each register can be referred to by number or name Number references: $0, $1, $2, … $30, $31
Assembly Variables: Registers (4/4) By convention, each register also has a name to make it easier to code For now: $16 - $22 $s0 - $s7 (correspond to C variables) $8 - $15 $t0 - $t7 (correspond to temporary variables) In general, use names to make your code more readable
Addition and Subtraction (1/2) Syntax of Instructions: 12,3,4 where: 1) operation by name 2) operand getting result ( “ destination ” ) 3) 1st operand for operation ( “ source1 ” ) 4) 2nd operand for operation ( “ source2 ” ) Syntax is rigid: 1 operator, 3 operands Why? Keep Hardware simple via regularity °Example: add$s0,$s1,$s2
Addition and Subtraction (2/2) Addition in Assembly Example: add$s0,$s1,$s2 (in MIPS) Equivalent to: a = b + c (in C) where registers $s0,$s1,$s2 are associated with variables a, b, c Subtraction in Assembly Example: sub$s3,$s4,$s5 (in MIPS) Equivalent to: d = e - f (in C) where registers $s3,$s4,$s5 are associated with variables d, e, f
MIPS assembly syntax # hello.s.globl main main:.data str:.asciiz "Hello, World!".text li $v0, 4# system call code for print_str la $a0, str# address of string to print syscall# print the string comments labels ( ends with a colon : ) directives
MIPS directives 命令 (see p.A- 51,A-52).globl sym sym is global and can be referenced from other files.data Subsequent items are stored in the data segment.text Subsequent items (instructions or.word) are put in the user text segment.asciiz str Store the string str in memory and null-terminate it
MIPS memory (Textbook A.5) : holds program’s instructions Object whose size is known to the compiler Ex. C global variables Data is allocated by the program as it executes. Ex. malloc() in C Program dynamically push values on the stack
Register window Text window Data & Stack window 32 bits addressMachine code Bare machine instruction Assembly code Message window
Example: MIPS assembly data.asciiz "Hello, World!" Hello,Wordl ! \0 NOTE: one byte in an address
System calls syscall: SPIM provides a small set of operating-system-like services 3 steps: li $v0, 4 #system call code la $a0, str #set argument syscall
3/13 Lab#1