Conditional branches
(beq, bne, slt, slti, sltu( chck unsigned number) )
MIPS | |
if ( i == j ) f = g + h; else g = g - h; | assume: f = $s0, g = $s1, h = $s2 i = $s3, j = $s4 #--------------- bne $s3, $s4, ELSE add $s0, $s1, $2 j Exit # skip else ELSE: sub $s1, $s1, $s2 Exit: |
if ( i < j ) f = g + h; else g = g - h; | slt $t0, $s3, $s4 # i < j, $t0 = 0, else $t0=1 bne $t0, 1, Else add $s0, $s1, $s2 j Exit # skip else part Else: sub $s1, $s1, $s2 Exit: |
While Loop
MIPS | |
while ( save[i] == k ) i += 1; | assume: i = $s3, k = $s5 , save[0] = $s6 #------------------------ (convert lw $t0, 4i($s6) ) Loop: sll $s3, $s3, 2 # i = 4i add $t1, $s3, $s6 # 4i($s6) lw $t0, 0($t1) # lw $t0, 4i($s6) bne $t0, $s5, Exit addi $s3,$s3, 1 j Loop # loop again Exit: |
Case / Switch Statement
switch (k) { case 0: f = i + j; break; case 1: f = g + h; break; case 2: f = i - j; break; case 3: f = g - h; break; } --------------------------------------- assume: f = $s0, g = $s1, h = $s2, i = $s3, j = $s4, k = $s5 $t2 = 4 $t4 contains address of an array, jumptable | slt $t0, $s5, $0 # k < 0; $t0 = 1; else $t0 =0 beq $t0, 1, Exit # k < 0, go to L1 beq $s5, 0, L0 # k = 0, go to L2 beq $s5, 1, L1 beq $s5, 2, L2 beq $s5, 3, L3 slt $t0, 3, $s5 # k > 3, $t0 = 1; else $t0 = 0 beq $t0, 1, Exit L0: add $s0, $s3, $s4 j Exit L1: add $s0, $s1, $s2 j Exit L2: sub $s0, $3, $s4 j Exit L3: sub $s0, $s1,$s2 j Exit Exit: |
Procedures
MIPS | |
int leaf_example ( int g, int h, int i, int j ) { int f; f = ( g + h ) - ( i + j ); return f; } f, g, h , i, j are local variables in the procedure. any declaired variable in procedure use $s0 - $s7 | addi $sp, $sp, -12 sw $t1, 8($sp) sw $t0, 4($sp) sw $s0, 0($sp) add $t0, $a0, $a1 add $t1, $a2, $a3 sub $s0, $t0, $t1 add $v0, $s0, $0 lw $s0, 0($sp) lw $t0, 4($sp) lw $t1, 8($sp) addi $sp, $sp, 12 jr $ra |
Does it work??? If I don’t store in the stack? add $v0, $a0, $a1 add $t0, $a2, $a3 sub $v0, $v0, $t0 jr $ra | is it right???? #-------------------------------- addi $sp, $sp, -8 sw $t0, 4($sp) sw $t1, 0($sp) add $t0, $a0, $a1 add $t1, $a2, $a3 sub $v0, $t0, $t1 lw $t1, 0($sp) lw $t0, 4($sp) addi $sp, $sp,8 jr $ra |
Nested Procedures
int fact ( int n ) { if ( n < 1 ) return (1); else return ( N * fact ( n - 1)); } | #---------------------- fact: addi $sp, $sp, -8 sw $ra, 4($sp) # save the return address sw $a0, 0($sp) # save the argument n slti $t0, $a0, 1 # n < 1; $t0 = 1; else $t0 = 0 beq $t0, $0, L1 # n >= 1 go to L1 addi $v0, $0, 1 # return 1 addi $sp, $sp, 8 jr $ra L1: addi $a0, $a0, -1 # n >= 1; argument gets ( n - 1 ) jal fact lw $a0, 0($sp) lw $v0, 4($sp) addi $sp, $sp,8 mul $v0, $a0, $v0 # return n * fact ( n - 1 ) jr $ra |
int fact ( int n ) { if ( n < 1 ) return (1); else return ( N * fact ( n - 1)); } | 2 values need to be stored: n, return value fact: addi $sp, $sp, -8 sw $a0, 4($sp) sw $ra, 0($sp) slt $t0, $a0, 1 # $a0 < 1 , $t0 = 1; else $t0 = 0 beq $t0, $0, L1 # n >= 1 # n < 1 add $v0, $0, 1 # return 1 addi $sp, 8($sp) # jr $ra L1: # (n >= 1) addi $a0, $a0, -1 # n-- jal fact lw $ra, 0($sp) lw $a0, 4($sp) addi $sp, $sp, -8 mul $v0, $a0, $v0 # N * fact (n -1) jr $ra |
No comments:
Post a Comment