[英]Copying content of array to another array and printing in MIPS assembly
[英]Assembly - Copying an array of bytes - MIPS
首先,我已經檢查了與此程序有關的問題,但是我仍然無法克服該程序遇到的問題。
基本上,我想做的是輸入一個byte []輸入並將其復制到另一個byte [],然后打印重復的數組。 我的代碼如上所述:
.data
hello: .asciiz "hello"
inp: .byte 5
dup: .byte 5
.text
main:
la $a0, inp #get input
li $v0, 8
syscall
la $s0, dup #load arrays on s0 and s1
la $s1, inp
li $t0, 0 #instantiate offsets as 0
li $t2, 0
Load:
lb $t1, 0($s1) #load first byte
sub $t1, $t1, 48 #test if it is <0
bltz, $t1, exit #if so go to exit
add $t1, $t1, 48
sb $t1, 0($s0) #else save the byte
add $s1, $s1, 1 #increment offsets
add $s0, $s0, 1
j Load
la $a0, hello
li $v0, 4
syscall
exit:
li $t1, 0
add $s0, $s0, 1
sb $t1, 0($s0) #add null to the end of dup
la $a0, dup
li $v0, 4
syscall
jr $ra
我是MIPS的新手,我無法識別問題所在。
順便說一句,我將123作為輸入傳遞,而我獲得了無數的1s作為輸出,這表明我陷入了循環,在$ s1(inp)中再也沒有得到任何回報。
您的代碼有兩個問題:
首先, .byte 5
不會保留5個字節的空間,它聲明一個值為5的單個字節。如果要5個字節,則應說.space 5
(這些字節將使用0 IIRC值初始化)。
其次,syscall 8還有一個參數。 $a1 = maximum number of characters to read
尚未指定。 如果緩沖區中有5個字節的空間,則應將$a1
設置$a1
5。請注意, “要讀取的 最大字符數 ”實際上意味着“要讀取的最大字符數,包括終止的空字符” 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.