2

I'm writing a program in assembly that takes one character of user input at a time and inserts it into an alphabetized string using recursion. I've been staring at this for a long time now, but it still seems like it should work. Some other eyes on this will be greatly appreciated.

        .data
text1:  .asciiz "To begin, type a lower case letter.\n" 
text2:  .asciiz "Any time you are prompted for an input, you may type a 'P' to print the alphabetized string.\n"
text3:  .asciiz "You can also type 'Q' to end the program.\n\n"
text4:  .asciiz "Input: "
text5:  .asciiz "End."
err:    .asciiz "Something broke\n"
str:    .asciiz "abcdefghijklmnopqrstuvwxyz"
buffer: .space 30
in:     .space 4

        .text
main:   la $a0, text1              #opening statements
        li $v0, 4
        syscall
        la $a0, text2
        syscall
        la $a0, text3
        syscall 

input:  la $a0, text4
        li $v0, 4
        syscall
        li $v0, 12                 #get input character
        syscall
        move $t0, $v0
        li $a0, 0x0a                #print new line
        li $v0, 11  
        syscall

        beq $t0, 0x50, print        #input is 'P'; print out string
        beq $t0, 0x51, end          #input is 'Q'; end

        la $t1, str
search: lb $t2, 0($t1)              #search for the starting location
        beqz $t2, error
        beq $t2, $t0, phase1
        addi $t1, $t1, 1
        j search

phase1: sw $t2, ($sp)           #store the letter on the stack
        sub $sp, $sp, 4     
        sw $t1, ($sp)           #store address of the letter on the stack
        sub $sp, $sp, 4
        addi $t1, $t1, 1        #increment address 
        lb $t2, 0($t1)          #getting next letter to prepare for next loop
        beqz $t2, phase2        #if end of string is reached, come out of loop
        jal phase1
        j input

phase2: lw $ra, ($sp)           #pull return location off stack
        addi $sp, $sp, 4
        lw $t3, ($sp)           #pull letter address off stack
        addi $sp, $sp 4
        lw $t4, ($sp)           #pull letter off stack
        addi $sp, $sp, 4
        addi $t3, $t3, 1        #increment old address to get new address
        sw $t4, 0($t3)          #store the letter in its new location
        jr $ra      

error:  la $a0, err         
        li $v0, 4
        syscall
        j input

print:  la $a0, str
        li $v0, 4
        syscall
        li $a0, 0x0a
        li $v0, 11
        syscall
        j input

end:    la $a0, text5
        li $v0, 4
        syscall
        li $v0, 10
        syscall

Specifically, I am getting errors on the line:

sw $t4, 0($t3)

It says: "Unaligned address in store: 0x100100e7"

After that, I get "Exception 5 [Address error in store] occurred and ignored"

followed by an unending chain of "Exception 6 [Bad instruction address] occurred and ignored"

I am using QtSpim.

Valentin Lorentz
  • 9,556
  • 6
  • 47
  • 69

1 Answers1

3

You're attempting to write a word to a non-word-aligned address with sw (store word). words on MIPS32 are 4 bytes long, and must be aligned on an address that is a multiple of 4.
You can store halfwords to halfword-aligned locations (address multiple of 2) by sh (store halfword), and bytes to byte-aligned addresses with sb (store byte, unsurprisingly).

EOF
  • 6,273
  • 2
  • 26
  • 50