computer architecture cse 3322

52
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

Upload: abiba

Post on 05-Feb-2016

50 views

Category:

Documents


6 download

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 Presentation

TRANSCRIPT

Page 1: Computer Architecture CSE 3322

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

Page 2: Computer Architecture CSE 3322

Computer Architecture CSE 3322Graduate Teaching Assistant

Pramod Kumar

Office Hours: Tues – Thurs 10 – 1 pm

114 Engr Annex West

pxk [email protected]

Page 3: Computer Architecture CSE 3322

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

Page 4: Computer Architecture CSE 3322

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

Page 5: Computer Architecture CSE 3322

Instructions for Making Decisionsif – then – else Constructif ( i = = j ) a = b; else a = c;

i = =j

a= ca = b

Exit:

NoYes

Page 6: Computer Architecture CSE 3322

Instructions for Making Decisionsif – then – else Constructif ( i = = j ) a=b; else a=c;

i = =j

a= ca = b

Exit:

NoYesbeqbranch onequal

Page 7: Computer Architecture CSE 3322

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

Page 8: Computer Architecture CSE 3322

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

Page 9: Computer Architecture CSE 3322

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

Page 10: Computer Architecture CSE 3322

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.

Page 11: Computer Architecture CSE 3322

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.

Page 12: Computer Architecture CSE 3322

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:

Page 13: Computer Architecture CSE 3322

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:

Page 14: Computer Architecture CSE 3322

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

Page 15: Computer Architecture CSE 3322

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

Page 16: Computer Architecture CSE 3322

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

Page 17: Computer Architecture CSE 3322

while ( A[i] >= 0)i = i + j

i ~ $s1j ~ $s2base of A ~ $s3

MIPS assembly code is:

Page 18: Computer Architecture CSE 3322

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.

Page 19: Computer Architecture CSE 3322

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

Page 20: Computer Architecture CSE 3322

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]

Page 21: Computer Architecture CSE 3322

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]

Page 22: Computer Architecture CSE 3322

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

Page 23: Computer Architecture CSE 3322

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:

Page 24: Computer Architecture CSE 3322

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:

Page 25: Computer Architecture CSE 3322

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:

Page 26: Computer Architecture CSE 3322

Case / Switch Statement

switch ( k ) {case 0: statement 0; breakcase 1: statement 1; breakcase 2: statement 2; break}

Page 27: Computer Architecture CSE 3322

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

Page 28: Computer Architecture CSE 3322

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

Page 29: Computer Architecture CSE 3322

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

Page 30: Computer Architecture CSE 3322

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

Page 31: Computer Architecture CSE 3322

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

Page 32: Computer Architecture CSE 3322

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:

Page 33: Computer Architecture CSE 3322

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

Page 34: Computer Architecture CSE 3322

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

Page 35: Computer Architecture CSE 3322

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

Page 36: Computer Architecture CSE 3322

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

Page 37: Computer Architecture CSE 3322

MIPS Assembly Instructions jump register

jr $s1 go to address in register $s1

op rs rt rd shamt funct

0 17 0 0 80

Page 38: Computer Architecture CSE 3322

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

Page 39: Computer Architecture CSE 3322

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!

Page 40: Computer Architecture CSE 3322

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

Page 41: Computer Architecture CSE 3322

Procedure Calls

1. Place parameters where the procedure can access them

Page 42: Computer Architecture CSE 3322

Procedure Calls

1. Place parameters where the procedure can access them2. Transfer control to the procedure

Page 43: Computer Architecture CSE 3322

Procedure Calls

1. Place parameters where the procedure can access them2. Transfer control to the procedure3. Perform the task of the procedure

Page 44: Computer Architecture CSE 3322

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

Page 45: Computer Architecture CSE 3322

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

Page 46: Computer Architecture CSE 3322

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

Page 47: Computer Architecture CSE 3322

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

Page 48: Computer Architecture CSE 3322

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?

Page 49: Computer Architecture CSE 3322

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 ;}

Page 50: Computer Architecture CSE 3322

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.

Page 51: Computer Architecture CSE 3322

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

Page 52: Computer Architecture CSE 3322

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?