# sample.s: initial sample spim program # everything on a line after a "#" is a comment # mips expect to call a function "main" .globl main main: # main is a global label addu $s7, $ra, $zero # save return address # next get the address of an "array" M of integers # M will hold all the storage that our programs will use la $s1, M ### Start of compiled code # f = 5; lw $t0, 20($s1) # $t0 = 5 sw $t0, 60($s1) # f = $t0 # g = 8; lw $t0, 32($s1) # $t0 = 8 sw $t0, 64($s1) # g = $t0 # r = f*f + g*g lw $t0, 60($s1) # $t0 = f mul $t1, $t0, $t0 # $t1 = $t0 * $t0 lw $t2, 64($s1) # $t2 = g mul $t3, $t2, $t2 # $t3 = $t1 * $t2 add $t4, $t1, $t3 # $t4 = $t1 + $t3 sw $t4, 108($s1) # r = $t4 # s = g*g + 2*f*g # note: g still in $t2, g*g still in $t3, f still in $t0 mul $t5, $t0, $t2 # $t5 = f * g add $t5, $t5, $t5 # Double $t5 ( = 2*f*g) add $t6, $t3, $t5 # $t6 = $t3 + $t5 = g*g + 2*f*g sw $t6, 112($s1) # s = $t6 = $t3 + $t5 # print r li $v0, 1 # magic code for int lw $a0, 108($s1) syscall # print Blank li $v0, 4 # magic code for string la $a0, Blank la $a0, Blank syscall # print s li $v0, 1 lw $a0, 112($s1) syscall # print NewL li $v0, 4 # magic code for string la $a0, NewL syscall ### End of complied code addu $ra, $s7, $zero jr $ra # return to where main was called from .data # storage for variables M: .word 0,1,2,3,4,5,6,7,8,9 # 10 constants .space 104 # 26 variables a to z .space 800 # 200 temporaries, M[36] to M[235] Blank: .asciiz " " NewL: .asciiz "\n" Tab: .asciiz "\t"