简体   繁体   English

在Mars MIPS中将单词存储到数组中

[英]Storing words into an array in Mars MIPS

I'm trying to teach myself Assembly using Mars for the MIPS architecture and am wondering how to store a series of words into an array. 我正在尝试使用适用于MIPS架构的Mars自学Assembly,并且想知道如何将一系列单词存储到数组中。

I know that if I have 4 words, I'd allocate 16 bytes like so: 我知道如果我有4个字,我将分配16个字节,如下所示:

.data
X: .space 16

I then have some words: 然后我有几句话:

.data
X:       .space 16
Ryan:    .asciiz "Ryan"
Tammi:   .asciiz "Tammi"
Mike:    .asciiz "Mike"
Jessica: .asciiz "Jessica"

Now, if I want to assign X[0] to Ryan, X[1] to Tammi, etc, do I first load the array address into $a0, then load the words into temp registers? 现在,如果我想将X [0]分配给Ryan,将X [1]分配给Tammi,等等,我是否要先将数组地址加载到$ a0,然后再将字加载到临时寄存器中? I'm not quite sure how to assign the values to their respective indexes and then print the values to the console. 我不太确定如何将值分配给它们各自的索引,然后将值打印到控制台。

If you don't need to do it at runtime, you can of course just initialize your array with the pointers directly: 如果您不需要在运行时执行此操作,则当然可以直接使用指针初始化数组:

.data
X:       .word Ryan, Tammi, Mike, Jessica
Ryan:    .asciiz "Ryan"
Tammi:   .asciiz "Tammi"
Mike:    .asciiz "Mike"
Jessica: .asciiz "Jessica"

Otherwise you'd have to manually do the assignment at runtime, such as: 否则,您将不得不在运行时手动进行分配,例如:

la $t0, X
la $t1, Ryan
sw $t1, ($t0)
la $t1, Tammi
sw $t1, 4($t0)
la $t1, Mike
sw $t1, 8($t0)
la $t1, Jessica
sw $t1, 12($t0)

Subsequently you can print them using a loop. 随后,您可以使用循环打印它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM