在MIPS程序方面需要帮助 [英] Need help with MIPS program

查看:94
本文介绍了在MIPS程序方面需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个将在pcspim上运行的mips程序,我需要一些帮助.该程序的描述是:编写一个程序(从键盘上读取一个字符串),将其存储在内存中,然后计算并打印每个字符的频率;然后反转字符串并打印反转的字符串.

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

现在,我只是想看看我是否实际上已将输入存储到userString数组中.因此,最后我尝试打印出第一个字母.但它似乎并没有打印任何东西.

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.

推荐答案

我已将您的代码分为三部分:提示,输入,显示.我假设前两部分已交给您,第三部分是您现在关注的重点.我将先解释第一个部分的内容,然后再解释第三个部分的当前内容,以及此时您可能想要执行的操作.

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.

    .data   # Data declaration section
userString: .space 256
Prompt:     .asciiz "\nEnter a word: "
newLine:    .asciiz "\n"
    .text

# Part I
main:       # Start of code section
li $v0, 4
la $a0, Prompt
syscall

# Part II
li $v0, 8
la $a0, userString
li $a1, 256
    syscall
jr $ra

# Part III
la $a0, userString
move $t0, $a0
lb $t1, 0($t0) 
li $v0, 4
move $a0, $t1
syscall     # prints first letter of word

第一部分

这很简单,当我们开始执行程序时,计数器将被设置为main标签的地址.它将值4装入$v0(这似乎是打印字符串系统调用号),然后将Prompt字符串的地址装入第一个参数寄存器$a0.最后一位只是执行将字符串显示在屏幕上的系统调用.

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 value 4 into $v0 (that seems to be the print string system call number), and then loads the address of the Prompt string into the first argument register $a0. The last bit just performs the system call that puts the string on the screen.

现在已经在屏幕上打印了"Enter a word: "字符串,我们想实际读取用户输入的内容.看起来在这里,我们正在使用系统调用#8(可能是读取的字符串),因此我们将该值加载到$v0中,为syscall做准备.然后,由于我们要将用户字符串读取到userString中,因此将标签的地址加载到$a0(读取字符串函数的第一个参数)中,然后,由于我们是精明的程序员,因此我们给出了$a1中最多可以容纳(256)个字节userString.然后我们执行系统调用,您在键盘上输入一个字符串,然后按Enter键,然后返回到下一行代码.

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 the syscall. Then, since we want to read the user string into userString, 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 bytes userString 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.

该行是jr $ra,表示跳转到寄存器$ra中存储的位置(返回地址)".您可能不希望这样做,因为它标志着main函数的结束,并且此时您的程序可能返回到命令行,最好将其删除.

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.

同样,您要将userString的地址加载到$a0(并在下一行将其移动到$t0中).现在变得很奇怪,您将userString的第一个字节0($t0)加载到$t1中.这是一个ASCII字符值(例如72之类的值).然后,使用打印字符串系统调用(#4)和$t1的参数再次启动系统调用内容.您认为它将打印该单词的第一个字母,但我对此表示不同意.这就是为什么.如果用户键入字符串"Hello,world!".这就是它在内存中的样子:

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 byte 0($t0) of userString 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:

userString: H  e  l  l  o  ,     w  o  r  l  d  !
    offset: 0  1  2  3  4  5  6  7  8  9 10 11 12

因此,加载0($t0)会将字母H移动到寄存器$t1中,然后在执行系统调用时,它会尝试将以H开头的字符串打印到屏幕上.但是,没有以字母H开头的字符串,而是以userString的地址开头.因此,如果仅将userString的地址移到寄存器$a0中,则执行系统调用#4,它将在屏幕上显示userString.

So, loading 0($t0) moves the letter H into register $t1, then when you perform the system call it tries to print the string starting at H to the screen. However, there is not a string starting at letter H, it starts at the address of userString. So if you just move the address of userString into register $a0, then do system call #4 it should print userString to the screen.

这篇关于在MIPS程序方面需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆