简体   繁体   中英

Memory addresses in assembly

I cannot figure out what is going wrong here. I am attempting to store 10 integers in memory and then access each of them in order. Here is my current code:

.data # Data declaration section
strInMsg: .asciiz "Please Enter An Integer:"

.align 2
memAddr: .space 40

.text
main: #Get 10 integers and store them in $t0
la $t0, memAddr #$t0 - 40 = first element in array
add $t3, $zero, $zero 
add $t1, $zero, 10 

loop: la $a0,strInMsg
add $v0, $zero, 4
syscall
add $v0, $zero, 5
syscall
sw $v0, 0($t0)
add $s1, $s1, 4
sub $t1, $t1, 1
bgtz $t1, loop

#la $a0, memAddr
#sub $t0, $t0, 40
#j QuickSort

sub $t0, $t0, 40 #<--- PROBLEM LINE
lw $t3, 0($t0)
add $a0, $t3, $zero  
addi $v0, $zero, 1   
syscall 

What happens is it correctly gets all 10 integers from the user, and if I get rid of the problem line it will print the last element as expected because that is the memory address stored in $t0. I thought I could subtract 40 from it to bring it back to the front but it just outputs 0. How can I get the first element?

您只能在循环开始之前递增地址,然后再读取它,因此它不应该是-36吗?

You're never incrementing $t0 within the loop, so you'll be writing all values to the same address, and when you subtract 40 after the end of the loop you end up pointing at memAddr - 40 .
Perhaps the line add $s1, $s1, 4 was meant to say add $t0, $t0, 4 ?

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