0

I am learning assembly language for 86-64.

I have learned that arguments are passed using the stacks. However, arguments have been passed using the registers, and one of them, edx is a volatile one. I am confused why values were not passed using stacks?

I used this site - https://godbolt.org/

I have written a following C code:-

int a,b=7,c=5,d=10;

int add(int bTemp,int cTemp, int dTemp){
    int temp = bTemp + cTemp + dTemp;
    return temp;
}

int main(){
    a = add(b,c,d);
    return 0;
}

It compiles to: -

a:
        .zero   4
b:
        .long   7
c:
        .long   5
d:
        .long   10
add(int, int, int):
        push    rbp
        mov     rbp, rsp
        mov     DWORD PTR [rbp-20], edi
        mov     DWORD PTR [rbp-24], esi
        mov     DWORD PTR [rbp-28], edx
        mov     edx, DWORD PTR [rbp-20]
        mov     eax, DWORD PTR [rbp-24]
        add     edx, eax
        mov     eax, DWORD PTR [rbp-28]
        add     eax, edx
        mov     DWORD PTR [rbp-4], eax
        mov     eax, DWORD PTR [rbp-4]
        pop     rbp
        ret
main:
        push    rbp
        mov     rbp, rsp
        mov     edx, DWORD PTR d[rip]
        mov     ecx, DWORD PTR c[rip]
        mov     eax, DWORD PTR b[rip]
        mov     esi, ecx
        mov     edi, eax
        call    add(int, int, int)
        mov     DWORD PTR a[rip], eax
        mov     eax, 0
        pop     rbp
        ret
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
killerprince182
  • 455
  • 2
  • 12
  • 1
    Why is because by definition of the x64 ABI. You should try using optimization option `-O3` and you'll see how the register parameter passing is more efficient than stack parameter passing. (FYI, all the parameter registers are volatile.) See https://stackoverflow.com/a/18024743/471129 – Erik Eidt Jun 09 '20 at 10:23
  • Because legacy calling conventions using stack args are slow. See [this](https://stackoverflow.com/questions/4429398/why-does-windows64-use-a-different-calling-convention-from-all-other-oses-on-x86/35619528#35619528) for some info on how the x86-64 SysV calling convention was designed, and what it was optimized for. All the arg-passing registers are volatile (call-clobbered). Note that x86-64 SysV makes more of the "legacy" regs volatile than i386 System V, which is probably what you were reading about. – Peter Cordes Jun 09 '20 at 10:30
  • Wouldn't the volatile system cause more security issues, problems and bugs than SysV? – killerprince182 Jun 09 '20 at 10:47
  • 2
    The EDX register is “volatile” because subroutines are allowed to change it. Routines are allowed to change some registers without restoring them but not others. The number of such registers is a choice by the ABI; allowing many to be volatile reduces the work a routine has to do saving and restoring registers for its caller but increases the work it has to do saving and restoring its own data when calling a subroutine. So selecting the number to be volatile is a balancing act. This has no effect on security; all the routines involved are within the program and have access to all data anyway. – Eric Postpischil Jun 09 '20 at 11:10

0 Answers0