Home > Net >  Understanding x86-64 assembly for simple program in C with a function call
Understanding x86-64 assembly for simple program in C with a function call

Time:02-11

I have simple C program that produces this x86-64 assembly for function func

#include <stdio.h>
#include <string.h>

void func(char *name)
{
    char buf[90];
    strcpy(buf, name);
    printf("Welcome %s\n", buf);
}

int main(int argc, char *argv[])
{
   func(argv[1]);
   return 0;
}

So I think this

   0x000000000000118d < 4>: push   %rbp

pushes the base pointer like placed argument which is char *name

then 0x000000000000118e < 5>: mov %rsp,%rbp set stack pointer to what at base pointer I belive that above and this makes stack point points to char *name at this point

then

   0x0000000000001191 < 8>: add    $0xffffffffffffff80,%rsp

I am little unsure about this. Why is 0xffffffffffffff80 added to rsp? What is the point of this instruction. Can any one please tell.

then in next instruction 0x0000000000001195 < 12>: mov %rdi,-0x78(%rbp) its just setting -128 decimal to rdi. But still no buffer char buf[90] can be seen, where is my buffer? in following assmebly, can anyone please tell?

also what this line 0x00000000000011a2 < 25>: mov %rax,-0x8(%rbp)

Dump of assembler code for function func:
   0x0000000000001189 < 0>: endbr64 
   0x000000000000118d < 4>: push   %rbp
   0x000000000000118e < 5>: mov    %rsp,%rbp
   0x0000000000001191 < 8>: add    $0xffffffffffffff80,%rsp
   0x0000000000001195 < 12>:    mov    %rdi,-0x78(%rbp)
   0x0000000000001199 < 16>:    mov    %fs:0x28,%rax
   0x00000000000011a2 < 25>:    mov    %rax,-0x8(%rbp)
   0x00000000000011a6 < 29>:    xor               
  • Related