MIPS指令`加载字'不加载字到寄存器中

时间:2018-03-31 16:04:00

标签: arrays assembly mips

我正在编写一个给出数组的程序,它打印出数组值并交换用户选择的两个元素。这是代码:

# 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

当我使用QtSpim逐步执行代码时,这些行似乎给出了问题:

lw $t2, ($t0)                 
lw $t3, ($t1)

$ t2和$ t3中的值应该是数组给出的整数值。例如,如果用户指定索引1和2,则$ t2和$ t3中的值应为5和17.相反,它在我的版本中显示了奇怪的数字:1634890305和1936269433(十进制)。

出了什么问题?我将不胜感激任何帮助!

1 个答案:

答案 0 :(得分:0)

Printloop会更改$ s1,因此当交换使用它时,它会指向列表末尾。

相关问题