About calling C function from Assembly and vice versa -


i've tried calling asm c , vice versa. worked perfect @ least have questions. here code:

test.s followed:

.text                 .global _start      .global _main        .type _main, @function   .global writeme                               .type writeme, @function      _start:          #; write hello world 5 times.     #; jump exit , call c function after that.     #; c function calls writeme assembly function     #; exit syscall      xorl %ecx, %ecx             #; ecx = 0     call    _get_eip            #; eip without labels. research.     pushl   %eax                #; push stack     incl %ecx               #; ++ecx     pushl %ecx              #; push stack      movl    $len,%edx           #; tell length of string     movl    $msg,%ecx               #; tell string position     movl    $1,%ebx                 #; fd = stdout     movl    $4,%eax                 #; syscall = write     int     $0x80               #; perform call      popl %ecx               #; pop counter      movl %ecx, %eax             #; eax = ecx     cmpl $0x5, %eax             #; compare 0x5 , eax     je _exit                #; eax == 0x5, jump exit      _jmp:         popl    %eax            #; pop instruction pointer         jmpl    %eax            #; jmp      _exit:         call    _main           #; call c function         movl    $0,%ebx             #; exit_success         movl    $1,%eax             #; syscall = exit         int     $0x80               #; perform call         ret  _get_eip:                   #; function getting eip     popl %eax               #; pop eip     pushl %eax              #; push again return     ret                 #; return location  writeme:                    #; function writing, called c     popl (__eip)                #; pop return location     popl %ecx               #; pop first argument, msg     popl %edx               #; pop second argument, len      movl $1, %ebx               #; fd = stdout     movl $4, %eax               #; syscall = write     int $0x80               #; perform call      pushl (__eip)               #; push return location     ret                 #; return location  writeme2:                   #; function writing, called c     popl %ecx               #; pop return location     popl %ecx               #; pop first argument, msg     popl %edx               #; pop second argument, len      movl $1, %ebx               #; fd = stdout     movl $4, %eax               #; syscall = write     int $0x80               #; perform call      subl $0x0c, %esp            #; restore stack     ret  .data                            __eip:  .long  msg:     .ascii    "hello, world!\n\0"        len = . - msg                 

main.c followed:

extern void writeme(const char *msg, int len);  int _strlen(const char *msg) {     int _len = 0;     while (*msg++ != 0x0)         _len++;     return _len;  }  void _main() {      const char * szmsg = "hello, world!\n";     writeme(szmsg, _strlen(szmsg)); } 

my output expected.

hello, world!
hello, world!
hello, world!
hello, world!
hello, world!
hello, world!

my questions followed:

1)

.type writeme, @function 

what code mean? information "gcc" ? do? have that?

2)

do have write informing op. if function declared in c file?

.type _main, @function 

_main declared in c file, have write ?

3)

popl (__eip)                #; pop return location popl %ecx                   #; pop first argument, msg popl %edx                   #; pop second argument, len ........ pushl (__eip)               #; push return location 

i've used code in writeme, safe? in other words, can pop arguments, or gcc pop automatically?

popl %ecx               #; pop return location popl %ecx               #; pop first argument, msg popl %edx               #; pop second argument, len .... subl $0x0c, %esp        #; restore stack 

i've used code in second function. asking you, 1 safe , correct?

4) need restore registers after calling assembly function c ? (i heard have restore edi others?)

thanks replies.

1) sets type of symbol function. it's not needed, except in special cases, example shared libraries.

2) no, has been done compiler functions defined in c.

3) both of wrong. should access arguments relative esp or, after setting standard stack frame, relative ebp.

4) should read appropriate abi documentation information calling convention. typically, can use eax, ecx , edx rest must preserved.


Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -