assembly - “Unaligned address in store” 保存单词时出错

标签 assembly recursion qtspim

我正在用汇编语言编写一个程序,该程序一次获取用户输入的一个字符,并使用递归将其插入按字母顺序排列的字符串中。我已经盯着这个很久了,但它似乎仍然应该有效。如果有其他人关注此事,我们将不胜感激。

        .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

具体来说,我在线上遇到错误:

sw $t4, 0($t3)

它说:“存储中未对齐的地址:0x100100e7”

之后,我收到“异常 5 [商店地址错误]发生并被忽略”

随后是一串无休止的“异常 6 [错误指令地址]发生并被忽略”

我正在使用 QtSpim。

最佳答案

您正尝试使用 sw(存储字)将一个写入非字对齐地址。 MIPS32 上的word 长度为 4 个字节,并且必须在 4 的倍数的地址上对齐。
您可以将 halfword 存储为半字对齐的位置(2的倍数地址)通过sh(存储半字),以及byte到字节对齐地址与sb(存储字节,毫不奇怪)。

关于assembly - “Unaligned address in store” 保存单词时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29832201/

相关文章:

java - Play Java Forms 不会递归检查约束

algorithm - Tcl 中用于排列的递归编程

mips - 最低有效位 mips

optimization - 在汇编程序中使用指针偏移是否比递减更快?

linux - 程序集中未知大小的缓冲区

assembly - AVR sbi 命令 - 错误 : number must be positive and less than 32

c++ - 为什么会发出如此复杂的代码来将有符号整数除以 2 的幂?

javascript - Vue组件中的递归组件错误

assembly - mips 组件的字符串长度

mips - 在 MIPS 中扩展寄存器的问题标志