CPS 104 Computer Organization and Programming Lecture-12: MIPS Assembler, SPIM
CPS 104 Computer Organization and Programming Lecture-12: MIPS Assembler, SPIM
CPS104 Lec-12.1
GK Spring 2004
Admin.
Homework-2 is online. Due: Wednesday, Feb. 11, 11:59pm. Use submit to hand-in your homework. Please read the online material in Handouts. Read the online FAQ! Start early!! Use the news group to ask questions. Ask questions in the recitations.
CPS104 Lec-12.2
GK Spring 2004
#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 %d\n, sum) ; }
CPS104 Lec-12.3
GK Spring 2004
# Exit
CPS104 Lec-12.4
GK Spring 2004
CPS104 Lec-12.5
GK Spring 2004
Memory Layout
0x7fffffff
Stack segment
Dynamic data
Data segment
Static data 0x10000000
Text segment
0x400000
CPS104 Lec-12.6
Reserved
GK Spring 2004
Example2
# Example for CPS 104 # Program to add together list of 9 numbers. .text .align .globl main: sw # Code 2 main # MAIN procedure Entrance $s0, 20($sp) # / move $v0, $0 move la la la $s1, $s0, $s2, $s3, $0 list msg list+36 #/ initialize exit code to 0 #\ # \ Initialization # / #/
CPS104 Lec-12.7
GK Spring 2004
Example2 (cont.)
# again: lw add li move syscall li move syscall li la syscall addiu bne $t6, 0(s0) $s1, $s1, $t6 $v0, 4 $a0, $s2 $v0, 1 $a0, $s1 $v0, 4 $a0, nln Main code segment # Begin main loop #\ #/ Actual "work" # SPIM I/O #\ # > Print a string #/ #\ # > Print a number #/ #\ # > Print a string (eol) #/ #\ index update and #/ end of loop
CPS104 Lec-12.8
GK Spring 2004
Example2 (cont.)
# li $v0, 10 syscall .end main # Data Segment .data # Start of data segment .word 35, 16, 42, 19, 55, 91, 24, 61, 53 .asciiz "The sum is " .asciiz "\n" Exit Code #/ #/ # load exit code ; exit end of program
CPS104 Lec-12.9
GK Spring 2004
System call
System call is used to communicate with the system and do simple I/O. Load system call code into Register $v0 Load arguments (if any) into registers $a0, $a1 or $f12 (for floating point). do: syscall Results returned in registers $v0 or $f0.
code 1 2 3 4 5 6 7 8 9 10
service print integer print float print double print string read integer read float read double read string sbrk exit
Result
comments
address in $v0
CPS104 Lec-12.10
GK Spring 2004
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: u logical immediate are zero extended to 32 bits u arithmetic immediate are sign extended to 32 bits The data loaded by the instructions lb and lh are extended as follows: u lbu, lhu are zero extended u lb, lh are sign extended Overflow can occur in these arithmetic and logical instructions: u add, sub, addi
u
it cannot occur in: addu, subu, addiu, and, or, xor, nor, shifts, mult, multu, div, divu
GK Spring 2004
CPS104 Lec-12.11
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
CPS104 Lec-12.12
GK Spring 2004
If ( ... ) { ... };
The C++ code a=b=c=0; if (i < 5) { a = 1; b = 2; c = 3; } d = 5;
Example
The Assembler code move $a0,0 # a=0 move $a1,0 # b=0 move $a2,0 # c=0 bge $s0,5,flbl # if i >=5 # goto flbl move $a0,1 # a=1 move $a1,2 # b=2 move $a2,3 # c=3 flbl: move $a3,5 # d=5
CPS104 Lec-12.13
GK Spring 2004
Example
The Assembler code move $a0,0 # a=0 move $a1,0 # b=0 move $a2,0 # c=0 bge $s0,5,lbl1 # if i >=5 # goto lbl1 move $a0,1 # a=1 move $a1,2 # b=2 move $a2,3 # c=3 j lbl2 # goto lbl2 lbl1: move $a0,4 # a=4 move $a1,5 # b=5 move $a2,6 # c=6 lbl2: move $a3,5 # d=5
CPS104 Lec-12.14
GK Spring 2004
The C++ code la move int a[100]; move . . . move sum = 0; for(i=0;i<100;i++) loop: mult addu sum=sum+a[i]; lw add addi blt $t0,a $a0,0 $a1,0 $a3,100 $a2,$a1,4 $t1,$t0,$a2 $t2,0($t1) $a0,$a0,$t2 $a1,$a1,1 $a1,$a3,loop # # # # # # # # # # # $t0 = &a[0] sum=0 i=0 $a3 = 100 $a2=i*4 $t1=&a[i] $t2=a[i] sum=sum+a[i] i++ if i<100 goto loop
CPS104 Lec-12.15
GK Spring 2004
The C++ code la $t0,a move $a0,0 int a[100]; move $a1,0 . . . move $a3,100 sum = 0; $t2,0($t0) for(i=0;i<100;i++) loop: lw add $a0,$a0,$t2 sum=sum+a[i]; addi $a1,$ai,1 addui $t0,4 blt $a1,$a3,loop # # # # # # # # # # p = &a[0] sum=0 i=0 $a3 = 100 $t2=a[i] sum=sum+a[i] i++ p++ if i<100 goto loop
CPS104 Lec-12.16
GK Spring 2004
Memory Layout
0x7fffffff
Stack segment
Dynamic data
Data segment
Static data 0x10000000
Text segment
0x400000
CPS104 Lec-12.17
Reserved
GK Spring 2004
A A B A B C
Procedures use a frame in the stack to: u Hold values passed to procedures as arguments. u Save registers that a procedure may modify, but which the procedures caller does not want changed. (ex: $s0-$s7) u Save the procedure return address ($ra), u provide space for local variables (variables with local scope) u Evaluate complex expressions. There is a special registers the $sp that are used as special data reference u The stack pointer $sp points to the top of the stack.
CPS104 Lec-12.19
GK Spring 2004
Stack Frame
Many variations on stacks possible (up/down, last pushed / next ) Block structured languages contain link to lexically enclosing frame Compilers normally keep scalar variables in registers, not memory!
GK Spring 2004
CPS104 Lec-12.20
Step-1: Save caller-saved registers u Save registers $t0-$t9 if they contain live values at the call site. Step-2: Pass the arguments: u The first four arguments are passed in registers $a0-$a3 u Remaining arguments are pushed into the stack
CPS104 Lec-12.21
GK Spring 2004
Step-1: Establish stack frame. u Subtract the frame size from the stack pointer. subiu $sp, $sp, <frame-size> u Typically, minimum frame size is 32 bytes (8 words). Step-2: Save callee saved registers in the frame. u Register $ra is saved if routine makes a call. u Registers $a0-$a3 are saved if they are changed. u Registers $s0-$s7 are saved if they are used.
CPS104 Lec-12.22
GK Spring 2004
Step-1: Put returned values in registers $v0, [$v1]. (if values are returned) Step-2: Restore callee-saved registers. t Restore $sp and other saved registers. [$ra,$a0-$a3,$s0-$s7] Step-3: Pop the stack t Add the frame size to $sp. addiu $sp, $sp, <frame-size> Step-4: Return t Jump to the address in $ra. jr $ra
GK Spring 2004
CPS104 Lec-12.23
fact: subiu $sp, $sp, 32 sw $ra, 20($sp) addiu $fp,$sp,28 . . . sw $a0, 0($fp) ... lw $ra, 20($sp) addiu $sp,$sp,32 jr $ra
SP ra ra
SP ra ra