Мой проект для моего курса по компьютерным наукам говорит, что мне нужно разрешить пользователю вводить имена учеников, возраст и ID в список, разрешать пользователю обмениваться студентами из одного места в другое и распечатывать массив. Мой профессор рекомендовал мне реализовать список в виде 2D-массива. В настоящее время я работаю над получением пользовательского ввода. Я создал al oop, чтобы поместить имя студента в столбец 1, возраст в столбце 2 и идентификатор в столбце 3.
Я считаю, что я бесконечный l oop, что означает мою программу никогда не переходит в главное меню после получения пользовательского ввода. После написания информации для 10 студентов, программа продолжает запрашивать информацию о студентах.
#Program 5
.data
studentList: .space 120 #Create space for student information
studentRow: .word 10 #2D array studentList has 10 rows
studentCol: .word 3 #2D array studentList has 3 columns
#Row 1 holds name, Row 2 holds age, Row 3 holds ID
getListPrompt: .asciiz "Enter the name, age, and ID for 10 students:\n" #Prompt user for student information
mainMenu: .asciiz "Menu: \n\nPress 1 to swap students\tPress 2 to exit the program"
getSwap: .asciiz "Which student do you want to move? "
getDestination: .asciiz "Where do you want to move the student? "
debugNextRow: .asciiz "\nReached next row!\n" #will be used for degugging purposes
exitMessage: .asciiz "\n\nAh, yes, the sweet release of death..."
.text
main:
#Intro message
li $v0, 4
la $a0, getListPrompt #prompt user to enter student information
syscall
la $s0, studentList #$s0 holds the base address of studentList
lw $a1, studentRow #$a1 has the row size
lw $a2, studentCol #$a2 has the column size
b getInfo
#start code for user input
getInfo:
li $t0, 0 #$t0 is our row index
li $t1, 0 #$t1 is our column index
li $t3, 0 #$t3 is used to avoid an infinite loop, tracks columns
writeInfoLoop: #addr = baseAddr + (rowInd * colSize + colInd) * dataSize
#$t2 will hold the index
mul $t2, $t0, $a2 # $t2 = rowIndex * colSize
add $t2, $t2, $t1 # $t2 = rowIndex * colSize + colIndex
mul $t2, $t2, 4 # $t2 = (rowInd * colSize + colInd) * dataSize
add $t2, $t2, $s0 # $t2 = baseAddr + (rowInd * colSize + colInd) * dataSize
#$t2 now has the index [i, j] where i represents the row and j represents the column
la $a0, studentList #Load address of studentList into $a0
li $a1, 41 #$a1 is the character limit of 41 - 1
li $v0, 8 #take input
syscall
li $v0, 4
syscall
addi $t1, $t1, 1 #increment column index, move from [i, j] to [i, j+1]
addi $t3, $t3, 1 #increment tracker
beq $t1, $a2, switchRow
ble $t3, 30, writeInfoLoop #if $t3 (j) < colSize, loop again
switchRow:
addi $t0, $t0, 1 #increment row index
li $t1, 0 #return to first column
li $v0, 4
la $a0, debugNextRow #debugging: prints when moving on to next row
syscall
b writeInfoLoop
b menu #branch to menu after getting all inputs
#end code for user input
menu:
li $v0, 4
la $a0, mainMenu #Display main menu
syscall
li $v0, 5
syscall
move $t0, $v0
b exit
exit:
#Program done
li $v0, 4
la $a0, exitMessage
syscall
li $v0, 10
syscall