Presentation is loading. Please wait.

Presentation is loading. Please wait.

Addressing in Jumps jump j Label go to Label op address 2 address

Similar presentations


Presentation on theme: "Addressing in Jumps jump j Label go to Label op address 2 address"— Presentation transcript:

1

2 Addressing in Jumps jump j Label go to Label op address 2 address
6 bits bits The complete 32 bit address is : address 00 4 bits bits bits Upper 4 bits of the Program Counter, PC jump uses word addresses address * 4 = address:00 This is Pseudodirect Addressing. Note: 256 MB word boundaries – 64M Instructions

3 Branch Addressing beq $s1, $s2, Label if ($s1 = =$s2) go to Label
op rs rt address 4 17 18 address 6 bits bits bits bits effective 32 bit address = ( PC + 4 ) + ( address * 4 ) Next Instruction address is the relative number of instructions address * 4 = address : 00 This is PC-relative addressing

4 Branch Addressing bne $t1, $s1, Label # if $t1!= $s1 goto Label
What if Label is more words than 16 bits ? ( 64K instructions or 256 KB )

5 Branch Addressing bne $t1, $s1, Label # if $t1!= $s1 goto Label
What if Label is more words than 16 bits ? (±32K instructions) Then change to the complement branch and add a jump. beq $t1, $s1, L2 # if $t1 = $s1 goto L2 j Label L2:

6 Sort an array v[n-1] v[j+1] v[j] v[2] v[1] v[0]

7 Swap procedure Swap the contents of v[ k ] and v [ k + 1 ]
swap ( int v[ ], int k ) { int temp; temp = v[ k ]; v[ k ] = v[ k + 1 ]; v[ k + 1 ] = temp ; }

8 Swap procedure } swap ( int v[ ], int k ) { int temp; temp = v[ k];
v[ k] = v[ k + 1 ]; v[ k + 1 ] = temp ; } leaf procedure $a0 = v base, $a1 = k Use $t0 for temp Save $a0, $a1, $ra ?

9 Swap procedure } swap ( int v[ ], int k ) { int temp; temp = v[ k];
v[ k] = v[ k + 1 ]; v[ k + 1 ] = temp ; } leaf procedure $a0 = v base, $a1 = k Use $t0 for temp swap: add $t1, $a1, $a1 # $t1 = k * 2 add $t1, $t1, $t1 # $t1 = k * 4 add $t1, $a0, $t1 # $t1 = v base + k * 4

10 Swap procedure swap ( int v[ ], int k ) {int temp; temp = v[ k];
v[ k] = v[ k + 1 ]; v[ k + 1 ] = temp ;} leaf procedure $a0 = v base, $a1 = k Use $t0 for temp swap: add $t1, $a1, $a1 # $t1 = k * 2 add $t1, $t1, $t1 # $t1 = k * 4 add $t1, $a0, $t1 # $t1 = v + k * 4 lw $t0, 0 ( $t1 ) # temp = v[ k ]

11 Swap procedure swap ( int v[ ], int k ) {int temp; temp = v[ k];
v[ k] = v[ k + 1 ]; v[ k + 1 ] = temp ;} leaf procedure $a0 = v base, $a1 = k Use $t0 for temp swap: add $t1, $a1, $a1 # $t1 = k * 2 add $t1, $t1, $t1 # $t1 = k * 4 add $t1, $a0, $t1 # $t1 = v + k * 4 lw $t0, 0 ( $t1 ) # temp = v[ k ] lw $t2, 4 ( $t1 ) # $t2 = v[ k + 1 ] sw $t2, 0 ( $t1 ) # v[ k ] = $t2

12 Swap procedure swap ( int v[ ], int k ) {int temp; temp = v[ k];
v[ k] = v[ k + 1 ]; v[ k + 1 ] = temp ;} leaf procedure $a0 = v base, $a1 = k Use $t0 for temp swap: add $t1, $a1, $a1 # $t1 = k * 2 add $t1, $t1, $t1 # $t1 = k * 4 add $t1, $a0, $t1 # $t1 = v + k * 4 lw $t0, 0 ( $t1 ) # temp = v[ k ] lw $t2, 4 ( $t1 ) # $t2 = v[ k + 1 ] sw $t2, 0 ( $t1 ) # v[ k ] = $t2 sw $t0, 4 ( $t1 ) # v[ k + 1 ] = temp

13 Swap procedure swap ( int v[ ], int k ) {int temp; temp = v[ k];
v[ k] = v[ k + 1 ]; v[ k + 1 ] = temp ;} leaf procedure $a0 = v base, $a1 = k Use $t0 for temp swap: add $t1, $a1, $a1 # $t1 = k * 2 add $t1, $t1, $t1 # $t1 = k * 4 add $t1, $a0, $t1 # $t1 = v + k * 4 lw $t0, 0 ( $t1 ) # temp = v[ k ] lw $t2, 4 ( $t1 ) # $t2 = v[ k + 1 ] sw $t2, 0 ( $t1 ) # v[ k ] = $t2 sw $t0, 4 ( $t1 ) # v[ k + 1 ] = temp jr $ra # return

14 Sort on array sort ( int v[ ], int n ) { int i, j ;
for ( i = 0; i < n; i = i + 1 ) { for ( j = i -1; j >= 0 && v[ j ] > v[ j + 1 ]; j = j - 1) { swap ( v, j ); } v[n-1] v[j+1] v[j] v[2] v[1] v[0]

15 Sort on array for ( i = 0; i < n; i = i + 1 ) {
for ( j = i -1; j >= 0 && v[ j ] > v[ j + 1 ]; j = j - 1){ swap ( v, j );} i = 2 1 3 9 2 1 9 3 2 9 1 3 2 9 3 1 9 2 3 1 9 3 2 1 j =

16 Sort on array sort ( int v[ ], int n ) { int i, j ;
for ( i = 0; i < n; i = i + 1 ) { for ( j = i -1; j >= 0 && v[ j ] > v[ j + 1 ]; j = j - 1) { swap ( v, j ); }}} This is a procedure that is called and calls another, “swap”. Assignment: $a0 to v base, $a1 to n to pass parameters $s0 to i, $s1 to j; Why not $t0 and $t1?

17 Sort on array sort ( int v[ ], int n ) { int i, j ;
for ( i = 0; i < n; i = i + 1 ) { for ( j = i -1; j >= 0 && v[ j ] > v[ j + 1 ]; j = j - 1) { swap ( v, j ); }}} This is a procedure that is called and calls another, “swap”. Assignment: $a0 to v base, $a1 to n to pass parameters $s0 to i, $s1 to j; Why not $t0 and $t1? As a called routine, must push what?

18 Sort on array sort ( int v[ ], int n ) { int i, j ;
for ( i = 0; i < n; i = i + 1 ) { for ( j = i -1; j >= 0 && v[ j ] > v[ j + 1 ]; j = j - 1) { swap ( v, j ); }}} This is a procedure that is called and calls another, “swap”. Assignment: $a0 to v base, $a1 to n $s0 to i, $s1 to j As a called routine, must push $s0, $s1 and $ra? Before calling swap must save ?

19 Sort on array sort ( int v[ ], int n ) { int i, j ;
for ( i = 0; i < n; i = i + 1 ) { for ( j = i -1; j >= 0 && v[ j ] > v[ j + 1 ]; j = j - 1) { swap ( v, j ); }}} This is a procedure that is called and calls another, “swap”. Assignment: $a0 to v base, $a1 to n $s0 to i, $s1 to j As a called routine, must push $s0, $s1 and $ra? Before calling swap must save $a0 and $a1. Options?

20 Sort on array sort ( int v[ ], int n ) { int i, j ;
for ( i = 0; i < n; i = i + 1 ) { for ( j = i -1; j >= 0 && v[ j ] > v[ j + 1 ]; j = j - 1) { swap ( v, j ); }}} This is a procedure that is called and calls another, “swap”. Assignment: $a0 to v base, $a1 to n $s0 to i, $s1 to j As a called routine, must push $s0, $s1 and $ra? Others? Before calling swap must save $a0 and $a1. Options? Use $s2 and $s3 for $a0 and $a1. Must push $s2 and $s3.

21 sort : addi $sp, $sp, -20 # move stack pointer
sw $ra, 16 ($sp) # save $ra on stack sw $s3, 12 ($sp) # save $s3 on stack sw $s2, 8 ($sp) # save $s2 on stack sw $s1, 4 ($sp) # save $s1 on stack sw $s0, 0 ($sp) # save $s0 on stack add $s2, $a0, $zero # $s2 = $a0 = v base add $s3, $a1, $zero # $s3 = $a1 = n Procedure Body exit1: lw $s0, 0 ($sp) # restore $s0 from stack lw $s1, 4 ($sp) # restore $s1 from stack lw $s2, 8 ($sp) # restore $s2 from stack lw $s3, 12 ($sp) # restore $s3 from stack lw $ra, 16 ($sp) # restore $ra from stack addi $sp, $sp, 20 # restore stack pointer jr $ra # return

22 Sort on array sort ( int v[ ], int n ) { int i, j ;
for ( i = 0; i < n; i = i + 1 ) { for ( j = i -1; j >= 0 && v[ j ] > v[ j + 1 ]; j = j - 1) { swap ( v, j ); }}} Outer loop for ( i = 0; i < n; i = i + 1 )

23 Procedure Body Outer loop $s0 = i, $s1 = j, $s3 = n for ( i = 0; i < n; i = i + 1 ) add $s0, $zero, $zero # i = 0

24 Procedure Body Outer loop $s0 = i, $s1 = j, $s3 = n for ( i = 0; i < n; i = i + 1 ) add $s0, $zero, $zero # i = 0 L1: slt $t0, $s0, $s3 # $t0 = 0 if i >= n beq $t0, $zero, exit1 # exit out if i >= n

25 Procedure Body Outer loop $s0 = i, $s1 = j, $s3 = n for ( i = 0; i < n; i = i + 1 ) add $s0, $zero, $zero # i = 0 L1: slt $t0, $s0, $s3 # $t0 = 0 if i >= n beq $t0, $zero, exit1 # exit out if i >= n Inner Loop exit2: addi $s0, $s0, 1 # i = i + 1 j L1 # jump to outer loop test

26 Sort on array sort ( int v[ ], int n ) { int i, j ;
for ( i = 0; i < n; i = i + 1 ) { for ( j = i -1; j >= 0 && v[ j ] > v[ j + 1 ]; j = j - 1) { swap ( v, j ); }}} Inner Loop

27 Inner Loop $s0=i, $s1=j, $s2=v, $s3=n
for ( j = i -1; j >= 0 && v[ j ] > v[ j + 1 ]; j = j - 1) { swap ( v, j ); addi $s1, $s0, -1 # j = i –1

28 Inner Loop $s0=i, $s1=j, $s2=v, $s3=n
for ( j = i -1; j >= 0 && v[ j ] > v[ j + 1 ]; j = j - 1) { swap ( v, j ); addi $s1, $s0, -1 # j = i –1 L2: slti $t0, $s1, 0 # $t0 = 1, if j <0 bne $t0, $zero, exit2 # if j <0 go to exit2

29 Inner Loop $s0=i, $s1=j, $s2=v, $s3=n
for ( j = i -1; j >= 0 && v[ j ] > v[ j + 1 ]; j = j - 1) { swap ( v, j ); addi $s1, $s0, -1 # j = i –1 L2: slti $t0, $s1, 0 # $t0 = 1, if j <0 bne $t0, $zero, exit2 # if j <0 go to exit2 add $t1, $s1, $s1 # $t1 = 2 * j add $t1, $t1, $t1 # $t1 = 4 * j add $t2, $s2, $t1 # $t2 = v + (4 *j) lw $t3, 0 ( $t2) # $t3 = v[ j ] lw $t4, 4 ( $t2) # $t4 = v[ j + 1]

30 Inner Loop $s0=i, $s1=j, $s2=v, $s3=n
for ( j = i -1; j >= 0 && v[ j ] > v[ j + 1 ]; j = j - 1) { swap ( v, j ); addi $s1, $s0, -1 # j = i –1 L2: slti $t0, $s1, 0 # $t0 = 1, if j <0 bne $t0, $zero, exit2 # if j <0 go to exit2 add $t1, $s1, $s1 # $t1 = 2 * j add $t1, $t1, $t1 # $t1 = 4 * j add $t2, $s2, $t1 # $t2 = v + (4 *j) lw $t3, 0 ( $t2) # $t3 = v[ j ] lw $t4, 4 ( $t2) # $t4 = v[ j + 1] slt $t0, $t4, $t3 # $t0 = 1, if v[j+1]<v[j] beq $t0, $zero, exit2 # if v[j+1]>=v[j] goto exit2

31 Inner Loop $s0=i, $s1=j, $s2=v, $s3=n
for ( j = i -1; j >= 0 && v[ j ] > v[ j + 1 ]; j = j - 1) { swap ( v, j ); add $a0, $s2,$zero # $a0 = v add $a1, $s1, $zero # $a1 = j jal swap # call swap routine

32 Inner Loop $s0=i, $s1=j, $s2=v, $s3=n
for ( j = i -1; j >= 0 && v[ j ] > v[ j + 1 ]; j = j - 1) { swap ( v, j ); add $a0, $s2,$zero # $a0 = v add $a1, $s1, $zero # $a1 = j jal swap # call swap routine addi $s1, $s1, -1 # j = j – 1 j L2

33 sort : addi $sp, $sp, -20 # move stack pointer
sw $ra, 16 ($sp) # save $ra on stack sw $s3, 12 ($sp) # save $s3 on stack sw $s2, 8 ($sp) # save $s2 on stack sw $s1, 4 ($sp) # save $s1 on stack sw $s0, 0 ($sp) # save $s0 on stack add $s2, $a0, $zero # $s2 = $a0 = v base add $s3, $a1, $zero # $s3 = $a1 = n Procedure Body exit1: lw $s0, 0 ($sp) # restore $s0 from stack lw $s1, 4 ($sp) # restore $s1 from stack lw $s2, 8 ($sp) # restore $s2 from stack lw $s3, 12 ($sp) # restore $s3 from stack lw $ra, 16 ($sp) # restore $ra from stack addi $sp, $sp, 20 # restore stack pointer jr $ra # return

34 Look at Addresses sort: 8 instructions L1: beq $t0, $zero, exit1 1 instruction L2: slti $t0, $s1, bne $t0, $zero, exit2 6 instructions beq $t0, $zero, exit2 4 instructions j L2 exit2: addi $s0, $s0, 1 j L1 exit1: lw $s0, 0 ($sp)

35 Look at Addresses sort: 8 instructions L1: ( PC + 4 ) + ( address * 4 ) beq $t0, $zero, exit1 1 instruction L2: slti $t0, $s1, bne $t0, $zero, exit2 6 instructions beq $t0, $zero, exit2 4 instructions j L2 exit2: addi $s0, $s0, 1 j L1 exit1: lw $s0, 0 ($sp)

36 Look at Addresses sort: 8 instructions L1: ( ) + ( address * 4 ) = 80112 beq $t0, $zero, exit1 1 instruction L2: slti $t0, $s1, bne $t0, $zero, exit2 6 instructions beq $t0, $zero, exit2 4 instructions j L2 exit2: addi $s0, $s0, 1 j L1 exit1: lw $s0, 0 ($sp)

37 Look at Addresses sort: 8 instructions L1: ( address * 4 ) = 68 beq $t0, $zero, exit1 1 instruction L2: slti $t0, $s1, bne $t0, $zero, exit2 6 instructions beq $t0, $zero, exit2 4 instructions j L2 exit2: addi $s0, $s0, 1 j L1 exit1: lw $s0, 0 ($sp)

38 Look at Addresses sort: 8 instructions L1: beq $t0, $zero, 17 1 instruction L2: slti $t0, $s1, bne $t0, $zero, exit2 6 instructions beq $t0, $zero, exit2 4 instructions j L2 exit2: addi $s0, $s0, 1 j L1 exit1: lw $s0, 0 ($sp)

39 Look at Addresses sort: 8 instructions L1: beq $t0, $zero, 17 1 instruction L2: slti $t0, $s1, 0 ( PC + 4 ) + ( ? * 4 ) bne $t0, $zero, exit2 6 instructions beq $t0, $zero, exit2 4 instructions j L2 exit2: addi $s0, $s0, 1 j L1 exit1: lw $s0, 0 ($sp)

40 Look at Addresses sort: 8 instructions L1: beq $t0, $zero, 17 1 instruction L2: slti $t0, $s1, ( ? * 4 )= bne $t0, $zero, exit2 6 instructions beq $t0, $zero, exit2 4 instructions j L2 exit2: addi $s0, $s0, 1 j L1 exit1: lw $s0, 0 ($sp)

41 Look at Addresses sort: 8 instructions L1: beq $t0, $zero, 17 1 instruction L2: slti $t0, $s1, 0 (? * 4 ) = bne $t0, $zero, 12 6 instructions beq $t0, $zero, exit2 4 instructions j L2 exit2: addi $s0, $s0, 1 j L1 exit1: lw $s0, 0 ($sp)

42 Look at Addresses sort: 8 instructions L1: beq $t0, $zero, 17 1 instruction L2: slti $t0, $s1, bne $t0, $zero, 12 6 instructions ( PC + 4 ) + ( ? * 4 ) beq $t0, $zero, exit2 4 instructions j L2 exit2: addi $s0, $s0, 1 j L1 exit1: lw $s0, 0 ($sp)

43 Look at Addresses sort: 8 instructions L1: beq $t0, $zero, 17 1 instruction L2: slti $t0, $s1, bne $t0, $zero, 12 6 instructions ( ? * 4 )=80104 beq $t0, $zero, exit2 4 instructions j L2 exit2: addi $s0, $s0, 1 j L1 exit1: lw $s0, 0 ($sp)

44 Look at Addresses sort: 8 instructions L1: beq $t0, $zero, 17 1 instruction L2: slti $t0, $s1, bne $t0, $zero, 12 6 instructions ( ? * 4 ) = 20 beq $t0, $zero, 5 4 instructions j L2 exit2: addi $s0, $s0, 1 j L1 exit1: lw $s0, 0 ($sp)

45 Look at Addresses sort: 8 instructions L1: beq $t0, $zero, 17 1 instruction L2: slti $t0, $s1, bne $t0, $zero, 12 6 instructions beq $t0, $zero, 5 4 instructions j L2 address * 4 = 80048 exit2: addi $s0, $s0, 1 j L1 exit1: lw $s0, 0 ($sp)

46 Look at Addresses sort: 8 instructions L1: beq $t0, $zero, 17 1 instruction L2: slti $t0, $s1, bne $t0, $zero, 12 6 instructions beq $t0, $zero, 5 4 instructions j address * 4 = 80048 exit2: addi $s0, $s0, 1 j L1 exit1: lw $s0, 0 ($sp)

47 Look at Addresses sort: 8 instructions L1: beq $t0, $zero, 17 1 instruction L2: slti $t0, $s1, bne $t0, $zero, 12 6 instructions beq $t0, $zero, 5 4 instructions j 20012 exit2: addi $s0, $s0, 1 j L1 ? * 4 = 80036 exit1: lw $s0, 0 ($sp)

48 Look at Addresses sort: 8 instructions L1: beq $t0, $zero, 17 1 instruction L2: slti $t0, $s1, bne $t0, $zero, 12 6 instructions beq $t0, $zero, 5 4 instructions j 20012 exit2: addi $s0, $s0, 1 j ? * 4 = 80036 exit1: lw $s0, 0 ($sp)

49 Look at Addresses sort: 8 instructions L1: beq $t0, $zero, 17 1 instruction L2: slti $t0, $s1, bne $t0, $zero, 12 6 instructions beq $t0, $zero, 5 4 instructions j 20012 exit2: addi $s0, $s0, 1 j 20009 exit1: lw $s0, 0 ($sp)


Download ppt "Addressing in Jumps jump j Label go to Label op address 2 address"

Similar presentations


Ads by Google