TEMPLATE

RAM SEGMENTS

  • Stack: Local variables and function call management; grows downwards.

  • Heap: Dynamically allocated memory (e.g., via malloc); grows upwards.

  • .bss (block started by symbol - READ/WRITE): Stores uninitialized global/static variables (they get zero-initialized by OS).

  • .data (initialized data - READ/WRITE - NOT EXECUTABLE): Stores initialized global and static variables.

  • .text (code section - READ-ONLY - EXECUTABLE): Contains program instructions; usually at the lowest addresses.

X86: GCC METHOD

;*######################################################################################
; Dev: cnd.dev
; Program Name: FileName-v1.0.0-linux-x86-64
; Version: 1.0.0                                               //Major.Minor.Update
; Date: 181445MAR25
; Filename: filename.c
; Dependency: N/A
; Compile Cmd: gcc -m64 -O1 filename.c -o filename-v1.0.0-linux-x86-64
; Synopsis: ...
;  - Overview: describes what the program does, how it works, and its key components
;  - Technical: ...
;#####################################################################################*/

;INCLUDE FILES

section .data
    ; initialized data

section .bss
    ; uninitialized data

section .text
    global _start                              ; Entry point for the program (used by linker)

  _start:
    
    ; ********** FUNCTION PROLOGUE *********
    push	ebp                            ; Save base pointer of the previous stack frame
    mov		ebp, esp                       ; Establish a new stack frame for this function
    ; ********** CODE STARTS HERE **********

  end:
  
    ; *********** CODE ENDS HERE ***********
    mov		eax, 0                          ; Set return value to 0 (success)
    mov		esp, ebp                        ; Restore original stack pointer
    pop		ebp                             ; Restore previous base pointer
    ret                                         ; Return from _start (only valid if this is called as a function)

X64: NASM METHOD

;*######################################################################################
; Dev: cnd.dev
; Program Name: FileName-v1.0.0-linux-x86-64
; Version: 1.0.0                                               ; Major.Minor.Update
; Date: 181445MAR25
; Filename: filename.asm
; Dependency: N/A
; Compile Cmd: nasm -f elf64 filename.asm -o filename.o && ld filename.o -o filename-v1.0.0-linux-x86-64
; Synopsis:
;  - Overview: describes what the program does, how it works, and its key components
;  - Technical: ...
;######################################################################################*/

; SECTION: INCLUDE FILES
; Declare printf as an external function (from libc)

section .data
    ; Initialized data (e.g., strings, format specifiers)

section .bss
    ; Uninitialized data (e.g., buffers, counters)

; Code section (actual instructions)
section .text
    ; Make the _start label visible to the linker
    global _start
    
; Entry point of the program
_start:
    ; ********** CODE STARTS HERE **********
    
    ; Example: write "Hello\n" to stdout
    mov     rax, 1          ; syscall: write
    mov     rdi, 1          ; file descriptor: stdout
    mov     rsi, message    ; pointer to message
    mov     rdx, message_len ; message length
    syscall

    ; Exit
    mov     rax, 60         ; syscall: exit
    xor     rdi, rdi        ; status: 0
    syscall

    ; *********** CODE ENDS HERE ***********

; SECTION: DATA

section .data
    message     db "Hello, world!", 10
    message_len equ $ - message

Last updated