ASM and BASIC sample comparison (and FN/XOR example)

Get technical support from the community & developers with specific X16 programs if you can't find the solution elsewhere
(for general non-support related chat about programs please comment directly under the program in the software library)
Post Reply
voidstar
Posts: 368
Joined: Thu Apr 15, 2021 8:05 am

ASM and BASIC sample comparison (and FN/XOR example)

Post by voidstar »

For a reference example, I came across the entropy example in the X16 ref docs, and decided to show what the X16 BASIC equivalent of that would look like. Maybe helps those not familiar with BASIC...

10 REM THROW A 6-SIDED DIE 20 SYS $FECF:REM JSR ENTROPY-GET 30 A=PEEK($030C):REM STORE REG .A RESULT 40 X=PEEK($030D):REM STORE REG .X RESULT 50 Y=PEEK($030E):REM STORE REG .Y RESULT 60 REM A = A XOR X 70 A=(A OR X) AND NOT (A AND X) 80 REM A = A XOR Y 90 A=(A OR Y) AND NOT (A AND Y) 100 AZ=A:REM STORE COPY OF A 110 REM A = A >> 4 (LSR, LOGICAL SHIFT RIGHT) 120 A=INT(A/16) 130 REM A = A XOR AZ 140 A=(A OR AZ) AND NOT (A AND AZ):REM A IS NOW 4-BIT 150 A=A AND $07:REM KNOCK OFF THE TOP BIT, NOW 3-BIT 160 IF A=0 GOTO 20:REM ZERO IS INVALID ROLL VALUE 170 IF A=7 GOTO 20:REM SEVEN IS INVALID ROLL VALUE 180 A=A OR $30:REM I.E. ASCII 48 + NUMERIC INDEX REM DONE, NOW SHOW THE OUTPUT... 190 POKE $030C,A:REM SET REG .A TO OUTPUT VALUE 200 SYS $FFD2:REM JSR OUTPUT CHAR IN REG.A 210 RETURN REM DO A LITTLE TEST CASE LOOP... 1000 FOR I = 1 TO 20 1010 GOSUB 10:PRINT "/";:PRINT CHR$(A);" "; 1020 NEXT I
Attachments
asm_and_basic.png
asm_and_basic.png (42.98 KiB) Viewed 2360 times
Last edited by voidstar on Fri Nov 17, 2023 6:28 am, edited 1 time in total.
voidstar
Posts: 368
Joined: Thu Apr 15, 2021 8:05 am

Re: ASM and BASIC sample comparison (and FN example)

Post by voidstar »

Realized this is also a good example to show how to make use of the BASIC FN keyword. Especially as this BASIC doesn't have a XOR keyword

FN is fairly limited (single line only, cannot use ":" for multiple statement function, single argument only). The defined function name (XO in this sample, for XOR) seems to be separate from the XO variable (i.e. defining a FN doesn't consume a variable name, but the FN names have the same variable name restrictions {e.g. two letters}).

5 DEF FN XO(Z) = (A OR Z) AND NOT (A AND Z) 10 REM THROW A 6-SIDED DIE 20 SYS $FECF:REM JSR ENTROPY-GET 30 A=PEEK($030C):REM STORE REG .A RESULT 40 X=PEEK($030D):REM STORE REG .X RESULT 50 Y=PEEK($030E):REM STORE REG .Y RESULT 70 A=FN XO(X):REM A = A XOR X 90 A=FN XO(Y):REM A = A XOR Y 100 AZ=A:REM STORE COPY OF A 110 REM A = A >> 4 (LSR, LOGICAL SHIFT RIGHT) 120 A=INT(A/16) 130 REM A = A XOR AZ 140 A=FN XO(AZ):REM A IS NOW 4-BIT 150 A=A AND $07:REM KNOCK OFF THE TOP BIT, NOW 3-BIT 160 IF A=0 GOTO 20:REM ZERO IS INVALID ROLL VALUE 170 IF A=7 GOTO 20:REM SEVEN IS INVALID ROLL VALUE 180 A=A OR $30:REM I.E. ASCII 48 + NUMERIC INDEX 190 POKE $030C,A:REM SET REG .A TO OUTPUT VALUE 200 SYS $FFD2:REM JSR OUTPUT CHAR IN REG.A 210 RETURN 1000 FOR I = 1 TO 20 1010 GOSUB 5:PRINT "/";:PRINT CHR$(A);" "; 1020 NEXT I
Attachments
basic_entropy_fn_sample.png
basic_entropy_fn_sample.png (14.66 KiB) Viewed 2355 times
Post Reply