Mips: store a variable in another variable

2019-07-13 06:22发布

问题:

I am trying to translate a C program into MIPS assembly code

In my C code I have a line like this:

int base;
int count;

count = base;

In MIPS, how would I store the value of base inside of count? The only instructions I see for loading and storing are lw and sw and their prototypes only go from a register source -> ram destination or a ram source -> register destination.

Any help would be appreciated.

EDIT I was hoping that this could be done in a single instruction, something like

move base acc

but apparently that is impossible (at least I found no example of an instruction similar to that), I opted for this:

lw $t0, base   //load base into $t0
sw $t0, count  //store the value of $t0 in count

If there is a one-line instruction to do this that would be better if anyone knows one.

回答1:

MIPS doesn't support direct memory-to-memory moves. (Neither do most common CPUs, actually -- even x86 doesn't.) You'll need to use lw/sw to move data around.

Architecturally, this is because MIPS is designed to only perform a single memory access per cycle -- doing a memory-to-memory move would require two accesses (one read, one write), or stall the pipeline.



回答2:

Here's how to do it in MIPS

la $t0, base     // load the address of "base"
la $t1, count    // load the address of "count"
lw $t2, 0($t0)   // load the data at location "base"
sw $t2, 0($t1)   // store that data at location "count"

It is not possible to do a memory to memory move in a single instruction.



标签: assembly mips