Addressing in Jumps jump j Label go to Label op address 2 address 6 bits 26 bits The complete 32 bit address is : address 00 4 bits 26 bits 2 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
Branch Addressing beq $s1, $s2, Label if ($s1 = =$s2) go to Label op rs rt address 4 17 18 address 6 bits 5 bits 5 bits 16 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
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 )
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:
Sort an array v[n-1] • v[j+1] v[j] v[2] v[1] v[0]
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 ; }
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 ?
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
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 ]
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
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
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
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]
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 = 1 2 3 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 = 0 1 0 2 1 0
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?
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?
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 ?
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?
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.
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
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 )
Procedure Body Outer loop $s0 = i, $s1 = j, $s3 = n for ( i = 0; i < n; i = i + 1 ) add $s0, $zero, $zero # i = 0
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
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
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
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
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
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]
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
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
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
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
Look at Addresses 80000 sort: 8 instructions 80036 L1: beq $t0, $zero, exit1 1 instruction 80048 L2: slti $t0, $s1, 0 bne $t0, $zero, exit2 6 instructions beq $t0, $zero, exit2 4 instructions j L2 80104 exit2: addi $s0, $s0, 1 j L1 80112 exit1: lw $s0, 0 ($sp)
Look at Addresses 80000 sort: 8 instructions 80036 L1: ( PC + 4 ) + ( address * 4 ) beq $t0, $zero, exit1 1 instruction 80048 L2: slti $t0, $s1, 0 bne $t0, $zero, exit2 6 instructions beq $t0, $zero, exit2 4 instructions j L2 80104 exit2: addi $s0, $s0, 1 j L1 80112 exit1: lw $s0, 0 ($sp)
Look at Addresses 80000 sort: 8 instructions 80036 L1: ( 80044 ) + ( address * 4 ) = 80112 beq $t0, $zero, exit1 1 instruction 80048 L2: slti $t0, $s1, 0 bne $t0, $zero, exit2 6 instructions beq $t0, $zero, exit2 4 instructions j L2 80104 exit2: addi $s0, $s0, 1 j L1 80112 exit1: lw $s0, 0 ($sp)
Look at Addresses 80000 sort: 8 instructions 80036 L1: ( address * 4 ) = 68 beq $t0, $zero, exit1 1 instruction 80048 L2: slti $t0, $s1, 0 bne $t0, $zero, exit2 6 instructions beq $t0, $zero, exit2 4 instructions j L2 80104 exit2: addi $s0, $s0, 1 j L1 80112 exit1: lw $s0, 0 ($sp)
Look at Addresses 80000 sort: 8 instructions 80036 L1: beq $t0, $zero, 17 1 instruction 80048 L2: slti $t0, $s1, 0 bne $t0, $zero, exit2 6 instructions beq $t0, $zero, exit2 4 instructions j L2 80104 exit2: addi $s0, $s0, 1 j L1 80112 exit1: lw $s0, 0 ($sp)
Look at Addresses 80000 sort: 8 instructions 80036 L1: beq $t0, $zero, 17 1 instruction 80048 L2: slti $t0, $s1, 0 ( PC + 4 ) + ( ? * 4 ) bne $t0, $zero, exit2 6 instructions beq $t0, $zero, exit2 4 instructions j L2 80104 exit2: addi $s0, $s0, 1 j L1 80112 exit1: lw $s0, 0 ($sp)
Look at Addresses 80000 sort: 8 instructions 80036 L1: beq $t0, $zero, 17 1 instruction 80048 L2: slti $t0, $s1, 0 80056 + ( ? * 4 )=80104 bne $t0, $zero, exit2 6 instructions beq $t0, $zero, exit2 4 instructions j L2 80104 exit2: addi $s0, $s0, 1 j L1 80112 exit1: lw $s0, 0 ($sp)
Look at Addresses 80000 sort: 8 instructions 80036 L1: beq $t0, $zero, 17 1 instruction 80048 L2: slti $t0, $s1, 0 (? * 4 ) = 48 bne $t0, $zero, 12 6 instructions beq $t0, $zero, exit2 4 instructions j L2 80104 exit2: addi $s0, $s0, 1 j L1 80112 exit1: lw $s0, 0 ($sp)
Look at Addresses 80000 sort: 8 instructions 80036 L1: beq $t0, $zero, 17 1 instruction 80048 L2: slti $t0, $s1, 0 bne $t0, $zero, 12 6 instructions ( PC + 4 ) + ( ? * 4 ) beq $t0, $zero, exit2 4 instructions j L2 80104 exit2: addi $s0, $s0, 1 j L1 80112 exit1: lw $s0, 0 ($sp)
Look at Addresses 80000 sort: 8 instructions 80036 L1: beq $t0, $zero, 17 1 instruction 80048 L2: slti $t0, $s1, 0 bne $t0, $zero, 12 6 instructions 80084 + ( ? * 4 )=80104 beq $t0, $zero, exit2 4 instructions j L2 80104 exit2: addi $s0, $s0, 1 j L1 80112 exit1: lw $s0, 0 ($sp)
Look at Addresses 80000 sort: 8 instructions 80036 L1: beq $t0, $zero, 17 1 instruction 80048 L2: slti $t0, $s1, 0 bne $t0, $zero, 12 6 instructions ( ? * 4 ) = 20 beq $t0, $zero, 5 4 instructions j L2 80104 exit2: addi $s0, $s0, 1 j L1 80112 exit1: lw $s0, 0 ($sp)
Look at Addresses 80000 sort: 8 instructions 80036 L1: beq $t0, $zero, 17 1 instruction 80048 L2: slti $t0, $s1, 0 bne $t0, $zero, 12 6 instructions beq $t0, $zero, 5 4 instructions j L2 address * 4 = 80048 80104 exit2: addi $s0, $s0, 1 j L1 80112 exit1: lw $s0, 0 ($sp)
Look at Addresses 80000 sort: 8 instructions 80036 L1: beq $t0, $zero, 17 1 instruction 80048 L2: slti $t0, $s1, 0 bne $t0, $zero, 12 6 instructions beq $t0, $zero, 5 4 instructions j 20012 address * 4 = 80048 80104 exit2: addi $s0, $s0, 1 j L1 80112 exit1: lw $s0, 0 ($sp)
Look at Addresses 80000 sort: 8 instructions 80036 L1: beq $t0, $zero, 17 1 instruction 80048 L2: slti $t0, $s1, 0 bne $t0, $zero, 12 6 instructions beq $t0, $zero, 5 4 instructions j 20012 80104 exit2: addi $s0, $s0, 1 j L1 ? * 4 = 80036 80112 exit1: lw $s0, 0 ($sp)
Look at Addresses 80000 sort: 8 instructions 80036 L1: beq $t0, $zero, 17 1 instruction 80048 L2: slti $t0, $s1, 0 bne $t0, $zero, 12 6 instructions beq $t0, $zero, 5 4 instructions j 20012 80104 exit2: addi $s0, $s0, 1 j 20009 ? * 4 = 80036 80112 exit1: lw $s0, 0 ($sp)
Look at Addresses 80000 sort: 8 instructions 80036 L1: beq $t0, $zero, 17 1 instruction 80048 L2: slti $t0, $s1, 0 bne $t0, $zero, 12 6 instructions beq $t0, $zero, 5 4 instructions j 20012 80104 exit2: addi $s0, $s0, 1 j 20009 80112 exit1: lw $s0, 0 ($sp)