I'm trying to understand what'd be the C equivalent of some nasm idioms like these ones:
%define CONSTANT1 1
%define CONSTANT2 2
1) section name_section data align=N
v1: dd 1.2345678
v2: dd 0x12345678
v3: dd 32767
v5: dd 1.0
dd 1.0, 2.0, 3.0, 4.0,
dd 5.0, 6.0, 7.0, 8.0
2) section name_section bss align=N
3) global _function_name@0
section name_section code align=N
4) global _g_structure1
section name_section data align=N
5) section section_name code align=N
dd stores a sequence of
DWORDS given by the arguments. So
dd 1 will store the 4-byte value 0x00000001 at the current location (since it's targeting a little endian architecture, you'll end up with the bytes
0x01 0x00 0x00 0x00).
Sections aren't generally exposed directly in C - it's more of a lower level concern handled by compilers, linkers and runtime loaders. So in general your toolchain will handle the proper allocation of your code and data into sections. For example, the compiler will put the actual assembled code into
.text sections, and will put statically initialized data into
.data sections, and finally will put uninitialized or zero-initialized statically allocated data into
.bss sections, and so on. The details aren't really part of C itself and will vary by platform and executable format (for example, not all platforms have the same types of sections).
When using assembly, on the other hand, you need to be a bit more aware of sections. For example, if you have mutable data it is important that it ends up a different section than your code, since you don't want to run into read-only
.text sections, or self-modifying-code false positives, etc.
The section alignment is a directive to the runtime loader that tells it the minimum required alignment for the section. You can impact this in your C code using some compiler or platform specific options - e.g. if you request a statically allocated array to have an alignment of 32, then the
.data section may be promoted to at least 32-byte alignment. C doesn't have a standard way to actually request such alignment, but you can use platform specific extensions such as
aligned attribute, or even
#pragma pack. C++11 on the other hand has
alignas to do this in a standard way.
@N suffix is a result of
stdcall name mangling.
You can declare global labels with the
GLOBAL directive in nasm.