0

I converted the following simple C program into assembly

#include <stdio.h>

int main()
{
  char buff[40];
  buff[39] = 42;
  return 0x100d;
}

The output was as follows(64-bit):

push   rbp
mov    rbp.rsp
mov    BYTE PTR -9[rbp], 42
mov    eax, 4109
pop    rbp
ret

I cannot understand the meaning of the third line of assembly. I also noticed that space isn't allocated for the buffer in the stack frame. Could somebody please help explain it to me?

Thanks in advance

Gagan
  • 101
  • 1
  • 2
    The SysV ABI has 128 bytes of red zone, available for use without allocation. The compiler decided to put your `buffer` at `rbp-48` hence `buffer[39]` is at `-9[rbp]`. Yeah I worked that out backwards :) – Jester Jun 10 '21 at 11:50
  • 1
    [Offset before square bracket in x86 intel asm on GCC](https://stackoverflow.com/q/61655590) explains that `-9[rbp]` is just another way to write `[rbp-9]`. (And see the other linked duplicates re: the red zone, RSP not moving). The weird thing to me in your code is the dot instead of comma in `mov rbp.rsp`. But I guess you had to type this by hand from a book or something, otherwise you would have copy/pasted it. – Peter Cordes Jun 10 '21 at 11:52
  • @PeterCordes the thing is I went through it step by step in gdb so it displayed each step seperately. Had to type it in myself. – Gagan Jun 11 '21 at 01:42
  • If you have it in GDB, you can copy/paste disassembly from a terminal. use `disas`. Or use `objdump -drwC -Mintel` and copy/paste that. – Peter Cordes Jun 11 '21 at 01:44

0 Answers0