Bitwise IO: Difference between revisions

(Added 11l)
Line 3,416:
<pre>
abcdefghijk
</pre>
 
=={{header|Z80 Assembly}}==
 
===Compressing a String of ASCII Zeroes and Ones===
<lang z80>CompressBinaryStrings_7bit:
; HL = pointer to output
; DE = pointer to input. Input is assumed to equal &30 or &31
; Usage:
; LD hl,OutputRam
; LD de,InputRam
; CALL CompressBinaryStrings_7bit
; If the string "runs out" before the 8 bit boundary, the rest are rotated into place.
; e.g. input = "0101" then the procedure will RLC until those bits
; are as far left as possible.
 
loop_compressBinaryStrings_7bit:
inc de ;skip bit 7
ld b,7 ;loop counter
innerloop_compressBinaryStrings_7bit:
ld a,(de)
or a ;compares accumulator to zero. Assumes a null-terminated string.
; Otherwise compare A to your terminator of choice.
 
jr z,HandleEarlyExit_compressBinaryStrings_7bit
 
sub &30 ;we're left with 0 if the ascii was "0" and 1 if the ascii was "1"
rra ;rotate the result into the carry
rl (hl) ;rotate it out of the carry into (HL)
inc de
z_djnz innerloop_compressBinaryStrings_7bit
;a macro that becomes DJNZ <label> on Zilog Z80 and DEC B JR NZ,<label> on Sharp LR35902
 
inc hl ;next output byte
jp loop_compressBinaryStrings_7bit
HandleEarlyExit_compressBinaryStrings_7bit:
xor a ;LD A,0
cp b ;compare B to zero
ret z ;if B=0, we're done. No need to adjust the last byte
loop_earlyExit_compressBinaryStrings_7bit:
rlc (hl)
z_djnz loop_earlyExit_compressBinaryStrings_7bit
ret</lang>
 
{{out}}
<pre>
(Tested using input string of "0101011101010". Printing routines left out for brevity but can be added upon request.
 
57 50
</pre>
 
1,489

edits