Demo entry 6647943

MIPS

   

Submitted by anonymous on Oct 22, 2017 at 09:44
Language: verilog. Code size: 915 Bytes.

.data
array:.space 40
num:.word 4
.text
set_array:
	lw $a0,num	#set num = a0
	la $s1,array	#load address to s1
	li $s0,0	#initialize i=s0=0
	li $s2,10	#initialize size=s2=10
for_1:	
	beq $s0,$s2,end_1	#if i==10, end the for_1
	move $a1,$s0
	jal compara		#jump compara, v0=compara(i,num)
	sw $v0,0($s1)		#save v0 to array[i]
	addi $s0,$s0,1		#i++
	addi $s1,$s1,4		#move s1 to array[i+1]
	j for_1
end_1:	li $v0,10	#exit
	syscall

compara:
	addi $sp,$sp,-4		#push to the stack
	sw $ra,0($sp)		#save ra to stack.top
	jal sub_function	#jump to sub, ra=sun(a,b)
	lw $ra,0($sp)		#load stack.top to ra
	addi $sp,$sp,4		#pop the stack
	bgez $v0,if 		#if sub(a,b)>=0, goto if
	li $v0,0		#return v=0
	j end_if		# else end, jump to the end of if
if:	li $v0,1		#if sub(a,b)>=0, return 1
end_if:	jr $ra			#jump to the for_1

sub_function:
	sub $v0,$a0,$s0		#v0=num-i
	jr $ra			#return to compara

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).