MIPS-如何对弦进行移位?

时间:2019-02-28 22:01:13

标签: string mips shift

我对MIPS编程非常陌生,但是我一直想解决一个试图编程的问题。我意识到我要做的事可能很愚蠢,但请忍受!这是我要尝试做的描述。

假设我有以下字符串:“ ~~ Hello World!”。我想获取字符串“ Hello World!”。将此字符串向左移动两个字符。到目前为止,我最接近执行此操作的尝试是:

让寄存器$ t0包含字符串“ ~~ Hello World!”。我想对该字符串执行2位左移并将其存储在寄存器$ t1中。

.data
     output1: .asciiz "The value in $t1 is: "

.text
     sll $t1, $t0, 2   # attempt at shifting left by 2 bits 
     li $v0, 4
     la $a0, output1
     syscall           # print "The value in $t1 is: "
     li $v0, 4
     move $a0, $t1
     syscall           # print the contents of the register $t1

但是,当我汇编这些说明时,遇到地址超出范围错误。有人可以指出我要去哪里哪里了,也许我应该怎么做才能实现这一目标?

1 个答案:

答案 0 :(得分:0)

我知道了!这是一个更新的代码段,现在包含工作说明。我将保留此帖子,以免对其他人有帮助。

.data
     output1: .asciiz "The value in $t1 is: "
.text
     add $t0, $t0, 2   # shifts the string left by 2 bits (CORRECT) 
     li $v0, 4
     la $a0, output1
     syscall           # print "The value in $t1 is: "
     li $v0, 4
     move $a0, $t1
     syscall           # print the contents of the register $t1