#################################################################### # # # CSE 410 Lecture 3 examples # # (taken from CSE 378, wi10 - original author unknown) # # Exercise: run an instruction at a time in SPIM # # # #################################################################### #==================================================================== # Static data allocation and initialization #==================================================================== .data A: .byte 1, 2, 3, 4, 5, 6 # Create space for A, and give # values in decimal: 1, 2, 3, 4, 5, 6 result: .word 9 # allocate 32 bits, # initialize to 9 for no good reason # Another array for fun, values in hex 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 B: .byte 'a', 'b', 'c', 'd', 'e', 'f' #==================================================================== # Program text #==================================================================== .text .globl main main: # # Main program entry # addiu $sp, $sp, -4 # allocate stack sw $ra, 0($sp) # save return address # # Main body # # Register usage: # $a0 = A's address # $a1 = result's address # Random calculations: li $t3, 5 # t3 = 5 li $t4, 2 # t4 = 2 add $t5, $t3, $t4 # t5 = t3 + t4 add $t5, $t5, $t4 # t5 = t5 + t4 add $t5, $t5, $t4 # t5 = t5 + t4 sub $t5, $t5, $t3 # t5 = t5 - t3 # Example from lecture: # # char A[6] = {1, 2, 3, 4, 5, 6}; # int result; # result = A[0] + A[1] + A[2] + A[3]; # Assume A's address in register $a0 la $a0, A # "load address" of A into $a0 # Assume result's address in register $a1 la $a1, result # "load address" of result into $a1 # load byte copies the byte to the low order byte of the register # and sign extends to the higher order bytes. lb $t0, 0($a0) # Assume A's address in register $a0, load A[0] lb $t1, 1($a0) # Get second element A[1] add $t0, $t1, $t0 # Add in second element lb $t1, 2($a0) # Get third element A[2] add $t0, $t1, $t0 # Add in third element lb $t1, 3($a0) # Get fourth element A[3] add $t0, $t1, $t0 # Add in fourth element sw $t0, 0($a1) # Assume result's address in register $a1, save # Here are some extra instructions to help clarify the diff # between sb and sw, write some values back into the original array. # Byte order (endian-ness) is discussed on page A-42 of Appendix A. sb $t0, 4($a0) # Note that only on byte is changed. sw $t0, 0($a0) # Note that the entire word is changed. # main exit # exit: lw $ra, 0($sp) # restore return address addiu $sp, $sp, 4 # pop stack jr $ra # return