Skip to content

Commit

Permalink
organized built-in macros
Browse files Browse the repository at this point in the history
  • Loading branch information
commonkestrel committed Dec 19, 2023
1 parent a098c59 commit e4c289a
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 18 deletions.
26 changes: 26 additions & 0 deletions asm/arithmetic.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/// Adds two 16-bit integers
@macro add16 (%h0:reg, %l0:reg, %h1:reg|imm, %l1:reg|imm) {
add %l0, %l1
adc %h0, %h1
}

/// Subtracts two 16-bit integers
@macro sub16 (%h0:reg, %l0:reg, %h1:reg|imm, %l1:reg|imm) {
sub %l0, %l1
sbc %h0, %h1
}

/// Increments the given byte
@macro inc (%reg:reg) {
add %reg, 1
}

/// Decrements the given byte
@macro dec (%reg:reg) {
sub %reg, 1
}

/// Bitwise inverts the given byte
@macro not (%reg:reg) {
nand %reg, %reg
}
16 changes: 0 additions & 16 deletions asm/macros.asm → asm/flow.asm
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
/// Adds two 16-bit integers
@macro add16 (%h0:reg, %l0:reg, %h1:reg|imm, %l1:reg|imm) {
add %l0, %l1
adc %h0, %h1
}

/// Subtracts two 16-bit integers
@macro sub16 (%h0:reg, %l0:reg, %h1:reg|imm, %l1:reg|imm) {
sub %l0, %l1
sbc %h0, %h1
}

@macro dec (%reg:reg) {
sub %reg, 1
}

@macro jmp {
() {
jnz 1
Expand Down
63 changes: 63 additions & 0 deletions asm/util.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
@macro push {
; push two registers
(%r0:reg, %r1:reg) {
push %r0
push %r1
}
; push three registers
(%r0:reg, %r1:reg, %r2:reg) {
push %r0
push %r1, %r2
}
; push four registers
(%r0:reg, %r1:reg, %r2:reg, %r3:reg) {
push %r0
push %r1, %r2, %r3
}
; push five registers
(%r0:reg, %r1:reg, %r2:reg, %r3:reg, %r4:reg) {
push %r0
push %r1, %r2, %r3, %r4
}
; push six registers
(%r0:reg, %r1:reg, %r2:reg, %r3:reg, %r4:reg, %r5:reg) {
push %r0
push %r1, %r2, %r3, %r4, %r5
}
}

@macro pop {
; pop two registers
(%r0:reg, %r1:reg) {
pop %r0
pop %r1
}
; pop three registers
(%r0:reg, %r1:reg, %r2:reg) {
pop %r0
pop %r1, %r2
}
; pop four registers
(%r0:reg, %r1:reg, %r2:reg, %r3:reg) {
pop %r0
pop %r1, %r2, %r3
}
; pop five registers
(%r0:reg, %r1:reg, %r2:reg, %r3:reg, %r4:reg) {
pop %r0
pop %r1, %r2, %r3, %r4
}
; pop six registers
(%r0:reg, %r1:reg, %r2:reg, %r3:reg, %r4:reg, %r5:reg) {
pop %r0
pop %r1, %r2, %r3, %r4, %r5
}
}

@macro pusha () {
push A, B, C, D, E, F
}

@macro popa () {
pop F, E, D, C, B, A
}
4 changes: 3 additions & 1 deletion src/assembler/include.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use clio::Input;
use phf::{phf_map, Map};

static BUILT_INS: Map<&'static str, &str> = phf_map! {
"macros" => include_str!("../../asm/macros.asm"),
"arithmetic" => include_str!("../../asm/arithmetic.asm"),
"flow" => include_str!("../../asm/flow.asm"),
"util" => include_str!("../../asm/util.asm"),
};

pub fn include(path: Path) -> Result<TokenStream, Errors> {
Expand Down
3 changes: 2 additions & 1 deletion tests/fib.asm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
@org 0x0000
@define COUNT 7

@include <macros>
@include <arithmetic>
@include <flow>

_start:
mv A, COUNT
Expand Down

0 comments on commit e4c289a

Please sign in to comment.