# add up the $s registers into $t0 add $t0,$s0,$s1 add $t0,$t0,$s2 add $t0,$t0,$s3 add $t0,$t0,$s4 add $t0,$t0,$s5 add $t0,$t0,$s6 add $t0,$t0,$s7 # if the sum is already 0, skip over next instruction beq $t0,$zero,skip # set $t0 to 1 li $t0,1 # do the next thing skip:
# first assume $s0 is the smallest move $t0,$s0 # if $s1 is larger, put it in $t0 ble $s1,$t0,skip1 move $t0,$s1 # if $s2 is larger, put it in $t0 skip1: ble $s2,$t0,skip2 move $t0,$s2 # do the next thing skip2:
# initialize sum (in $t3), offset (in $t7), and max offset (in $t6) li $t3,0 li $t7,0 li $t6,400 # 100 elements * 4 bytes/element # load next integer loop: add $t1,$t5,$t7 # compute address of next array element lw $t2,0($t1) # load next array element # add it to sum add $t3,$t3,$t2 # increment offset (by 4) addi $t7,$t7,4 # loop until done blt $t7,$t6,loop
# read first integer and save in $t0 li $v0,5 # set read_int code syscall move $t0,$v0 # read second integer li $v0,5 # set read_int code syscall # add them (into $t0) add $t0,$t0,$v0 # print the sum move $a0,$t0 li $v0,1 # set print_int code syscall
mult: # initialize sum move $v0, $zero # check if loop is finished (i.e., second number is 0) loop: beq $a1,$zero,done # add first number to sum add $v0,$v0,$a0 # subtract 1 from the second number and repeat addi $a1,$a1,-1 j loop # return result done: jr $ra
# set position to 31 li $t2,31 # check sign bit (high order bit), done if 1 loop: blt $s7,$zero,done # get next lower bit into sign position sll $s7,$s7,1 # decrement bit position and try again addi $t2,$t2,-1 j loop # do the next thing done:
split: # put upper 16 bits of $a0 into lower 16 bits of $v0 srl $v0,$a0,16 # put lower 16 bits of $a0 into lower 16 bits of $v1 andi $v1,$a0,0xffff # 0xffff = 16 1's # return from function jr $ra
# expects n in $a0, returns nfact(n) in $v0, uses $s0 nfact: # save $s0 and return address on the stack addi $sp,$sp,-8 sw $s0,0($sp) sw $ra,4($sp) # if n == 1, return 1 move $v0,$a0 li $t0,1 beq $a0,$t0,return # otherwise call nfact(n-1) move $s0,$a0 # save n in $s0 addi $a0,$a0,-1 # compute n-1 jal nfact # then return n*nfact(n-1) mul $v0,$s0,$v0 # restore $s0 and return address from stack and return return: lw $ra,4($sp) lw $s0,0($sp) addi $sp,$sp,8 jr $ra
# expects address of string in $a0, returns length in $v0 strlen: # set count to 0 move $v0,0 # check for a zero byte loop: lb $t0,0($a0) beq $t0,$zero,done # if not, increment address and count and try again addi $a0,$a0,1 addi $v0,$v0,1 j loop # return length done: jr $ra
# expects from string address in $a0, to address in $a1 copy: # copy next byte lb $t0,0($a0) sb $t0,0($a1) # if copied byte is 0, return beq $t0,$zero,done # increment addresses and loop addi $a0,$a0,1 addi $a1,$a1,1 j copy # return done: jr $ra
# expects addresses of strings in $a0 and $a1, returns result in $v0 # first see if the first string is at the end loop: lb $t0,0($a0) beq $t0,$zero,oneends lb $t1,0($a1) beq $t1,$zero,unequal bne $t0,$t1,unequal addi $a0,$a0,1 addi $a1,$a1,1 j loop oneends: lb $t1,0($a1) beq $t1,$zero,equal unequal: li $v0,0 jr $ra equal: li $v0,1 jr $ra
move $s0,$s1 => add $s0,$s1,$zero li $s0,7 => addi $s0,$zero,7 b target => beq $zero,$zero,target bgt $s0,$s1,target => slt $at,$s1,$s0 bne $at,$zero,target bge $s0,$s1,target => slt $at,$s0,$s1 beq $at,$zero,target blt $s0,$s1,target => slt $at,$s0,$s1 bne $at,$zero,target ble $s0,$s1,target => slt $at,$s1,$s0 beq $at,$zero,target