A Housing scheme draw the numbers 10,13,14,16,21 for their five lucky customers out of 50 numbers who wins the plots of 400 yards in main downtown of Karachi during the event held in Regent Crown Plaza. Write a program to generate the output of mentioned numbers through while loop using MIPS.
#MIPS assembly
.data
DisplayBefor: .asciiz "A Housing scheme draw the following numbers:\n"
DisplayAfter: .asciiz "\n\nHere are five out of 50 happy customers:\n"
Space_: .asciiz " "
range: .byte 50
size: .byte 5
.align 4
number: .word 10,13,14,16,21
array: .space 20
# code segment
.text
.globl main
main:
# Take rand integer elements to array
la $s0, array # array[0] address
la $s1, size
lb $s1, ($s1) # array size (counter)
loop:
beq $s1, $0, done # IF(counter==0) goto done
jal GenerateRand # ELSE function GenerateRand
sw $v0, 0($s0) # store rand integer to array
addi $s0, $s0, 4 # next address in array
addi $s1, $s1,-1 # counter--
j loop
done:
li $v0, 4 # Print string
la $a0, DisplayBefor # "The Housing Scheme is playing the following numbers...: "
syscall
la $a0, number # begining address in array
la $a1, size
lb $a1, ($a1) # array size
jal PrintArr # call PrintArr Function.
li $v0, 4 # Print string
la $a0, DisplayAfter # "Here are five happy customers: "
syscall
la $a0, array # begining address in array
la $a1, size
lb $a1, ($a1) # array size
jal PrintArr # call PrintArr Function.
final:
li $v0, 10 # finished ... stop … return
syscall # make the syscall
#***************************************
# Function Generate random number
#***************************************
GenerateRand:
Lb $a1, range # upper bound of range of returned values
Subi $a1, $a1, 1 # Subtract 1 to range
li $v0, 42 # Generate random number
# $a0 contains random
syscall # make the syscall
addi $a0, $a0, 1 # Add 1 (range 1 to [range-1])
add $v0, $0, $a0
jr $ra # jump to parent call
############################################
# function PrintArr
# input: $a0 array address
# $a1 array size
############################################
PrintArr:
addi $sp, $sp, -8 # make space on stack to store registers
sw $s1, 4($sp) # save $s1 on stack
sw $s0, 0($sp) # save $s0 on stack
li $t0,0 # counter array elements
add $s0,$0, $a0 # $s0 = begining array address
add $s1,$0, $a1 # size
Print:
Slt $t7, $t0, $s1 # IF(i < size) continue
Beq $t7, $zero, endPrint # ELSE goto end print
li $v0, 1 # print integer from $a0
lw $a0,0($s0) # load a[i] to $a0
syscall # make the syscall
li $v0,4 # print string
la $a0,Space_ # " "
syscall # make the syscall
addi $t0, $t0, 1 # counter++
addi $s0, $s0, 4 # next address in array
j Print
endPrint:
lw $s0, 0($sp) # restore $s0 from stack
lw $s1, 4($sp) # restore $s1 from stack
addi $sp, $sp, 8 # deallocate stack space
jr $ra
Comments
Leave a comment