1 / 52

Computer Architecture CSE 3322

Computer Architecture CSE 3322. Lecture 3 crystal.uta.edu/~jpatters Assignment: 3.1, 3.2, 3.3, 3.4, 3.10 Due Mon 9/8/03 Read 3:8 – 3.10. Computer Architecture CSE 3322. Graduate Teaching Assistant Pramod Kumar Office Hours: Tues – Thurs 10 – 1 pm 114 Engr Annex West

abiba
Download Presentation

Computer Architecture CSE 3322

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. Computer Architecture CSE 3322 Lecture 3 crystal.uta.edu/~jpatters Assignment: 3.1, 3.2, 3.3, 3.4, 3.10 Due Mon 9/8/03 Read 3:8 – 3.10

  2. Computer Architecture CSE 3322 Graduate Teaching Assistant Pramod Kumar Office Hours: Tues – Thurs 10 – 1 pm 114 Engr Annex West pxk 3008@exchange.uta.edu

  3. MIPS Assembly Instructions Instruction Example Meaning add add $s1, $s2, $s3 $s1 = $s2 + $s3 subtract sub $s1, $s2, $s3 $s1 = $s2 - $s3 $s1, $s2, $s3, … are registers. The $ indicates a Register in the MIPS Assembly Language

  4. MIPS Assembly Instructions Instruction Example Meaning load word lw $s1, 300 ($s2) $s1 = Mem[$s2+300] store word sw $s1, 300 ($s2) Mem[$s2+300] = $s1 $s1, $s2, $s3, … are registers 300 is a constant

  5. Instructions for Making Decisions if – then – else Construct if ( i = = j ) a = b; else a = c; Yes No i = =j a = b a= c Exit:

  6. Instructions for Making Decisions if – then – else Construct if ( i = = j ) a=b; else a=c; Yes No beq i = =j branch on equal a = b a= c Exit:

  7. Instructions for Making Decisions if – then – else Construct if ( i = = j ) a=b; else a=c; Yes bne No beq i = =j branch on not equal branch on equal a = b a= c Exit:

  8. Instructions for Making Decisions if – then – else Construct if ( i = = j ) a=b; else a=c; Yes bne No beq i = =j branch on not equal branch on equal a = b a= c Exit: jump j

  9. branch on equal beq rs, rt, Label means if (rs = =rt) go to Label I type format op = 4 Label is the target statement label. It is an address that is calculated by the assembler. Instr Format op rs rt address beq I 4 reg reg “Label” bits 6 5 5 16

  10. branch on equal beq rs, rt, Label means if (rs = =rt) go to Label I type format op = 4 branch on not equal bne rs, rt, Label means if (rs != rt) go to Label I type format op = 5 Label is the target statement label. It is an address that is calculated by the assembler.

  11. branch on equal beq rs, rt, Label means if (rs = =rt) go to Label I type format op = 4 branch on not equal bne rs, rt, Label means if (rs != rt) go to Label I type format op = 5 jump j Label means go to Label J type format op = 2 Label is the target statement label. It is an address that is calculated by the assembler.

  12. if ( i = = j ) a=b; else a=c; Yes No beq bne i = =j branch on equal branch on not equal a = b a= c Exit: j jump branch Else #go to Else if ? statement 1 j Exit #go to Exit Else: statement 2 Exit:

  13. if ( i = = j ) a=b; else a=c; Yes No beq bne i = =j branch on equal branch on not equal a = b a= c Exit: j jump branch Else #go to Else if ? Most Likely statement 1 Case j Exit #go to Exit Else: statement 2 Exit:

  14. if ( i = = j ) a=b; else a=c; Yes bne No beq i = =j branch on equal branch on not equal a ~ $s1 b ~ $s2 c ~ $s3 i ~ $s4 j ~ $s5 a = b a= c Exit: j jump

  15. if ( i = = j ) a=b; else a=c; Yes bne No beq i = =j branch on equal branch on not equal a ~ $s1 b ~ $s2 c ~ $s3 i ~ $s4 j ~ $s5 a = b a= c Exit: j jump bne $s4, $s5, Else # go to Else if i!=j Else: add $s1, $s3, $zero # a=c , Note: $zero is 0 Exit:

  16. if ( i = = j ) a=b; else a=c; Yes bne No beq i = =j branch on equal branch on not equal a ~ $s1 b ~ $s2 c ~ $s3 i ~ $s4 j ~ $s5 a = b a= c Exit: j jump bne $s4, $s5, Else # go to Else if i!=j add $s1, $s2, $zero # a=b j Exit # go to Exit Else: add $s1, $s3, $zero # a=c Exit:

  17. i ~ $s1 j ~ $s2 base of A ~ $s3 while ( A[i] >= 0) i = i + j MIPS assembly code is:

  18. i ~ $s1 j ~ $s2 base of A ~ $s3 while ( A[i] >= 0) i = i + j MIPS assembly code is: Words in an Array in memory are 4 bytes apart, so the Address increments by 4. A[i] • • • A[3] A[2] A[1] A[0] Base + 4 * i Base + 12 Base + 8 Base + 4 Base

  19. i ~ $s1 j ~ $s2 base of A ~ $s3 while ( A[i] >= 0) i = i + j MIPS assembly code is: Loop: add $t1, $s1, $s1 # $t1 = 2 * i add $t1, $t1, $t1 # $t1 = 4 * i

  20. i ~ $s1 j ~ $s2 base of A ~ $s3 while ( A[i] >= 0) i = i + j MIPS assembly code is: Loop: add $t1, $s1, $s1 # $t1 = 2 * i add $t1, $t1, $t1 # $t1 = 4 * i add $t1, $t1, $s3 # $t1 = addr of A[i]

  21. i ~ $s1 j ~ $s2 base of A ~ $s3 while ( A[i] >= 0) i = i + j MIPS assembly code is: Loop: add $t1, $s1, $s1 # $t1 = 2 * i add $t1, $t1, $t1 # $t1 = 4 * i add $t1, $t1, $s3 # $t1 = addr of A[i] lw $t0, 0 ($t1) # $t0 = A[i]

  22. i ~ $s1 j ~ $s2 base of A ~ $s3 while ( A[i] >= 0) i = i + j MIPS assembly code is: Loop: add $t1, $s1, $s1 # $t1 = 2 * i add $t1, $t1, $t1 # $t1 = 4 * i add $t1, $t1, $s3 # $t1 = addr of A[i] lw $t0, 0 ($t1) # $t0 = A[i] slt set on less than slt rd, rs, rt means if rs < rt, rd = 1, else rd=0

  23. i ~ $s1 j ~ $s2 base of A ~ $s3 while ( A[i] >= 0) i = i + j MIPS assembly code is: Loop: add $t1, $s1, $s1 # $t1 = 2 * i add $t1, $t1, $t1 # $t1 = 4 * i add $t1, $t1, $s3 # $t1 = addr of A[i] lw $t0, 0($t1) # $t0 = A[i] slt $t2, $t0, $zero # $t2 = 1 if $t0 < 0 Exit:

  24. i ~ $s1 j ~ $s2 base of A ~ $s3 while ( A[i] >= 0) i = i + j MIPS assembly code is: Loop: add $t1, $s1, $s1 # $t1 = 2 * i add $t1, $t1, $t1 # $t1 = 4 * i add $t1, $t1, $s3 # $t1 = addr of A[i] lw $t0, 0 ($t1) # $t0 = A[i] slt $t2, $t0, $zero # $t2 = 1 if $t0 < 0 bne $t2, $zero, Exit # if A[i]<0 goto Exit Exit:

  25. i ~ $s1 j ~ $s2 base of A ~ $s3 while ( A[i] >= 0) i = i + j MIPS assembly code is: Loop: add $t1, $s1, $s1 # $t1 = 2 * i add $t1, $t1, $t1 # $t1 = 4 * i add $t1, $t1, $s3 # $t1 = addr of A[i] lw $t0, 0 ($t1) # $t0 = A[i] slt $t2, $t0, $zero # $t2 = 1 if $t0 < 0 bne $t2, $zero, Exit # if A[i]<0 goto Exit add $s1, $s1, $s2 # i = i + j j Loop # goto Loop Exit:

  26. Case / Switch Statement switch ( k ) { case 0: statement 0; break case 1: statement 1; break case 2: statement 2; break}

  27. Case / Switch Statement switch ( k ) { case 0: statement 0; break case 1: statement 1; break case 2: statement 2; break } if k < 0, then Exit slt set on less than slt rd, rs, rt means if rs < rt, rd = 1, else rd=0

  28. Case / Switch Statement switch ( k ) { case 0: statement 0; break case 1: statement 1; break case 2: statement 2; break } if k < 0, then Exit slt set on less than slt rd, rs, rt means if rs < rt, rd = 1, else rd=0 So, if k ~ $s0 slt $t0, $s0, $zero # $t0 = 1 if k < 0 bne $t0, $zero, Exit # goto Exit if k < 0

  29. Case / Switch Statement switch ( k ) { case 0: statement 0; break case 1: statement 1; break case 2: statement 2; break } if k < 0, then Exit slt set on less than slt rd, rs, rt means if rs < rt, rd = 1, else rd=0 So, if k ~ $s0 slt $t0, $s0, $zero # $t0 = 1 if k < 0 bne $t0, $zero, Exit # goto Exit if k < 0 And the test for k > 2 is, assuming $s1 = 3 slt $t0, $s0, $s1 # $t0 = 1 if k < 3 beq $t0, $zero, Exit # goto Exit if k >=3

  30. Case / Switch Statement switch ( k ) { case 0: statement 0; break case 1: statement 1; break case 2: statement 2; break } JumpTable[ k ] addr of statement 2 addr of statement 1 addr of statement 0 For a given k, place JumpTable[ k ] in register $t0 using lw instruction

  31. Case / Switch Statement switch ( k ) { case 0: statement 0; break case 1: statement 1; break case 2: statement 2; break } JumpTable[ k ] addr of statement 2 addr of statement 1 addr of statement 0 load JumpTable[ k ] in a register and jump to it jr jump register jr rs means go to address in register rs

  32. Case / Switch Statement switch ( k ) { case 0: statement 0; break k is in $s0, case 1: statement 1; break Start of JumpTable is case 2: statement 2; break } in $t1 slt $t0, $s0, $zero # $t0 = 1 if k < 0 bne $t0, $zero, Exit # goto Exit if k < 0 slt $t0, $s0, $s1 # $t0 = 1 if k < 3 beq $t0, $zero, Exit # goto Exit if k >=3 add $t0, $s0, $s0 # $t0 = 2 * k add $t0, $t0, $t0 # $t0 = 4 * k add $t0, $t0, $t1 # $t0 = addr of JumpTable[k] lw $t2, 0( $t0) # $t2 = JumpTable[k] jr $t2 # jump to addr in $t2 Exit:

  33. MIPS Assembly Instructions add add $s1, $s2, $s3 $s1 = $s2 + $s3 op rs rt rd shamt funct 18 19 17 32 0 0 subtract sub $s1, $s2, $s3 $s1 = $s2 - $s3 op rs rt rd shamt funct 18 19 0 17 34 0

  34. MIPS Assembly Instructions load word lw $s1, 300 ($s2) $s1 = Mem[$s2+300] op rs rt address 18 17 300 35 store word sw $s1, 300 ($s2) Mem[$s2+300] = $s1 op rs rt address 18 17 300 43

  35. MIPS Assembly Instructions branch on equal beq $s1, $s2, Label if ($s1 = =$s2) go to Label op rs rt address 17 18 4 address branch on not equal bne $s1, $s2, Label if ($s1 != $s2) go to Label op rs rt address 17 18 address 5

  36. MIPS Assembly Instructions jump j Label go to Label op address 2 address set on less than slt $s1, $s2, $s3 if $s2 < $s3, $s1 = 1, else $s1=0 op rs rt rd shamt funct 18 19 42 17 0 0

  37. MIPS Assembly Instructions jump register jr $s1 go to address in register $s1 op rs rt rd shamt funct 17 0 8 0 0 0

  38. MIPS Assembly Instructions Pseudo instructions Instructions supported by the Assembler but not implemented in hardware. Ex: move multiply branch less than, less than or equal, greater than, greater than or equal

  39. MIPS Immediate Addressing Very common to use a constant in arithmetic operations. Examples? Make it faster to access small constants. Keep the constant in the instruction. add immediate addi $s1, $s2, constant $s1 = $s2 + constant op rs rt immediate 8 18 17 constant 6 5 5 16 I type of format The constant can be negative!

  40. MIPS Immediate Addressing Very common to use a constant in comparison operations. Examples? Make it faster to do comparisons. Keep the constant in the instruction slt immediate slti $t0, $s2, constant $t0 = 1 if $s2 < constant else $t0 = 0 op rs rt immediate 10 18 8 constant 6 5 5 16 I type of format

  41. Procedure Calls • Place parameters where the procedure can access them

  42. Procedure Calls • Place parameters where the procedure can access them • Transfer control to the procedure

  43. Procedure Calls • Place parameters where the procedure can access them • Transfer control to the procedure • Perform the task of the procedure

  44. Procedure Calls • Place parameters where the procedure can access them • Transfer control to the procedure • Perform the task of the procedure • Place the results where the calling program can access • them

  45. Procedure Calls • Place parameters where the procedure can access them • Transfer control to the procedure • Perform the task of the procedure • Place the results where the calling program can access • them • 5. Return control to the point of the call

  46. Place parameters where the procedure can access them • Transfer control to the procedure • Perform the task of the procedure • Place the results where the calling program can access • them • 5. Return control to the point of the call Procedure Calls Allocate registers to hold data for procedure calls $a0 - $a3 : four registers to pass parameters $v0 - $v1 : two registers to return values $ra : one return address register

  47. Place parameters where the procedure can access them • Transfer control to the procedure • Perform the task of the procedure • Place the results where the calling program can access • them • 5. Return control to the point of the call Procedure Calls Allocate registers to hold data for procedure calls $a0 - $a3 : four registers to pass parameters $v0 - $v1 : two registers to return values $ra : one return address register Need jump-and-link instruction : jal ProcedureAddress means :save return address in $ra and jumps to ProcedureAddress

  48. Place parameters where the procedure can access them • Transfer control to the procedure • Perform the task of the procedure • Place the results where the calling program can access • them • 5. Return control to the point of the call Procedure Calls Allocate registers to hold data for procedure calls $a0 - $a3 : four registers to pass parameters $v0 - $v1 : two registers to return values $ra : one return address register Need jump-and-link instruction : jal ProcedureAddress means :save return address in $ra and jumps to ProcedureAddress How do you return?

  49. Compiling a “leaf” Procedure (Does not Call another Procedure) int leaf_example ( int g, int h, int i, int j) { int f ; f = ( g + h ) – ( i + j ) ; return f ;}

  50. Compiling a “leaf” Procedure (Does not Call another Procedure) int leaf_example ( int g, int h, int i, int j) { int f ; f = ( g + h ) – ( i + j ) ; return f ;} Assign g to $a0, h to $a1, i to $a2, j to $a3 and f to $v0.

More Related