MIPS and QTSPIM Recap Cptr280 Dr Curtis Nelson MIPS Architecture The MIPS architecture is considered to be a typical RISC architecture Simplified instruction set Programmable storage 32 x 32-bit General Purpose Registers (register 0 is a fixed value of 0) 2^32 bytes of addressable main memory 1
MIPS-32 Instruction Set Architecture Registers R0 - R31 PC HI LO 3 Instruction Formats: all 32 bits wide op rs rt rd sa funct op rs rt immediate op jump target R format I format J format Register Names in MIPS Assembly Language With MIPS, there is a convention for mapping register names into general purpose register numbers. name register number usage $zero 0 constant 0 $v0-$v1 2-3 results $a0-$a3 4-7 arguments $t0-$t7 8-15 temporaries $s0-$s7 16-23 saved $t8-$t9 24-25 more temps $gp 28 global pointer $sp 29 stack pointer $fp 30 frame pointer $ra 31 return address 2
MIPS Arithmetic Instructions MIPS assembly language arithmetic instructions: add $t0, $s1, $s2 sub $t0, $s1, $s2 Each arithmetic instruction performs one operation. Each specifies exactly three operands that are all contained in the datapath's register file ($t0,$s1,$s2) destination source1 op source2 Instruction Format (R format) 0 17 18 8 0 0x22 MIPS Instruction Fields MIPS fields are given names to make them easier to refer to: op rs rt rd shamt funct op 6-bits opcode that specifies the operation rs 5-bits register file address of the first source operand rt 5-bits register file address of the second source operand rd 5-bits register file address of the result s destination shamt 5-bits shift amount (for shift instructions) funct 6-bits function code augmenting the opcode 3
MIPS Memory Model Address space 32 address bits meaning 2 32 unique memory locations MIPS Assembly Language Program Structure Just plain text file with data declarations, program code (name of file should end in suffix.s to be used with SPIM simulator) Data declaration section followed by program code section 4
Data Declarations Placed in section of program identified with assembler directive.data Declares variable names used in program; storage allocated in main memory (RAM) Code Placed in section of text identified with assembler directive.text Contains program code (instructions) Starting point for code execution given label main: Ending point of main code should use exit system call (covered later under System Calls) 5
Comments Anything following # on a line # This stuff would be considered a comment Template for a MIPS assembly language program # Comment giving name of program and description of function # Template.s # Bare-bones outline of MIPS assembly language program.data # variable declarations here #....text main: # indicates start of code (first instruction to execute) # remainder of program code here #... #... 6
Example Program: add2numbersprog1.asm ## Program adds 10 and 11.text.globl main # text section # call main by SPIM 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 Code Translation Example Assumes Variables Stored in Register File if-then-else structure written in C: if (a < 0) then { b = 0 a; d++; } else { b = a; e++;} a is stored in: $t8 b $s0 d $t1 e $t2 if-then-else structure written in MIPS assembly language: bgez $t8, else sub $s0, $r0, $t8 addi $t1, $t1, 1 b next else: mov $s0, $t8 addi $t2, $t2, 1 next: 7
Code Translation Example Assumes Variables Stored in Main Memory QTSPIM Introduction What is SPIM? a simulator that runs assembly programs for MIPS R2000/R3000 RISC computers What does SPIM do? reads MIPS assembly language files and translates to machine language executes the machine language instructions shows contents of registers and memory works as a debugger (supports break-points and single-stepping) provides basic Operating System (OS)-like services, like simple I/O 8
Learning MIPS & SPIM MIPS assembly is a low-level programming language. The best way to learn any programming language is from live code. We will start by going through a few example programs and explaining the key concepts. Further, an examples directory has been created in /wwu/class/cptr280 of several simple, well-documented assembly programs for you to experiment with. Line-by-line syntax can best be learned by picking up what you need from the textbook and on-line tutorials. 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. Using SPIM Loading source file Use File -> Open menu Simulation Simulator -> Settings : In the Display section check only the first two items: Save window positions and General registers in hexadecimal In the Execution section check only Allow pseudo instructions Simulator -> Set Value : to load PC with address of first instruction enter Address or Register Name as PC and enter Value as 0x00400000 reason: the text area of memory, where programs are stored, starts here Simulator -> Go : run loaded program Click the OK button in the Run Parameters pop-up window if the StartingAddress: value is 0x00400000 Simulator -> Break : stop execution Simulator -> Clear Registers and Reinitialize : clean-up before new run 9
Using SPIM Simulator -> Reload : load file again after editing Simulator -> Single Step or Multiple Step : stepping to debug Simulator -> Breakpoints : set breakpoints Notes: Text segment window of SPIM shows assembly and corresponding machine code pseudo-instructions each expand to more than one machine instruction If Load trap file is checked in Simulator -> Settings then text segment shows additional trap-handling code If Delayed Branches is checked in Simulator -> Settings then statementx will execute before control jumps to L1 in following code to avoid this, insert nop before statementx: jal L1 statementx L1: nop SPIM Example Program: add2numbersprog1.asm ## Program adds 10 and 11.text.globl main # text section # call main by SPIM 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 10
MIPS Assembly Code Layout Typical Program Layout.data #data section # user program data.text.globl main #code section #starting point: must be global main: # user program code 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 11
More 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 characters are represented in single quotes a strings are represented in double-quotes str special characters, e.g.. \n, \t, follow C convention.asciiz str store the string in memory and null-terminate it SPIM System Calls System Calls (syscall) OS-like services Method load system call code into register $v0 (see following table for codes) load arguments into registers $a0,, $a3 call system with SPIM instruction syscall after call, return value is in register $v0, or $f0 for floating point results 12
SPIM System Call Codes Service Code (put in $v0) Arguments Result print_int 1 $a0=integer print_float 2 $f12=float print_double 3 $f12=double print_string 4 $a0=address of string read_int 5 int in $v0 read_float 6 float in $f0 read_double 7 double in $f0 read_string 8 $a0=buffer, $a1=length sbrk 9 $a0=amount addr in $v0 exit 10 SPIM Example Program: systemcalls.asm ## Enter two integers in ## console window ## Sum is displayed.text.globl main main: la $t0, value lw $t1, 0($t0) lw $t2, 4($t0) add $t3, $t1, $t2 sw $t3, 8($t0) li $v0, 4 la $a0, msg1 syscall system call code for print_string li $v0, 5 syscall sw $v0, 0($t0) li $v0, 5 syscall sw $v0, 4($t0) system call code for read_int result returned by call li $v0, 1 move $a0, $t3 syscall li $v0, 10 syscall.data value:.word 0, 0, 0 msg1:.asciiz Sum = " argument to print_string call system call code for print_int argument to print_int call system call code for exit 13
More SPIM Example Programs In the class examples directory you will find 18 simple well-documented MIPS assembly programs. Run the code in the order below of increasing complexity. 1. add2numbersprog1 2. add2numbersprog2 3. storewords 4. swap2memorywords 5. branchjump 6. systemcalls 7. overflow 8. averageofbytes 9. printloop 10. sumofsquaresprog1 11. sumofsquaresprog2 12. sumofsquaresprog3 13. proccallsprog1 14. proccallsprog1modified 15. proccallsprog2 16. addfirst100 17. factorialnonrecursive 18. factorialrecursive Conclusions 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. 14