You are on page 1of 2

- Process run a high level program on computer can be broken into following steps -

- **High Level program** is compiled by *compiler* => **Assembly Code**


- **Assembly Code** is translated by *Assembler* => **Object Code**
- **Object Code** + **Library code/Object from other files** is linked together by
*Linker* => **Executable**
- **Executable** is loaded by *Loader* in => Memory
- Once in the memory, the processor PC can be pointed to the starting address of the program code
to start execution.
### The Memory Map
- With 32-bit addresses, the RISC-V address space spans 4 GB
- Word address are multiples of 4 and range from 0x0000_0000 to 0xFFFF_FFFC
- Memory Maps divides the avalible memory in 5 segments ->
- the text,
- global data,
- dynamic data segment,
- exception handler
- OS, which include I/O segment
* RISC-V does not define a specfic memory map, but
* Exception handler are typically placed at either low or high addresses
* user (system designer?) can define where the text, memory I/O, stack and global data are
placed.
* This is to allow flexibility, specially for smaller handheld devices where the complete 5 GB
might be populated.
- Example memory map -
```mono
Address Segment
|-------------------------|
0xFFFF_FFFC | |
| Operating System & IO |
0xC000_0000 | |
|-------------------------|
0xBFFF_FFF0 | Stack (grows down) | <- sp
| Dynamic Data |
0x1000_1000 | Heap (grows up ) |
|-------------------------|
0x1000_0FFC | |
| Global Data | <- gp (0x1000_0800)
0x1000_0000 | |
|-------------------------|
| |
| Text |
0x0001_0000 | | <- PC
|-------------------------|
| |
| Exception Handlers |
0x0000_0000 | |
|-------------------------|
```
**The Text Segment**
- Stored the machine language user program
- It also includes any literals (constants) and read-only data
**The Global Data Segment**
* 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