简体   繁体   English

汇编-复制字节数组-MIPS

[英]Assembly - Copying an array of bytes - MIPS

First of all, I have already checked related questions to this one, yet I am still not able to overcome the problem I have with this program. 首先,我已经检查了与此程序有关的问题,但是我仍然无法克服该程序遇到的问题。

What I am trying to do is, basically, take a byte[] input and duplicate it to another byte[], and print the duplicate array. 基本上,我想做的是输入一个byte []输入并将其复制到另一个byte [],然后打印重复的数组。 My code is as above: 我的代码如上所述:

.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

I am new to MIPS and, I am not able to recognize what the problem is. 我是MIPS的新手,我无法识别问题所在。

By the way, I am passing 123 as an input and I am getting countless of 1s as output, which tells me that I am stuck in the loop and never getting any further in $s1 (inp). 顺便说一句,我将123作为输入传递,而我获得了无数的1s作为输出,这表明我陷入了循环,在$ s1(inp)中再也没有得到任何回报。

There are a couple of problems with your code: 您的代码有两个问题:

First, .byte 5 doesn't reserve space for 5 bytes, it declares a single byte with the value 5. If you want 5 bytes you should say .space 5 (the bytes will be initialized with the value 0 IIRC). 首先, .byte 5不会保留5个字节的空间,它声明一个值为5的单个字节。如果要5个字节,则应说.space 5 (这些字节将使用0 IIRC值初始化)。

Second, syscall 8 takes one more argument; 其次,syscall 8还有一个参数。 $a1 = maximum number of characters to read , which you haven't specified. $a1 = maximum number of characters to read尚未指定。 If you have room for 5 bytes in your buffer you should set $a1 to 5. Note that "maximum number of characters to read" actually means "maximum number of characters to read including the terminating null-character" . 如果缓冲区中有5个字节的空间,则应将$a1设置$a1 5。请注意, “要读取的 最大字符数 实际上意味着“要读取的最大字符数,包括终止的空字符”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM