MIPS-阵列中的阵列索引 [英] MIPS - Array in array index

查看:130
本文介绍了MIPS-阵列中的阵列索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MIPS中的以下C代码是什么?

What is the following C code in MIPS?

f = A[B[i]]

有人告诉我它可以用6行完成,但还不知道怎么做. f 位于 $ t0 中, i 位于 $ t3 中, A [] $ s0 中,而 B [] $ s1 中.所有类型都是整数.

I'm told it can be done in 6 lines but can't quite figure out how. f is in $t0, i is in $t3, A[] is in $s0, and B[] is in $s1. All types are integer.

我能想到的最好的是

lw $t5, $t3($s0);  # Doesn't work because lw syntax doesn't accept a register as an offset
lw $t6, $t5($s1);
sadd $t0, $t6, $zero

显然这是错误的.我将如何获得每行的正确偏移量?

Obviously this is wrong. How would i go about getting the correct offset for each line?

谢谢.

推荐答案

可能有更有效的方法,但这是6行中的一种方法:

There might be more efficient ways, but here's one way in 6 lines:

sll $t2,$t3,2    # t2 = i * sizeof(int)
addu $t2,$t2,$s1 # t2 = &B[i]
lw $t0,0($t2)    # t0 = B[i]
sll $t0,$t0,2    # t0 *= sizeof(int)
addu $s0,$s0,$t0 # s0 = &A[B[i]]
lw $t0,0($s0)    # t0 = A[B[i]]

阅读 MIPS指令集参考以获得有关各个说明的详细信息.

Read a MIPS instruction set reference to get more information about what individual instructions do.

这篇关于MIPS-阵列中的阵列索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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