computer architecture cse 3322
DESCRIPTION
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 - PowerPoint PPT PresentationTRANSCRIPT
Computer Architecture CSE 3322Lecture 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 3322Graduate Teaching Assistant
Pramod Kumar
Office Hours: Tues – Thurs 10 – 1 pm
114 Engr Annex West
MIPS Assembly Instructions
Instruction Example Meaning
add add $s1, $s2, $s3 $s1 = $s2 + $s3subtract 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 registers300 is a constant
Instructions for Making Decisionsif – then – else Constructif ( i = = j ) a = b; else a = c;
i = =j
a= ca = b
Exit:
NoYes
Instructions for Making Decisionsif – then – else Constructif ( i = = j ) a=b; else a=c;
i = =j
a= ca = b
Exit:
NoYesbeqbranch onequal
Instructions for Making Decisionsif – then – else Constructif ( i = = j ) a=b; else a=c;
i = =j
a= ca = b
Exit:
NoYes bnebeqbranch on not equalbranch on
equal
Instructions for Making Decisionsif – then – else Constructif ( i = = j ) a=b; else a=c;
i = =j
a= ca = b
Exit:
NoYes bnebeq
j
branch on not equalbranch onequal
jump
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 addressthat 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, Labelmeans if (rs != rt) go to Label
I type format op = 5
Label is the target statement label. It is an addressthat 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, Labelmeans if (rs != rt) go to Label
I type format op = 5
jump j Labelmeans go to Label
J type format op = 2
Label is the target statement label. It is an addressthat is calculated by the assembler.
if ( i = = j ) a=b; else a=c;
i = =j
a= ca = b
Exit:
NoYesbne
beq
j
branch on not equalbranch onequal
jump
branch Else #go to Else if ?statement 1j Exit #go to Exit
Else: statement 2Exit:
if ( i = = j ) a=b; else a=c;
i = =j
a= ca = b
Exit:
NoYesbnebeq
j
branch on not equalbranch onequal
jump
branch Else #go to Else if ? Most Likelystatement 1 Casej Exit #go to Exit
Else: statement 2Exit:
if ( i = = j ) a=b; else a=c;
i = =j
a= ca = b
Exit:
NoYes bnebeq
j
branch on not equalbranch onequal
jump
a ~ $s1b ~ $s2c ~ $s3i ~ $s4j ~ $s5
if ( i = = j ) a=b; else a=c;
i = =j
a= ca = b
Exit:
NoYes bnebeq
j
branch on not equalbranch onequal
jump
bne $s4, $s5, Else # go to Else if i!=j
Else: add $s1, $s3, $zero # a=c , Note: $zero is 0 Exit:
a ~ $s1b ~ $s2c ~ $s3i ~ $s4j ~ $s5
if ( i = = j ) a=b; else a=c;
i = =j
a= ca = b
Exit:
NoYes bnebeq
j
branch on not equalbranch onequal
jump
bne $s4, $s5, Else # go to Else if i!=jadd $s1, $s2, $zero # a=bj Exit # go to Exit
Else: add $s1, $s3, $zero # a=cExit:
a ~ $s1b ~ $s2c ~ $s3i ~ $s4j ~ $s5
while ( A[i] >= 0)i = i + j
i ~ $s1j ~ $s2base of A ~ $s3
MIPS assembly code is:
while ( A[i] >= 0)i = i + j
i ~ $s1j ~ $s2base of A ~ $s3
MIPS assembly code is:
A[i] • • •A[3]A[2]A[1]A[0]
Base + 4 * i
Base + 12Base + 8Base + 4Base
Words in an Arrayin memory are 4 bytes apart, so the Address incrementsby 4.
while ( A[i] >= 0)i = i + j
i ~ $s1j ~ $s2base of A ~ $s3
MIPS assembly code is:
Loop: add $t1, $s1, $s1 # $t1 = 2 * iadd $t1, $t1, $t1 # $t1 = 4 * i
while ( A[i] >= 0)i = i + j
i ~ $s1j ~ $s2base of A ~ $s3
MIPS assembly code is:
Loop: add $t1, $s1, $s1 # $t1 = 2 * iadd $t1, $t1, $t1 # $t1 = 4 * iadd $t1, $t1, $s3 # $t1 = addr of A[i]
while ( A[i] >= 0)i = i + j
i ~ $s1j ~ $s2base of A ~ $s3
MIPS assembly code is:
Loop: add $t1, $s1, $s1 # $t1 = 2 * iadd $t1, $t1, $t1 # $t1 = 4 * iadd $t1, $t1, $s3 # $t1 = addr of A[i]lw $t0, 0 ($t1) # $t0 = A[i]
while ( A[i] >= 0)i = i + j
i ~ $s1j ~ $s2base of A ~ $s3
MIPS assembly code is:
Loop: add $t1, $s1, $s1 # $t1 = 2 * iadd $t1, $t1, $t1 # $t1 = 4 * iadd $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
while ( A[i] >= 0)i = i + j
i ~ $s1j ~ $s2base of A ~ $s3
MIPS assembly code is:
Loop: add $t1, $s1, $s1 # $t1 = 2 * iadd $t1, $t1, $t1 # $t1 = 4 * iadd $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:
while ( A[i] >= 0)i = i + j
i ~ $s1j ~ $s2base of A ~ $s3
MIPS assembly code is:
Loop: add $t1, $s1, $s1 # $t1 = 2 * iadd $t1, $t1, $t1 # $t1 = 4 * iadd $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:
while ( A[i] >= 0)i = i + j
i ~ $s1j ~ $s2base of A ~ $s3
MIPS assembly code is:
Loop: add $t1, $s1, $s1 # $t1 = 2 * iadd $t1, $t1, $t1 # $t1 = 4 * iadd $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 Exitadd $s1, $s1, $s2 # i = i + jj Loop # goto Loop
Exit:
Case / Switch Statement
switch ( k ) {case 0: statement 0; breakcase 1: statement 1; breakcase 2: statement 2; break}
Case / Switch Statement
switch ( k ) {case 0: statement 0; breakcase 1: statement 1; breakcase 2: statement 2; break }
if k < 0, then Exitslt 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; breakcase 1: statement 1; breakcase 2: statement 2; break }
if k < 0, then Exitslt set on less than slt rd, rs, rt
means if rs < rt, rd = 1, else rd=0So, if k ~ $s0
slt $t0, $s0, $zero # $t0 = 1 if k < 0bne $t0, $zero, Exit # goto Exit if k < 0
Case / Switch Statement
switch ( k ) {case 0: statement 0; breakcase 1: statement 1; breakcase 2: statement 2; break }
if k < 0, then Exitslt set on less than slt rd, rs, rt
means if rs < rt, rd = 1, else rd=0So, if k ~ $s0
slt $t0, $s0, $zero # $t0 = 1 if k < 0bne $t0, $zero, Exit # goto Exit if k < 0
And the test for k > 2 is, assuming $s1 = 3slt $t0, $s0, $s1 # $t0 = 1 if k < 3beq $t0, $zero, Exit # goto Exit if k >=3
Case / Switch Statement
switch ( k ) {case 0: statement 0; breakcase 1: statement 1; breakcase 2: statement 2; break }
JumpTable[ k ] addr of statement 2addr of statement 1addr 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; breakcase 1: statement 1; breakcase 2: statement 2; break }
JumpTable[ k ] addr of statement 2addr of statement 1addr of statement 0
load JumpTable[ k ] in a register and jump to it
jr jump register jr rsmeans go to address in register rs
Case / Switch Statementswitch ( k ) {
case 0: statement 0; break k is in $s0, case 1: statement 1; break Start of JumpTable iscase 2: statement 2; break } in $t1
slt $t0, $s0, $zero # $t0 = 1 if k < 0bne $t0, $zero, Exit # goto Exit if k < 0slt $t0, $s0, $s1 # $t0 = 1 if k < 3beq $t0, $zero, Exit # goto Exit if k >=3add $t0, $s0, $s0 # $t0 = 2 * kadd $t0, $t0, $t0 # $t0 = 4 * kadd $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
subtract sub $s1, $s2, $s3 $s1 = $s2 - $s3
op rs rt rd shamt funct
0
op rs rt rd shamt funct
0 1718 19 0 32
1718 19 0 34
MIPS Assembly Instructionsload word lw $s1, 300 ($s2) $s1 = Mem[$s2+300]
store word sw $s1, 300 ($s2) Mem[$s2+300] = $s1
op rs rt address
35 18 17
op rs rt address
43 18 17
300
300
MIPS Assembly Instructions
op rs rt address
4 17 18
op rs rt address
5 17 18
address
address
branch on equal beq $s1, $s2, Label if ($s1 = =$s2) go to Label
branch on not equal bne $s1, $s2, Label if ($s1 != $s2) go to Label
MIPS Assembly Instructions
op address
2
op rs rt rd shamt funct
0 18 19
address
set on less than slt $s1, $s2, $s3 if $s2 < $s3, $s1 = 1, else $s1=0
jump j Label go to Label
0 4217
MIPS Assembly Instructions jump register
jr $s1 go to address in register $s1
op rs rt rd shamt funct
0 17 0 0 80
MIPS Assembly Instructions
Pseudo instructionsInstructions supported by the Assembler butnot implemented in hardware.Ex: move
multiplybranch less than, less than or equal,
greater than, greater than or equal
MIPS Immediate AddressingVery 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 AddressingVery 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
1. Place parameters where the procedure can access them
Procedure Calls
1. Place parameters where the procedure can access them2. Transfer control to the procedure
Procedure Calls
1. Place parameters where the procedure can access them2. Transfer control to the procedure3. Perform the task of the procedure
Procedure Calls
1. Place parameters where the procedure can access them2. Transfer control to the procedure3. Perform the task of the procedure4. Place the results where the calling program can access
them
Procedure Calls
1. Place parameters where the procedure can access them2. Transfer control to the procedure3. Perform the task of the procedure4. Place the results where the calling program can access
them5. Return control to the point of the call
Procedure Calls1. Place parameters where the procedure can access them2. Transfer control to the procedure3. Perform the task of the procedure4. Place the results where the calling program can access
them5. Return control to the point of the call
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
Procedure Calls1. Place parameters where the procedure can access them2. Transfer control to the procedure3. Perform the task of the procedure4. Place the results where the calling program can access
them5. Return control to the point of the call
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
Procedure Calls1. Place parameters where the procedure can access them2. Transfer control to the procedure3. Perform the task of the procedure4. Place the results where the calling program can access
them5. Return control to the point of the call 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 ProcedureAddressHow 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.
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.Leaf_example:
add $t0, $a0, $a1 # Temp $t0 = g + hadd $t1, $a2, $a3 # Temp $t1 = i + jsub $v0, $t0 , $t1 # $v0 = (g+h) – (i+j)jr $ra # jump back to calling routine
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.Leaf_example:
add $t0, $a0, $a1 # Temp $t0 = g + hadd $t1, $a2, $a3 # Temp $t1 = i + jsub $vo, $t0 , $t1 # $v0 = (g+h) – (i+j)jr $ra # jump back to calling routine
What if the calling procedure uses $t0 and $t1?