From 310a4f58d7fd7592c561f1ec9f1a8dbf3a79f113 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 3 May 2020 14:41:14 +0200 Subject: [PATCH] Add primes to the examples --- examples/primes.sasm | 95 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 examples/primes.sasm diff --git a/examples/primes.sasm b/examples/primes.sasm new file mode 100644 index 0000000..4f2f3b9 --- /dev/null +++ b/examples/primes.sasm @@ -0,0 +1,95 @@ +# memory structure prt_next_val, counter, current_number, numbers... + +set 0x03 rgp +write +set 0x02 rgp +set 0x03 rgd +write +set 0x01 rgp +set 0x02 rgd +write + +# loop through all numbers until the current one to check by divisibility +label 0x03 +set 0x01 rgp +load # load the current counter value +copy rgd rgi +set 0x02 rgp +load # load the current number +mod +copy rgo rgd # copy the modulus result to rgd +clear rgi +set 0x01 rgl +je # check if the result is 0 +set 0x01 rgp +load +set 0x01 rgi +add +copy rgo rgd +write # increase the counter by one and write it back +set 0x02 rgp +load # load the current number +set 0x02 rgi +nrt +set 0x01 rgp +load # load the current counter +copy rgd rgi +copy rgo rgd +set 0x04 rgl +je # jump to 0x04 if the counter has reached the square root of the current number +set 0x03 rgl +goto # repeat if the counter is still smaller than the current number + +# -- exit jump to avoid undefined behaviour +set 0xFF rgl +goto +# -- write the current number and load the next +label 0x04 +set 0x02 rgp +set 0xFF rgi +set 0xFF rgd +mul # calculate 0xFF * 0xFF +copy rgo rgi +load +set 0xFF rgl +jg # jump to the end if the current number is larger than 0xFF * 0xFF +set 0x00 rgp +load # load the current pointer value +set 0x01 rgi +add # increase the value by 1 +copy rgo rgd +write # write back the increased value +set 0x02 rgp +load # load the current number +print rgd +copy rgd rgo # temporarily store the number in rgo +set 0x00 rgp +load # load the pointer value +copy rgd rgp +copy rgo rgd # load the current number back into rgd +write # write the current number +set 0x01 rgl +goto # goto the part that increases the current value + + +# -- exit jump to avoid undefined behaviour +set 0xFF rgl +goto +# -- skips to the next number +label 0x01 +set 0x02 rgp +load # load the current number +set 0x02 rgi +add # add 2 to it +copy rgo rgd +write # write it back +set 0x01 rgp +set 0x02 rgd +write # set the counter back to 2 +set 0x03 rgl +goto # go back to the beginning + +label 0xFF +debug +clear rgd +exit rgd \ No newline at end of file