5

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.

2 Answers 2

13

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.

Sign up to request clarification or add additional context in comments.

4 Comments

The values of base and counter aren't in registers, they are stored in the RAM. So when I do this I just get an error message.
Oh, if they aren't registers then it's different. Updating.
It'd be nice it they could optimize that instruction, even though it is one more line for lw/sw equivalent, I spent a good 1hr+ searching for the instruction.
Like I said, though, there's no way to do it in a single instruction on a classic MIPS-style architecture without pipeline stalls (or a double-ported memory).
8

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.

6 Comments

You can actually do it in only 2 instructions: lw $t0, base then sw $t0, count
lw $t0, base is just MIPS assembler shorthand for lui $at,%hi(base) followed by lw $t0,%lo(base)(at). The load then store sequence is still a total of 4 machine instructions.
but only two physical instructions.
No. My orignal comment was not well explained. lw $t0, base expands to 2 instructions. sw $t0,count expands to 2 instructions. This gives a total of 4 instructions for the sequence. Physical instructions and machine instructions and instructions are all the same thing on MIPS. Each one occupies a 32 bit word in the executable file and in memory.
Who can explain what does 0($t1) mean?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.