1 / 29

Spim part II

Spim part II. Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU. Outline. Useful tables SPIM p rogram e xamples Array, control, and expression Debug Homework bonus Mid-term Exam. Table of MIPS registers. Set $v0. Table of system services. MIPS Assembler Directives.

arlais
Download Presentation

Spim part II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Spimpart 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. Set $v0 Table of system services

  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(\”) .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

  7. Arrays /* Address calculation in assembler: */ address of A [x] = address of A [0] + (x * sizeof (element of A)); # $t0 = address of start of A. # $t1 = n. mul $t2, $t1, 4 # compute offset from the start of the array # assuming sizeof(element)=4 add $t2, $t0, $t2 # add the offset to the address of A [0]. # now $t2 = &A [n]. sw $t3, ($t2) # A [n] = whatever is in $t3. 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 sw $v0, 0($t0) li $v0, 5 # read_integer syscall 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 la $a0, msg1 syscall li $v0, 1 # print_integer move $a0, $t3 #pseudoinstructions 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 .globl main main: la $a0, array addi $a1, $0, 0 addi $sp, $sp, -4 sw $ra, 0($sp) jal swap 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. if ( condition ) { statements } # MIPS code for the # condition expression. beqz $t0, if_end_label # MIPS code for the statements. if_end_label: if ( condition ) { 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: Control statements (if, if-else)

  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 ) { 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 ) { 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. switch ( expr ) { case const1: statement1 case const2: statement2 ... case constN: statementN default: 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 Control statements (switch)

  19. Example • 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 */ • } • A Program with “switch” # $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 # 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 # 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 • Your program will not compile • Run error • Your program will compile but will crash or hang • Logical error • Your program will run successfully but not do what you intend

  23. When loading … .globl 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

  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 ) • 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 • 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 R105: 雙數座號

More Related