![](/img/trans.png)
[英]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.