Hi there. I haven't the foggiest on the best way to do this in ASM. What I need is to convert a hex number to the hex representation of the decimal number. For example:
Start with:
2Eh (It iss equal to 46d)
End with:
46h
I need to end up with the byte in hex because of how I send the byte to the screen by converting each nibble into its ascii.
If theres a simpler way to send a hex value's decimal equivalent to the display I'd be glad to hear that as well!
To convert Binary to BCD this is a double-dabble algorithm, it only requires N shifts for an N bit number, but more overhead in tracking adjustments. **broken link removed**
If your number is always under 100d, then the Double-Dabble can be reduced to
Code:
PSEUDOCODE
Result = 0
IF Number>99 THEN
Number = 0 // Clear on Error
ENDIF
LSL Number // Number Shift Left into Carry
RLC Result // Result Rotate Left through Carry
LSL Number
RLC Result
DO
IF (Result & 0Fh) > 4 THEN // if >=5 then adjust
Result = Result + 3
ENDIF
LSL Number
RLC Result
UNTIL Number = 0
This algorithm uses no multiply or divide, just simple add and shifts found on almost all cpu's. The main loop will execute 1 to 5 times maximum.
Some low end CPU's dont have multiply and divide, and if they do execution times are slow.