我正在嘗試讀取用戶輸入字串并顯示前 5 個字符。我能夠獲取并列印回字串,但是由于我的分配 錯誤,我最有可能 將記憶體地址超出界限。我無法完成這項作業。move $a0,$t8lb $t8
#data segment
.data
buffer: .space 20 #allocate space for 20 bytes=characters
#text segment
.text
.globl __start
#program start
__start:
#get user input
li $v0,8
#load byte space
la $a0,buffer
#tell the system the max length
li $a1,20
move $t1,$a0
syscall
#display the input
li $v0,4
syscall
#print 5 first characters
li $t6,5
loop: lb $t8,($t1)
li $v0,4
la $a0,buffer
move $a0,$t8
syscall
add $t1,1
#1 less letter to print
sub $t6,1
#have we printed all 5
bne $t6,0,loop
t1 沒有第一個字串的位元組嗎?
感謝所有幫助和/或此問題之外的任何一般提示。
uj5u.com熱心網友回復:
移動指令不可能導致記憶體越界例外。只有加載和存盤指令會導致這種情況。
您正在使用系統呼叫 #4 列印單個位元組,因此系統呼叫嘗試取消參考$a0作為指標提供的位元組,并且作為不匹配,這將出錯。
lb $t8,($t1)
li $v0,4
la $a0,buffer # putting buffer address in $a0
move $a0,$t8 # overwriting the buffer address with byte value in $t8
syscall
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/468271.html
