0

having some trouble with an assignment, not looking for solution code, just some answers to why I keep encountering this error.

The program asks the user for 10 digits and then displays the resulting sum (using the stack and recursion).

I know from debugging that the read: portion works fine, but I get the error "Runtime exception at 0x0040005c: fetch address not aligned on word boundary 0x00000001". The error refers to the line in summation: lw $t0, 0($a0)

Not sure what is wrong since I only ever increment by 4 bytes. I've checked other similar questions but couldn't find any clarification. Any help would be greatly appreciated.

.data

A1:     .space 40
input:  .asciiz "please enter a number: "
Str:    .asciiz "the sum is: "


.text

  main:
    li $s1, 0       
    la $t0, A1
    addi $s0, $zero, 0

  read:
    beq $s1, 10, prep
    li $v0, 4          
    la $a0, input       
    syscall 
    li $v0, 5          
    syscall            
    sw $v0, ($t0)       
    addi $t0, $t0, 4      
    addi $s1, $s1, 1        
    j read

  prep:
    la $a0, A1
    addi $a1, $a0, 40
    jal summation

  summation:

    subi $sp, $sp, 8    
    sw $ra, 4($sp)      
    lw $t0, 0($a0)
    sw $t0, 0($sp)      
    sgt $t1, $a0, $a1
    beq $t1, $zero, L1
    addi $v0, $zero, 0
    addi $sp, $sp, 8
    jr $ra

  L1:
    addi $a0, $a0, 4
    jal summation
    lw $a0, 0($sp)
    lw $ra 4($sp)
    addi $sp, $sp, 8
    add $v0, $a0, $v0
    jr $ra
  • as you can see e.g. here: https://stackoverflow.com/questions/33105872/what-does-space-do-in-mips ``.space`` does not care about alignment. But mips CPU does care about natural alignment. Hence, your exception. Consider using ``.align 4`` prior to your ``.space`` line. – BitTickler Apr 06 '18 at 22:58
  • Check in debugger (single stepping over each instruction, watching if the code is doing what you intended and values in registers are as expected), where the address 0x000001 comes from, it looks completely invalid (even outside of `.data` section address range). – Ped7g Apr 06 '18 at 23:00
  • And your code flow looks wrong on first random read of part of source: `jal summation` will jump to `summation` label, storing address of next instruction into `$ra`, but next instruction is at the same `summation` address, so even when the code will return by `jr $ra` back, it will do `summation` part second time. This looks completely undebugged, how did you want to verify correctness of your code? Just by checking output? That doesn't work in assembly, you have to debug it per instruction and meticulously reason about every one of them, if it is really doing what you really did want... – Ped7g Apr 06 '18 at 23:02
  • @Ped, on first read, I also thought it was not handling $ra correctly, but now I think it is: if it doesn't branch to L1, $ra has not been overwritten, so its value is still correct, and if it does branch to L1, $ra is reloaded from 4($sp) [assuming the missing comma really is present]. – prl Apr 06 '18 at 23:05
  • @prl it will still end executing the `summation` subroutine twice in total (if everything else is correct) ... `jal label` followed by `label:` usually doesn't make sense, unless you are size-optimizing and you really want to call the subroutine twice, and then it should be nested call by the top `jal that_second_jal`, which has more meaningful continuation after. (missing coma is not a problem for MARS, OP didn't specify assembler, but judging by that missing coma and syscall usage it will be probably MARS). EDIT: even if this would work, it's very unlogical way of code = should've comment! – Ped7g Apr 06 '18 at 23:07
  • @Ped, Oh, sorry, duh, I was just looking *within* summation, not at the call preceding it. Thanks. – prl Apr 06 '18 at 23:10

0 Answers0