MIPS Assembly如何将值存储到数组中?
我正在尝试创建一个程序,可以在数组中存储最多 8 个值,然后比较所有这些值以找到最小的数字。由于某种原因,我的循环每次都会覆盖数组中的第一个位置。这就是我所拥有的。然后我在循环中将 4 添加到 $t1 中,因此一旦返回,它应该将下一个整数存储在之后的空间中。我不明白我在这里做错了什么?
.data
myArray: .space 32
Msg1: .asciiz "Enter an integer: "
.text
main:
# Print Message
li $v0, 4
la $a0, Msg1
syscall
# Prompt the user to enter an integer
li $v0, 5
syscall
# Store the first integer in $t0
move $t0, $v0
# Declare $t1 for the array position that the integer will be stored at
addi $t1, $zero, 0
# Store the integer in the array
sw $t0, myArray($t1)
#Add 4 to $t1 so store the next value in the next array position
addi $t1, $zero, 4
beq $t0, $zero, Exit
j main
Exit:
# Declare an exit to the program
li $v0, 10
syscall
I am trying to create a program where I can store up to 8 values in an array and then compare all these values to find the smallest number. For some reason my loop overwrites the first position in the array every time. Here's what I have. I then add 4 to $t1 on the loop so once it goes back around it should store the next integer in the space after that. I don't see what I'm doing wrong here?
.data
myArray: .space 32
Msg1: .asciiz "Enter an integer: "
.text
main:
# Print Message
li $v0, 4
la $a0, Msg1
syscall
# Prompt the user to enter an integer
li $v0, 5
syscall
# Store the first integer in $t0
move $t0, $v0
# Declare $t1 for the array position that the integer will be stored at
addi $t1, $zero, 0
# Store the integer in the array
sw $t0, myArray($t1)
#Add 4 to $t1 so store the next value in the next array position
addi $t1, $zero, 4
beq $t0, $zero, Exit
j main
Exit:
# Declare an exit to the program
li $v0, 10
syscall
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,让我们从 C 语言的工作算法开始:
好的,现在我们将进行一些简单的逻辑转换,以将其转换为汇编语言。首先,我们删除 for 循环,转而使用稍微简单的 while 循环结构。
接下来,我们将 while 循环转换为程序集的 if-goto-label。 (我们可以先执行 if-then;我们转换的顺序并不重要。)
接下来,我们将执行 if-then 语句。我们本来可以先这样做,但这不会改变分析或结果。
接下来我们将把它转化为汇编语言。
首先,将变量分配给物理存储,这里最好的选择是寄存器。思维导图:
第二,按照上面最后一个翻译代码:
剩下的就是在其周围放置一些开始和结束代码,假设寄存器编号匹配。
起始代码会将数组的地址放入
$a0
中,并将元素计数放入$a1
中。当然可以使用不同的寄存器,并进行适当的修改。结束代码应该期望
$v0
中的结果,打印或以其他方式。起始代码将完全在此代码之前,结束代码完全在此代码之后。
是的,有几个步骤 - 但每个步骤都是相对简单且符合逻辑的转换。逻辑转换可以翻译 C 代码,首先保留 C 代码,然后进行简化,以便轻松直接进行汇编。
每个控制结构(if、while)一次可以更改一个,留在 C 中,并不断检查代码是否继续在 C 中工作!控制结构转换的顺序并不重要(从内到外,从外到内)。
一个。将C的逻辑变量映射到机器代码的物理存储
b.转换语句 &从 C 语言到汇编语言的表达式
First, let's start with a working algorithm in C:
Ok, now we'll make some simple logical transformations on the way to taking this to assembly language. First, we remove the for loop in favor of the slightly simpler while loop construct.
Next, we'll transform the while loop into assembly's if-goto-label. (We could work the if-then first instead; the order we transform doesn't matter.)
Next, we'll do the if-then statement. We could have done it first, that wouldn't change the analysis or results.
Next we'll take this to assembly language.
First, assign variables to physical storage, here good choice is registers. Mental map:
Second, translate code as per the last above:
All that's left is to put some starting and ending code around that, assuming the register numbers match up.
The starting code for this would put the address of the array into
$a0
, and the count of elements into$a1
. Could use different registers of course, with appropriate modifications.The ending code should expect the result in
$v0
, to print or otherwise.The starting code would be entirely before this code, and the ending code entirely after.
Yes, there are a few steps — but each one is a relatively simple and logical transformation. Logical transformations enable translating the C code, first staying in C but simplifying to make it easy to go right to assembly.
Each control structure (if, while) can be changed one at a time, stay in C, and keep checking that the code continues to work in C! Order of control structure transformations doesn't matter (inside out, outside in).
a. Map logical variables of C into physical storage of machine code
b. Convert statements & expressions from C into assembly language