Arrays 数组mips中的最大值
我试图找到数组中的最大值,并最终打印出该值Arrays 数组mips中的最大值,arrays,mips,Arrays,Mips,我试图找到数组中的最大值,并最终打印出该值 .globl start .data array: .word 8,2,31,81,12,10 size: .word 6 max: .word 0 .text main: jal start start: lw $t3, size #size la $t1, array # get array address lw $s5, ($t1) # set max, $s5 to array[0] add $t1, $t1,
.globl start
.data
array: .word 8,2,31,81,12,10
size: .word 6
max: .word 0
.text
main:
jal start
start:
lw $t3, size #size
la $t1, array # get array address
lw $s5, ($t1) # set max, $s5 to array[0]
add $t1, $t1, 4 # skip array[0]
add $t3, $t3, -1 # len = len - 1
loop:
lw $t4, ($t1) # get n of array[n]
ble $t4,$s5,L1 #if t4 is not less than t5 we got a new max otherwise not.
lw $s5, 0($t4) #get element in array <-- Pretty sure im doing something wrong here.
#sw $s5,0($t1) #max
L1:
add $t3, $t3, -1 #counter-1
addi $t1, $t1, 4 # advance array pointer
bnez $t3, loop #if not 0 then go on and loop
sw $s5, max #printing the max val of the array
lw $a0, max
li $v0, 1
syscall
li $v0, 10
.globl启动
.数据
数组:。字8,2,31,81,12,10
大小:.word 6
最大值:。字0
.文本
主要内容:
日航发车
开始:
lw$t3,尺码#尺码
la$t1,数组#获取数组地址
lw$s5,($t1)#将max$s5设置为数组[0]
添加$t1、$t1、4#跳过数组[0]
加上$t3、$t3、-1#len=len-1
循环:
lw$t4,($t1)#获取数组的n[n]
ble$t4,$s5,L1#如果t4不小于t5,我们得到了一个新的最大值,否则不会。
lw$s5,0($t4)#在数组中获取元素而不是lw$s5,0($t4)
你应该有mov$s5,$t4
。问题是s5获取t4获取的每个值,它在最后给出的最大值为10。lw
位于ble
的延迟槽中,因此即使执行了分支,它也会被执行。在ble
之后添加nop
以将lw
移出延迟槽。哇,它成功了,非常感谢markgz!我将阅读更多关于nop和延迟槽的信息。我不知道那件事。