CENG 311 MIPS Assembly Programming and SPIM Simulator
CENG 311 MIPS Assembly Programming and SPIM Simulator
Assembler and Assembly Language Machine language is a sequence of binary words. Assembly language is a text representation for machine language plus extras that make assembly language programming easier (more readable too!). program compiler Assembler Linker executable code
The SPIM Simulator SPIM is a simulator that let you run and debug MIPS assembler programs. XSPIM is an X-window version of the simulator. The simulator allows you to look at the content of registers and memory, and to single step through the simulation. Documentation in postscript format and PDF format are in the course homepage Also available a MAN page for SPIM. Try to run the provided example of an simple assembler program: MIPS. asm
Assembly Language One instruction per line. Numbers: are base-10 integers or Hex. Identifiers: alphanumeric, _, string starting in a letter or _ Labels: identifiers starting at the beginning of a line followed by “: ” Comments: everything following # till end-of-line. Instruction format: Space and “, ” separated fields. [Label: ] <op> Arg 1, [Arg 2], [Arg 3] [# comment] [Label: ] <op> arg 1, offset(reg) . Directive [arg 1], [arg 2], . . . [# comment]
Assembly Language (cont. ) Pseudo-instructions: extending the instruction set for convenience. Examples: move $2, $4 Translates to: add $2, $4, $0 # $2 = $4, (copy $4 to $2) li $8, 40 addi $8, $0, 40 # $8 = 40, (load 40 into $8) sd $4, 0($29) sw $4, 0 ($29) sw $5, 4($29) # mem[$29] = $4; Mem[$29+4] = $5 la $4, 0 x 1000056 c lui $4, 0 x 1000 ori $4, 0 x 056 c # Load address $4 = <address>
Assembly Language (cont. ) Directives: “. ”<string> [arg 1], [arg 2]. . . Examples: . align n # align datum on 2 n byte boundary. . ascii <string> # store a string in memory. . asciiz <string> # store a null terminated string in memory. data [address] # start a data segment. # [optional beginning address]. text [address] # start a code segment. . word w 1, w 2, . . . , wn # store n words in memory.
The routine written in C #include <stdio> int main ( ) { int i; int sum = 0; for(i=0; i <= 100; i++) sum = sum + i*i ; printf(“The sum from 0. . 100 is %dn”, sum) ; }
Assembly Language Example 1: . text. align 2 main: la $10, Temp lw mul lw add sw addi sw ble la li syscall li lw syscall $14, 4($10) $15, $14 $24, 0($10) $25, $24, $15 $25, 0($10) $8, $14, 1 $8, 4($10) $8, 100, loop $4, str $2, 4 li $2, 10 syscall # Exit loop: $2, 1 $4, 0($10) . data. align 2. word 0, 0 Temp: str: . asciiz The sum from 0. . 100 is
MIPS: Software conventions for Registers 0 zero constant 0 16 s 0 callee saves 1 at . . . 2 v 0 expression evaluation & 23 s 7 3 v 1 function results 24 t 8 4 a 0 arguments 25 t 9 5 a 1 26 k 0 reserved for OS kernel 6 a 2 27 k 1 7 a 3 28 gp Pointer to global area 8 t 0 reserved for assembler temporary: caller saves temporary (cont’d) 29 sp Stack pointer . . . 30 fp frame pointer 15 t 7 31 ra Return Address (HW)
0 x 7 fffffff Memory Layout Stack segment Dynamic data Static data Data segment 0 x 10000000 Text segment 0 x 400000 Reserved
Example 2 # Example for CPS 104 # Program to add together list of 9 numbers. . text. align. globl # Code 2 main: subiu sw sw sw move $sp, $ra, $s 3, $s 2, $s 1, $s 0, $v 0, 40 36($sp) 32($sp) 28($sp) 24($sp) 20($sp) $0 # MAIN procedure Entrance # Push the stack # Save return address # # > Entry Housekeeping # / save registers on stack # / #/ initialize exit code to 0 move la la la $s 1, $s 0, $s 2, $s 3, $0 list msg list+36 # # Initialization # / #/
Example 2 (cont. ) # Main code segment again: lw add $t 6, 0(s 0) $s 1, $t 6 li move syscall li la syscall $v 0, 4 $a 0, $s 2 addiu bne $s 0, 4 $s 0, $s 3, again $v 0, 1 $a 0, $s 1 $v 0, 4 $a 0, nln # Begin main loop # #/ Actual "work" # SPIM I/O # # > Print a string #/ # # > Print a number #/ # # > Print a string (eol) #/ # index update and #/ end of loop
Example 2 (cont. ) # Exit Code move lw lw lw addiu jr. end # list: msg: nln: $v 0, $0 $s 0, 20($sp) $s 1, 24($sp) $s 2, 28($sp) $s 3, 32($sp) $ra, 36($sp) $sp, 40 $ra main # # # Closing Housekeeping # / restore registers # / load return address # / Pop the stack #/ exit(0) ; # end of program Data Segment. data # Start of data segment. word 35, 16, 42, 19, 55, 91, 24, 61, 53. asciiz "The sum is ". asciiz "n"
System call is used to communicate with the system and do simple I/O. Load system call code into Register $v 0 Load arguments (if any) into registers $a 0, $a 1 or $f 12 (for floating point). do: syscall Results returned in registers $v 0 or $f 0. code 1 2 3 4 5 6 7 8 service print integer print float print double print string read integer read float read double read string 9 10 sbrk exit Arguments Result $a 0 $f 12 $a 0 (address) integer in $v 0 float in $f 0 double in $f 0 $a 0=buffer, $a 1=length $a 0=amount comm address in $v 0
Details of the MIPS instruction set Register zero always has the value zero (even if you try to write it) Link instructions put the return address PC+4 into the link register All instructions change all 32 bits of the destination register (including lui, lb, lh) and all read all 32 bits of sources (add, sub, and, or, …) Immediate arithmetic and logical instructions are extended as follows: logical immediate are zero extended to 32 bits arithmetic immediate are sign extended to 32 bits The data loaded by the instructions lb and lh are extended as follows: lbu, lhu are zero extended lb, lh are sign extended Overflow can occur in these arithmetic and logical instructions: add, sub, addi it cannot occur in addu, subu, addiu, and, or, xor, nor, shifts, multu, divu
Miscellaneous MIPS Instructions break A breakpoint trap occurs, transfers control to exception handler syscall A system trap occurs, transfers control to exception handler coprocessor instrs. Support for floating point. TLB instructions Support for virtual memory: discussed later restore from exception Restores previous interrupt mask & kernel/user mode bits into status register load word left/right Supports misaligned word loads store word left/right Supports misaligned word stores
- Slides: 16