diff --git a/asm/arithmetic.asm b/asm/arithmetic.asm new file mode 100644 index 0000000..0db68bf --- /dev/null +++ b/asm/arithmetic.asm @@ -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 +} diff --git a/asm/macros.asm b/asm/flow.asm similarity index 59% rename from asm/macros.asm rename to asm/flow.asm index e61803f..a99333e 100644 --- a/asm/macros.asm +++ b/asm/flow.asm @@ -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 diff --git a/asm/util.asm b/asm/util.asm new file mode 100644 index 0000000..cb4d80b --- /dev/null +++ b/asm/util.asm @@ -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 +} diff --git a/src/assembler/include.rs b/src/assembler/include.rs index 9ab2be4..d5aa281 100644 --- a/src/assembler/include.rs +++ b/src/assembler/include.rs @@ -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 { diff --git a/tests/fib.asm b/tests/fib.asm index d2a1da5..44de80e 100644 --- a/tests/fib.asm +++ b/tests/fib.asm @@ -3,7 +3,8 @@ @org 0x0000 @define COUNT 7 -@include +@include +@include _start: mv A, COUNT