Write an ASSEMBLY program to generate 30 random integers bet
Write an ASSEMBLY program to generate 30 random integers between 0 an 990. Print the random numbers diagonally on a light-green screen.
Solution
# it will generate 30 random numbers between 0 and 990
.data
# Create a string to separate the numbers with a space
spacestr: .asciiz \" \"
.text
##############################################################################
# seed the random number generator
##############################################################################
# get the time
li $v0, 30 # get time in milliseconds (as a 64-bit value)
syscall
move $t0, $a0 # save the lower 32-bits of time
# seed the random generator (just once)
li $a0, 1 # random generator id (will be used later)
move $a1, $t0 # seed from time
li $v0, 40 # seed random number generator syscall
syscall
##############################################################################
# seeding done
##############################################################################
# generate 30 random integers in the range [0-990] from the
# seeded generator (whose id is 1)
li $t2, 29 # max number of iterations + 1
li $t3, 0 # current iteration number
# # # # Start Loop # # # # #
LOOP:
li $a0, 0 # as said, this id is the same as random generator id
li $a1, 990 # upper bound of the range range is [0-990]
li $v0, 42 # random int in range, service
syscall
# $a0 now holds the random number
# lets add one to make the range [0-990]
addi $a0, $a0, 1
# loop terminating condition
addi $t3, $t3, 1 # increment the number of iterations
beq $t3, $t2, EXIT # branch to EXIT if iterations is 10
# $a0 still has the random number
# print it
li $v0, 1 # print integer syscall
syscall
# print a space
la $a0, spacestr # load the address of the string (pseudo-op)
li $v0, 4 # print string syscall
syscall
# Do another iteration
j LOOP
##############################################################################
# Tell MARS to exit the program
##############################################################################
EXIT:
li $v0, 10 # exit syscall
syscall

