Skip to content

Instantly share code, notes, and snippets.

@0x61nas
Last active February 16, 2024 16:26
Show Gist options
  • Save 0x61nas/b7c011bd4ac18408757551ba2bfadc3d to your computer and use it in GitHub Desktop.
Save 0x61nas/b7c011bd4ac18408757551ba2bfadc3d to your computer and use it in GitHub Desktop.

Ordinary user-space programs that written in NASM for Linux are divided into three sections: .data, .bss, and .text. The older in which these sections fall in your program really doesn't matter, but by convention the .data section comes first, followed by .bss section and then the .text section.

.data

The .data section contains data deffinitions of initialized data items.

Initialized data is data that has a value before the program begins running. These values are part of the executable file. They are loaded into memory when the executable file is loaded into memrory for execution.

Note

That you don't load the data defined in .data section manually, and no machine cycles are used in there certion beyond what it takes to load the program as a whole into memory.

Remember that the .data section affect your executable file size. And the more your executable file size the more time the kernel takes to load it into memory.

Examples

MyByte    db  07h                   ; 8 bits
MyWord    dw  0ffffh                ; 16 bits
MyDouble  dd  0b8000000h            ; 32 bits
MyQuad    dq  08ffff_ffff_ffff_fffh ; 64 bits

Think of DB directive as "Define Byte", DW as "Define Word", DD as "Define Double", and DQ as "Define Quad".

A string is just a sequence of bytes (a.k.a. characters), all in a row in memory.

secrit: db "ISLS"
btw: db "I ", "use Arch", 10
why: db 'am still "here"?', 10

Note

You can combine several separate substrings into a single string variable by separating the substrings with commas.

Note

The end of line (EOL) character has the numeric value of 10 decimal, or 0AH.

.bss

The Block Start Symbol (.bss) section, some times called Buffer Start Sympol section. Is used to allocate blocks of memory to be used later and give those blocks names.

There's a crucial difference between data items defined in the .data section and those who defined in the .bss section: Data items in the .data section add to the size of your executable file. While data items in the .bss do not.

Note

Data items defined in .bss add about 50 bytes to the executable size regards of there actual size.

.text

The actual machine instructions that make up your program go into the .text section.

Ordinarily, there are no data items defined in .text. The .text section contains symbols called labels that identify lodations in the program code for jumps and calls.

Note

All global labels must be declared in the .text section, or the labels cannot be seen outside your proram.

Labels

A label is a sort of bookmark, descriibing a place in the program code and giving it a name that's easier to remember than a naked memory address.

Labels are used to indicate the place where jump instructions should jump to and give names to callable assembly procedures.

Note

Labels must begin with a letter or else with an underscore, period, or question mark. These last three have a special meanings to NASM.

Note

Labels are case sensitive.

Examples

section .data
section .bss
section .text
  global _start
_start:
  mav al, 8
  work:
    dec al
    ; do something...
    jnz work
  xor edi, edi
  mov eax, 60 ; exit syscall
  syscall

Note

The _start label is a little bit special, because the linker need it to indnctes where the program begins. And it must be marked as global at the top ou the .text section.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment