+1 678 648 4277 

Creating an Array Guessing Game using Syscalls 

Implementing an array guessing game in MIPS assembly assignment help

.data array: .space 12 # space for 3 words input: .space 12 prompt: .asciiz "Input numbers: " out_msg: .asciiz "Out\n" good_msg: .asciiz "Good Job!\n" ball_msg: .asciiz " ball" strike_msg: .asciiz " strike" .text main: # generate random array la $a0, array # save in array li $a1, 3 # fill 3 numbers jal generate_random game_loop: # print prompt li $v0, 4 # syscall number to print string la $a0, prompt # string address syscall la $s0, input # point to array of input values li $s1, 3 # read 3 values read_loop: # read number li $v0, 12 # syscall to read a character syscall addi $v0, $v0, -48 # convert ascii to integer sw $v0, 0($s0) # save read value in input array addi $s0, $s0, 4 # advance to next space in array # read space li $v0, 12 # syscall to read a character syscall addi $s1, $s1, -1 # decrement number of values to read bne $s1, $zero, read_loop # read while number is not zero # look for input numbers in array la $s0, input # point to array of input values li $s1, 3 # loop through the 3 values li $s2, 0 # current index li $s3, 0 # initialize with 0 balls li $s4, 0 # initialize with 0 strikes compare_loop: lw $a2, 0($s0) # load value to search from input array addi $s0, $s0, 4 # advance to next space in array # search for value in array la $a0, array # load address of array to search value li $a1, 3 # array size jal search slt $t0, $v0, $zero # see if the result is less than zero bne $t0, $zero, compare_next # if the number was not in the array, go to next bne $s2, $v0, inc_balls # if the position is not the same, go to increment balls addi $s4, $s4, 1 # else, increment number of strikes j compare_next # go to next inc_balls: addi $s3, $s3, 1 # increment number of balls compare_next: addi $s2, $s2, 1 # increment index addi $s1, $s1, -1 # decrement number of values to compare bne $s1, $zero, compare_loop # compare while number is not zero add $t0, $s3, $s4 # add balls and strikes beq $t0, $zero, out # if no balls and no strikes, out beq $s4, 3, good # if 3 strikes, good job beqz $s3, print_strikes # if no balls, print strikes addi $a0, $s3, 48 # convert number to ascii li $v0, 11 # syscall number to print character syscall li $v0, 4 # syscall number to print string la $a0, ball_msg # string address for ball message syscall beq $s3, 1, print_space # if only one ball, go to print space li $a0, 's' # else, add s to make it plural li $v0, 11 # syscall number to print character syscall print_space: li $a0, ' ' # space character li $v0, 11 # syscall number to print character syscall print_strikes: beqz $s4, print_nl # if no strikes, print line addi $a0, $s4, 48 # convert number to ascii li $v0, 11 # syscall number to print character syscall li $v0, 4 # syscall number to print string la $a0, strike_msg # string address for strike message syscall beq $s4, 1, print_nl # if only one strike, go to print newline li $a0, 's' # else, add s to make it plural li $v0, 11 # syscall number to print character syscall print_nl: li $a0, 10 # newline character li $v0, 11 # syscall number to print character syscall j game_loop # start game again out: # print out la $a0, out_msg # address of message li $v0, 4 # syscall number to print string syscall j game_loop # start game again good: # print good job la $a0, good_msg # address of message li $v0, 4 # syscall number to print string syscall # exit program li $v0, 10 # syscall number to exit program syscall # Function # generate_random (arr[], n). # Fills the given array with random values # a0 = arr[] # a1 = n generate_random: add $t2, $zero, $a0 # copy address to t2 add $t3, $zero, $a1 # copy number of elements to t3 gen_loop: # generate random number li $v0, 42 # syscall to generate random int range li $a0, 0 # pseudo random generator li $a1, 9 # generate number between 0 and 8 syscall addi $a0, $a0, 1 # add 1 to get range 1-9 sw $a0, 0($t2) # save in array addi $t2, $t2, 4 # advance to next space in array addi $t3, $t3, -1 # decrement number of elements to generate bne $t3, $zero, gen_loop # repeat while there are elements jr $ra # return to caller # Function # search (arr[], n, value). # Searches for a value in the array, returns the index if found, -1 if not found # a0 = arr[] # a1 = n # a2 = value to search # On return: $v0 = index or -1 search: li $v0, 0 # start at index 0 search_loop: lw $t0, 0($a0) # load value from array addi $a0, $a0, 4 # advance to next space in array beq $t0, $a2, search_return # if the value is the same as the one in the array, return the index addi $v0, $v0, 1 # increment index addi $a1, $a1, -1 # decrement number of elements to search bne $a1, $zero, search_loop # repeat while there are elements li $v0, -1 # if we searched all array, return -1 as it was not found search_return: jr $ra # return to caller