diff --git a/examples/primes.sasm b/examples/primes.sasm index 0144dd3..abddb11 100644 --- a/examples/primes.sasm +++ b/examples/primes.sasm @@ -1,4 +1,4 @@ -# memory structure prt_next_val, counter, current_number, numbers... +# memory structure: prt_next_val, counter, current_number, numbers... set 0x03 rgp write @@ -72,9 +72,103 @@ set 0x01 rgl goto # goto the part that increases the current value +# -- exit jump to avoid undefined behaviour +set 0xFF rgl# 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 +goto # -- skips to the next number label 0x01 set 0x02 rgp diff --git a/examples/rainbow.sasm b/examples/rainbow.sasm new file mode 100644 index 0000000..b72dc45 --- /dev/null +++ b/examples/rainbow.sasm @@ -0,0 +1,95 @@ +set 0xFF rcs +set 0xFF rcb + +# increase red +label 0x01 +set 0xFF rgd +send +pause +pause +copy rcr rgd +set 0x0F rgi +add +copy rgo rcr +copy rgo rgd +set 0xFF rgi +set 0x01 rgl +jl + +# decrease blue +label 0x06 +set 0xFF rgd +send +pause +pause +set 0x0F rgi +copy rcb rgd +sub +copy rgo rcb +copy rgo rgd +clear rgi +set 0x06 rgl +jg + +# increase green +label 0x02 +set 0xFF rgd +send +pause +pause +set 0x0F rgi +copy rcg rgd +add +copy rgo rcg +copy rgo rgd +set 0xFF rgi +set 0x02 rgl +jl + +# decrease red +label 0x03 +set 0xFF rgd +send +pause +pause +set 0x0F rgi +copy rcr rgd +sub +copy rgo rcr +copy rgo rgd +clear rgi +set 0x03 rgl +jg + +# increase blue +label 0x04 +set 0xFF rgd +send +pause +pause +set 0x0F rgi +copy rcb rgd +add +copy rgo rcb +copy rgo rgd +set 0xFF rgi +set 0x04 rgl +jl + +# decrease green +label 0x05 +set 0xFF rgd +send +pause +pause +set 0x0F rgi +copy rcg rgd +sub +copy rgo rcg +copy rgo rgd +clear rgi +set 0x05 rgl +jg + +set 0x01 rgl +goto \ No newline at end of file