需要 MIPS 程序帮助
我正在开发一个将在 pcspim 上运行的 mips 程序,我需要一些帮助。程序的描述是:编写一个程序,读取一个字符串(从键盘),将其存储在内存中,并计算并打印每个字符的频率;然后它反转字符串并打印反转的字符串。
到目前为止我已经......
.data # Data declaration section
userString: .space 256
Prompt: .asciiz "\nEnter a word: "
newLine: .asciiz "\n"
.text
main: # Start of code section
li $v0, 4
la $a0, Prompt
syscall
li $v0, 8
la $a0, userString
li $a1, 256
syscall
jr $ra
la $a0, userString
move $t0, $a0
lb $t1, 0($t0)
li $v0, 4
move $a0, $t1
syscall # prints first letter of word
现在我只是想看看我是否真的将输入存储到了 userString 数组中。所以最后我尝试打印出第一个字母。但它似乎没有打印任何东西。
有什么建议吗? 感谢。
I'm working on a mips program that will run on pcspim and i need a little help. The description of the program is: Write a program that reads a string (from a keyboard), stores it in the memory, and computes and prints the frequency of each character; and then it reverses the string and prints the reversed string.
so far i have is...
.data # Data declaration section
userString: .space 256
Prompt: .asciiz "\nEnter a word: "
newLine: .asciiz "\n"
.text
main: # Start of code section
li $v0, 4
la $a0, Prompt
syscall
li $v0, 8
la $a0, userString
li $a1, 256
syscall
jr $ra
la $a0, userString
move $t0, $a0
lb $t1, 0($t0)
li $v0, 4
move $a0, $t1
syscall # prints first letter of word
Right now i just wanted to see if i've actually stored the input into the userString array. So at the end i tried to print out the first letter. but it doesnt seem to be printing anything.
Any suggestion?
thank.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将你的代码分为三个部分:提示、输入、显示。我认为前两部分已经提供给您,第三部分是您现在关注的内容。我将解释第一部分正在做什么,然后解释第三部分现在正在做什么以及此时您可能希望它做什么。
第一部分
这非常简单,当我们开始执行时,程序计数器将被设置为
main
标签的地址。它将值4
加载到$v0
(这似乎是打印字符串系统调用号),然后加载Prompt
的地址字符串放入第一个参数寄存器$a0
。最后一位仅执行将字符串显示在屏幕上的系统调用。第二部分
既然
“Enter a word:”
字符串已经打印在屏幕上,我们想要实际读取用户正在输入的内容。看起来我们在这里使用的是系统调用 #8(可能是读取字符串),因此我们将该值加载到$v0
中,为syscall
做准备。然后,由于我们要将用户字符串读取到userString
中,因此我们将该标签的地址加载到$a0
(读取字符串函数的第一个参数)中,因为我们是精明的程序员,我们给出了$a1
中userString
可以容纳多少字节的上限 (256)。然后我们执行系统调用,您在键盘上输入一个字符串并按 Enter 键,然后我们返回到下一行代码。该行是
jr $ra
,意思是“跳转到寄存器$ra
(返回地址)中存储的位置”。您可能不想要这个,因为它标志着主函数的结束,并且您的程序可能此时退出回到命令行,可能最好将其删除。第三部分
同样,您将
userString
的地址加载到$a0
中(并将其移动到下一行的$t0
中)。现在事情变得很奇怪,您将userString
的第一个字节0($t0)
加载到$t1
中。这是一个 ASCII 字符值(例如 72 等)。然后,使用打印字符串系统调用 (#4) 和$t1
参数再次启动系统调用内容。你认为这会打印单词的第一个字母,我不同意。原因如下。如果用户键入字符串“Hello, world!”这就是它在内存中的样子:因此,加载
0($t0)
将字母H
移动到寄存器$t1
中,然后当您执行系统调用时,它会尝试将从H
开始的字符串打印到屏幕上。但是,没有以字母H
开头的字符串,而是从userString
的地址开始。因此,如果您只是将userString
的地址移动到寄存器$a0
中,然后执行系统调用#4,它应该将userString
打印到屏幕上。I've broken your code down into three parts: prompting, input, display. I assume the first two parts were given to you and the third is what you are focusing on right now. I'll explain what the first to parts are doing then explain what the third is doing right now and what you probably want it to do at this point.
Part I
This is pretty straightforward, when we start executing the program counter will be set to the address of the
main
label. It loads the value4
into$v0
(that seems to be the print string system call number), and then loads the address of thePrompt
string into the first argument register$a0
. The last bit just performs the system call that puts the string on the screen.Part II
Now that the
"Enter a word: "
string has been printed on screen, we want to actually read what the user is typing. It looks like here we're using system call #8 (probably read string), so we load that value into$v0
in preparation for thesyscall
. Then, since we want to read the user string intouserString
, we load the address of that label into$a0
(the first argument for the read string function) then, since we are savvy programmers, we give the upper bound of how many bytesuserString
can hold (256) in$a1
. Then we perform the system call, you type in a string at the keyboard and hit enter and we return to the next line of code.That line is
jr $ra
, which means "jump to the location stored in register$ra
(return address)". You probably don't want this, because it marks the end of the main function and likely you program exits back to the command line at this point, probably best to remove it.Part III
Again, you're loading the address of
userString
into$a0
(and also moving it into$t0
in the next line). Now it gets weird, you load the first byte0($t0)
ofuserString
into$t1
. This is a ASCII character value (like 72 or something). Then you start up the system call stuff again with the print string system call (#4) and the argument of$t1
. Which you think will print the first letter of the word, which I disagree with. Here's why. If the user types the string, "Hello, world!" this is what it looks like in memory:So, loading
0($t0)
moves the letterH
into register$t1
, then when you perform the system call it tries to print the string starting atH
to the screen. However, there is not a string starting at letterH
, it starts at the address ofuserString
. So if you just move the address ofuserString
into register$a0
, then do system call #4 it should printuserString
to the screen.@mjsultz
我稍微改变了它。没想到我需要 2 个循环。另外,我将其增加了四,因为我认为每个字符都是 4 个字节,因此要转到下一个字母,我需要将偏移量增加四。
@mjshultz
i've changed it up a little. Didnt think i needed 2 loops. Also i've increment it by four because i thought each character is 4 bytes so to go to the next letter i need to increment the offset by four.