520 likes | 645 Views
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
E N D
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 pxk 3008@exchange.uta.edu
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
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
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:
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:
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:
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
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
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.
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.
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:
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:
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
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:
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:
i ~ $s1 j ~ $s2 base of A ~ $s3 while ( A[i] >= 0) i = i + j MIPS assembly code is:
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
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
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]
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]
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
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:
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:
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:
Case / Switch Statement switch ( k ) { case 0: statement 0; break case 1: statement 1; break case 2: statement 2; break}
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
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
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
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
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
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:
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
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
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
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
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
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
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!
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
Procedure Calls • Place parameters where the procedure can access them
Procedure Calls • Place parameters where the procedure can access them • Transfer control to the procedure
Procedure Calls • Place parameters where the procedure can access them • Transfer control to the procedure • Perform the task of the procedure
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
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
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
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
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?
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 ;}
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.