Created
November 15, 2017 06:28
-
-
Save kamontat/336db828ab99a27e6236facc72ffcd45 to your computer and use it in GitHub Desktop.
summation array on MIPS assembly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.data # data section | |
array_a: .word 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 | |
array_b: .word 0x7fffffff, 0x7ffffffe, 0x7ffffffd, 0x7ffffffc, 0x7ffffffb, 0x7ffffffa, 0x7ffffff9, 0x7ffffff8, 0x7ffffff7, 0x7ffffff6 | |
output_a: .asciiz "Sum a = " | |
output_b: .asciiz "Sum b = " | |
new_line: .asciiz "\n" | |
.text # text section | |
.globl main # call main by SPIM | |
main: | |
# out of loop when we got 10 numbers | |
la $t0, array_a | |
la $t1, array_b | |
li $t2, 0 # int i | |
li $t3, 0 # int sum | |
# sum all number in a array "a" | |
loopA: | |
# condition to terminal loop | |
sltiu $t7, $t2, 20 | |
beq $t7, $0, endA | |
# load array to $t4 | |
lw $t4, ($t0) | |
addu $t3, $t3, $t4 | |
# advance $t0 by 4 | |
addiu $t0, $t0, 4 | |
# i++ | |
addiu $t2, $t2, 1 | |
j loopA | |
endA: | |
# print string "sum" | |
la $a0, output_a | |
li $v0, 4 | |
syscall | |
#print sum of "a" | |
addu $a0, $0, $t3 | |
li $v0, 1 | |
syscall | |
# new line | |
la $a0, new_line | |
li $v0, 4 | |
syscall | |
# reset `i` and `sum` | |
li $t2, 0 # int i | |
li $t3, 0 # int sum | |
# sum all number in a array "b" | |
loopB: | |
# condition to terminal loop | |
sltiu $t7, $t2, 10 | |
beq $t7, $0, endB | |
# load array to $t4 | |
lw $t4, ($t1) | |
addu $t3, $t3, $t4 | |
# advance $t0 by 4 | |
addiu $t1, $t1, 4 | |
# i++ | |
addiu $t2, $t2, 1 | |
j loopB | |
endB: | |
# print string "sum" | |
la $a0, output_b | |
li $v0, 4 | |
syscall | |
#print sum of "b" | |
addu $a0, $0, $t3 | |
li $v0, 1 | |
syscall | |
# call exit once everything is done | |
li $v0, 10 | |
syscall |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment