0

Im trying to learn assembly, first i was using NASM for the compiling, but then i understood that i could use .s files in gcc. This interested me greatly, since my goal for this is to be able to write a compiler for a custom language, so this was very intriguing, as it would allow me to link and compile with c code. So filled with excitement, I started compiling c to assembly (.s files) with gcc, and examen it. As I was doing this, it seamed to be structured in a different way then NASM assembly, with only main label, f.eks, and not _start, and other weird structure, and im not talking about Intel- vs AT&T syntax. So then my question follows:

Is it a different structure, in normal assembly and the .s files in gcc, or is it just me not having a good enough knowlage of assembly? If it is a different structure, does it have a name?

I have been trying to google my way to this for hours, but when i search for gcc assembly, and other things I can think of, I only get c inline assembly...

Please help, im going crazy from not figuring this out.

Elias Fyksen
  • 879
  • 9
  • 12
  • 3
    Unless you use GCC's `-nostartfiles` options there is no `_start` label generated because that entry point is in the _C_ startup runtime code (that isn't seen in the outputted `.s` files). The entry point in that case is `main` and `main` conforms to the CDECL calling convention. – Michael Petch Oct 29 '17 at 02:04
  • 2
    You're probably looking for `as`: http://sourceware.org/binutils/docs/as/ By the way, I think its a lot easier to target LLVM IL rather than assembly, if you're going to write your own compiler. – tkausl Oct 29 '17 at 02:12
  • You **can** write+compile functions conforming to your target platform ABI even in NASM, then you can link it together with C/C++ code, which is actually one of reasonable ways how to use assembly in application, to write only few critical tiny bits in ASM and keep most of it in C++. There's no additional magic about .s files making them somehow more usable for linking, you can follow the same rules plus define global labels in NASM too. gcc can be then used for linking, as it will add the C runtime lib by default (saving you some work with linker configuration). – Ped7g Oct 29 '17 at 09:15

1 Answers1

0

gcc emits definitions for all the functions present in the translation unit. (unless they're static inline or static and unused or it chooses to inline them everywhere...).

The CRT start files (linked by default by gcc, not re-built from source every time you compile) provides the definition for _start and the other functions you'll see if you disassemble the binary. They're only linked in at the link stage, not as part of compiling a .c to a .s, so you don't see them in gcc -S output.

Related: How to remove "noise" from GCC/clang assembly output? for tips on making compiler asm output human-readable.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847