Update assembler/usage
-117
@@ -774,120 +774,3 @@ include print "../resources/dsa/print.dsa"
|
|||||||
include fib "../resources/dsa/fib.dsa"
|
include fib "../resources/dsa/fib.dsa"
|
||||||
```
|
```
|
||||||
|
|
||||||
## Complete Example Program
|
|
||||||
|
|
||||||
Here's a complete example showing various instruction usage, including label-based memory operations:
|
|
||||||
|
|
||||||
```assembly
|
|
||||||
include print "../resources/dsa/print.dsa"
|
|
||||||
include fib "../resources/dsa/fib.dsa"
|
|
||||||
|
|
||||||
; Data definitions
|
|
||||||
dw stack: 0x10000
|
|
||||||
dw display: 0x20000
|
|
||||||
dw current: 0x20000
|
|
||||||
db message: "Computing Fibonacci sequence...", 0
|
|
||||||
db result_buffer: 0, 0, 0, 0 ; Space for 32-bit result
|
|
||||||
dh config_values: 100, 200, 300
|
|
||||||
|
|
||||||
init:
|
|
||||||
; Load stack address using label expansion
|
|
||||||
ldw stack, bpr ; Expands to: lli stack,bpr; lui stack,bpr; ldw bpr,bpr
|
|
||||||
mov bpr, spr ; Set stack pointer
|
|
||||||
|
|
||||||
; Store display address to current using label expansion
|
|
||||||
ldw display, rg0 ; Load display address
|
|
||||||
stw rg0, current ; Expands to: lli current,rgf; lui current,rgf; stw rg0,rgf
|
|
||||||
|
|
||||||
start:
|
|
||||||
; Load message address for printing
|
|
||||||
lwi message, rg1 ; Load message address directly
|
|
||||||
|
|
||||||
; Print message
|
|
||||||
push rg1 ; Push message address
|
|
||||||
push pcx ; Push return address
|
|
||||||
jmp print::print ; Call print function
|
|
||||||
|
|
||||||
; Calculate 25th Fibonacci number
|
|
||||||
lli 25, rg0 ; Load 25 into rg0
|
|
||||||
push rg0 ; Push argument
|
|
||||||
push pcx ; Push return address
|
|
||||||
jmp fib::fib_n ; Call Fibonacci function
|
|
||||||
|
|
||||||
; Store result using label expansion
|
|
||||||
stw acc, result_buffer ; Expands to: lli result_buffer,rgf; lui result_buffer,rgf; stw acc,rgf
|
|
||||||
|
|
||||||
; Load and modify config value
|
|
||||||
ldh config_values, rg2, 2 ; Load second config value (offset 2)
|
|
||||||
; Expands to: lli config_values,rg2; lui config_values,rg2; ldh rg2,rg2,2
|
|
||||||
iadd rg2, 50 ; Add 50 to the config value
|
|
||||||
sth rg2, config_values, 2 ; Store back to second position
|
|
||||||
; Expands to: lli config_values,rgf; lui config_values,rgf; sth rg2,rgf,2
|
|
||||||
|
|
||||||
hlt ; Halt execution
|
|
||||||
|
|
||||||
; Example of label addressing in a loop
|
|
||||||
copy_data:
|
|
||||||
lli 0, rg0 ; Counter = 0
|
|
||||||
lli 10, rg1 ; Loop limit = 10
|
|
||||||
|
|
||||||
copy_loop:
|
|
||||||
; Load byte from source array
|
|
||||||
ldb source_array, rg2, rg0 ; Load source_array[counter]
|
|
||||||
; Note: This would need array indexing logic in practice
|
|
||||||
|
|
||||||
; Store to destination array
|
|
||||||
stb rg2, dest_array, rg0 ; Store to dest_array[counter]
|
|
||||||
|
|
||||||
inc rg0 ; counter++
|
|
||||||
cmp rg0, rg1 ; Compare with limit
|
|
||||||
jlt copy_loop ; Continue if less than limit
|
|
||||||
|
|
||||||
jmp 4, ret ; Return
|
|
||||||
|
|
||||||
; Data arrays for copy example
|
|
||||||
db source_array: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
|
|
||||||
resb dest_array: 10 ; Reserve 10 bytes for destination
|
|
||||||
```
|
|
||||||
|
|
||||||
## Label Addressing Summary
|
|
||||||
|
|
||||||
**Load Instructions with Labels:**
|
|
||||||
|
|
||||||
- All load instructions (`ldb`, `ldbs`, `ldh`, `ldhs`, `ldw`) can use labels
|
|
||||||
- They expand into 3 instructions: `lli` → `lui` → original load instruction
|
|
||||||
- The destination register is used as a temporary for the address calculation
|
|
||||||
|
|
||||||
**Store Instructions with Labels:**
|
|
||||||
|
|
||||||
- All store instructions (`stb`, `sth`, `stw`) can use labels
|
|
||||||
- They expand into 3 instructions: `lli` → `lui` → original store instruction
|
|
||||||
- The `rgf` register is used as a temporary for the address calculation
|
|
||||||
- Source register remains unchanged
|
|
||||||
|
|
||||||
**Key Points:**
|
|
||||||
|
|
||||||
- Label expansion happens at assembly time, not runtime
|
|
||||||
- Offsets work the same way with labels as with direct register addressing
|
|
||||||
- The temporary register (`rgf` for stores) should not be used for other purposes during store operations
|
|
||||||
- Load operations modify the destination register during address calculation
|
|
||||||
|
|
||||||
## Register Usage
|
|
||||||
|
|
||||||
The DSA architecture includes several special registers:
|
|
||||||
|
|
||||||
- `acc` - Accumulator register
|
|
||||||
- `zero` - Always contains zero
|
|
||||||
- `pcx` - Program counter
|
|
||||||
- `spr` - Stack pointer
|
|
||||||
- `bpr` - Base pointer
|
|
||||||
- `ret` - Return address register
|
|
||||||
- `rg0`-`rg15` - General purpose registers
|
|
||||||
|
|
||||||
## Addressing Modes
|
|
||||||
|
|
||||||
1. **Register Direct**: `mov rg0, rg1`
|
|
||||||
2. **Immediate**: `lli 100, rg0`
|
|
||||||
3. **Memory Direct**: `ldw buffer, rg0`
|
|
||||||
4. **Memory with Offset**: `ldw rg0, rg1, 8`
|
|
||||||
5. **Label/Symbol**: `jmp start`
|
|
||||||
Reference in New Issue
Block a user