Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Spim part II Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU."— Presentation transcript:

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

2 Outline Useful tables SPIM program examples Array, control, and expression Debug Homework bonus Mid-term Exam

3 Table of MIPS registers

4 Table of system services Set $v0

5 MIPS Assembler Directives Data Types.word,.half - 32/16 bit integer.byte - 8 bit integer (similar to ‘char’ type in C).ascii,.asciiz - string (asciiz is null terminated) Strings are enclosed in double-quotas( ” ) Special characters in strings follow the C convention newline (\n), tab (\t), quote (\”) newline (\n), tab (\t), quote (\”).double,.float - floating point.text - Indicates that following items are stored in the user text segment.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

6 MIPS instruction set MIPS instructions Name Addadd Subtractsub Add immediate addi Load word lw Store word sw Load byte lb Store byte sb Load upper immediate lui Branch on equal beq Branch on not equal bne Set less than slt Set less than immediate slti MIPS instructions NameJumpj Jump register jr Jump and link jal Movemove Multiplymult Multiply immediate multi Load immediate li Branch less than blt Branch less than or equal ble Branch greater than bgt Branch greater than or equal bge

7 Arrays /* 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, 4 # compute offset from the start of the array mul $t2, $t1, 4 # compute offset from the start of the array # assuming sizeof(element)=4 # assuming sizeof(element)=4 add $t2, $t0, $t2 # add the offset to the address of A [0]. add $t2, $t0, $t2 # add the offset to the address of A [0]. # now $t2 = &A [n]. # now $t2 = &A [n]. sw $t3, ($t2) # A [n] = whatever is in $t3. sw $t3, ($t2) # A [n] = whatever is in $t3. lw $t3, ($t2) # $t3 = A [n]. lw $t3, ($t2) # $t3 = A [n].

8 SPIM Program Example I A Simple Program #sample example 'add two numbers’.text # text section.globl main# call main by SPIM main:la $t0, value# load address ‘value’ into $t0 lw $t1, 0($t0)# load word 0(value) into $t1 lw $t2, 4($t0)# load word 4(value) into $t2 add $t3, $t1, $t2# add two numbers into $t3 sw $t3, 8($t0)# store word $t3 into 8($t0).data# data section value:.word 10, 20, 0 # data for addition

9 SPIM Program Example II A Program with System Call #sample example 'system call'.text.globl main main: la $t0, value li $v0, 5# read_integer syscall syscall sw $v0, 0($t0) sw $v0, 0($t0) li $v0, 5# read_integer li $v0, 5# read_integer syscall syscall sw $v0, 4($t0) sw $v0, 4($t0) lw $t1, 0($t0) lw $t2, 4($t0) add $t3, $t1, $t2 sw $t3, 8($t0) li $v0, 4# print_string li $v0, 4# print_string la $a0, msg1 la $a0, msg1 syscall syscall li $v0, 1# print_integer li $v0, 1# print_integer move $a0, $t3 #pseudoinstructions move $a0, $t3 #pseudoinstructions syscall syscall.data value:.word 0, 0, 0 msg1:.asciiz "Result = "

10 SPIM Program Example III A Program with Procedure Call # sample example ‘swap two numbers’.text.globlmain main: la$a0, array addi$a1, $0, 0 addi$sp, $sp, -4 sw$ra, 0($sp) jalswap lw$ra, 0($sp) addi$sp, $sp, 4 jr$ra.data array:.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, $a1 add$t1, $t1, $t1 add$t1, $a0, $t1 lw$t0, 0($t1) lw$t2, 4($t1) sw$t2, 0($t1) sw$t0, 4($t1) jr$ra

11 Control statements (if, if-else) if ( condition ) { statements statements} # MIPS code for the # condition expression. beqz $t0, if_end_label # MIPS code for the statements. if_end_label: if ( condition ) { if-statements if-statements } else { else-statements} # MIPS code for the # condition expression. beqz $t0, if_else_label # MIPS code for the if-statements. j if_end_label if_else_label: # MIPS code for the else-statements. if_end_label:

12 Example 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 j add $s0, $s1, $s2# f = g + h ( skipped if i equals j ) L:sub $s0, $s0, $s3# f = f – i ( always executed )

13 Example A Program with “if-else” if ( i == j ) f = g + h; else f = g - h; # $s0 = f; $s1 = g; $s2 = h # $s3 = i; $s4 = j; bne $s3, $s4, Else# go to Else if i  j add $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:

14 Control statements (while) while ( condition ) { while ( condition ) { statements statements }while_start_label: # MIPS code for the condition expression beqz $t0, while_end_label # MIPS code for the statements. j while_start_label while_end_label: Break ----> j while_end_label Continue ----> j while_start_label

15 Control statements (do-while) do { statements } while ( condition ); do_start_label: # MIPS code for the statements. do_cond_label: # MIPS code for the condition expr: beqz $t0, do_end_label j do_start_label do_end_label: Break---> j do_end_label Continue---> j do_cond_label

16 Example 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 * i add $t1, $t1, $t1# Temp reg $t1 = 4 * i add $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]  k add $s3, $s3, $s4# i = i + j j Loop Exit:

17 Control statements (for) for ( init ; condition ; incr ) { for ( init ; condition ; incr ) { statements statements} # MIPS code for the init expression. for_start_label: # MIPS code for the condition expression beqz $t0, for_end_label # MIPS code for the statements. for_incr_label: # MIPS code for the incr expression. j for_start_label for_end_label: Break ---> j for_end_label Continue---> j for_incr_label

18 Control statements (switch) switch ( expr ) { switch ( expr ) { case const1: statement1 case const2: statement2... case constN: statementNdefault:default-statement} # MIPS code to compute expr. # Assume that this leaves the value in $t0. beq $t0, const1, switch_label_1 beq $t0, const2, switch_label_2... beq $t0, constN, switch_label_N # If there is a default, then add b switch_default # Otherwise, add the following line instead: b switch_end_label switch_label_1: # MIPS code to compute statement1. switch_label_2: # MIPS code to compute statement2....switch_label_N: # MIPS code to compute statementN. # If there's a default: switch_default: # MIPS code to compute default-statement. switch_end_label: Break ---> b switch_end_label

19 Example 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 < 0 bne $t3, $zero, Exit# if k < 0, go to Exit slt $t3, $s5, $t2# Test if k < 4 beq $t3, $zero, Exit# if k >= 4, go to Exit add $t1, $s5, $s5# Temp reg $t1 = 2 * k add $t1, $t1, $t1# Temp reg $t1 = 4 * k add $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 + j j Exit# end of this case so go to Exit L1:add $s0, $s1, $s2# k = 1 so f gets g + h j Exit# end of this case so go to Exit L2:sub $s0, $s1, $s2# k = 2 so f gets g - h j Exit# end of this case so go to Exit L3:sub $s0, $s3, $s4# k = 3 so f gets i - j Exit:# end of switch statement

20 Expression evaluation (and) cond1 && cond2 cond1 && cond2 # MIPS code to compute cond1. # Assume that this leaves the value in $t0. # If $t0 is zero, we're finished (and the result is FALSE). beqz $t0, and_end # MIPS code to compute cond2. # Assume that this leaves the value in $t0. and_end:

21 Expression evaluation (or) cond1 || cond2 cond1 || cond2 # MIPS code to compute cond1. # Assume that this leaves the value in $t0. # If $t0 is not zero, we‘re finished (and the result is TRUE). bnez $t0, or_end # MIPS code to compute cond2. # Assume that this leaves the value in $t0. or_end:

22 Debug Three kinds of error Syntax error Syntax error Your program will not compile Run error Run error Your program will compile but will crash or hang Logical error Logical error Your program will run successfully but not do what you intend

23 Debug syntax error # add.s.text.global main main:la $t0, value lw $t1, 0($t0) lw $t2, 4($t0) add $t3, $t1, $t2 sw $t3, 8($t0).data value:.word 10, 20, 0.globl When loading …

24 Debug run error Set break points

25 Debug run error Set break points

26 Debug run error Set break points

27 Debug logical error Review the program Run the program step by step, and watch the dynamics of registers

28 Homework bonus Practice MIPS procedures Chap3: 3.26-27 ( tail recursion ) Chap3: 3.26-27 ( tail recursion ) Due: 5:00 PM, 11/25/2003 Due: 5:00 PM, 11/25/2003 Save as ID_1.s and ID_2.s, and send to sanlin@cobra.ee.ntu.edu.tw Save as ID_1.s and ID_2.s, and send to sanlin@cobra.ee.ntu.edu.tw sanlin@cobra.ee.ntu.edu.tw The sample code of answers will be online after two weeks

29 Mid-term Exam 日期: 11/11 9:10 AM ~ 11:00 分數分配座位 EE R143: 單數座號 EE R143: 單數座號 EE R105: 雙數座號 EE R105: 雙數座號


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

Similar presentations


Ads by Google