41 lines
486 B
NASM
41 lines
486 B
NASM
# Copyright (c) 2024 Freya Murphy
|
|
|
|
# file: recursion.asm
|
|
# test: should recurse sum n..0 numbers
|
|
|
|
.text
|
|
.align 2
|
|
.globl main
|
|
|
|
main:
|
|
# init $a0
|
|
li $a0, 5
|
|
|
|
sum:
|
|
# save stack
|
|
addiu $sp, $sp, -8
|
|
sw $ra, 0($sp)
|
|
sw $s0, 4($sp)
|
|
|
|
# load n from a0
|
|
move $s0, $a0
|
|
|
|
# skip if n is zero
|
|
li $v0, 0
|
|
beq $s0, $zero, add
|
|
nop
|
|
jal sum
|
|
addi $a0, $s0, -1
|
|
|
|
add:
|
|
# n = n + returned
|
|
add $v0, $s0, $v0
|
|
|
|
# restore stack
|
|
lw $ra, 0($sp)
|
|
lw $s0, 4($sp)
|
|
addiu $sp, $sp, 8
|
|
|
|
# return
|
|
jr $ra
|
|
nop
|