【问题标题】:MIPS: store address not aligned on word boundaryMIPS:存储地址未在字边界上对齐
【发布时间】:2013-12-23 23:00:29
【问题描述】:

我正在尝试编写一个程序,该程序使用 MIPS 汇编语言计算句子中非空格字符、字符和单词的数量。我已经成功地做到了,但最后我没有什么问题。

我已经得到了这些东西的值,比如随机寄存器中的空格数,并试图将它们保存到彼此相邻的 3 个寄存器中($s1、$s2、$s3)但是当我运行我的程序时(使用 Mars ) 我收到以下错误:0x00400070 处的运行时异常:存储地址未在字边界 0x0000001e 上对齐

我将在下面发布我的代码。提前感谢任何答案!

    .data 

char_count: .word 0 

space_count: .word 0 

word_count: .word 0 

text_input: .asciiz "I'm having an old friend for dinner." 

    .text  
    .globl main

main:
la $s0, char_count        #t1 storing char_count address         

la $t0, text_input # Loading the text
li $t1, 0 # Making the character counter 0
addi $t5, $t5, 0x20 # Loading up the hex value for space
addi $t8, $t8, 0 # Making the register which decides whether or not the characters     (spaces/letters) before the space were a word.
addi $t9, $t9, 1 # Used for comparing the $t8 register to
addi $s4, $s4, 0 # Used for camping the $t8 register to

count:  
lb $t2, 0($t0)  # Load the first byte from address in $t0  
beqz $t2, end   # If the byte is 0 then jump to the end of the program
beq $t2, $t5, space # If the byte is a space then jump to the part of the program which     adds one to the space counter
beq $t8, $t9, continue   # If the word deciding register equals 1 then skip to the         continue part (to stop $t8 getting bigger than 1).
addi $t8, $t8, 1 # Add 1 to the register to indicate that a letter was the last byte.
continue:
add $t1, $t1, 1 # Increment the counter
add $t0, $t0, 1      # Increment the address  
j count      # This then loops this part of the program  

space:
addi $t4, $t4, 1 # Add 1 to the space counter
beq $t8, $s4, continue # If word deciding register equals 0 then jump to continue
sub $t8, $t8, $t9 # Make word deciding register ($t8) go back to 0
addi $t3, $t3, 1 # Add 1 to word counter
jal continue    

end:
beq $t8, $s4, finish
addi $t3, $t3, 1 # If last byte in the string was a character than another word must be         added. This stops spaces at the end being counted as a word.

finish:

sub $t6, $t1,$t4 # This works out the number of non space characters by doing number of     characters minus spaces.

sw $s1, ($t6)           # Trying to store number of non spaces in s1
sw $s2, ($t4)            #  Trying to store number of spaces in s2
sw $s3, ($t3)            # trying to store number of words in s3

li $v0, 10
syscall # Terminates the program.

【问题讨论】:

    标签: memory assembly store mips


    【解决方案1】:

    您使用 sw 的方式与您似乎想要做的不匹配:

    sw $s1, ($t6)           # Trying to store number of non spaces in s1
    

    该行实际上将包含在$s1 中的单词存储在$t6 指向的内存位置。

    如果您只是想将$t6 的内容复制到$s1 中,您应该使用:

    move $s1, $t6
    

    如果你想将$t6 存储在内存中$s1 指向的位置,你应该使用:

    sw $t6, ($s1)
    

    请记住,在这种情况下,您需要先在$s1 中放置一个有效地址;例如la $s1, char_count.

    【讨论】:

    • 感谢您的回答!我想要做的是将$t6 存储在$t1 的内存中。我将 la $s1, char_count 放在 main 中,但它仍然不起作用。我这样做对吗?另外,对于三个不同的商店,我需要放置 3 个有效地址吗?
    • 好的,我再次尝试了您的建议,现在它运行没有任何错误。但是,我没有在 $s1 中看到 $t6 中的值。我应该看到 $s1 中的值,还是对 sw 函数的含义感到困惑?
    • “in $s1”是什么意思?你说的是$s1的值还是$s1指向的内存中的值? sw 用于将寄存器的值存储到内存中,而不是将值从一个寄存器移动到另一个。
    • 我想将值存储在 s1 中。说 t6 显示 5 我希望 s1 也显示 5。我需要为此使用移动功能还是 sw 可以工作?谢谢
    • 你应该使用move
    猜你喜欢
    • 2016-08-24
    • 2017-06-25
    • 2014-05-27
    • 2012-03-05
    • 2016-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多