diff --git a/avr/common/itoa.asm b/avr/common/itoa.asm index 8b44865..0213037 100644 --- a/avr/common/itoa.asm +++ b/avr/common/itoa.asm @@ -90,6 +90,92 @@ IntToAscii_ret: +; --------------------------------------------------------------------------- +; @routine HexWordToAscii @global +; +; Shares buffer with @ref IntToAscii ! +; +; @param R21:R20 value to transform to ascii +; @return X pointer to result string (will be overwritten by next call!) +; @clobbers R16, R17 + +HexWordToAscii: + ldi xl, LOW(itoa_buffer) + ldi xh, HIGH(itoa_buffer) + mov r16, r21 + rcall writeHexByteToBuffer ; write hi byte + mov r16, r20 + rcall writeHexByteToBuffer ; write low byte + clr r16 + st X+, r16 + sbiw xh:xl, 5 + ret +; @end + + + +; --------------------------------------------------------------------------- +; @routine HexByteToAscii @global +; +; Shares buffer with @ref IntToAscii ! +; +; @param R16 value to transform to ascii +; @return X pointer to result string (will be overwritten by next call!) +; @clobbers R16, R17 + +HexByteToAscii: + ldi xl, LOW(itoa_buffer) + ldi xh, HIGH(itoa_buffer) + rcall writeHexByteToBuffer + clr r16 + st X+, r16 + sbiw xh:xl, 3 + ret +; @end + + + +; --------------------------------------------------------------------------- +; @routine writeHexByteToBuffer +; +; @param R16 value to transform to ascii +; @param X pointer to current pos in result buffer +; @clobbers R16, R17 + +writeHexByteToBuffer: + push r16 + swap r16 + rcall hexNibbleToAscii ; transform high nibble (r16, r17) + st X+, r16 + pop r16 + rcall hexNibbleToAscii ; transform low nibble (r16, r17) + st X+, r16 + ret +; @end + + + +; --------------------------------------------------------------------------- +; @routine hexNibbleToAscii +; +; Convert a nibble to an ASCII char. +; @return R16 ASCII representation of that nibble (e.g. '0' for 0) +; @param R16 byte (in bits 0-3) +; @clobbers r16, r17 + +hexNibbleToAscii: + andi r16, 0xf + cpi r16, 10 + brcs hexNibbleToAscii_l1 + ldi r17, 7 + add r16, r17 +hexNibbleToAscii_l1: + ldi r17, '0' + add r16, r17 + ret +; @end + + #endif