Fast divisibility check in ZX81 BASIC

Since many Project Euler problems require you to check for divisibility quite a few times, I tried to figure out the fastest way to complete this task in the ZX81 BASIC.

So far I have compared (N/D)with INT(N/D)to check if it can be Ndivided by Dor not.
I was thinking of running a test in Z80 machine code , I still have not figured out how to use variables in BASIC in machine code.

How can this be achieved?

+3
source share
5 answers

, RANDOMIZE USR ZX81, , . , POKE RANDOMIZE USR.

, , ROM ZX Basic. , .

. , .

Sinclair. , ZX Spectrum

+2

, . , :

set accumulator to N
subtract D
if carry flag is set then it is not divisible
if zero flag is set then it is divisible
otherwise repeat subtraction until one of the above occurs

8- :

DIVISIBLE_TEST:
LD B,10
LD A,100

DIVISIBLE_TEST_LOOP:
SUB B
JR C, $END_DIVISIBLE_TEST
JR Z, $END_DIVISIBLE_TEST
JR $DIVISIBLE_TEST_LOOP

END_DIVISIBLE_TEST:
LD B,A
LD C,0
RET

USR. , USR , - - BC, , , - :

REM poke the memory addresses with the operands to load the registers
POKE X+1, D
POKE X+3, N
LET r = USR X
IF r = 0 THEN GOTO isdivisible
IF r <> 0 THEN GOTO isnotdivisible

, Z80, . , . Z80 , Spectrum, ZX81.

16- , . 16 , .

, DATA POKE. , , !

+7

. - , , .

(, ).

, ZX81 FAST.

+4

. Z80. .

, ( ) ZX Spectrum 48. , hw.;/

0

The problem with the Z80 machine code is that it does not have floating point operations (and does not have integer division or multiplication, for that matter). Implementing your own FP library in Z80 assembler is not trivial. Of course, you can use the BASIC built-in procedures, but then you can just stick with BASIC.

0
source

Source: https://habr.com/ru/post/1704787/


All Articles