0% found this document useful (0 votes)
75 views24 pages

CPS 104 Computer Organization and Programming Lecture-12: MIPS Assembler, SPIM

Computer Organization and design

Uploaded by

praches
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views24 pages

CPS 104 Computer Organization and Programming Lecture-12: MIPS Assembler, SPIM

Computer Organization and design

Uploaded by

praches
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

CPS 104 Computer Organization and Programming Lecture-12 : MIPS Assembler, SPIM.

Feb. 6, 2004 Gershon Kedem http://kedem.duke.edu/cps104/Lectures

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

Review: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 %d\n, sum) ; }

CPS104 Lec-12.3

GK Spring 2004

Review: Assembly Language Example1:


.text .align 2 main: la loop: lw mul lw add sw addi sw ble la lw jal $14, 4($10) $15, $14, $14 $24, 0($10) $25, $24, $15 $25, 0($10) $8, $14, 1 $8, 4($10) $8, 100, loop $4, str $5, 0($10) printf $10, Temp .data .align 2 .word 0, 0 li $2, 10 syscall

# Exit

Temp: str: .asciiz The sum from 0 .. 100 is %d\n

CPS104 Lec-12.4

GK Spring 2004

Review: MIPS: Software conventions for Registers


0 1 2 3 4 5 6 7 8 ... 15 t7 zero constant 0 at reserved for assembler 16 s0 callee saves ... 23 s7 24 t8 25 t9 26 k0 reserved for OS kernel 27 k1 28 gp Pointer to global area temporary: caller saves 29 sp Stack pointer 30 fp 31 ra frame pointer Return Address (HW) temporary (contd)

v0 expression evaluation & v1 function results a0 arguments a1 a2 a3 t0

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

$s0, $s0, 4 $s0, $s3, again

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

list: msg: nln:

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

Arguments $a0 $f12 $f12,$f13 $a0

Result

comments

(address) integer in $v0 float in $f0 double in $f0

$a0=buffer, $a1=length $a0=amount

address in $v0

CPS104 Lec-12.10

GK Spring 2004

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: 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

Assembly Examples: code fragments

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

Assembly Examples: code fragments

If ( ... ){ ... } else { ...} ;


The C++ code a=b=c=0; if (i < 5) { a = 1; b = 2; c = 3; } else{ a = 4; b = 5; c = 6; } d = 5;

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

Assembly Examples: code fragments

for(i=0; i<100; i++)sum=sum+A[i]; Example-1:


The Assembler code

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

Assembly Examples: code fragments

for(i=0; i<100; i++)sum=sum+A[i];Example-2:


The Assembler code

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

Calls: Why Are Stacks So Great?


Stacking of Subroutine Calls & Returns and Environments: A: CALL B B: CALL C C: RET A B RET A Some machines provide a memory stack as part of the architecture (e.g., VAX) Sometimes stacks are implemented via software convention (e.g., MIPS)
CPS104 Lec-12.18
GK Spring 2004

A A B A B C

Procedure Call (Stack) Frame

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

Call-Return Linkage: Stack Frames


High Mem
Argument 6 Argument 5 Reference is relative to the stack pointer $SP

Callee Save Registers


ra, [a0-a3] [s0-s6]

Stack Frame

Local Variables SP Low Mem

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

MIPS/GCC Procedure Calling Conventions Calling Procedure:

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

(in reversed order arg5 is at the top of the stack).

Step-3: Execute a jal instruction.

CPS104 Lec-12.21

GK Spring 2004

MIPS/GCC Procedure Calling Conventions (cont.)


Called Routine

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

MIPS/GCC Procedure Calling Conventions (cont.)

On return from a call


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

MIPS / GCC Calling Conventions


SP ra low address

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

First four arguments are passed in registers!


CPS104 Lec-12.24
GK Spring 2004

You might also like