Как поменять местами две записи в массиве в Assembly? - PullRequest
0 голосов
/ 28 апреля 2020

Я работаю над программой сборки в MIPS, и у меня возникают проблемы с заменой двух указанных c записей и последующей печатью. Пользователь должен иметь возможность указать, какие два местоположения он хочет поменять местами (используя строку select и swap), а затем отобразить список из 10 записей, где swap выполняется правильно (например, если пользователь попросит 3 поменять местами с 10, 10 сейчас в 3 и наоборот). Заранее спасибо! Код:

.data

EnterInfo: .asciiz "Enter in the name, age, and ID for 10 students please:\n"
newline: .asciiz "\n"
space: .asciiz " "
menu: .asciiz "Menu:\n
1) Swap two records.\n
2) Exit\n
Please choose one of the above options: "
Select: .asciiz "Which record do you select first? "
Swap: .asciiz "Which record do you want to swap it with? "

menuEnd: .word 0
Student: .space 88
StuEnd:

.text
main: 
    la $a0,EnterInfo #Ask for info to be entered
    li $v0,4
    syscall

    la $a0, Student
    li $a1, 41
    jal InputRecords   # jump to InputRecords

    la $a0, Student
    li $a1, 41
    jal PrintRecords   # jump to PrintRecords


    li $v0,4
    la $a0, newline #Print a new line
    syscall

    li $v0,4
    la $a0,menu #Print menu string
    syscall

    li $v0,5 #Take in user input
    syscall

    move $t2, $v0   #move user input from v0 and store to t2

    beq $t2, 1, SwapRecords     # replace element at position
    beq $t2, 2, Exit            # remove max

Exit:  
    li $v0,10       # exit the program
    syscall

InputRecords:
    li $t0,0 #Load 0 into register t0

loop:
    beq $t0,10,exitInLoop  #If t0 is 10, go to exitInLoop branch
    li $v0, 8 #Load 8 into v0 register, take in name
    syscall

    li $v0, 5 #Take in user input, take in age
    syscall

    sw $v0, 40($a0) #Save word, allocate 40 bytes, into instruction 
    li $v0, 5       #Take in user string input, take in ID int
    syscall

    sw $v0, 44($a0) #Save word into instruction
    add $a0,$a0,48  #Add 48 and content of register a0 and store into a0
    add $t0,$t0,1   #Add one and t0 and store into t0

    j loop  #jump to loop, redo until condition in 1st line are met

exitInLoop:
    jr $ra           # return to user

PrintRecords:
    li $t0,0 #Load 0 into t0

printLoop:
    beq $t0,10,exitPrintLoop  #Keep going until condition of t0 equal 10 is met, then go to exitPrintLoop branch
    li $v0, 4
    syscall

    addi $sp,$sp,-4 #add immediate -4 and content of sp register
    sw $a0,0($sp)   #Save word

    li $v0,4
    la $a0, space #Print out space string
    syscall

    lw $a0,0($sp)
    add $sp,$sp,4
    addi $sp,$sp,-4
    sw $a0,0($sp)
    lw $a0, 40($a0)
    li $v0,1
   syscall

    lw $a0,0($sp)
    add $sp,$sp,4

    addi $sp,$sp,-4
    sw $a0,0($sp)

    li $v0,4
    la $a0, space #Print the space string
    syscall

    lw $a0,0($sp)
    add $sp,$sp,4
    addi $sp,$sp,-4
    sw $a0,0($sp)
    lw $a0, 44($a0)
    li $v0,1
    syscall

    lw $a0,0($sp)
    add $sp,$sp,4
    addi $sp,$sp,-4
    sw $a0,0($sp)

    li $v0,4
    la $a0, newline #Print out new line
    syscall

    lw $a0,0($sp)
    add $sp,$sp,4
    add $a0,$a0,48
    add $t0,$t0,1

    j printLoop #printLoop kep repeating until condition of the first line are met

exitPrintLoop:
    jr $ra           # return to user

SwapRecords:
    li $v0, 4
    la $a0, Select #Ask user for first position
    syscall

    li $v0, 5 #Get first position
    syscall

    move $t6, $v0 #Store value from user to t6

    li $v0, 4
    la $a0, Swap #Ask user for 2nd position for record to be swap with
    syscall 

    li $v0, 5 #Get 2nd position 
    syscall 
    move $t7, $v0 #Store value from user to t7

    #Swapping the two records
    lw $t6 0($t0) #Perform swap? put values of t6 into t7 
    lw $t7 4($t0)
    sw $t6 4($t0)
    sw $t7 0($t0)
    syscall

    jal printLoop  # print the newly swapped records
...