Skip to content

Instantly share code, notes, and snippets.

@chintanparikh
Created February 25, 2014 05:20
Show Gist options
  • Save chintanparikh/9203188 to your computer and use it in GitHub Desktop.
Save chintanparikh/9203188 to your computer and use it in GitHub Desktop.
!************************************************************************!
! !
! general calling convention: !
! !
! (1) Register usage is as implied in the assembler names !
! !
! (2) Stack convention !
! !
! The stack grows towards higher addresses. The stack pointer !
! ($sp) points to the next available (empty) location. !
! !
! (3) Mechanics !
! !
! (3a) Caller at call time: !
! o Write any caller-saved stuff not saved at entry to !
! space on the stack that was reserved at entry time. !
! o Do a JALR leaving the return address in $ra !
! !
! (3b) Callee at entry time: !
! o Reserve all stack space that the subroutine will need !
! by adding that number of words to the stack pointer, !
! $sp. !
! o Write any callee-saved stuff ($ra) to reserved space !
! on the stack. !
! o Write any caller-saved stuff if it makes sense to !
! do so now. !
! !
! (3c) Callee at exit time: !
! o Read back any callee-saved stuff from the stack ($ra) !
! o Deallocate stack space by subtract the number of words !
! used from the stack pointer, $sp !
! o return by executing $jalr $ra, $zero. !
! !
! (3d) Caller after return: !
! o Read back any caller-saved stuff needed. !
! !
!************************************************************************!
!vector table
vector0: .fill 0x00000000 !0
.fill 0x00000000 !1
.fill 0x00000000 !2
.fill 0x00000000
.fill 0x00000000 !4
.fill 0x00000000
.fill 0x00000000
.fill 0x00000000
.fill 0x00000000 !8
.fill 0x00000000
.fill 0x00000000
.fill 0x00000000
.fill 0x00000000
.fill 0x00000000
.fill 0x00000000
.fill 0x00000000 !15
!end vector table
main:
addi $sp, $zero, initsp ! initialize the stack pointer
lw $sp, 0($sp)
! Install timer interrupt handler into the vector table
!FIX ME
ei !Don't forget to enable interrupts...
addi $a0, $zero, 2 !load base for pow
addi $a1, $zero, 1 !load power for pow
addi $at, $zero, POW !load address of pow
jalr $at, $ra !run pow
halt
POW:
addi $sp, $sp, 2 ! push 2 slots onto the stack
sw $ra, -1($sp) ! save RA to stack
sw $a0, -2($sp) ! save arg 0 to stack
beq $zero, $a1, RET1 ! if the power is 0 return 1
beq $zero, $a0, RET0 ! if the base is 0 return 0
addi $a1, $a1, -1 ! decrement the power
la $at, POW ! load the address of POW
jalr $at, $ra ! recursively call POW
add $a1, $v0, $zero ! store return value in arg 1
lw $a0, -2($sp) ! load the base into arg 0
la $at, MULT ! load the address of MULT
jalr $at, $ra ! multiply arg 0 (base) and arg 1 (running product)
lw $ra, -1($sp) ! load RA from the stack
addi $sp, $sp, -2 ! pop the RA and arg 0 off the stack
jalr $ra, $zero ! return
RET1: addi $v0, $zero, 1 ! return a value of 1
addi $sp, $sp, -2
jalr $ra, $zero
RET0: add $v0, $zero, $zero ! return a value of 0
addi $sp, $sp, -2
jalr $ra, $zero
MULT: add $v0, $zero, $zero ! zero out return value
AGAIN: add $v0,$v0, $a0 ! multiply loop
addi $a1, $a1, -1
beq $a1, $zero, DONE ! finished multiplying
beq $zero, $zero, AGAIN ! loop again
DONE: jalr $ra, $zero
ti_inthandler:
!FIX ME
initsp: .fill 0xA00000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment