如何将MIPS中的字符串(逐行)加载到内存中?

时间:2016-10-06 20:50:40

标签: mips

450A 480C 0258 A200 B000 数据 000A 0005 END

例如,我有一个字符串:" 450A / n480C / N .." 对于每个存储器地址(一个字节),如何将字符串逐字节存储(每行为4个字节)到存储器地址。 例如: 我有地址:0x00,0x01,0x02,0x03,我应该如何将450A / n480C存储到内存中? 像0x00-4,0x01-5, 我是MIPS的新手,有人可以给我一些帮助吗?

1 个答案:

答案 0 :(得分:0)

每行都是4个字节,所以每个字符都是1个字节......

您可以使用'sb'=存储字节指令来执行此操作,使用循环...如果在$ s0中您想要保存每个字符并且您的字符串“45 .....”在$ t0中可以做到这一点:

(我已经为你制作了这个程序......虽然这可以通过一个功能完成)

    .data

save:      .space 55    # this is where you are going to save byte by byte your string
string:    .asciiz "450A/n480C/n.."

    .globl __start
    .text

__start:


    la $s0, save    # we are just loading adresses
    la $t0, string

    Loop:

        lb $t1, 0($t0)    # 'load byte' of your string... in other words take the 1st
                          # byte of your string and put it in $t1

        sb $t1, 0($s0)    # storing the 1st byte of the string in the 1st adress of $s0

        addi $s0 $s0, 1   # we have to increment the adress, so the next byte 
                          # of $t0 will be stored in the next adress of $s0

        addi $t0, $t0, 1   # we also have to move the pointer in the string, so we can take 
                          # the next character

        beq $t1, $zero, End # every string ends in '\0' (null) character, which is zero
                           # so 'beq' means that if the string is finished and
                           # the char we have is '\0', we will go to End

        j Loop    # if its not the end of the string; we are going to loop again 
                           # and do the same steps until the string is finished

    End:

    li $v0, 10
    syscall             # exit and finish program