You are on page 1of 3

***Midterm and final cant use branch if less than, greater than, just use branch if

not equal, switch case, with each case can't adjust case's variable
***Buffer overflow
slt $t0, $s3, $s4
if ($s3 < $s4) then set $t0 = 1
otherwise $t0 = 0

slti $t0, $s2, 10

Combine with bne


slt $t0, $s0, $s1 #if (s0 < s1) t0 = 1 else t0 = 0
bne $t0, $zero, Less #if (t0 != 0) goto Less

Register zero always contains 0

No branch on less because it's too complicated

**instruction jump register


jr reg - unconditional jump to the address
ex: jr $t0

C code: while(save[i] < k)


i = i + j;
Assume: save is a word array
i -> $s3, j -> $s4, k -> $s5, base of array is save in $s6
**solution:

j begin
loop:
add $s3, $s3, $s4 #i = i + j
begin:
sll $t0, $s3, 2 #t0 =4i
add $t0, $t0, $s6 #t0 = address of save[i]
lw $t1, 0($t0) #t1 = save[i]
slt $t2, $t1, $s5 #if (save[i] < k ) $t2 = 1, else $t2 = 0
bne $t2, $zero, loop #if(t2 != 0) goto loop

C code: switch (k){


case 0: f = i + j; break;
case 1: f = g + h; break;
case 2: f = g - h; break;
case 3: f = i - j; break;
Assume: f through k correspond to registers $s0 through $s5 respectively
$t2 = 4
jump table A: L0 -> L3
Base address of A is in $s6

j begin
L0:
add $s0, $s3, $s4 #f = i + j
j exit
L1:
add $s0, $s1, $s2 #f = g + h
j exit
L2:
sub $s0, $s1, $s2 #f = g - h
j exit
L3:
sub $s0, $s3, $s4 #f = i - j
j exit
begin:
slt $t0, $s5, $zero #if k < 0, $t0 = 1 else $t0 = 0
bne $t0, $zero, exit #if k < 0 exit
subi $t3, $t2, 1 #t3x3
slt $t0, $t3, $s5 #if 3<k, t0 = 0 else t0 = 1
bne $t0, $zero, $exit if 3< k, exit
exit:

***registers are fastest place to store data


$a0-a3: four arguments registers in which to pass parameters
$v0-v1: two value registers in which to return values
$ra: one return address registers in which to return values
PC: program counter to hold the address of the current instruction being executed
jump and link constructions:
jal ProcedureAddress
+ jump to the address(procedure)
+ simultaneously save the address of the following instruction in register
* $ra = pc + 4

Procedure frame, activation record, frame pointer $fp

Ex: void strcpy(char x[], char y[]){


int i;
i = 0;
while(x[i] = y[i] != 0)
i +=1 ; }
$a0 = x, $a1 = y, i = $s0

strcpy:
addi $sp, $sp, 4 #move stack pointer
sw $s0, 0($sp) #save i to stack
add $s0, $zero, $zero #i = 0
loop:
add $t1, $s0, $a1 #$t1 = addr(y[i])
lb $t2, 0($t1) #$t2 = y[i]
add $t3, $s0, $a0 #$t3 = addr(x[i])
sb $t2, 0($t3) #x[i] = y[i]
beq $t2, $zero, exit #if y[i] = 0, exit
addi $s0, $s0, 1 #i += 1
j loop
exit:
lw $s0, 0($sp) #restore stack pointer
addi $sp, $sp, 4 #pop 1 word off stack
jr $ra #return address

Ex: void byteadd(char x[], char y[], char z[]){


int i;
i = 0;
while(y[i] != 0){
x[i] = y[i] + z[i];
i += 1;}
}

byteadd:
addi $sp, $sp, -4 #sp = $sp . 4
sw $s0, 0($sp) #store $s0 into stack
addi $s0, $zero, 0 #i = 0
loop:
add $t0, $a1, $s0 #t0 = address(y[i])
lb $t0, 0($t0) #t0 = y[i]
beq $t0, $zero, exit #if(y[i] == 0) goto exit
add $t1, $a2, $s0 #$t1 = addr(z[i])
lb $t1, 0($t1) #$t1 = z[i]
add $t0, $t1, $t0 #$t0 = z[i] + y[i]
add $t1, $a0, $s0 #$t1 = address(x[i])
sb $t0, 0($t1) #x[i] = z[i] + y[i]
addi $s0, $s0, 1 #i += 1
j loop
exit:
lw $s0, 0($sp) #restore $s0
addi $sp, $sp, 4 #free the stack
jr $ra #return

Check urself 2.10


1. From -2^17 -> 2^17 (can be negative or positive)
2. From 0 -> 256 M (long -> positive)

You might also like