Jump to content
IGNORED

kenjennings' Blog - Part 8 of 11 -- Simple Assembly for Atari BASIC


RSS Bot

Recommended Posts

Convert Integer To Bit String

This machine language routine converts a 16-bit integer into a string of values representing 0 and 1 bits. This is useful for visualizing bits in a byte, word, or address for diagnostic purposes. This is practical on the Atari for displaying character set or player/missile graphics data in a human-readable way. This routine has a different number of arguments from the INT2HEX string conversion routine and so is presented on its own.


This routine has similar problems to solve as the INT2HEX routine. It must deal with the low byte stored in memory before the high byte, because it must output the bit values in order of highest bits first and lowest bits last. (e.g 258 or hex $0102 should be output as “0000000100000010” though it is stored as $02 $01).

Converting a byte to a string of bits is fairly easy in 6502. In general terms: test the high bit and output a “0” or a “1” as needed, then shift the bits of the byte and repeat until 8 bits have been extracted. The routine below includes a touch of customization – the USR() routine may also specify the characters (ASCII/ATASCII) used to represent “0” and “1”.

INT2BITS in Mac/65 Assembler Code

0475     LDA ZZERO0480     CMP ZONE0485     BEQ EXIT    ; zero == one

Unlike the hex conversion there is just one part to the bit conversion:
 
Now that we have routines to convert values into hex strings and bit representation, the test program for BITS can be revisited to provide improved diagnostic information.


Testing BITS Again (with INT2HEX and INT2BITS)

Below is the Atari BASIC TESTBITS.BAS program upgraded with the new conversion utilities to produce much more sensible output illustrating the changes in the bits:
;" ";BIT$(9,12);" ";BIT$(13,16)408 RETURN500 REM501 REM OUTPUT BYTE VALUE CONVERTED TO502 REM USEFUL FORMAT:503 REM $HEX = BITS BITS504 REM505 RH=USR(INT2HEX,VALUE,ADR(HX$))506 RB=USR(BIT2STR,VALUE,ADR(BIT$),AZERO,AONE)507 ? "$";HX$(3,4);" = ";BIT$(9,12);" ";BIT$(13,16)508 RETURN9997 REM9998 REM SETUP ML UTILITIES9999 REM10000 DIM BT$(162),B2S$(67),I2H$(72)10001 BITS=ADR(BT$)10002 RESTORE 27000:? "Loading BITS"10003 FOR I=0 TO 16110004 READ D:POKE BITS+I,D10005 NEXT I:?10006 BIT2STR=ADR(B2S$)10007 RESTORE 25000:? "Loading BIT2STR"10008 FOR I=0 TO 6610009 READ D:POKE BIT2STR+I,D10010 NEXT I:?10011 INT2HEX=ADR(I2H$)10012 RESTORE 26000:? "Loading INT2HEX"10013 FOR I=0 TO 7110014 READ D:POKE INT2HEX+I,D10015 NEXT I:?10016 RETURN24996 REM H1:INT2BITS.OBJ24997 REM SIZE  = 6724998 REM START = 3763224999 REM END   = 3769825000 DATA 169,0,133,212,133,213,104,24025001 DATA 57,10,168,201,8,240,5,10425002 DATA 136,208,252,96,104,153,213,025003 DATA 136,208,249,165,218,5,219,24025004 DATA 33,165,216,197,214,240,27,16225005 DATA 1,24,22,220,165,216,144,225006 DATA 165,214,145,218,200,192,8,24025007 DATA 4,192,16,208,236,202,16,23325008 DATA 230,212,9625996 REM H1:INT2HEX.OBJ25997 REM SIZE  = 7225998 REM START = 3788825999 REM END   = 3795926000 DATA 169,0,133,212,133,213,104,24026001 DATA 62,10,168,201,4,240,5,10426002 DATA 136,208,252,96,104,153,213,026003 DATA 136,208,249,5,215,240,40,21626004 DATA 162,1,181,216,74,74,74,7426005 DATA 145,214,181,216,41,15,200,14526006 DATA 214,200,202,16,237,136,177,21426007 DATA 201,10,144,2,105,6,105,4826008 DATA 145,214,136,16,241,230,212,9626996 REM H1:BITS.OBJ26997 REM SIZE  = 16226998 REM START = 3814426999 REM END   = 3830527000 DATA 169,0,133,212,133,213,104,24027001 DATA 43,10,168,201,6,240,5,10427002 DATA 136,208,252,96,104,153,213,027003 DATA 136,208,249,164,218,240,21,13627004 DATA 240,19,136,240,29,136,240,3927005 DATA 136,240,49,136,240,61,136,24027006 DATA 73,136,240,90,96,165,216,527007 DATA 214,133,212,165,217,5,215,13327008 DATA 213,96,165,216,37,214,133,21227009 DATA 165,217,37,215,133,213,96,16527010 DATA 216,69,214,133,212,165,217,6927011 DATA 215,133,213,96,165,216,133,21227012 DATA 166,214,240,208,74,202,208,25227013 DATA 133,212,96,165,216,133,212,16627014 DATA 214,240,193,10,202,208,252,13327015 DATA 212,96,165,216,133,212,166,21427016 DATA 240,178,24,106,144,2,9,12827017 DATA 202,208,247,133,212,96,165,21627018 DATA 133,212,166,214,240,158,24,4227019 DATA 144,2,9,1,202,208,247,13327020 DATA 212,96

Here is the kinder, gentler TESTBIT2 output with the values and bits illustrated:


Loading BITS

Loading BIT2STR

Loading INT2HEX

$0000 = 0000 0000 0000 0000
OR $0000 = 0000 0000 0000 0000
=== ===== ===================
$0000 = 0000 0000 0000 0000

$0101 = 0000 0001 0000 0001
OR $0000 = 0000 0000 0000 0000
=== ===== ===================
$0101 = 0000 0001 0000 0001

$0000 = 0000 0000 0000 0000
OR $0102 = 0000 0001 0000 0010
=== ===== ===================
$0102 = 0000 0001 0000 0010

$0101 = 0000 0001 0000 0001
OR $0102 = 0000 0001 0000 0010
=== ===== ===================
$0103 = 0000 0001 0000 0011

$0000 = 0000 0000 0000 0000
AND $0000 = 0000 0000 0000 0000
=== ===== ===================
$0000 = 0000 0000 0000 0000

$0101 = 0000 0001 0000 0001
AND $0000 = 0000 0000 0000 0000
=== ===== ===================
$0000 = 0000 0000 0000 0000

$0000 = 0000 0000 0000 0000
AND $0102 = 0000 0001 0000 0010
=== ===== ===================
$0000 = 0000 0000 0000 0000

$0101 = 0000 0001 0000 0001
AND $0102 = 0000 0001 0000 0010
=== ===== ===================
$0100 = 0000 0001 0000 0000

$0000 = 0000 0000 0000 0000
EOR $0000 = 0000 0000 0000 0000
=== ===== ===================
$0000 = 0000 0000 0000 0000

$0101 = 0000 0001 0000 0001
EOR $0000 = 0000 0000 0000 0000
=== ===== ===================
$0101 = 0000 0001 0000 0001

$0000 = 0000 0000 0000 0000
EOR $0102 = 0000 0001 0000 0010
=== ===== ===================
$0102 = 0000 0001 0000 0010

$0101 = 0000 0001 0000 0001
EOR $0102 = 0000 0001 0000 0010
=== ===== ===================
$0003 = 0000 0000 0000 0011

Testing LSR on Value $81 = 1000 0001
LSR 0 = $81 = 1000 0001
LSR 1 = $40 = 0100 0000
LSR 2 = $20 = 0010 0000
LSR 3 = $10 = 0001 0000
LSR 4 = $08 = 0000 1000
LSR 5 = $04 = 0000 0100
LSR 6 = $02 = 0000 0010
LSR 7 = $01 = 0000 0001
LSR 8 = $00 = 0000 0000

Testing LSL on Value $81 = 1000 0001
LSL 0 = $81 = 1000 0001
LSL 1 = $02 = 0000 0010
LSL 2 = $04 = 0000 0100
LSL 3 = $08 = 0000 1000
LSL 4 = $10 = 0001 0000
LSL 5 = $20 = 0010 0000
LSL 6 = $40 = 0100 0000
LSL 7 = $80 = 1000 0000
LSL 8 = $00 = 0000 0000

Testing ROR on Value $81 = 1000 0001
ROR 0 = $81 = 1000 0001
ROR 1 = $C0 = 1100 0000
ROR 2 = $60 = 0110 0000
ROR 3 = $30 = 0011 0000
ROR 4 = $18 = 0001 1000
ROR 5 = $0C = 0000 1100
ROR 6 = $06 = 0000 0110
ROR 7 = $03 = 0000 0011
ROR 8 = $81 = 1000 0001

Testing ROL on Value $81 = 1000 0001
ROL 0 = $81 = 1000 0001
ROL 1 = $03 = 0000 0011
ROL 2 = $06 = 0000 0110
ROL 3 = $0C = 0000 1100
ROL 4 = $18 = 0001 1000
ROL 5 = $30 = 0011 0000
ROL 6 = $60 = 0110 0000
ROL 7 = $C0 = 1100 0000
ROL 8 = $81 = 1000 0001

The output is considerably easier to understand. So neat, so organized, so obvious and informative!


Below are the source files and examples of how to load the machine language routine into BASIC included in the disk image and archive:


INT2BITS File List:

INT2BITS.M65 Saved Mac/65 source
INT2BITS.L65 Mac/65 source listing
INT2BITS.T65 Mac/65 source listed to H6: (linux)
INT2BITS.ASM Mac/65 assembly listing
INT2BITS.TSM Mac/65 assembly listing to H6: (linux)
INT2BITS.OBJ Mac/65 assembled machine language program (with load segments)
INT2BITS.BIN Assembled machine language program without load segments
INT2BITS.LIS LISTed DATA statements for INT2BITS.BIN routine.
INT2BITS.TLS LISTed DATA statements for INT2BITS.BIN routine to H6: (linux)

MAKEI2B.BAS BASIC program to create the INT2BITS.BIN file. This also contains the INT2BITS routine in DATA statements.
MAKEI2B.LIS LISTed version of MAKEI2B.BAS
MAKEI2B.TLS LISTed version of MAKEI2B.BAS to H6: (linux)

TESTI2B.BAS BASIC program that tests the INT2BITS USR() routines.
TESTI2B.LIS LISTed version of TESTI2B.BAS.
TESTI2B.TLS LISTed version of TESTI2B.BAS to H6: (linux)



ZIP archive of files:




Tar archive of files (remove the .zip after download)







The Lord will fulfill his purpose for me; your steadfast love, O Lord, endures forever. Do not forsake the work of your hands.
Psalm 138:8



Attached File(s)


http://atariage.com/forums/blog/576/entry-13183-part-8-of-11-simple-assembly-for-atari-basic/
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...