Small 'putbits' routine (ASM CodeSnip)

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Small 'putbits' routine (ASM CodeSnip)

Post by BackupUser »

Code updated for 5.20+

Restored from previous forum. Originally posted by MrVainSCL.

Maybe i have found something interested for advanced coders!?

Small 'putbits' routine
Here is a simple code snippet to write a single bit into a datastream. Could this be the smallest possible 'putbit' routine?

The code
The following should be pretty obvious. Call the 'init' with DS:DI pointing to the bitstream buffer then call 'putbit' with a single bit in the CF (carry flag). Use the 'flush' to align the final bits in the byte.

Code: Select all

 [DS:DI] --> bitstream buffer 
 CF --> the bit to write 
 
 init: 
      mov byte ptr [di],01h           ; C6 05 01 
      ret                             ; C3 
 
 putbit: 
      mov byte ptr [di+1], 01h        ; C6 45 01 01 
      rcl byte ptr [di], 1            ; D0 15 
      adc di, 0                       ; 83 D7 00 
      ret                             ; C3 
 
 flush: 
      rcl byte ptr [di], 1            ; D0 15 
      jnc short flush                 ; 73 FC 
      inc di                          ; 47 
      ret                             ; C3 


PIII450, 256MB Ram, 80GB HD + 6,4 GB, RivaTNT, DirectX8.1, SB AWE64, Win2000 + all Updates...

greetz
MrVainSCL! aka Thorsten
Last edited by fsw on Thu Aug 22, 2013 8:30 pm, edited 1 time in total.
Reason: Code updated for 5.20+