Frage

I am trying copy some words from memory and saving it to another memory address using assembly. I am trying to write the code for it but I am not sure about some of the parts. I will briefly describe what I want to do.

The source address, destination address and the number of words to copy are input arguments of the function.

War es hilfreich?

Lösung

From your description it sounds like a regular memcpy, except that you specify the number of words to copy rather than the number of bytes. Not sure where the whole stack buffer idea comes from(?).

Something like this would copy the words from the source to the destination address:

sll $a2,$a2,2
addu $a2,$a1,$a2  ; $a2 = address of first byte past the dest buffer
Loop:
    lw $t0,0($a0)
    sw $t0,0($a1)
    addiu $a0,$a0,4
    addiu $a1,$a1,4
    bne $a1,$a2,Loop
    nop

EDIT: If your source and destination buffers are not aligned on word boundaries you need to use lb/sb instead to avoid data alignment exceptions.

Andere Tipps

EDIT: added nops after branches

So think about how you would do this in C...At a low level.

unsigned int *src,*dst;
unsigned int len;
unsigned int temp;
...
//assume *src, and *dst and len are filled in by this point
top:
  temp=*src;
  *dst=temp;
  src++;
  dst++;
  len--;
  if(len) goto top;

you are mixing too many things, focus on one plan. First off you said you had a source and destination address in two registers, why is the stack involved? you are not copying or using the stack, you are using the two addresses.

it is correct to multiply by 4 to get the number of bytes, but if you copy one word at a time you dont need to count bytes, just words. This is assuming the source and destination addresses are aligned and or you dont have to be aligned. (if unaligned then do everything a byte at a time).

so what does this look like in assembly, you can convert to mips, this is pseudocode: rs is the source register $a0, rd is the destination register $a1 and rx is the length register $a2, rt the temp register. Now if you want to load a word from memory use the load word (lw) instruction, if you want to load a byte do an lb (load byte).

top:
branch_if_equal rx,0,done
nop
load_word rt,(rs)
store_word rt,(rd)
add rs,rs,4
add rd,rd,4
subtract rx,rx,1
branch top
nop
done:

Now if you copy bytes at a time instead of words then

shift_left rx,2
top:
branch_if_equal rx,0,done
nop
load_byte rt,(rs)
store_byte rt,(rd)
add rs,rs,1
add rd,rd,1
subtract rx,rx,1
branch top
nop
done:
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top