简体   繁体   中英

Loading address stored in register in MIPS

I'm having a small issue that I can't seem to get around. I have several numbers stored in the stack in the following fashion:

|5|   0($sp)
|4|   4($sp)
|3|
|8|

I want to traverse the stack, comparing two numbers at a time. By this, I mean that I want to compare 0($sp) with 4($sp) and then 0($sp) with 8($sp), ..., and then 4($sp) with 8($sp). So, 5 vs 4, 5 vs 3, 5 vs 8, 4 vs 3, 4 vs 8, 3 vs 8.

My attempt at this is to

lw $t3, 0($sp) ##
la $t4, 4($sp) ##
Sum2:
    beq $t2, $zero, Exit
    lw $t5, $t4  ##

    add $a0, $t5, $zero
    li $v0, 1
    syscall 
    add $t4, $t4, 4
    addi $t2, $t2, -1
    j Sum2

Mainly the 1st, 2nd, and 5th lines. I'm loading the value the integer on the top of the stack into $t3 and loading the address of the next item in the stack into $t4.

Then I want to load the value at the address stored in $t4, do my comparison (right now i'm just adding to zero so I can print it) and then increment the address stored in t4 by 4 bytes to get the 3rd item in the stack. I would keep doing this until a register value hits 0 . Once this occurs, I'll increment the $sp by 4 and repeat the process.

Whenever I try to load this in PCSpim, I get a syntax error. What am I doing wrong?

If I'm reading your problem correctly, you want

lw $t5, 0($t4)

to load t5 with the value stored where $t4 points.

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