Question

I'm trying to figure out some stack frame business with assembly, and i admittingly have little idea what i'm doing... but i think i'm close?

My program calls a subroutine that counts down from 10 and then quits. This actually seems to work fine, but i'm receiving some exception errors.

Here's my output

enter image description here

Here is my code:

main: ######################################################################

    ################### SAVE STACK ###################

    subu $sp, $sp, 32           # create 32 byte stack frame

    ########## save ra and fp
    sw $ra, 0($sp)              # save return address
    sw $fp, 4($sp)              # save frame pointer

    #addu $fp, $sp, 28          # setup new frame pointer ?????

    ########## save general purpose registers

    sw $s0, 8($s0)              # save counter 

    ##################################################

    ########## Begin Message
    li, $v0, 4                  # sys call #4, print string
    la, $a0, STR_BEGIN          # load string
    syscall                     # execute call

    li, $s0, 10                 # init loop counter

    ########## Print Starting Point
    li $v0, 1                   # sys call #1, print int
    move $a0, $s0               # load starting count
    syscall                     # execute call

    ########## Call Subroutine
    jal subroutine              # call subroutine
    # print something to show when it returns

    ########## END Message
    #li, $v0, 4                 # sys call #4, print string
    #la, $a0, STR_END           # load string
    #syscall                    # execute call

    ################## RESTORE STACK #################

    ########## restore general purpose registers
    lw $s0, 8($sp)              # restore counter

    ########## restore ra and fp
    lw $ra, 0($sp)              # restore return address
    lw $fp, 4($sp)              # restore frame pointer

    addu $fp, $sp, 32           # restore callers stack pointer

    ##################################################

ending:    jr $31               # stop the program, why does this loop if i uncomment the above syscall?

subroutine: ######################################################################

    ################### SAVE STACK ###################

    subu $sp, $sp, 32           # create 32 byte stack frame

    ########## save ra and fp
    sw $ra, 0($sp)              # save return address
    sw $fp, 4($sp)              # save frame pointer

    addu $fp, $sp, 28           # setup new frame pointer ?????? why am i doing this?

    ########## save general purpose registers

    sw $s0, 8($s0)              # save counter 

    ##################################################

    sub $s0, $s0, 1             # decrement by one

    ########## Count Down
    li, $v0, 4                  # sys call #4, print string
    la, $a0, STR_ELIPS          # load string
    syscall                     # execute call

    li $v0, 1                   # sys call #1, print int
    move $a0, $s0               # load starting count
    syscall                     # print output

    ########## subroutine condition
    bgt $s0, 0, subroutine      # if count < 10, then loop  

    ################## RESTORE STACK #################

    ########## restore general purpose registers
    lw $s0, 8($sp)              # restore counter

    ########## restore ra and fp
    lw $ra, 0($sp)              # restore return address
    lw $fp, 4($sp)              # restore frame pointer

    addu $fp, $sp, 32           # restore callers stack pointer

    ##################################################

    jr  $ra                     # return to caller

# END subroutine ######################################################################
Was it helpful?

Solution

The first obvious error I could spot was your

sw $s0, 8($s0)              # save counter 

line (2 occurrences). You probably wanted sw $s0, 8($sp) given that you restore s0 from there later. Fix that and use a debugger to find further errors if any.

Update: also this is wrong:

addu $fp, $sp, 32           # restore callers stack pointer

Should probably be addu $sp, $sp, 32.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top