0

I wanted to try and learn some assembly recently, and, having some knowledge of the C programming language, I thought that it'd be a good idea to look at the assembly equivalent of my C code.

main.c:

int main()
{
    return 2;
}

main.s after gcc -S main.c -o main.s:

    .file   "main.c"
    .text
    .def    __main; .scl    2;  .type   32; .endef
    .globl  main
    .def    main;   .scl    2;  .type   32; .endef
    .seh_proc   main
main:
    pushq   %rbp
    .seh_pushreg    %rbp
    movq    %rsp, %rbp
    .seh_setframe   %rbp, 0
    subq    $32, %rsp
    .seh_stackalloc 32
    .seh_endprologue
    call    __main
    movl    $2, %eax
    addq    $32, %rsp
    popq    %rbp
    ret
    .seh_endproc
    .ident  "GCC: (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0"

As you can see, the assembly equivalent of my C code is very verbose and it seems that a lot of it is unnecessary. Is there a way to make the assembly code neater, like a flag that removes the preamble or something similar?

William Ryman
  • 231
  • 2
  • 9
  • 3
    godbolt.org does all the cleanup plus you can try a bunch of different compilers. That being said, I'm not sure that's a great way to learn assembly. – Mat Aug 02 '22 at 16:39
  • 1
    Assembly, no matter the target system, is very hard to learn by just looking at it, even when "cleaned up" and high-lighted as in the compiler explorer (godbolt.org) and comparing to the C code. Code generated by a compiler (especially if optimization is enabled) might not always make sense. There are plenty of online tutorials and books that could help you get better understanding of the selected variant language before you start looking at compiler-generated code. – Some programmer dude Aug 02 '22 at 16:42
  • 1
    As @Mat wrote godbolt is the best way: https://godbolt.org/z/Y5WMnWd9T – 0___________ Aug 02 '22 at 16:49
  • 1
    Do you have a reference guide for all the assembly instructions for this ISA? If not, that's the first thing to get your hands on. – tadman Aug 02 '22 at 16:49
  • 1
    I would discourage you from learning assembler. It is very unlikely that you'll ever need to write even a single line of the assembly code. Better practice C language. – 0___________ Aug 02 '22 at 16:54
  • 3
    On the other hand, for me, it was exactly looking at assembly and C code together that finally made pointers make sense. I had been flailing about (like most) and having a difficult time with them even after more than a year of C programming. Then one day I was examining generated assembly and had an absolute epiphany. – SoronelHaetir Aug 02 '22 at 17:44
  • 2
    someone needs to know assembly language and machine code, otherwise there will be no processors and the (compiler, etc) tools will continue to get worse since nobody knows or cares. And they have been getting worse for many years now (there are other factors too like lack of competition). – old_timer Aug 02 '22 at 17:49
  • 1
    @Mat: Looking at compiler output is definitely one of the things a beginner should do. At least for the instructions themselves; some of the fiddly directives like `.type 32` are totally unnecessary to understand program logic in assembly. After watching Matt Godbolt's CPPCon talk (linked from [How to remove "noise" from GCC/clang assembly output?](//stackoverflow.com/q/38552116)), play around with modifying compiler output, or write something yourself; good ways to build more understanding. – Peter Cordes Aug 02 '22 at 21:03
  • 1
    Also single-step code with a debugger to see register values change! asm debug tips in https://stackoverflow.com/tags/x86/info - not using a debugger is a big waste of time. And yeah, as tadman says, consult an instruction-set reference manual, especially any time an instruction didn't do exactly what you expected. Intel's vol.1 PDF has some beginner intro stuff for x86 https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html while vol.2 is the instruction-set reference. (See also the x86 tag wiki for links to some decent tutorials, too.) – Peter Cordes Aug 02 '22 at 21:07

0 Answers0