|
Written by Jan Verhoeven
|
The Challenge
-------------
Write a routine to convert the value of a bit to ASCII in under 10 bytes, with no conditional jumps.
The Solution
Load the number into the AX register and shift through the bits. If a bit is
cleared [0], you want to print a "0" character; if a bit is set [1], you want
to print a "1".
Prime the BL register with the ASCII character "0"; if the next bit in AX is
set, carry will be set after the SHL and BL will thus be incremented to an
ASCII "1". The key, as you will see, is the ADC [AddWithCarry] instruction:
L0: B330 MOV BL,30 ; try with al = ZERO
D1E0 SHL AX,1 ; ... but if bit = set, ...
80D300 ADC BL,00 ; ... make it a ONE,
7 bytes all told; with a loop and mov instruction for storing each value in
BL to the location of your choice, you will have a full-fledged binary-toascii
converter in a handful of bytes.
|