MARS 编写 MIPS指令示例 找最大值 汇编语言

.data                     
a: .space 24   
i: .word 4
.text 
main:
la $t0,a    #t0 = a


li $t1,1                
sw $t1,0($t0)	#indirect address

li $t1,2                
sw $t1,4($t0)	# shift 4 bytes from  t0 a 

li $t1,8                
sw $t1,8($t0)	# shift 4 bytes from  t0 a 

li $t1,4               
sw $t1,12($t0)	# shift 4 bytes from  t0 a    
  
li $t1,16              
sw $t1,16($t0)  
    
li $t1,10                
sw $t1,20($t0)     

move $t2,$t0    #t2 = t0 = a

lw $t3,0($t0)   # t3 = max = a[0]

li $t6,0 	#t6 = i = 0

#supprogram of loop (fetch the element of array)
loop:

add $t2,$t2,4	#t0 = a t2 = &a[i] 
lw $t7,($t2)	#t7 = t2 = a[i]
addi $t6,$t6,1	#i = i+1
 
# teel if have reached the bound
#brance equal
beq $t6,5,end   #if i == 5
 
#branch greater than
bgt $t7,$t3,change #t7 = a[i] if(a[i]>max)

#continue the loop
j loop                

#change the maximum
change:
move $t3,$t7	#t3(max) stores bigger one
j loop                  

#store the result and retrun
end:                      
li $v0 1                   
move $a0 $t3        
syscall

 

你可能感兴趣的:(MARS 编写 MIPS指令示例 找最大值 汇编语言)