我正在尝试编写一个程序,使用MIPS汇编语言计算句子中非空格字符,字符和单词的数量。我已经成功地做到了这一点,但最后我没有什么问题。
我已经得到了这些东西的值,比如随机寄存器中的空格数,我试图将它们保存到3个寄存器中,彼此相邻($ s1,$ s2,$ s3)但是当我运行我的程序时(使用火星) )我收到以下错误: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.
答案 0 :(得分: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
。