Spim part II Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU.

29
Spim Spim part II part II Chun-Cheng Lin Chun-Cheng Lin ( ( 林林林 林林林 ) ) & Jiunn-Jye Lee ( & Jiunn-Jye Lee ( 林林林 林林林 ) ) CS, EE, NTU CS, EE, NTU

Transcript of Spim part II Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU.

Page 1: Spim part II Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU.

SpimSpimpart IIpart II

Chun-Cheng LinChun-Cheng Lin ( ( 林春成 林春成 ))

& Jiunn-Jye Lee ( & Jiunn-Jye Lee ( 李俊頡 李俊頡 ))

CS, EE, NTUCS, EE, NTU

Page 2: Spim part II Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU.

OutlineOutline

Useful tablesUseful tables

SPIM SPIM pprogram rogram eexamplesxamples

Array, control, and expressionArray, control, and expression

DebugDebug

Homework bonusHomework bonus

Mid-term ExamMid-term Exam

Page 3: Spim part II Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU.

Table of MIPS registersTable of MIPS registers

Page 4: Spim part II Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU.

Table of system servicesTable of system services

Set $v0

Page 5: Spim part II Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU.

MIPS Assembler DirectivesMIPS Assembler Directives

Data TypesData Types.word, .half.word, .half - 32/16 bit integer - 32/16 bit integer.byte.byte - 8 bit integer (similar to ‘char’ type in C) - 8 bit integer (similar to ‘char’ type in C).ascii, .asciiz.ascii, .asciiz - string (asciiz is null terminated) - string (asciiz is null terminated)

Strings are enclosed in double-quotas(Strings are enclosed in double-quotas(””))Special characters in strings follow the C conventionSpecial characters in strings follow the C convention

newlinenewline(\n),(\n), tab tab(\t),(\t), quote quote(\”)(\”)

.double, .float.double, .float - floating point - floating point

.text.text - Indicates that following items are stored in the user text - Indicates that following items are stored in the user text segmentsegment

.data.data - Indicates that following data items are stored in the data - Indicates that following data items are stored in the data segmentsegment

.globl .globl symsym - Declare that symbol - Declare that symbol symsym is global and can be is global and can be referenced from other filesreferenced from other files

Page 6: Spim part II Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU.

MIPS instruction setMIPS instruction set

MIPS instructionsMIPS instructions NameName

AddAdd addadd

SubtractSubtract subsub

Add immediateAdd immediate addiaddi

Load wordLoad word lwlw

Store wordStore word swsw

Load byteLoad byte lblb

Store byteStore byte sbsb

Load upper immediateLoad upper immediate luilui

Branch on equalBranch on equal beqbeq

Branch on not equalBranch on not equal bnebne

Set less thanSet less than sltslt

Set less than immediateSet less than immediate sltislti

MIPS instructionsMIPS instructions NameName

JumpJump jj

Jump registerJump register jrjr

Jump and linkJump and link jaljal

MoveMove movemove

MultiplyMultiply multmult

Multiply immediateMultiply immediate multimulti

Load immediateLoad immediate lili

Branch less thanBranch less than bltblt

Branch less than or equalBranch less than or equal bleble

Branch greater thanBranch greater than bgtbgt

Branch greater than or Branch greater than or equalequal

bgebge

Page 7: Spim part II Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU.

ArraysArrays

/* Address calculation in assembler: *//* Address calculation in assembler: */ address of A [x] = address of A [0] + (x * sizeof (element of A));address of A [x] = address of A [0] + (x * sizeof (element of A));

# $t0 = address of start of A.# $t0 = address of start of A. # $t1 = n.# $t1 = n. mul $t2, $t1, 4mul $t2, $t1, 4 # compute offset from the start of the array # compute offset from the start of the array

# assuming sizeof(element)=4# assuming sizeof(element)=4 add $t2, $t0, $t2add $t2, $t0, $t2 # add the offset to the address of A [0]. # add the offset to the address of A [0]. # now $t2 = &A [n].# now $t2 = &A [n].

sw $t3, ($t2)sw $t3, ($t2) # A [n] = whatever is in $t3.# A [n] = whatever is in $t3. lw $t3, ($t2)lw $t3, ($t2) # $t3 = A [n].# $t3 = A [n].

Page 8: Spim part II Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU.

SPIM Program Example ISPIM Program Example I

A Simple ProgramA Simple Program

##sample example 'add two numbers’sample example 'add two numbers’

.text .text # text section# text section

.globl main.globl main # call main by SPIM# call main by SPIM

main:main: la $t0, valuela $t0, value # load address ‘# load address ‘value’value’ into $t0 into $t0

lw $t1, 0($t0)lw $t1, 0($t0) # load word 0(value) into $t1# load word 0(value) into $t1

lw $t2, 4($t0)lw $t2, 4($t0) # load word 4(value) into $t2# load word 4(value) into $t2

add $t3, $t1, $t2add $t3, $t1, $t2 # add two numbers into $t3# add two numbers into $t3

sw $t3, 8($t0)sw $t3, 8($t0) # store word $t3 into 8($t0)# store word $t3 into 8($t0)

.data.data # data section# data section

value:value: .word 10, 20, 0 .word 10, 20, 0 # data for addition# data for addition

Page 9: Spim part II Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU.

SPIM Program Example IISPIM Program Example II

A Program with System CallA Program with System Call

##sample example 'system call'sample example 'system call'

.text.text

.globl main.globl main

main:main:

la $t0, value la $t0, value

li $v0, 5li $v0, 5 # read_integer# read_integer

syscallsyscall

sw $v0, 0($t0)sw $v0, 0($t0)

li $v0, 5li $v0, 5 # read_integer# read_integer

syscallsyscall

sw $v0, 4($t0) sw $v0, 4($t0)

lw $t1, 0($t0)lw $t1, 0($t0)

lw $t2, 4($t0)lw $t2, 4($t0)

add $t3, $t1, $t2add $t3, $t1, $t2

sw $t3, 8($t0)sw $t3, 8($t0)

li $v0, 4li $v0, 4 # print_string# print_string

la $a0, msg1la $a0, msg1

syscallsyscall li $v0, 1li $v0, 1 # print_integer# print_integer move $a0, $t3move $a0, $t3 #pseudoinstructions #pseudoinstructions

syscallsyscall

.data.data

value: .word 0, 0, 0value: .word 0, 0, 0

msg1: .asciiz "Result = "msg1: .asciiz "Result = "

Page 10: Spim part II Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU.

SPIM Program Example IIISPIM Program Example III

A Program with Procedure CallA Program with Procedure Call

# sample example ‘swap two numbers’.text.globl mainmain:

la $a0, arrayaddi $a1, $0, 0

addi $sp, $sp, -4sw $ra, 0($sp)

jal swap

lw $ra, 0($sp)addi $sp, $sp, 4

jr $ra

.dataarray: .word 5, 4, 3, 2, 1

.text# swap(int v[], int k)# {# int temp;# temp = v[k];# v[k] = v[k+1];# v[k+1] = temp;# }

swap: add $t1, $a1, $a1add $t1, $t1, $t1add $t1, $a0, $t1lw $t0, 0($t1) lw $t2, 4($t1)sw $t2, 0($t1)sw $t0, 4($t1)jr $ra

Page 11: Spim part II Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU.

Control statements (if, if-else)Control statements (if, if-else)

if ( condition ) {if ( condition ) {

statementsstatements

}}

# MIPS code for the# MIPS code for the

# condition expression.# condition expression.

beqz $t0, if_end_labelbeqz $t0, if_end_label

# MIPS code for the # MIPS code for the statementsstatements..

if_end_label:if_end_label:

if ( condition ) {if ( condition ) { if-statementsif-statements} else {} else {

else-statementselse-statements}}

# MIPS code for the# MIPS code for the# condition expression.# condition expression.beqz $t0, if_else_labelbeqz $t0, if_else_label# MIPS code for the # MIPS code for the if-statementsif-statements..j if_end_labelj if_end_label

if_else_label:if_else_label:# MIPS code for the # MIPS code for the else-else-statementsstatements..

if_end_label:if_end_label:

Page 12: Spim part II Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU.

ExampleExample

A Program with “if”A Program with “if”

if ( i == j ) go to L1;f = g + h;

L: f = f – i;

# $s0 = f; $s1 = g; $s2 = h# $s3 = i; $s4 = j;

beq $s3, $s4, L1 # go to L1 if i equals jadd $s0, $s1, $s2 # f = g + h ( skipped if i equals j )

L: sub $s0, $s0, $s3 # f = f – i ( always executed )

Page 13: Spim part II Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU.

ExampleExample

A Program with “if-else”A Program with “if-else”

if ( i == j )f = g + h;

elsef = g - h;

# $s0 = f; $s1 = g; $s2 = h# $s3 = i; $s4 = j;

bne $s3, $s4, Else # go to Else if i jadd $s0, $s1, $s2 # f = g + h ( skipped if i j )j Exit # go to Exit

Else: sub $s0, $s1, $s2 # f = g – h ( skipped if i = j )Exit:

Page 14: Spim part II Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU.

Control statements (while)Control statements (while)

while ( condition ) {while ( condition ) { statementsstatements }}

while_start_label:while_start_label:# MIPS code for the condition expression# MIPS code for the condition expressionbeqz $t0, while_end_labelbeqz $t0, while_end_label# MIPS code for the # MIPS code for the statementsstatements..j while_start_labelj while_start_label

while_end_label:while_end_label:

Break Break ----> ----> j while_end_labelj while_end_labelContinueContinue ----> ----> j while_start_labelj while_start_label

Page 15: Spim part II Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU.

Control statements (do-while)Control statements (do-while)

do {do {statementsstatements

} while ( condition );} while ( condition );

do_start_label:do_start_label:# MIPS code for the # MIPS code for the statementsstatements..

do_cond_label:do_cond_label:# MIPS code for the condition expr:# MIPS code for the condition expr:beqz $t0, do_end_labelbeqz $t0, do_end_labelj do_start_labelj do_start_label

do_end_label:do_end_label:

BreakBreak ---> ---> j do_end_labelj do_end_labelContinueContinue ---> ---> j do_cond_labelj do_cond_label

Page 16: Spim part II Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU.

ExampleExample

A Program with “while”A Program with “while”

while ( save[i] == k )i = i + j;

# $s3 = i; $s4 = j; $s5 = k# $s6 = address of save[0]

Loop: add $t1, $s3, $s3 # Temp reg $t1 = 2 * iadd $t1, $t1, $t1 # Temp reg $t1 = 4 * iadd $t1, $t1, $s6 # $t1 = address of save[i]lw $t0, 0($t1) # Temp reg $t0 = save[i]bne $t0, $s5, Exit # go to Exit if save[i] kadd $s3, $s3, $s4 # i = i + jj Loop

Exit:

Page 17: Spim part II Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU.

Control statements (for)Control statements (for)

for ( init ; condition ; incr ) {for ( init ; condition ; incr ) { statementsstatements}}

# MIPS code for the init expression.# MIPS code for the init expression.for_start_label:for_start_label:

# MIPS code for the condition expression# MIPS code for the condition expressionbeqz $t0, for_end_labelbeqz $t0, for_end_label# MIPS code for the # MIPS code for the statementsstatements..

for_incr_label:for_incr_label:# MIPS code for the # MIPS code for the incr expressionincr expression..j for_start_labelj for_start_label

for_end_label:for_end_label:BreakBreak ---> ---> j for_end_labelj for_end_labelContinueContinue ---> ---> j for_incr_labelj for_incr_label

Page 18: Spim part II Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU.

Control statements (switch)Control statements (switch)

switch ( expr ) {switch ( expr ) {

case const1:case const1:

statement1statement1

case const2:case const2:

statement2statement2

......

case constN:case constN:

statementNstatementN

default:default:

default-default-statementstatement

}}

# MIPS code to compute expr.# MIPS code to compute expr.# Assume that this leaves the value in $t0.# Assume that this leaves the value in $t0.

beq $t0, const1, switch_label_1beq $t0, const1, switch_label_1beq $t0, const2, switch_label_2beq $t0, const2, switch_label_2......beq $t0, constN, switch_label_Nbeq $t0, constN, switch_label_N# If there is a default, then add# If there is a default, then addb switch_defaultb switch_default# Otherwise, add the following line instead:# Otherwise, add the following line instead:b switch_end_labelb switch_end_label

switch_label_1:switch_label_1:# MIPS code to compute # MIPS code to compute statement1statement1..

switch_label_2:switch_label_2:# MIPS code to compute # MIPS code to compute statement2statement2..

......switch_label_N:switch_label_N:

# MIPS code to compute # MIPS code to compute statementNstatementN..# If there's a default:# If there's a default:switch_default:switch_default:

# MIPS code to compute # MIPS code to compute default-statementdefault-statement..switch_end_label:switch_end_label:

BreakBreak ---> ---> b switch_end_labelb switch_end_label

Page 19: Spim part II Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU.

ExampleExampleA Program with “switch”A Program with “switch”

switch ( k ) {case 0: f = i + j; break; /* k = 0 */

case 1: f = g + h; break; /* k = 1 */case 2: f = g - h; break; /* k = 2 */case 3: f = i - j; break; /* k = 3 */

}

# $s0 = f; $s1 = g; $s2 = h;# $s3 = i; $s4 = j; $s5 = k;# $t2 = 4;

slt $t3, $s5, $zero # Test if k < 0bne $t3, $zero, Exit # if k < 0, go to Exitslt $t3, $s5, $t2 # Test if k < 4beq $t3, $zero, Exit # if k >= 4, go to Exitadd $t1, $s5, $s5 # Temp reg $t1 = 2 * kadd $t1, $t1, $t1 # Temp reg $t1 = 4 * kadd $t1, $t1, $t4 # $t1 = address of JumpTable[k]lw $t0, 0($t1) # Temp reg $t0 = JumpTable[k]jr $t0 # jump based on register $t0

L0: add $s0, $s3, $s4 # k = 0 so f gets i + jj Exit # end of this case so go to Exit

L1: add $s0, $s1, $s2 # k = 1 so f gets g + hj Exit # end of this case so go to Exit

L2: sub $s0, $s1, $s2 # k = 2 so f gets g - hj Exit # end of this case so go to Exit

L3: sub $s0, $s3, $s4 # k = 3 so f gets i - jExit: # end of switch statement

Page 20: Spim part II Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU.

Expression evaluation (and)Expression evaluation (and)

cond1 && cond2cond1 && cond2

# MIPS code to # MIPS code to compute cond1compute cond1..

# Assume that this # Assume that this leaves the value in $t0leaves the value in $t0..

# If $t0 is zero, we're finished (and the result is FALSE).# If $t0 is zero, we're finished (and the result is FALSE).

beqz $t0, and_endbeqz $t0, and_end

# MIPS code to # MIPS code to compute cond2compute cond2..

# Assume that this # Assume that this leaves the value in $t0leaves the value in $t0..

and_end:and_end:

Page 21: Spim part II Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU.

Expression evaluation (or)Expression evaluation (or)

cond1 || cond2cond1 || cond2

# MIPS code to # MIPS code to compute cond1compute cond1..

# Assume that this # Assume that this leaves the value in $t0leaves the value in $t0..

# If $t0 is not zero, we‘re finished (and the result is TRUE).# If $t0 is not zero, we‘re finished (and the result is TRUE).

bnez $t0, or_endbnez $t0, or_end

# MIPS code to # MIPS code to compute cond2compute cond2..

# Assume that this # Assume that this leaves the value in $t0leaves the value in $t0..

or_end:or_end:

Page 22: Spim part II Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU.

DebugDebug

Three kinds of errorThree kinds of error Syntax errorSyntax error

Your program will not compileYour program will not compile Run errorRun error

Your program will compile but will crash or hangYour program will compile but will crash or hang Logical errorLogical error

Your program will run successfully but not do what Your program will run successfully but not do what you intendyou intend

Page 23: Spim part II Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU.

Debug syntax errorDebug syntax error

# add.s# add.s

.text .text

.global main.global main

main:main: la $t0, valuela $t0, valuelw $t1, 0($t0)lw $t1, 0($t0)lw $t2, 4($t0)lw $t2, 4($t0)add $t3, $t1, $tadd $t3, $t1, $t

22sw $t3, 8($t0)sw $t3, 8($t0)

.data.datavalue:value: .word 10, 20, 0.word 10, 20, 0

.globlWhen loading …

Page 24: Spim part II Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU.

Debug run errorDebug run error

Set break pointsSet break points

Page 25: Spim part II Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU.

Debug run errorDebug run error

Set break pointsSet break points

Page 26: Spim part II Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU.

Debug run errorDebug run error

Set break pointsSet break points

Page 27: Spim part II Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU.

Debug logical errorDebug logical error

Review the programReview the program

Run the program step by step, and watch Run the program step by step, and watch the dynamics of registersthe dynamics of registers

Page 28: Spim part II Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU.

Homework bonusHomework bonus

Practice MIPS proceduresPractice MIPS procedures Chap3: 3.26-27 ( tail recursion )Chap3: 3.26-27 ( tail recursion ) Due: 5:00 PM, 11/25/2003Due: 5:00 PM, 11/25/2003 Save as ID_1.s and ID_2.s, and send to Save as ID_1.s and ID_2.s, and send to

[email protected]@cobra.ee.ntu.edu.tw

The sample code of answers will be online The sample code of answers will be online after two weeksafter two weeks

Page 29: Spim part II Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU.

Mid-term ExamMid-term Exam

日期:日期: 11/11 9:10 AM ~ 11:0011/11 9:10 AM ~ 11:00

分數分配分數分配座位座位 EE R143: EE R143: 單數座號單數座號 EE R105: EE R105: 雙數座號雙數座號