MIPS汇编语言遍历数组 [英] MIPS Assembly language traversing an array

查看:97
本文介绍了MIPS汇编语言遍历数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要遍历数组,您是将逻辑左移 2 次还是每次将指向基地址的指针增加 4?什么是首选方法?什么是更好的方法?

To traverse an array would you shift left logical 2 times or would you increment the pointer to the base address each time by 4? What is the preferred method? What is the better method?

推荐答案

假设您从 $t0 开始,指向单词数组开头的内存位置:

Let's assume you're starting with $t0 pointing to the memory location at the start of your word array:

.data
mywords: .word 0:100 # 100 words, initialized to 0x0
.text
la $t0, mywords

假设您从 $t0 中的一个我们不想移动的值开始,这里有一些替代方案:

Assuming you start with a value in $t0 that we don't want to shift, here are some alternatives:

sll 方法

# $t0 points to base address, $t1 word counter initialized to zero
loop:
sll $t2, $t1, 2 # Turn word pointer into a byte pointer
add $t2, $t0, $t2 # Get pointer to the word we want
lw $s0, 0($t2) # Load the word into $s0
addi $t1, $t1, 1 # Point to next word
# (do something useful here)
j loop

添加方法,保留 $t0

add method, preserving $t0

# $t0 points to base address, $t1 byte counter initialized to zero
loop:
add $t2, $t0, $t1 # Get pointer to the word we want
lw $s0, 0($t2) # Load the word into $s0
addi $t1, $t1, 4 # Point to next word
# (do something useful here)
j loop

添加方法,删除 $t0

add method, trashing $t0

# $t0 points to base address
loop:
lw $s0, 0($t0) # Load the word into $s0
addi $t0, $t0, 4 # Point to next word
# (do something useful here)
j loop

我们只是在课后讨论,这些构建循环的方法中哪种更有效.我一直在做 sll 方法,因为我喜欢玩位......但它看起来效率最低(通过一条指令).

We were just discussing after class, which of these methods of building a loop is more efficient. I'd been doing the sll method, because I like playing with bits... but it looks the least efficient of them all (by one instruction).

我想这取决于您需要保留哪些变量.

I guess it's going to depend on what variables you need to preserve.

  • sll 方法:$t0 告诉你从哪里开始,$t1 告诉你哪个词你在,$t2 是划痕

  • sll method: $t0 tells you where you started, $t1 tells you which word you're on, $t2 is scratch

add 方法 1:$t0 告诉你从哪里开始,$t1 告诉你从哪里开始byte 你在,$t2 是草稿

add method 1: $t0 tells you where you started, $t1 tells you which byte you're on, $t2 is scratch

添加方法 2:不知道你在哪里,但它释放了 $t1 和 $t2

add method 2: No idea where you are, but it frees up $t1 and $t2

像往常一样,哪个更好"取决于您的程序需求.

Which one is "better" will, as usual, depend on your program's needs.

这篇关于MIPS汇编语言遍历数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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