使用MIPS程序集进行整数数组索引 [英] Integer array indexing with MIPS assembly

查看:129
本文介绍了使用MIPS程序集进行整数数组索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将此C代码转换为MIPS.

I wanted to convert this C code into MIPS.

C代码:

f = A[B[h-g]]

我们假设h > gB[h-g] > 0. hgf是整数.

We assume that h > g and B[h-g] > 0. h, g, f are integers.

还假定已将f分配给寄存器$s0g$s1h$s2.

Also assume that f is assigned to register $s0, g to $s1, h to $s2.

A-> $s6B-> $s7

这是我的尝试:

sub $t0, $s2, $s1                   
mult $t0, $t0, 4                     
lw $t0, $t0($s7)           
mult $t0, $t0, 4           
sw $s0, $t0($s6)

推荐答案

除了最后一行,看起来不错,

It looks good, apart from the last line, which should most likely be:

lw $s0, $t0($s6)

请注意,您应该始终对代码进行注释,尤其是当其为asm时,例如

Note that you should always comment your code, particularly so when it's asm, e.g.

sub $t0, $s2, $s1         ; t0 = h - g          
mult $t0, $t0, 4          ; t0 = (h - g) * sizeof(int) = byte index into B
lw $t0, $t0($s7)          ; t0 = B[h - g]
mult $t0, $t0, 4          ; t0 = B[h - g] * sizeof(int) = byte index into A
lw $s0, $t0($s6)          ; s0 = A[B[h - g]]

还请注意,您应该始终测试您的代码-我建议您使用模拟器,例如

Note also that you should always test your code - I would recommend using a simulator such as SPIM for this.

这篇关于使用MIPS程序集进行整数数组索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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