The aim of this task is to write functions (or create a class if your
language is Object Oriented and you prefer) for reading and writing sequences of
bits, most significant bit first. While the output of a asciiprint "STRING" is the ASCII byte sequence
"S", "T", "R", "I", "N", "G", the output of a "print" of the bits sequence
0101011101010 (13 bits) must be 0101011101010; real I/O is performed always
quantized by byte (avoiding endianness issues and relying on underlying
buffering for performance), therefore you must obtain as output the bytes
0101 0111 0101 0000 (bold bits are padding bits), i.e. in hexadecimal 57 50.

As test, you can implement a rough (e.g. don't care about error handling or
other issues) compression/decompression program for ASCII sequences
of bytes, i.e. bytes for which the most significant bit is always unused, so that you can write
seven bits instead of eight (each 8 bytes of input, we write 7 bytes of output).

These bit oriented I/O functions can be used to implement compressors and
decompressors; e.g. Dynamic and Static Huffman encodings use variable length
bits sequences, while LZW (see LZW compression) use fixed or variable words
nine (or more) bits long.

 Limits in the maximum number of bits that can be written/read in a single read/write operation are allowed.
 Errors handling is not mandatory


 
define StringRam $1200                    ;not actually used in the code, but it's here for clarity.
define z_BC $00                           ;fake Z80-style register for pseudo-16-bit operations.
define z_C $00                            ;6502 uses the low byte as the reference point for indirect lookups. 
define z_B $01                            ;high byte
define tempMath $02                       ;temp storage of input
define tempBitMask $03                    ;temp storage of the bit filter
 
lda #$12
sta z_B
lda #$00
sta z_C                                   ;load address $1200 into zero page memory for an indirect lookup.
 
lda #$0F                                  ;test value
LDY #0                                    ;initialize offset to zero
 
jsr Hex2BinAscii
brk                                       ;on easy6502 this terminates the program.
 
Hex2BinAscii:
sta tempMath                              ;store our input, in this case #$0F
lda #%10000000
sta tempBitMask         
 
loop_Hex2BinAscii:
lda tempMath                              ;load input into accumulator.
and tempBitMask                           ;filter out all bits except the one we are checking this pass of the loop
bne bitIsOne
	lda #$30                          ;ascii for zero
	bne StoreBit
bitIsOne:
	lda #$31                          ;ascii for one
StoreBit:
	sta (z_BC),y                      ;store in StringRam+Y
loopOverhead:
	iny                               ;y++
	lsr tempBitMask                   ;shift to next bit in sequence
	beq loop_Hex2BinAscii             ;if mask is zero, we are done. BCC would have worked here as well.
lda #$00                                  ;load the null terminator 
sta (z_BC),y	                          ;store the null terminator after the string
rts