assembly - 使用 mips32 程序集的数组中的最大元素
问题描述
我必须编写 MIPS 汇编代码来查找数组的最大元素,其中我们知道:数组的索引在寄存器 $9 中,数组的长度在 $11 中,数组的起始地址是 500,最大元素必须在寄存器 $10 中设置。所以我的想法是将最大元素初始化为array[0],然后通过for循环并比较array[i]>max_elem。这是我的汇编代码:
add $9,$0,$0 #set index to 0
beg $11,$0,EXIT #if array is empty go to label EXIT
muli $13,$9,4 #4*i(relative address)
lw $14,500($13) #in register $14 now is array[0]
add $10,$14,$0 #initialize max_elem to array[0]
addi $9,$9,1 #now we go through loop starting from index 1
Loop:
muli $13,$9,4 #in register 13 now is 4*i where i>=1
lw $14,500($13) #now in $14 we have array[1]
slt $8,$10,$14 #in register 8 we have 1 if array[1]>max, otherwise 0
beq $8,$0,L #if array[1]<max then go to label L
add $10,$14,$0 #otherwise set max to that element
L:
addi $9,$9,1 #set index i to i+1
beq $9,$11,EXIT #if index is equal to length of array we traversed through array
j Loop #otherwise jump to loop again
Exit:
如果有人可以帮我检查一下,我将不胜感激:)。
解决方案
推荐阅读
- android - 通过Picasso lib by image url在android新设备中的图像加载问题
- azure - Azure 自动化中的多个 RunAs 帐户
- oracle - 我需要每个“组”的 ID
- javascript - 为什么 Safari Content Blocker 在我的网站上单击时会阻止此重定向?
- spring-cloud-config - 如何以编程方式更新 Spring Cloud Config 客户端上的属性
- android - Android BluetoothAdapter.LeScanCallback onLeScan 方法在 BluetoothLeScanner 工作时未调用
- android - Android:在通知栏关闭时,视图在状态栏下调整大小。如何禁用调整大小?
- vue.js - 如何让 vue DevTools 自动提交 vuex 突变?
- javascript - AWS 扩大认知用户
- c++ - 二进制加法从左到右携带