jeudi 21 juillet 2016

How do I properly push and pop existing register values using g++ extended asm

I've been working on performing a call to a function using assembly. I'm new to this, and from what I read I should push and restore registers that I use because the program will not be aware of these changes. So far I have this code:

  int a=3, b=6, c=9, d=12;
    __asm__(
/*      "pushq %%rcx;"                                                                                                                                                                           
        "pushq %%rdx;"                                                                                                                                                                                   
        "pushq %%rsi;"                                                                                                                                                                                   
        "pushq %%rdi;"
*/
        "movq %3, %%rcx;"
        "movq %2, %%rdx;"
        "movq %1, %%rsi;"
        "movq %0, %%rdi;"
        "call %4;"
/*
        "popq %%rdi;"                                                                                                                                                                            
        "popq %%rsi;"                                                                                                                                                                                    
        "popq %%rdx;"                                                                                                                                                                                    
        "popq %%rcx;"
*/
      :
      : "g"(a), "g"(b), "g"(c), "g"(d), "a"(foo)
      : /* clobber list for foo */
      );

I am having success with this __asm__ call without the pushq and popq, which is why it is commented out. I'm not sure why though because I thought it was necessary to push/pop them. Can anyone explain this to me? I think it's with maybe allocating room on the stack fro these pushes, but I'm unclear how to do that and haven't been able to figure it out.

Aucun commentaire:

Enregistrer un commentaire