From 6d08ffeee63a9b4d114542eb9ee59589eed573d2 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Thu, 31 Jul 2025 17:10:06 +0300 Subject: feat(isprime): Print numbers and their prime results for a (dynamic) range of values --- isprime/kernal.oph.s | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) (limited to 'isprime/kernal.oph.s') diff --git a/isprime/kernal.oph.s b/isprime/kernal.oph.s index a9fd007..b39d212 100644 --- a/isprime/kernal.oph.s +++ b/isprime/kernal.oph.s @@ -2,13 +2,90 @@ ; print [null-terminated string start address] .macro print + txa ; backup = x + pha ldx #0 ; x = 0 _loop: lda _1, x ; while (_1[x] != 0) { beq _done jsr putchar ; putchar(_1[x]) inx ; ++x bne _loop ; if (x == 0) break } This allows max 255 character to be read - _done: + _done: pla ; x = backup + tax +.macend + +printx: +.scope + txa ; a = x + cmp #10 ; if (a < 10) + bcc _1digit + cmp #100 ; if (a < 100) + bcc _2digit + +_3digit: pha + `div 100 + clc + adc #'0 + jsr putchar + pla + `mod 100 + +_2digit: pha + `div 10 + clc + adc #'0 + jsr putchar + pla + `mod 10 + +_1digit: clc + adc #'0 + jsr putchar + rts +.scend + +; a = a % _1 +.macro mod ; 10 cycles (2+2+2+2+2) raw + cmp #_1 ; if (a < _1) return + bcc _end + + sec ; carry = 1 +_loop: sbc #_1 ; do a -= _1 + !carry + beq _end ; if (a == 0) break + cmp #_1 ; while (a >= _1) + bcs _loop +_end: +.macend + +buffer: .byte 0 + +; a = a / _1 +.macro div + ora #0 ; The CMP (and friends) instruction changes the carry and zero flag, + ; which can cause the flags (and therefore the branch instructions) + ; to get "desynced" with the actual accumulator value. + ; With this instruction, we force a "refresh" of the flags. + ; It took me half an hour to figure this out... + + beq _return ; if (a == 0) return 0 + cmp #_1 ; if (a < _1) return 0 + bcc _zero + + stx buffer ; buffer = x + ldx #0 ; x = 0 + sec ; carry = 1 +_loop: sbc #_1 ; a -= _1 + !carry + inx ; ++x + beq _end ; if (a == 0) break + cmp #_1 ; while (a >= _1) + bcs _loop + +_end: txa ; a = x + ldx buffer ; x = buffer + jmp _return + +_zero: lda #0 +_return: .macend .macro lowercase -- cgit v1.3.1-26-gb76a