有人可以向我解释这个MIPS代码的某些部分吗?

时间:2015-09-16 15:05:10

标签: string assembly char mips

 1 .data
 2 msg1: .word 0:24
 3 .text
 4 .globl main
 5 main:
 6 li $v0, 8 #syscall for read str
 7 la $a0, msg1 #load address of msg1 to store string
 8 li $a1, 100 #msg1 is 100 bytes
 9 syscall
10 lb $t0, 5($a0) #load the character into $t0
11 li $t1, 'a' #get value of 'a'
12 blt $t0, $t1, nomodify #do nothing if letter is less than 'a'
13 li $t1, 'z' #get value of 'z'
14 bgt $t0, $t1, nomodify #do nothing if letter is greater than 'z'
15 addi $t0, $t0, -0x20 #encap the letter
16 sb $t0, 5($a0) #store the new letter
17 nomodify:
18 li $v0, 4 #syscall for print str
19 syscall
20 li $v0, 10 #system call for exit
21 syscall # we are out of here.

首先,此代码的目的是从用户处获取字符串并将字符串打印回来。

我的第一个问题:

在第10行中,为什么要从$ a0 + 5加载一个字节?我得到$ a0是要打印的输入字符串,但我不明白为什么它被5偏移。

第二个问题:

在第11-14行中,如果字符小于'a'或字符大于'z',为什么分支到nomodify?如果它不在a-z的范围内,那不就是打印字符吗?

第三个问题:

在第11-16行中,如果字符既不小于'a'也不大于'z',则第15行表示将立即值-0x20添加到$ t0中,评论说这是“封装”信件”。那是什么意思?

最后:

继续使用“char”这个词令我困惑。这段代码是读取/打印字符串的吗? char只是字符串中的一个字符吗?

1 个答案:

答案 0 :(得分:0)

18 li $v0, 4 #syscall for print str 19 syscall 20 li $v0, 10 #system call for exit 21 syscall # we are out of here. {{1}}

这将是简单读/写输入字符串的真正代码。问题中的代码(特别是第10-17行)我问的是为第6个字符做了额外的事情。

相关问题