Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Print the values of an array using a for loop.
- .data
- numElements:
- .word 7
- elements:
- .word 55
- .word 66
- .word 77
- .word 0
- .word -16
- .word -19
- .word 82
- newline:
- .asciiz "\n"
- .text
- main:
- # Function prologue -- even main has one
- subu $sp, $sp, 24 # allocate stack space -- default of 24 here
- sw $fp, 0($sp) # save caller's frame pointer
- sw $ra, 4($sp) # save return address
- addiu $fp, $sp, 20 # setup main's frame pointer
- # for ( i = 0; i < numElements; i++ )
- # print elements[i]
- addi $s1, $zero, 0 # i = 0
- la $t0, numElements
- lw $s2, 0($t0) # $s2 = numElements
- la $t0, elements # $t0 = address of elements[0]
- loopBegin:
- # test if for loop is done
- slt $t1, $s1, $s2 # $t1 = i < numElements
- beq $t1, $zero, loopEnd
- # Compute address of elements[i]
- add $t1, $s1, $s1
- add $t1, $t1, $t1 # $t1 = 4 * i
- add $t2, $t0, $t1 # $t2 = address of elements[i]
- lw $a0, 0($t2) # $a0 = elements[i]
- addi $v0, $zero, 1
- syscall
- # print newline
- la $a0, newline
- addi $v0, $zero, 4
- syscall
- addi $s1, $s1, 1 # i++
- j loopBegin
- loopEnd:
- done: # Epilogue for main -- restore stack & frame pointers and return
- lw $ra, 4($sp) # get return address from stack
- lw $fp, 0($sp) # restore the caller's frame pointer
- addiu $sp, $sp, 24 # restore the caller's stack pointer
- jr $ra # return to caller's code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement