0

I am trying to understand assembly which is generated by the gcc(Ubuntu 9.2.1-9ubuntu2) 9.2.1 20191008.

I have written simple c code.

#include<stdio.h>
int main()
{
    volatile int a;
    a=45;
    return 0;
}

and complied with is command - gcc -S -O -o prog_assembly.asm prog_assembly.c

I got assembly

    .file   "prog_assembly.c"
    .text
    .globl  main
    .type   main, @function
main:
.LFB23:
    .cfi_startproc
    endbr64
    movl    $45, -4(%rsp)
    movl    $0, %eax
    ret
    .cfi_endproc
.LFE23:
    .size   main, .-main
    .ident  "GCC: (Ubuntu 9.2.1-9ubuntu2) 9.2.1 20191008"
    .section    .note.GNU-stack,"",@progbits
    .section    .note.gnu.property,"a"
    .align 8
    .long    1f - 0f
    .long    4f - 1f
    .long    5
0:
    .string  "GNU"
1:
    .align 8
    .long    0xc0000002
    .long    3f - 2f
2:
    .long    0x3
3:
    .align 8
4:

I want to understand each statement of assembly. Where I can find the documents which will contain all information?

AmitJoshi
  • 11
  • 1
  • 1
  • 1
    The docs for the gnu assembler are [here](https://sourceware.org/binutils/docs/as/). That should get you everything that starts with a dot. Are you not familiar with x86 assembly language? There are a number of references that google can find for you. – David Wohlferd Jan 03 '20 at 05:08
  • 1
    Much of that is "noise" that you really don't need to understand to just understand how assembly language works. See [How to remove "noise" from GCC/clang assembly output?](//stackoverflow.com/q/38552116). (A lot of it is object file metadata and unwind info.) I'm surprised to see an [`endbr64` CFE instruction](https://stackoverflow.com/questions/56905811/what-does-endbr64-instruction-actually-do), I guess Ubuntu configures GCC differently from some distros. Normally you just see that in CRT and library functions. – Peter Cordes Jan 03 '20 at 05:55
  • when you read the documentation for the assembler and the instruction set what part of those documents did you not understand. – old_timer Jan 03 '20 at 06:22
  • thanks David and Peter. Currently I want to understand each line of this code only. for example -> .file "prog_assembly.c" what this means to OS? processes related to this line. May you please explain in brief about it? – AmitJoshi Jan 04 '20 at 10:38
  • .file "prog_assembly.c" doesn't mean *anything* to the OS. You can't execute this file, so the OS is never going to see this. This file is sent to the assembler which sees lines that start with a dot as instructions about how to create the actual executable. If you read the docs I linked for you, you'd see the purpose of `.file` is *When emitting DWARF2 line number information, .file assigns filenames to the .debug_line file name table.* In other words, it's used to create information used during debugging. – David Wohlferd Jan 04 '20 at 11:43

1 Answers1

0
    movl    $45, -4(%rsp)   # The meaning copy 45 to rsp (current location in stack, growing downwards)
    movl    $0, %eax        # eax is zero
    ret

For endbr64 instruction you can see here

Note :

  1. Immediate values are prefixed by $.
  2. Register names are prefixed by %.
  3. l suffix = long (32 bit integer or 64-bit floating point).
  • `-4(%rsp)` is 4 bytes *below* the current RSP, in the red-zone. It's not "copy 45 to rsp" that would be either `mov $45, %rsp` which would destroy the stack pointer, or `mov $45, (%rsp)` which would overwrite the return address, also breaking `ret`. If you're going to describe what an instruction does, don't ignore parts of the addressing mode! That's probably not obvious to someone who has to ask this question. – Peter Cordes Jan 07 '20 at 13:33