开发者

How to write binary data to file in assembler?

I tried a binary write via sys_write syscall (to stdout or file) but I can have only text file. How to have no-text (binary) bytes too?

"mov   %eax,    %ebx;" 
"mov   $4, %eax;" 
"mov   -0x40(%rbp),       %ecx;" 
"mov   $14,      %edx;" 
"int   $0x80;"
开发者_Python百科

Trying with this example I can't have 14 bytes written if they are not text-bytes (files by no-text bytes are 0 length!).


I'm not sure what your problem is. As long as you pass the correct length in %edx, sys_write will write any value in the buffer to the file descriptor in %ebx. This includes any value in the range from 0 to 255 (i.e. the full byte range).

Maybe you should post the code which you use to open the file, too. But even if you open the file in text mode, writing to it should still work with any value.

Some more ideas where to look at: Do you flush the output? Do you close the file? If you don't, then the string might still hang in a buffer waiting to be flushed.


I think you miss the point of programming in Assembler.

An assembler program is a text file. The fragment you included in your question is text. I don't know if it's the correct text, but it must be text.

During compilation the text is converted to machine code; binary if you will.

If you want to define binary characters to be output by your program you will need to specify the text which represents those binary codes and will be converted by compilation into a block of bytes which the program can output.

I can't tell you what the text representation would be for the binary you need, that would require a more detailed understanding of both Assembler language and your program.


Yes, you're right. It's not a binary data problem, but pxor result. This is my test, really just for my understanding. Well, in the sys_write syscall, when i point %ecx to memory where stored pxor xmm0 value 'mov -0x40(%rbp),%ecx', then i have 0 length file created! On the contrary if i point to bigbuff '-0x30(%rbp)' it's all ok.

#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include <emmintrin.h>

main(int argc, char **argv)


{  

unsigned char __attribute__ ((aligned (16))) *bigbuff = (char *) malloc(16);

unsigned char __attribute__ ((aligned (16))) *buff = (char *) malloc(16);

unsigned char __attribute__ ((aligned (16))) *nome_file = (char *) malloc(16);

bigbuff="gatto sisvestr\n";

buff="micio sisvestro2";

nome_file="nuovo.txt"; //la lunghezza massima del nome è data da malloc!!!


    asm (
        "movdqa -0x30(%rbp), %xmm0;"
        "movdqa -0x20(%rbp), %xmm1;"
        "pxor %xmm1,%xmm0;"
        "movdqa %xmm0,-0x40(%rbp);"
        "movdqa -0x40(%rbp), %xmm2;"
        "movdqa -0x20(%rbp), %xmm1;"
        "pxor %xmm1,%xmm2;"
        "movdqa %xmm2,-0x30(%rbp);"

   "mov $8, %eax;"

   "mov -0x10(%rbp),%ebx;"

   "mov $448, %ecx;" 

   "int $0x80;"

   "mov   %eax,    %ebx;"

    "mov   $4, %eax;" 

    "mov   -0x40(%rbp),       %ecx;" 

    "mov   $14,      %edx;" 

    "int   $0x80;"
);

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜