From b3e731e9da1825d9601f4c239d9841e6d8ddcf1c Mon Sep 17 00:00:00 2001 From: starcells Date: Fri, 12 Jun 2026 21:07:53 +0800 Subject: [PATCH] Full Adder!!! --- asm/add.r8asm | 87 ++++++++++++++++++++++++++++++++++++ asm/and.r8asm | 62 +++++++++++++++++++------- asm/asm.r8asm | 27 +++++++----- asm/lib.r8asm | 83 ++++++++++++++++++++-------------- asm/or.r8asm | 70 +++++++++++++++++++++-------- asm/xor.r8asm | 88 +++++++++++++++++++++++++++++++++++++ include/r8asm/r8asm_core.hh | 1 + src/r8asm_parse.cc | 2 + tests/r8_test.cc | 4 ++ 9 files changed, 345 insertions(+), 79 deletions(-) create mode 100644 asm/add.r8asm create mode 100644 asm/xor.r8asm diff --git a/asm/add.r8asm b/asm/add.r8asm new file mode 100644 index 0000000..3522a01 --- /dev/null +++ b/asm/add.r8asm @@ -0,0 +1,87 @@ +; Macro bit_add +; %1: Unit A +; %2: Unit B +; %3: Unit C +; %4: S +; %5: Ci +%macro bit_add 5 +.anchor before_bit_add + +.tp %1 +bit_copy_to_buffer %5 +stp +.anchor bit_add_buf_1 +.tp %2 +bit_copy_to_buffer %5 +stp +.anchor bit_add_buf_2 + +.tp %3 +stp +.anchor bit_add_buf_3 +.tp %4 +set_0 +.tp %5 +set_0 + +bit_unsafe_xor bit_add_buf_1 bit_add_buf_2 %5 +.tp %3 +bit_copy_to_buffer %4 +bit_unsafe_and bit_add_buf_3 %5 %4 +.tp %1 +bit_copy_to_buffer bit_add_buf_3 +.tp %2 +bit_copy_to_buffer bit_add_buf_3 +bit_unsafe_and bit_add_buf_1 bit_add_buf_2 bit_add_buf_3 +bit_unsafe_or bit_add_buf_3 %4 %5 + +.tp %1 +bit_copy_to_buffer %4 +.tp %2 +bit_copy_to_buffer %4 +bit_unsafe_xor bit_add_buf_1 bit_add_buf_2 %4 +.tp %3 +bit_copy_to_buffer bit_add_buf_1 +bit_unsafe_xor %4 bit_add_buf_3 bit_add_buf_1 +.tp bit_add_buf_1 +bit_move_to %4 + +.tp before_bit_add +%endmacro + + +; Macro add +; %1: Unit A +; %2: Unit B +; %3: Unit C +; %4: S +; %5: Unit Ci +%macro add 5 +.anchor before_add + + +.tp %3 +bit_copy_to_buffer %5 +stp +.anchor add_buf_1 +bit_move_to %5 + +%repeat _UNIT_SIZE +bit_add %1 %2 %5 %4 add_buf_1 + +.tp add_buf_1 +bit_move_to %5 + +.tp %1 +ror +.tp %2 +ror +.tp %4 +ror +%endrepeat + +.tp add_buf_1 +bit_move_to %5 + +.tp before_add +%endmacro \ No newline at end of file diff --git a/asm/and.r8asm b/asm/and.r8asm index 8ec7ccf..7310998 100644 --- a/asm/and.r8asm +++ b/asm/and.r8asm @@ -1,3 +1,29 @@ +; Macro bit_unsafe_and +; %1: arg1 +; %2: arg2 +; %3: ans +%macro bit_unsafe_and 3 +.anchor before_bit_unsafe_and +.tp %3 +set_0 +.tp %1 +biz + .tp %2 + biz + .tp %3 + set_1 + .tp %2 + set_0 + rnz + .tp %1 + set_0 +rnz + +.tp before_bit_unsafe_and +%endmacro + + + ; Macro unsafe_and ; %1: arg1 ; %2: arg2 @@ -6,21 +32,7 @@ .anchor before_unsafe_and %repeat _UNIT_SIZE - .tp %3 - set_0 - .tp %1 - biz - .tp %2 - biz - .tp %3 - set_1 - .tp %2 - set_0 - rnz - .tp %1 - set_0 - rnz - + bit_unsafe_and %1 %2 %3 .tp %1 ror .tp %2 @@ -34,6 +46,26 @@ +; Macro bit_and +; %1: Unit arg1 +; %2: Unit arg2 +; %3: ans +%macro bit_and 3 +.anchor before_and +.tp %1 +bit_copy_to_buffer %3 +stp +.anchor bit_and_buf_1 +.tp %2 +bit_copy_to_buffer %3 +stp +.anchor bit_and_buf_2 +bit_unsafe_and bit_and_buf_1 bit_and_buf_2 %3 +.tp before_and +%endmacro + + + ; Macro and ; %1:unit1 ; %2:unit2 diff --git a/asm/asm.r8asm b/asm/asm.r8asm index 1ac31e3..a2f2879 100644 --- a/asm/asm.r8asm +++ b/asm/asm.r8asm @@ -1,17 +1,20 @@ -step 3 -.xor 13 -.anchor arg1 -step 1 -.xor 7 -.anchor arg2 -back 2 -.anchor ans +.xor 123 -or arg1 arg2 ans +.tp 2 +.xor 23 -.tp arg1 +.tp 4 +flb + +add 0 2 4 6 8 + +.tp 0 out -.tp arg2 +.tp 2 out -.tp ans +.tp 4 +out +.tp 6 +out +.tp 8 out \ No newline at end of file diff --git a/asm/lib.r8asm b/asm/lib.r8asm index 132e9dc..4f221fb 100644 --- a/asm/lib.r8asm +++ b/asm/lib.r8asm @@ -29,48 +29,65 @@ ror %endrepeat %endmacro +%macro bit_move_to 1 +.anchor before_bit_move_to +.tp %1 +set_0 +.tp before_bit_move_to +biz + .tp %1 + flb + .tp before_bit_move_to + flb +rnz +.tp before_bit_move_to +%endmacro + + +; Macro bit_copy_to_buffer. +; %1: tmp + +%macro bit_copy_to_buffer 1 +.anchor before_bit_copy_to_buffer + +stp +set_0 +.tp %1 +set_0 +.tp before_bit_copy_to_buffer +biz + flb + stp + flb + .tp %1 + flb + .tp before_bit_copy_to_buffer +rnz +.tp %1 +biz + flb + .tp before_bit_copy_to_buffer + flb + .tp %1 +rnz + +.tp before_bit_copy_to_buffer +%endmacro + + ; Macro copy_to_buffer. ; %1: tmp %macro copy_to_buffer 1 .anchor before_copy_to_buffer -stp -erase -.tp %1 -erase -.tp before_copy_to_buffer %repeat _UNIT_SIZE -biz + bit_copy_to_buffer %1 + ror stp - set_1 + ror .tp %1 - set_1 + ror .tp before_copy_to_buffer - flb -rnz -.tp before_copy_to_buffer -ror -stp -ror -.tp %1 -ror -.tp before_copy_to_buffer %endrepeat - -.tp %1 -%repeat _UNIT_SIZE -biz - .tp before_copy_to_buffer - set_1 - .tp %1 - flb -rnz -.tp before_copy_to_buffer -ror -.tp %1 -ror -%endrepeat - -.tp before_copy_to_buffer %endmacro diff --git a/asm/or.r8asm b/asm/or.r8asm index 9a1e940..9c26bd8 100644 --- a/asm/or.r8asm +++ b/asm/or.r8asm @@ -1,3 +1,31 @@ +; Macro bit_unsafe_or +; %1: arg1 +; %2: arg2 +; %3: ans +%macro bit_unsafe_or 3 +.anchor before_bit_unsafe_or +.tp %3 +set_0 +.tp %1 +biz + .tp %3 + flb + .tp %1 + flb +rnz +.tp %2 +biz + .tp %3 + set_1 + .tp %2 + flb +rnz + +.tp before_bit_unsafe_or +%endmacro + + + ; Macro unsafe_or ; %1: arg1 ; %2: arg2 @@ -6,23 +34,7 @@ .anchor before_unsafe_or %repeat _UNIT_SIZE - .tp %3 - set_0 - .tp %1 - biz - .tp %3 - set_1 - .tp %1 - set_0 - rnz - .tp %2 - biz - .tp %3 - set_1 - .tp %2 - set_0 - rnz - + bit_unsafe_or %1 %2 %3 .tp %1 ror .tp %2 @@ -36,6 +48,26 @@ +; Macro bit_or +; %1: Unit arg1 +; %2: Unit arg2 +; %3: ans +%macro bit_or 3 +.anchor before_or +.tp %1 +bit_copy_to_buffer %3 +stp +.anchor bit_or_buf_1 +.tp %2 +bit_copy_to_buffer %3 +stp +.anchor bit_or_buf_2 +bit_unsafe_or bit_or_buf_1 bit_or:_buf_2 %3 +.tp before_or +%endmacro + + + ; Macro or ; %1:unit1 ; %2:unit2 @@ -45,11 +77,11 @@ .tp %1 copy_to_buffer %3 -stp ;Before r8asm_arg_stoi suppports evaluating,it's a compromise. +stp ;Before r8asm_arg_stoi suppports evaluating,it's a compromise. .anchor or_buf_1 ; .tp %2 copy_to_buffer %3 -stp ; +stp ; .anchor or_buf_2 ; unsafe_or or_buf_1 or_buf_2 ans .tp before_or diff --git a/asm/xor.r8asm b/asm/xor.r8asm new file mode 100644 index 0000000..67b9fef --- /dev/null +++ b/asm/xor.r8asm @@ -0,0 +1,88 @@ +; Macro bit_unsafe_xor +; %1: arg1 +; %2: arg2 +; %3: ans +%macro bit_unsafe_xor 3 +.anchor before_bit_unsafe_xor +.tp %3 +set_0 +.tp %1 +biz + .tp %3 + flb + .tp %1 + set_0 +rnz +.tp %2 +biz + .tp %3 + flb + .tp %2 + set_0 +rnz + +.tp before_bit_unsafe_xor +%endmacro + + + +; Macro unsafe_xor +; %1: arg1 +; %2: arg2 +; %3: ans_unit +%macro unsafe_xor 3 +.anchor before_unsafe_xor + +%repeat _UNIT_SIZE + bit_unsafe_xor %1 %2 %3 + .tp %1 + ror + .tp %2 + ror + .tp %3 + ror +%endrepeat +.tp before_unsafe_xor +%endmacro + + + + +; Macro bit_xor +; %1: Unit arg1 +; %2: Unit arg2 +; %3: ans +%macro bit_xor 3 +.anchor before_xor +.tp %1 +bit_copy_to_buffer %3 +stp +.anchor bit_xor_buf_1 +.tp %2 +bit_copy_to_buffer %3 +stp +.anchor bit_xor_buf_2 +bit_unsafe_xor bit_xor_buf_1 bit_xor_buf_2 %3 +.tp before_xor +%endmacro + + + +; Macro xor +; %1: Unit arg1 +; %2: Unit arg2 +; %3: ans +%macro xor 3 +.anchor before_xor + +.tp %1 +copy_to_buffer %3 +stp ;Before r8asm_arg_stoi suppports evaluating,it's a compromise. +.anchor xor_buf_1 ; +.tp %2 +copy_to_buffer %3 +stp ; +.anchor xor_buf_2 ; +unsafe_xor xor_buf_1 xor_buf_2 ans +.tp before_xor +%endmacro \ No newline at end of file diff --git a/include/r8asm/r8asm_core.hh b/include/r8asm/r8asm_core.hh index 41f6938..af9bdc8 100644 --- a/include/r8asm/r8asm_core.hh +++ b/include/r8asm/r8asm_core.hh @@ -37,6 +37,7 @@ enum class r8asm_builtin { ENDMACRO, REPEAT, ENDREPEAT, + INCLUDE, }; using R8Operand = std::variant; diff --git a/src/r8asm_parse.cc b/src/r8asm_parse.cc index 8dd9f53..bac2b8f 100644 --- a/src/r8asm_parse.cc +++ b/src/r8asm_parse.cc @@ -1,4 +1,5 @@ #include "r8asm/r8asm_parse.hh" +#include "r8asm/r8asm_core.hh" #include "r8asm/r8asm_macro.hh" #include @@ -15,6 +16,7 @@ std::map r8asm_parsemap = { {"%endmacro", r8asm_builtin::ENDMACRO}, {"%repeat", r8asm_builtin::REPEAT}, {"%endrepeat", r8asm_builtin::ENDREPEAT}, + {"%include",r8asm_builtin::INCLUDE},//TODO:Expand include }; void r8asm_trim(std::string &str) { // Clean empty chars in the string. diff --git a/tests/r8_test.cc b/tests/r8_test.cc index 7ec9b18..fa998cc 100644 --- a/tests/r8_test.cc +++ b/tests/r8_test.cc @@ -7,6 +7,8 @@ int main(int argc, char *argv[]) { auto lib = read_src("asm/lib.r8asm"); auto r8and = read_src("asm/and.r8asm"); auto r8or = read_src("asm/or.r8asm"); + auto r8xor = read_src("asm/xor.r8asm"); + auto r8add = read_src("asm/add.r8asm"); /* for (auto i : lib.raw_src) for (auto j : i) @@ -14,6 +16,8 @@ int main(int argc, char *argv[]) { lib.preprocess(); r8and.preprocess(); r8or.preprocess(); + r8xor.preprocess(); + r8add.preprocess(); auto src = read_src("asm/asm.r8asm"); auto out_tape = r8asm_tape_out(expand_macros(r8asm_preprocess(src))); for (auto i : out_tape)