Can't figure out how to retrieve values from heap in MIPS (MARS) - heap-memory

I am new to using dynamic memory in mips. I have to create a program that takes 2 integers from the user, find the positions where the binary forms of those numbers have a 1, print the positions for each number/set (For example: 1000 1001 1100 0000 0000 0010 1000 1110 corresponds to the set: {2, 3, 4, 8, 10, 23, 24, 25, 28, 32}). Then take another integer from the user and see if it is one of the positions in either set. Eventually I will have to find the union and intersection of the two sets but I am not there yet.
So I printed out both sets normally but I also saved them to heap because I figured that would be the only way to be access them later to see if the next number is a member of either set. My problem is that, for the life of me, I can't figure out how to retrieve the sets from heap to use them again.
My code is below. I know it is kind of long so for reference, I store them in dynamic memory in the SetOne and SetTwo functions, and I am trying to retrieve them in the ElementSetOne and ElementSetTwo functions. Sorry if I'm not being descriptive enough, I'm still very new to StackOverflow. Thanks.
.data #Data section
Prompt1: .asciiz "\nEnter first number: "
Prompt2: .asciiz "\nEnter second number: "
Result1: .asciiz "\nMembers of Set 1: "
Result2: .asciiz "\nMembers of Set 2: "
Prompt3: .asciiz "\nEnter an element to find: "
Result3: .asciiz "\nIt is a member of set 1"
Result4: .asciiz "\nIt is not a member of set 1"
Result5: .asciiz "\nIt is a member of set 2"
Result6: .asciiz "\nIt is not a member of set 2"
Result7: .asciiz "\nUnion of the sets: "
Result8: .asciiz "\nIntersection of the sets: "
Prompt4: .asciiz "\nDo you want to compute set functions again? "
.globl main
.text #Code section
main:
li $v0, 4 #system call code for Print String
la $a0, Prompt1 #load address of Prompt1 into $a0
syscall #print the Prompt1 message
li $v0, 5 #system call code for Read Integer
syscall #reads the value of 1st integer into $v0
move $t0, $v0 #move first integer to $t0
li $v0, 4 #system call code for Print String
la $a0, Prompt2 #load address of Prompt2 into $a0
syscall #print the Prompt2 message
li $v0, 5 #system call code for Read Integer
syscall #reads the value of second integer into $v0
move $t1, $v0 #move second integer to $t1
First:
li $v0, 4 #System call code for Print String
la $a0, Result1 #load address of Print into $a0
syscall #print the string
li $v0, 9 #syscall for dynamic memory allocation (sbrk)
li $a0, 80
syscall
move $s0, $v0
li $t4, 0 #sets count to 0
FirstMask:
beq $t0, 0, Second #if first set is done, go to second
andi $t6, $t0, 1 #first number and 1
addi $t4, $t4, 1 #increase count by 1
beq $t6, 1, SetOne #if and with 1, go to SetOne
srl $t0, $t0, 1 #shift number right 1 position
j FirstMask #restart loop
SetOne: #STORE SET 1
sw $t4, 0($s0) #store position in memory
add $s0, $s0, 4 #increment memory
li $v0, 1 #system call code for Print Integer
move $a0, $t4 #move value to be printed to $a0
syscall #print the the count positiom
li $v0, 11 #syscall number for printing character
li $a0, 32 #move ascii code for space in $a0
syscall #print blank space
srl $t0, $t0, 1 #shift input number right 1 position
j FirstMask
Second:
li $v0, 4 #System call code for Print String
la $a0, Result2 #load address of Print into $a0
syscall #print the string
li $v0, 9 #syscall for dynamic memory allocation (sbrk)
li $a0, 80
syscall
move $s1, $v0
li $t4, 0 #reset count to 0
SecondMask:
beq $t1, 0, Element #if number is done go to next step
andi $t6, $t1, 1 #and input with 1
addi $t4, $t4, 1 #increase count by 1
beq $t6, 1, SetTwo #if and with 1, save position number
srl $t1, $t1, 1 #shift right 1 position
j SecondMask
SetTwo: #STORE SET 2
sw $t4, 0($s1) #store position in memory
add $s1, $s1, 4 #increment memory
li $v0, 1 #system call code for Print Integer
move $a0, $t4 #move value to be printed to $a0
syscall #print the count
li $v0, 11 #syscall number for printing character
li $a0, 32 #move ascii code for space into $a0
syscall #print blank space
srl $t1, $t1, 1 #shift right 1 position
j SecondMask
Element:
li $v0, 4 #System call code for Print String
la $a0, Prompt3 #load address of Print into $a0
syscall #print the string
li $v0, 5 #system call code for Read Integer
syscall #reads the value of 1st integer into $v0
move $t2, $v0 #move first integer to $t2
ElementSetOne: #RETRIEVE SET 1
lw $t3, 0($s0) #load first number from memory into $t3
beq $t2, $t3, InSetOne #if element is in memory, go to InSetOne
bgt $s0, 40, NotSetOne #if element isnt in entire array, go to NotSetOne
addi $s0, $s0, 4 #increment memory by 4
j ElementSetOne
InSetOne:
li $v0, 4 #System call code for Print String
la $a0, Result4 #load address of Print into $a0
syscall #print the string
j ElementSetTwo
NotSetOne:
li $v0, 4 #System call code for Print String
la $a0, Result5 #load address of Print into $a0
syscall #print the string
j ElementSetTwo
ElementSetTwo: #RETREIVE SET 2
lw $t3, 0($s1) #load first number from memory into $t3
beq $t2, $t3, InSetTwo #if element is in memory, go to InSetOne
bgt $s1, 40, NotSetTwo #if element isnt in entire array, go to NotSetOne
addi $s1, $s1, 4 #increment memory by 4
j ElementSetTwo
InSetTwo:
li $v0, 4 #System call code for Print String
la $a0, Result5 #load address of Print into $a0
syscall #print the string
j Exit
NotSetTwo:
li $v0, 4 #System call code for Print String
la $a0, Result6 #load address of Print into $a0
syscall #print the string
j Exit
Exit:
li $v0, 10 #terminate program
syscall #return control to the system
Update: I am taking programming in C next semester so I don't know very much about the language. Essentially, my problem is that I stored a set of numbers into heap. And I can't figure out how to retrieve them now.
I know they were successfully stored because when I look in the heap section while running the program in MARS, I can see them being stored one by one. I'm just trying to retrieve all the numbers for each set now, but what I have just returns 0. So I'm thinking I must be using the wrong address or I'm missing a step somewhere.
I think this is all the code that is relevant:
li $v0, 9 #syscall for dynamic memory allocation (sbrk)
li $a0, 80
syscall
move $s0, $v0
#Theres a loop here to find the values of $t4
SetOne: #STORE SET 1
sw $t4, 0($s0) #store values in $s0
add $s0, $s0, 4 #increment memory
li $v0, 9 #syscall for dynamic memory allocation (sbrk)
li $a0, 80
syscall
move $s1, $v0
#Theres another loop here to find different values of $t4
SetTwo: #STORE SET 2
sw $t4, 0($s1) #store position in memory
add $s1, $s1, 4 #increment memory
#Then later on I want to retrieve these values so i can use them in other loops
ElementSetOne:
lw $t3, 0($s0) #load first number from memory into $t3
beq $t2, $t3, InSetOne #if element is in memory, go to InSetOne
bgt $s0, 40, NotSetOne #if element isnt in entire array, go to NotSetOne
addi $s0, $s0, 4 #increment memory by 4
j ElementSetOne. #loop
ElementSetTwo: #RETREIVE SET 2
lw $t3, 0($s1) #load first number from memory into $t3
beq $t2, $t3, InSetTwo #if element is in memory, go to InSetOne
bgt $s1, 40, NotSetTwo #if element isnt in entire array, go to NotSetOne
addi $s1, $s1, 4 #increment memory by 4
j ElementSetTwo #loop

Related

MIPS Problem in printing the array and get the third element multiply to 4

I'm new to MIPS and I'm stuck in the printing array and the function also kinda mess up
This is my code, it works for the get input I guess but after that, they display the array and have error.
This is the question:
Prompts the user to input an integer number between 1 and 10 using the console
Write a function to initialize an array of 5 elements with the value entered by the user. This function must include the following
A loop
A branch
Write a function to multiply the third element of the array initialized above by the number 4.
Write a function to print all the elements of the array to the console. The third element must be 4 times the other elements. This function must include the following
A loop
A branch
Your code must have three separate functions (in addition to the main section) and function calls
Function to initialize the array
Function to multiply the third element of the array by the user input number
Function to print the arrays to screen.
.data
array: .space 20
prompt: .asciiz "Input 5 integers\n"
.align 3
.text
main:
li $v0, 4
la $a0, prompt
syscall
loop:
beq $t0, 20, exit
li $v0, 5
syscall
sw $v0, array($t0)
add $t0, $t0, 4 #increase index
j loop
exit:
jr $ra
print_loop:
la $t1, array # get array address
li $t2, 0 # set loop counter
beq $t2, 5, exit_print_loop
lw $a0, ($t1) # print value at the array pointer
li $v0, 1
syscall
addi $t2, $t2, 1 # advance loop counter
addi $t1, $t1, 4 # advance array pointer
j print_loop # repeat the loop
exit_print_loop:
li $v0, 10
This is my code that I made and it works perfectly for my test. Thank you!
.data
array: .space 20 #create an array for 5 integer
prompt1: .asciiz "Input 5 integers from 1 to 10 \n"
prompt2: .asciiz "\nThe array is: \n"
prompt3: .asciiz "Third element times 4 is: "
newLine: .asciiz "\n"
.align 3
.text
main:
#prompt the user input
li $v0, 4
la $a0, prompt1
syscall
jal loop
#Calculate and print out the result
li $v0, 4
la $a0, prompt3
syscall
jal cal
#Print out the array
li $v0, 4
la $a0, prompt2
syscall
addi $t1, $zero, 0 #clear t1
jal display
li $v0, 10
syscall
loop:
beq $t0, 20, exit
li $v0, 5
syscall
sw $v0, array($t0)
add $t0, $t0, 4 #increase index
j loop
exit:
jr $ra
cal:
la $s5, array
lw $t5, 8($s5)
mul $t3, $t5, 4
li $v0, 1
move $a0, $t3
syscall
jr $ra
display:
beq $t1, 20, exit_display
lw $t6, array($t1)
addi $t1, $t1, 4
#print current number
li $v0, 1
move $a0, $t6
syscall
#print the new line
li $v0, 4
la $a0, newLine
syscall
j display
exit_display:
li $v0, 10
syscall

Store a user input string of integers into an integer array mips assembly

just like the title states, I am trying to write a program in MIPS assembly that will take a string input by a user of 4 integers between 0-9 separated by spaces and store each of those numbers as an integer into an array. Here's what I have so far:
.data
input1: asciiz "Input Row 1: "
row1: .asciiz "Row 1: "
array: .space 16
list: .space 8
.text
la $s0, array #load address of array to s0
li $s1, 0 #index of array
li $v0, 4 #call for print string
la $a0, input1 #load string to be printed
syscall
li $v0, 8 #call for user input
la $a0, list #address of where the input will be stored
li $a1, 8 #space allowed for input
syscall
loop:
la $t0, list #load address of the string
add $t2, $zero, $zero #index of the string
load:
sll $t3, $t2, 1 #multiply string index by 2 to skip spaces
sll $t4, $s1, 2 #multiply index of array by 4 for size of word
addu $t0, $t0, $t3 #position string
addu $s0, $s0, $t4 #position array
lbu $t1, 0($t0) #load byte of the string to t1
addiu $t1, $t1, -48 #convert char to integer
sb $t1, 0($s0) #store byte into the array
addi $t2, $t2, 1 #increment index of string by 1
addi $s1, $s1, 1 #increment index of array by 1
blt $t2, 4, load #if the index of the string is less than 4, load the next character
li $v0, 11 #printing first input as integers from here
addi $a0, $zero, 0xA #new line ascii character
syscall
li $v0, 4 #call for print string
la $a0, row1 #load string to be printed
syscall
li $v0, 1 #print integer call
lb $a0, -24($s0) #load first number input
syscall
li $a0, 32
li $v0, 11 # syscall number for printing character
syscall
li $v0, 1
lb $a0, -20($s0) #load second number input
syscall
li $a0, 32
li $v0, 11 # syscall number for printing character
syscall
li $v0, 1
lb $a0, -16($s0) #load third number input
syscall
li $a0, 32
li $v0, 11 # syscall number for printing character
syscall
li $v0, 1
lb $a0, -12($s0) # load fourth number input
syscall
exit:
li $v0, 10
syscall
The two problems I am having are that I expected the offset of the array to be at -12 (4 words x 4 bytes - 4 for 0 start point), but it starts at -24, and for the 3rd input, no matter what it is, the int 0 gets stored. I am running this program in MARS 4.5. Sample output below:
Input Row 1: 1 2 3 4
Row 1: 1 2 0 4
-- program is finished running --
Eventually I am going to have the user input 4 strings to create a 4x4 matrix and store all of those strings in the same array.
Jester pointed out to me in the comments what I was doing incorrectly. Instead of incrementing the array address by 4 every time, I was adding 4 more than the last increment. So I was adding 0 the first time, then 4, then 8 to that, putting me at 12, and then 12 more, which is why I ended up at an offset of 24 instead of the expected 12. I moved the declaration of la $s0, array and la $t0, list into the loop, so that it continues to set it back to the base address.
The new loop looks like the following:
loop:
add $t2, $zero, $zero #index of the string
load:
la $s0, array #load address of array to s0
la $t0, list #load adress of the string
sll $t3, $t2, 1 #multiply string index by 2 to skip spaces
sll $t4, $s1, 2 #multiply index of array by 4 for size of word
addu $t0, $t0, $t3 #position string
addu $s0, $s0, $t4 #position array
lbu $t1, 0($t0) #load byte of the string to t1
addiu $t1, $t1, -48 #convert char to integer
sb $t1, 0($s0) #store byte into the array
addi $t2, $t2, 1 #increment index of string by 1
addi $s1, $s1, 1 #increment index of array by 1
blt $t2, 4, load #if the index of the string is less than 4, load the next character
li $v0, 11 #printing first input as integers from here
addi $a0, $zero, 0xA
syscall
li $v0, 4 #call for print string
la $a0, row1 #load string to be printed
syscall
li $v0, 1
lb $a0, -12($s0)
syscall
li $a0, 32
li $v0, 11 # syscall number for printing character
syscall
li $v0, 1
lb $a0, -8($s0)
syscall
li $a0, 32
li $v0, 11 # syscall number for printing character
syscall
li $v0, 1
li $a0, 32
li $v0, 11 # syscall number for printing character
syscall
li $v0, 1
lb $a0, 0($s0)
syscall
exit:
li $v0, 10
syscall
Credit to Jester for steering me in the right direction.

MIPS instruction `load word` not loading word into the register

I am writing a program that given an array, it prints out array value and swap two elements of choice by users. This is the code:
# Declare main as a global function
.globl main
# Declare the data
.data
list:
.align 4 # Forces a word to line up at boundary/4
.word 5, 17, 43, 22, 120, 3 # A six-element array
endList:
space:.asciiz " "
newLine: .asciiz "\n"
msg1:.asciiz "Array is: "
msg2:.asciiz "Enter fisrt index to swap: "
msg3:.asciiz "Enter second index to swap: "
msg4:.asciiz "Array after swapping is: "
#.text assembler directive to place the code
.text
# Start the program at main
main:
la $s1, list # Load beginning address of array
into register $s1
la $s2, endList # Load end address of array into register $s2
la $a0, msg1 # Load address of message string into $a0
li $v0, 4 # Load print string system call code into $v0
syscall # System call to print
jal printLoop
jal swap
# Exit
li $v0, 10
syscall
printLoop:
beq $s1, $s2, printDone # Check when array end, go to done
lw $a0, ($s1) # Load word at address of $s1 into $a0
li $v0, 1 # Load print integer system call code
into $v0
syscall # System call to print number
la $a0, space # Load address of space stringinto $a0
li $v0, 4 # Load print string system call code into $v0
syscall # System call to print a space
addi $s1, $s1, 4 # Advance array pointer to the next element
j printLoop # Loop back to next element
printDone:
j $ra
swap:
la $a0, newLine # Print a new line
li $v0, 4
syscall
la $a0, msg2 # Print a message to ask for first index to swap
li $v0, 4
syscall
li $v0, 5 # Get user input, save into $s3
syscall
addi $s3, $v0, 0
la $a0, msg3 # Print a message to ask for second index to swap
li $v0, 4
syscall
li $v0, 5 # Get user input, save into $s4
syscall
addi $s4, $v0, 0
sll $t0, $s3, 2 # Multiply first index by 4 to get its offset
add $t0, $s1, $t0 # Get the address of the first index
sll $t1, $s4, 2 # Multiply second index by 4 to get its offset
add $t1, $s1, $t1 # Get the address of the second index
lw $t2, ($t0) # Load first number at address contained in $t0 into $t2
lw $t3, ($t1) # Load second number at address contained in $t1 into $t3
sw $t2, ($t1) # Store first number into address of second number
sw $t3, ($t0) # Store second number into address of first number
move $a0, $t0
li $v0, 4
syscall
move $a0, $t1
li $v0, 4
syscall
j $ra
When I step through the code using QtSpim, these lines seem to be giving problems:
lw $t2, ($t0)
lw $t3, ($t1)
The values in $t2 and $t3 should be integer values given by the array. For example, if user specifies index 1 and 2, values in $t2 and $t3 should be 5 and 17. Instead it showed weird numbers: 1634890305 and 1936269433 (decimal) in my version.
What is going wrong? I would appreciate any help!
Printloop changes $s1, so when swap uses it, it points at the list end.

User ask how many input you want to take and then show their sum in mips

I want to make a program in mips, in which the user will enter the number of inputs he wants to take. Finally the program will print the sum of the inputs.
Here is my code:
.data
myMessage: .asciiz "ENTER numbers you want to sum\n"
value: .asciiz "ENTER Value \n"
sum : .word 0
.text
li $v0, 4
la $a0, myMessage
syscall
li $v0, 5
syscall
move $t0, $v0 #num of time user will enter num
la $t1, 0 #count value first initiallize to 0
see:
bne $t1,$t0,add #checking if count is less than the num of value
li $v0, 1 #printing sum finally
la $a0, ($s2)
add:
li $v0,4
la $a0,value
syscall
li $v0,5
syscall
move $t3,$v0
la $a1, sum #load address of 'bal' in '$a1'
lw $s3, 0($a1) #load sum from '$a1' to '$s2' (initially 0)
add $s3, $s3, $t3 #adding the sum
sw $s2, 0($a1) #load latest sum ('$s2') in .word balance ('$a1')
addi $t1,$t1,1 inc in count
j see
The problem is that the program does not stop after the wished number of inputs and continues to ask for new input.
.data # Data declaration section
instring1: .asciiz "ENTER numbers you want to sum\n"
instring2: .asciiz "Enter value\n"
outstring: .asciiz "Sum: "
.text#
main:
#Print instring1
li $v0, 4 # system call code for printing string = 4
la $a0, instring1 # load address of string to be printed into $a0
syscall # call operating system to perform operation in $v0
# syscall takes its arguments from $a0, $a1,
#Taking n as input
li $v0, 5 # read int
syscall
move $s0, $v0 # the result of the syscall is stored in v0
# we move it to t0 to prevent overwriting
#Creation heap memory
mul $t1, $s0, 4
li $v0, 9
move $a0, $t1
syscall
move $s1, $v0
#Print instring2
li $v0, 4 # system call code for printing string = 4
la $a0, instring2 # load address of string to be printed into $a0
syscall # call operating system to perform operation in $v0
# syscall takes its arguments from $a0, $a1,
li $s2, 0 # $s2 is the index, and loop induction variable
Start_Input:
bge $s2, $s0, End_Input
li $v0, 5 # Read integer values
syscall
mul $t0, $s2, 4 # $t0 is the offset
add $t1, $s1, $t0 # $t1 is the address of desired index
sw $v0, ($t1) # store the value in the array
addi $s2, $s2, 1 # increment the index
j Start_Input
End_Input:
add $t0, $zero, $zero # i is initialized to 0, $t0 = 0
add $s2, $zero, $zero # sum = 0
beq $s0, $zero, print_sum # if number = 0 then goto print_sum
Loop: #stuff
mul $t1, $t0, 4
add $t2, $s1, $t1
lw $t3, ($t2)
add $s2, $s2, $t3
addi $t0, $t0, 1 # i ++
slt $t1, $t0, $s0 # $t1 = 1 if i < n
bne $t1, $zero, Loop # go to Loop if i < n
#Print Sum
print_sum:
li $v0, 4 # system call code for printing string = 4
la $a0, outstring # load address of string to be printed into $a0
syscall # call operating system to perform operation in $v0
# syscall takes its arguments from $a0, $a1,
li $v0 1 # print int
move $a0 $s2
syscall
#Exit Syscall
EXIT:
li $v0 10# exit
syscall
This is working code. Comment provided to understand code.
.data
myMessage:.asciiz "ENTER numbers you want to sum\n"
value:.asciiz "ENTER Value: "
show: .asciiz "\nSum is: "
.text
li $v0,4
la $a0,myMessage
syscall
li $v0,5
syscall
move $t0,$v0 #num of time user will enter num
la $t1, 0 #count value first initiallize to 0
la $t5, 0
see:
bne $t1,$t0,add #checking if count is less than the num of value
li $v0, 4
la $a0, show
syscall #print message "Sum is: "
li $v0, 1
move $a0, $t5
syscall #print the result
j end
add:
li $v0,4
la $a0,value
syscall
li $v0, 5
syscall
add $t5, $t5, $v0
sub $t0, $t0, 1
j see
end:

MIPS user input array storing and printing trouble

I am trying to write a program in MIPS that prompts the user to guess a random number that is generated from a random number generator. They have 10 tries to guess correctly or they lose. Once the game is over it will tell the user whether they have won or lost and will print out an array of the numbers they guessed. I am having trouble with storing the user's input and displaying back once they finish the game.
You could use dynamic memory to store the guesses by incrementing the offset of your pointer with each guess:
li $v0, 9 #allocate memory for new record
li $a0, 60 #enough memory for 15 guesses
syscall
move $s0, $v0 #hang onto the initial address of our array (memory)
li $t6, 0 #Current Offset of our array = 0
Then, in your loop:
li $v0, 5 #enter integer
syscall
add $s1, $s0, $t6 #add our offset to our initial memory location
sw $v0, 0($s1) #Store our guess in the offset location
add $t6, $t6, 4 #add 4 to our offset
To display them, you can do it like this:
showlist: #Show us what we guessed
li $t1, 0 #Set our initial offset to 0
listloop:
add $s1, $s0, $t1 #Add our offset to our initial memory location
li $v0, 1
lw $a0, 0($s1) #Print the number at our memory address + offset
syscall
add $t1, $t1, 4 #Add 4 to our offset
bge $t1, $t6, done #If our offset is >= our final guess offset, we're done
li $v0, 4 #Printa coma and space
la $a0, space
syscall
j listloop
See the complete solution below:
#t0 = Number to guess
.data
nl: .asciiz "\n"
space: .asciiz ", "
prompt1: "\nPlease enter a number: "
toohigh: "\nYou guessed too high"
toolow: "\nYou guessed too low"
winner: "\nThat's correct! You Win!"
guesses: "\nHere are your guesses: "
youlose: "\nYou Lose!"
youlose2: "\nThe Correct Number Was: "
.text
################Generate our random number###################
li $v0, 42 #random number generator
li $a1, 21 #upper bound, gen number from 0-20
syscall # runs whatever is in $v0 (command 42-RNG)
move $t0, $a0 #move stuff from $a0 to $t0
############################################################
li $v0, 9 #allocate memory for new record
li $a0, 60 #enough memory for 15 guesses
syscall
move $s0, $v0 #hang onto the initial address of our array (memory)
li $t6, 0 #Current Offset of our array = 0
##################
# Our Guess Loop #
##################
loop:
li $v0, 4 #prompt for Number:
la $a0, prompt1
syscall
li $v0, 5 #enter integer
syscall
add $s1, $s0, $t6 #add our offset to our initial memory location
sw $v0, 0($s1) #Store our guess in the offset location
add $t6, $t6, 4 #add 4 to our offset
beq $v0, $t0, win #Did we win?
bge $t6, 60, lose #Did we lose? (60 = 4 * 15) We can use that to tell how many guesses we tried
blt $v0, $t0, less #Is our number less than the right number?
li $v0, 4 #Must have guessed too high - let us know
la $a0, toohigh
syscall
j loop
less:
li $v0, 4 #Must have guessed too low - let us know
la $a0, toolow
syscall
j loop
###################
win:
li $v0, 4 #We won!
la $a0, winner
syscall
showlist: #Show us what we guessed
li $v0, 4
la $a0, guesses
syscall
li $t1, 0 #Set our initial offset to 0
listloop:
add $s1, $s0, $t1 #Add our offset to our initial memory location
li $v0, 1
lw $a0, 0($s1) #Print the number at our memory address + offset
syscall
add $t1, $t1, 4 #Add 4 to our offset
bge $t1, $t6, done #If our offset is >= our final guess offset, we're done
li $v0, 4 #Printa coma and space
la $a0, space
syscall
j listloop
lose: #We lost
li $v0, 4
la $a0, youlose #Let us know
syscall
li $v0, 4
la $a0, youlose2 #Display "The correct number was: "
syscall
move $a0, $t0 #Move the right number into $a0 to display it
li $v0, 1
syscall
j showlist #Then show us our guesses
done:
li $v0, 10 #Terminate Program
syscall
Win:
Lose:

Resources