.data
.align 2 # Align the next data item on a word boundary
intArr: .space 28 # Array to hold integers (7 * 4 bytes)
vtdPrompt: .asciiz "vals to do? "
entIntPrompt: .asciiz "enter an int: "
adjMsg: .asciiz " is bad, make it "
origLab: .asciiz "original:\n"
backLab: .asciiz "backward:\n"
dmPrompt: .asciiz "do more? "
invalid_input_msg: .asciiz "Invalid input. Please enter a valid integer.\n"
.text
.globl main
main:
li $v0, 4 # syscall code for printing string
la $a0, vtdPrompt # load the address of the prompt
syscall
li $v0, 4 # syscall code for printing string
la $a0, origLab # load the address of the original label prompt
syscall
main_loop:
jal GetOneIntByVal # call GetOneIntByVal to get valsToDo
move $t0, $v0 # move valsToDo to $t0
li $a1, 1 # load minInt
li $a2, 7 # load maxInt
la $a3, adjMsg # load address of the message
jal ValidateInt # call ValidateInt to validate valsToDo
# Check for zero input before proceeding
beq $t0, $zero, exit_program # exit loop if valsToDo is 0
# Initialize loop variables
move $t1, $zero # Initialize i to 0
move $t2, $t0 # Initialize j to valsToDo
swap_loop:
beq $t1, $t2, show_backwards # exit loop if i equals (valsToDo - 1)
sll $t4, $t1, 2 # calculate offset for intArr[i]
add $t3, $t4, $gp # add offset to base address of intArr
lw $a0, 0($t3) # load intArr[i]
addi $t4, $t2, -1 # calculate (valsToDo - 1)
sll $t5, $t4, 2 # calculate offset for intArr[j]
add $t5, $t5, $gp # add offset to base address of intArr
lw $a1, 0($t5) # load intArr[j]
jal SwapTwoInts # call SwapTwoInts to swap intArr[i] and intArr[j]
addi $t1, $t1, 1 # increment index i
addi $t2, $t2, -1 # decrement index j
j swap_loop
show_backwards:
move $a0, $t0 # move valsToDo to $a0
jal ShowIntArray # call ShowIntArray to show reversed array
li $v0, 4 # syscall code for printing string
la $a0, dmPrompt # load the address of the prompt
syscall
li $v0, 12 # syscall code for reading character
syscall
lb $a0, 0($v0) # load the byte read
li $t6, 'n' # load 'n' into $t6
li $t7, 'N' # load 'N' into $t7
beq $a0, $t6, exit_program # exit loop if character is 'n'
beq $a0, $t7, exit_program # exit loop if character is 'N'
j main_loop
exit_program:
li $v0, 10 # syscall code for exit
syscall
# Function to get an integer by value
# Arguments:
# $a0: Address of the prompt
# Returns:
# $v0: The integer entered by the user
GetOneIntByVal:
li $v0, 5 # syscall code for reading integer
syscall
# Check if the input is a valid integer
bltz $v0, invalid_input # If the input is negative, it's invalid
j input_valid # Otherwise, input is valid
input_valid:
jr $ra # Return if the input is valid
invalid_input:
# Print an error message
li $v0, 4
la $a0, invalid_input_msg
li $v0, 11 # syscall code for printing character
li $a0, '\n' # load newline character to print
syscall
# Re-prompt the user to enter an integer
j GetOneIntByVal
# Function to validate an integer
# Arguments:
# $a0: Address of the integer to validate
# $a1: Minimum allowed value
# $a2: Maximum allowed value
# $a3: Address of the message
ValidateInt:
lw $t0, 0($a0) # load the integer to validate
blt $t0, $a1, less_than_min
bgt $t0, $a2, greater_than_max
jr $ra # return if the integer is within range
less_than_min:
# Handle less than min
j set_min
greater_than_max:
# Handle greater than max
j set_max
set_min:
sw $a1, 0($a0) # set the integer to the minimum value
jr $ra # return to caller
set_max:
sw $a2, 0($a0) # set the integer to the maximum value
jr $ra # return to caller
# Function to show an array of integers
# Arguments:
# $a0: Address of the array
# $a1: Size of the array
# $a2: Address of the label
ShowIntArray:
li $v0, 4 # syscall code for printing string
move $a0, $a2 # load the address of the label
syscall
li $t0, 0 # initialize index k to 0
show_loop:
beq $t0, $a1, end_show # exit loop if k equals size
lw $t1, 0($a0) # load the integer at array[k]
li $v0, 1 # syscall code for printing integer
move $a0, $t1 # load the integer to print
syscall
li $v0, 11 # syscall code for printing character
li $a0, ' ' # load space character to print
syscall
addi $a0, $a0, 4 # move to the next integer in the array
addi $t0, $t0, 1 # increment index k
j show_loop
end_show:
li $v0, 11 # syscall code for printing character
li $a0, '\n' # load newline character to print
syscall
jr $ra # return to caller
# Function to swap two integers
# Arguments:
# $a0: Address of the first integer
# $a1: Address of the second integer
SwapTwoInts:
lw $t0, 0($a0) # load the first integer
lw $t1, 0($a1) # load the second integer
sw $t1, 0($a0) # store the second integer at the first address
sw $t0, 0($a1) # store the first integer at the second address
jr $ra # return to caller
It gives me that error. What should I do?
New contributor
Ryder is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.