# Program Segments

A program generally has several different uses for memory. The most obvious use is for code which is stored in an area of memory referred to as the "text" segment identified as ".text". In an assembly program you switch to the data segment using

            segment .text

You can also use the keyword "section" rather than segment.

## Defined data

The segment for storing data with predefined values is the "data" segment identified as ".data":

            segment .data

You can use this to store data like a counter or a printf format:

            segment .data
    i:      dd      1
    format: db      "The count in %d", 0x0a, 0 ; C string with newline

## Reserved data

Part of the data for a program is for some memory with data initialized to all zeroes. This is called the "bss" segment which stands for "block started by symbol". Typically this data will be used for global data which will be written by the program when it executes.

            segment .bss

You define variables in the bss segment using a collection of pseudo-ops beginning with the prefix "res" meaning "reserve":

            segment .bss
    sum:    resw    1       ; reserve 1 word of memory
    data:   resd    10      ; reserve 10 double words (4 bytes each)
    name:   resb    17      ; reserve bytes for a C string
    avg:    resq    1       ; reserve 1 quad-word (8 bytes)

## Stack

A program uses a run-time stack to implement function calls. There is no explicit reference required to reference a stack segment in assembly language. The stack is used for function call frames which hold pointers to previous call frames, parameters, local variables and the return address.

## Heap

When a program allocates memory it uses a region of memory commonly referred to as the heap. Like the stack there is no reason to explicitly refer to the heap segment. Instead a program can call a function like malloc receiving an address from the heap which it will generally need to save somewhere in memory. The pointer to the allocated memory could be stored in a global variable is the data or bss segment, in a local variable in a function's call frame or possibly in a data structure which itself was dynamically allocated.