简体   繁体   English

从内存地址帮助(程序集)复制单词

[英]Copying words from memory address assistance (assembly)

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. 源地址,目标地址和要复制的单词数是函数的输入参数。

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. 根据您的描述,这听起来像是常规的memcpy,只是您指定要复制的字数而不是字节数。 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. 编辑:如果您的源缓冲区和目标缓冲区未在字边界上对齐,则需要使用lb / sb来避免数据对齐异常。

EDIT: added nops after branches 编辑:分支后添加点

So think about how you would do this in C...At a low level. 因此,请考虑一下如何在C语言中进行此操作。

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. 乘以4以获得字节数是正确的,但是如果您一次复制一个单词,则无需计数字节,只需单词。 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. 所以这在汇编中是什么样子,可以转换为mips,这是伪代码:rs是源寄存器$ a0,rd是目标寄存器$ a1,rx是长度寄存器$ a2,rt是临时寄存器。 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). 现在,如果要从内存中加载一个字,请使用加载字(lw)指令,如果要加载一个字节,请执行lb(加载字节)。

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:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM