You are on page 1of 1

* Holds global variable

* These are loaded in memory before the program starts


* All functions can access these variables
* Access is provided by having the `gp`, global pointer point to the middle of this segment, and with
12-bit signed offset, complete region can be accessed
**The Dynamic Data Segment**
- This segment contains the stack and heap
- Data in this is not know at start of the program, it is populated during run time, thus its called
Dynamic Data
- Stack is used by programs to store local variable. Save/Restore between function calls. Pass
arguments
- When the program starts, OS set the SP to the bottom of the stack, 0xBFFF_FFFF here.
- Heap store the data that the program allocated during run-time. `malloc` in C/C++, `new` in Java
- Heap and Stack grow towards each other, if they intersect, data corruption would occurs. Memory
allocator prevents this, and issues out-of-memory errors, if no more space is available.
**The Exception Handler, OS, and I/O Segments**
- Lowest part is revered for exception handler and boot code, that is run at start-up
- Highest part is reserved for operating system and memory-mapped I/Os
### Assembler Directives
- Assembler directives are keyword used in assembly programs to guide the assembler in allocating
& initializing global variable, defining constants, differentiating between code and data.
* The `.data`, `.text`, `.bss`, `.rodata` are assembler directives that tell assembler to places
proceeding data/code in global, text, BSS, or read-only data segments.
* BSS -> Block Started Symbol, this is initially a keyword to allocate a block of
uninitialized data. Now, most OS initialize data in BSS segment to 0.
- Common assembler directives -
| Assembler Directive | Description |
| ------------------- | --------------------------------- |
| `.text` | Text section, code & constants
| `.data` | Global data
| `.bss` | Global data initialized to 0
| `.section .foo` | Section named `.foo`
| `.align N` | Align next data/instruction on 2^N-byte boundary
| `.balign N` | Align next data/instruction on N-byte boundary
| `.globl sym` | Label sym is global
| `.string "str"` | Store string "str" in memory
| `.word w1, w2, ... wN` | Store N 32-bit values in successive memory words
| `.byte b1, b2, ... bN` | Store N 8-bit values in successive memory bytes
| `.space N` | Reserve N bytes to store variables
| `.equ name, constant` | Define a symbol name with value constant
| `.end` | End of assembly code

### Compiling
- Compiler translates high-level code into assembly language code.
- GCC by default produces the final executable, but we can stop it at compile step, resulting in
assembly file, as -
```
gcc -O1 -S prog.c -o prog.s
```
- `-S` instructs to compile, but not assemble or link
- `-O1` tells compiler to do basic optimization
### Assembling

You might also like