简体   繁体   中英

If I want to store the user input in an array in Risc-v,how do I store the values and compare it

If I want to store the user input in an array in risc-v,how do I store the values and compare it

for example in mips I can do it like:

addi $t1,$v0,0
sw $t1,array($t0)
addi $t0,$t0,4

and then compare it like:

lw $t2,array($t3)
beqz $t2,label # if t2 = 0 then go to label
addi $t3,$t3,4 

The sw you're using here on MIPS is a pseudo instruction. The pseudo instruction expand into two-three instructions in actual MIPS machine code: these instructions are interconnected through a register, namely $at — the assembler temporary register.

The exact form you're using is 3 instructions; it expands to

    lui $at, %hi(array)
    addu $at, $at, $t0
    sw $t1, %lo(array)($at)

RISC V cleaned up a lot of the register usages of MIPS, including removing the reserved-for-assembler temporary register. They also increased the number of parameter registers from MIPS' 4 to 8, while removing reserved-for-operating system registers $k0 & $k1 . Further, they use the same a0 and a1 for return values as for the first two parameters. As a result, RISC V has a more useful registers set.

You can do the same things in RISC V as in MIPS except for some of the pseudo instructions. The load pseudo instructions could work, using the load target as the intermediate register to connect the two instructions, but some assemblers won't take it either. But there is no available temporary register for the assembler to use for the store pseudo instruction expansion so that isn't supported.

Therefore, you have to avoid the sw pseudo instruction. You can use straight pointers instead, and its also more efficient, since (in my example, inside the loop) these are just straight instructions instead of a multiple instruction sequence. You can then separate the la from the sw , moving it outside the loop, for example.

    la t0, array
    ...
loop:
    sw t1, 0(t0)       # or sw t1, (t0)
    addi t0, t0, 4
    ...
    beqz t2, loop

Also, don't use $ on register names in some RISC V assemblers.


If you had wanted to use the MIPS pseudo instruction, sw with global array and register offset:

sw $t1, array($t0)    # expands to three instructions, also sets & uses `$at`
addi $t0, $t0, 4
...

You can substitute with the following:

la $t7, array                  # pick a free temp register
add $t7, $t7, $t0
sw $t1, 0($t7)
...

The observation here of course, is that if you have another register available for longer usage, you might keep the address of array in that register for a while, so you can reuse it as needed.

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