Bitwise IO: Difference between revisions

Line 3,418:
 
=={{header|Z80 Assembly}}==
 
===Uncompressing Packed Bytes into ASCII Zeroes and Ones===
<lang z80>StoreBinaryString:
;INPUT:
; HL = SOURCE ADDRESS
; DE = OUTPUT STRING RAM
; BC = HOW MANY BYTES OF SOURCE DATA TO CONVERT
ld a,(hl)
push bc
ld b,a ;backup a for later
ld c,%10000000 ;a "revolving bit mask" is used to compare
; each bit of A, in sequence.
loop:
ld a,b ;restore A
and c
ld a,'0' ;get ascii 0 into A. This does not affect the flags!
jr z,skip ;this jump is based on the result of B AND C
inc a ;convert ascii 0 into ascii 1, only if B AND C was nonzero.
skip:
LD (DE),A ;store in output string
inc de ;next byte of output string
rrc c ;shift the bit mask down to the next bit of A
jr nc,loop ;once a 1 is shifted into the carry, we're finished. Otherwise, check next bit.
pop bc
inc hl
dec bc
ld a,b
or c
jp nz,StoreBinaryString
ret</lang>
{{out}}
<pre>
Hexdump (using &57 &50 as input):
 
30 31 30 31 30 31 31 31 01010111
30 31 30 31 30 30 30 30 01010000
</pre>
 
===Compressing a String of ASCII Zeroes and Ones===
1,489

edits