Created
April 29, 2014 16:54
-
-
Save giuscri/11405995 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
;; triangleNumbers.asm | |
.text | |
.globl main | |
main: | |
;; Push $ra onto the stack ... | |
subu $sp, $sp, 4 | |
sw $ra, 0($sp) | |
;; Push $fp onto the stack ... | |
subu $sp, $sp, 4 | |
sw $fp, 0($sp) | |
;; Make room for two variables `a` and `b` ... | |
subu $fp, $sp, 8 | |
move $sp, $fp | |
;; Initialize `a` to 0 ... | |
sw $zero, 0($fp) | |
;; Initialize `b` to 0 ... | |
sw $zero, 0($fp) | |
;; Prompt the user for the value of `a` ... | |
la $a0, N | |
li $v0, 4 | |
syscall | |
li $v0, 5 | |
syscall | |
;; Store value of `a` ... | |
sw $v0, 4($fp) | |
;; Call triangle( a ) ... | |
lw $a0, 4($fp) | |
nop | |
nop | |
jal triangle | |
nop | |
;; Store the output into `b` | |
sw $v0, 0($fp) | |
;; Print result ... | |
la $a0, Triangle | |
li $v0, 4 | |
syscall | |
la $a0, blankSpace | |
li $v0, 4 | |
syscall | |
la $a0, leftCurve | |
li $v0, 4 | |
syscall | |
la $a0, blankSpace | |
li $v0, 4 | |
syscall | |
lw $a0, 4($fp) | |
nop | |
nop | |
li $v0, 1 | |
syscall | |
la $a0, blankSpace | |
li $v0, 4 | |
syscall | |
la $a0, rightCurve | |
li $v0, 4 | |
syscall | |
la $a0, blankSpace | |
li $v0, 4 | |
syscall | |
la $a0, equal | |
li $v0, 4 | |
syscall | |
la $a0, blankSpace | |
li $v0, 4 | |
syscall | |
lw $a0, 0($fp) | |
nop | |
nop | |
li $v0, 1 | |
syscall | |
la $a0, lf | |
li $v0, 4 | |
syscall | |
;; Delete room for the two variables ... | |
addu $sp, $sp, 8 | |
;; Pop out $fp ... | |
lw $fp, 0($sp) | |
nop | |
nop | |
addu $sp, $sp, 4 | |
;; Pop out $ra ... | |
lw $ra, 0($sp) | |
nop | |
nop | |
addu $sp, $sp, 4 | |
exit: | |
;; Jump back to the OS ... | |
jr $ra | |
nop | |
triangle: | |
;; Push $ra onto the stack ... | |
subu $sp, $sp, 4 | |
sw $ra, 0($sp) | |
;; Push $fp onto the stack ... | |
subu $sp, $sp, 4 | |
sw $fp, 0($sp) | |
;; Make room for two variables `c` and `d` ... | |
subu $fp, $sp, 8 | |
move $sp, $fp | |
;; Initialize them ... | |
sw $zero, 0($fp) | |
sw $zero, 4($fp) | |
;; Store `c` as the input of triangle() ... | |
sw $a0, 4($fp) | |
;; Load it ... | |
lw $t0, 4($fp) | |
nop | |
nop | |
;; If `c` > 1, recurseTriangle ... | |
bge $t0, 1, recurseTriangle | |
nop | |
;; Else, notRecurseTriangle ... | |
j notRecurseTriangle | |
nop | |
recurseTriangle: | |
;; Call triangle( c -1 ) ... | |
lw $t0, 4($fp) | |
nop | |
nop | |
subu $a0, $t0, 1 | |
jal triangle | |
nop | |
;; Then, store `d` as `c + triangle( c -1 )` ... | |
lw $t0, 4($fp) | |
nop | |
nop | |
addu $t0, $t0, $v0 | |
sw $t0, 0($fp) | |
;; Then return ... | |
j returnTriangle | |
nop | |
notRecurseTriangle: | |
;; Store `d` as c ... | |
lw $t0, 4($fp) | |
nop | |
nop | |
sw $t0, 0($fp) | |
;; Then return ... | |
j returnTriangle | |
nop | |
returnTriangle: | |
;; Load `d` as the output ... | |
lw $v0, 0($fp) | |
nop | |
nop | |
;; Delete room for the two variables ... | |
addu $fp, $fp, 8 | |
move $sp, $fp | |
;; Pop out $fp ... | |
lw $fp, 0($sp) | |
nop | |
nop | |
addu $sp, $sp, 4 | |
;; Pop out $ra ... | |
lw $ra, 0($sp) | |
nop | |
nop | |
addu $sp, $sp, 4 | |
;; Return back to the caller ... | |
jr $ra | |
nop | |
.data | |
Triangle: .asciiz "Triangle" | |
leftCurve: .asciiz "(" | |
rightCurve: .asciiz ")" | |
blankSpace: .asciiz " " | |
equal: .asciiz "=" | |
lf: .asciiz "\n" | |
N: .asciiz "N? " |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment