In-class MIPS assembly examples #1 -- add -------------------------------- # sum = a + b .data a: .word 3 b: .word 4 sum: .word 0 .text lw \$4,0(\$0) # \$4 = a lw \$5,4(\$0) # \$5 = b add \$6,\$4,\$5 # \$6 = a + b sw \$6,8(\$0) #2 -- add immediate ---------------------- # sum = a + 100 .data a: .word 3 b: .word 4 sum: .word 0 .text lw \$4,0(\$0) # \$4 = a addi \$6,\$4,100 # \$6 = a + 100 sw \$6,8(\$0) #3 -- add immediate ---------------------- will not assemble -- constant too large # sum = a + 100000 .data a: .word 3 b: .word 4 sum: .word 0 .text lw \$4,0(\$0) # \$4 = a addi \$6,\$4,100000 # \$6 = a + 100 sw \$6,8(\$0) #4 -- add immediate --------------------- switch MARS to enable psuedoinstructions and automatic generation of large constants using lui and \$1 # sum = a + 100000 .data a: .word 3 b: .word 4 sum: .word 0 .text lw \$4,a # \$4 = a addi \$6,\$4,100000 # \$6 = a + 100 sw \$6,sum #5 -- branch on equal (beq and bne are only conditional branches in MIPS) # if (a == b) sum = sum + 1; .data a: .word 3 b: .word 3 sum: .word 0 .text lw \$4,a # \$4 = a lw \$5,b # \$5 = b beq \$4, \$5, mynextline done: j done mynextline: lw \$6, sum addi \$6, \$6, 1 sw \$6, sum j done #6 -- set less than ----------------6 (branch on < requires slt and bne) # if (a < b) sum = sum + 1; .data a: .word 3 b: .word 4 sum: .word 0 .text lw \$4,a # \$4 = a lw \$5,b # \$5 = b slt \$6, \$4, \$5 # set \$6 to 1 if a < b bne \$6, \$0, mynextline done: j done mynextline: lw \$6, sum addi \$6, \$6, 1 sw \$6, sum j done