简体   繁体   中英

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更改$ s1,因此,当swap使用它时,它指向列表末尾。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM