简体   繁体   English

使用Assembly将数据写入char

[英]Write data to a char using Assembly

I created a program in C to generate a char* processedData. 我用C创建了一个程序来生成char *处理数据。 I send to my assembly program and put it in a register: 我发送到我的汇编程序并将其放入寄存器中:

mov     edx, [ebp+12]
mov     edi, edx

How can i write a char into it. 我怎样才能写一个字符。 I know i need to Write a char and inc edi... that in a loop. 我知道我需要写一个char和inc edi ...一个循环。 But how can i write a char, i already have the value into another register. 但是我怎么写一个字符,我已经把值存入另一个寄存器了。 But if i do mov edx, 49; 但是如果我做edx,49; char code i'll lose the pointer. 字符代码,我会失去指针。 I want to do something like 我想做类似的事情

for(p=malloc(100*sizeof(char*)); p!=NULL;p++){
    *p=//my char code
}

Assembly for linux (DEBIAN) x86 Linux(DEBIAN)x86的程序集

edx is the address of the destination of the char. edx是字符目的地的地址。 That is, edx is a pointer to the location you want to write to. 也就是说, edx是指向您要写入的位置的指针。 Therefore, do this: 因此,请执行以下操作:

mov byte ptr ds:[edx], 49

This might even work: 这甚至可以工作:

mov byte ptr ds:[edx], '1'

You say your character is already in some register. 您说您的角色已经在某个寄存器中。 I assume it is an 8 bit register ( ah/al/bh/bl/ch/cl/dh/dl ), in which case you can just do: 我假设它是一个8位寄存器( ah/al/bh/bl/ch/cl/dh/dl ),在这种情况下,您可以执行以下操作:

mov [edx], ah

The assembler can infer the size of the data [edx] is pointing to in this case. 在这种情况下,汇编程序可以推断数据[edx]指向的大小。


I'm on Windows right now so this code is for VC++. 我现在在Windows上,因此此代码适用于VC ++。 It demonstrates copying a string: 它演示了如何复制字符串:

#include <stdlib.h>
#include <stdio.h>

char src[] = "hello";
char dest[8];

int main( void )
{
   __asm
   {
      xor ecx, ecx

      mov eax, offset src
      mov edx, offset dest

        loop_dest:

      mov bh, byte ptr ds:[eax+ecx]
      mov [edx+ecx], bh

      inc ecx
      cmp ecx, size src
      jnz loop_dest
   }

   printf("%s\n", dest);
   return EXIT_SUCCESS;
}

If your assembler uses AT&T syntax, you'll need to do some minor translation, but hopefully this points you in the right direction. 如果您的汇编器使用AT&T语法,则需要进行一些小的翻译,但是希望这会为您指明正确的方向。

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

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