From e9f1aec1227762cfd5777eb3344ab78455ebb808 Mon Sep 17 00:00:00 2001 From: Martin Preuss Date: Sun, 18 Jan 2026 14:25:21 +0100 Subject: [PATCH] avr: added itoa --- avr/common/0BUILD | 1 + avr/common/itoa.asm | 96 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 avr/common/itoa.asm diff --git a/avr/common/0BUILD b/avr/common/0BUILD index a5cf151..838a3ed 100644 --- a/avr/common/0BUILD +++ b/avr/common/0BUILD @@ -33,6 +33,7 @@ eeprom-r.asm eeprom-w.asm ressource.asm + itoa.asm diff --git a/avr/common/itoa.asm b/avr/common/itoa.asm new file mode 100644 index 0000000..8b44865 --- /dev/null +++ b/avr/common/itoa.asm @@ -0,0 +1,96 @@ +; *************************************************************************** +; copyright : (C) 2026 by Martin Preuss +; email : martin@libchipcard.de +; +; *************************************************************************** +; * This file is part of the project "AqHome". * +; * Please see toplevel file COPYING of that project for license details. * +; *************************************************************************** + +#ifndef AQH_AVR_COMMON_ITOA_ASM +#define AQH_AVR_COMMON_ITOA_ASM + + + +; *************************************************************************** +; defines + + +.equ ITOA_BUFFER_SIZE = 8 + + + +; *************************************************************************** +; global data + +.dseg + + +itoa_buffer: ; max uint16 65535, 5 digits, plus komma, sign, ZERO + .byte ITOA_BUFFER_SIZE + + + +; *************************************************************************** +; code + +.cseg + + + +; --------------------------------------------------------------------------- +; @routine IntToAscii @global +; +; @param R21:R20 value to transform to ascii +; @param R24 number of digits after komma +; @return X pointer to result string (will be overwritten by next call!) +; @clobbers R16, R17, R18, R19, R20, R21, R22, R23 + +IntToAscii: + ldi xl, LOW(itoa_buffer) + ldi xh, HIGH(itoa_buffer) + adiw xh:xl, ITOA_BUFFER_SIZE + clr r16 + st -X, r16 + ldi r25, ITOA_BUFFER_SIZE-1 ; byte counter + +IntToAscii_loop: + ; divide by 10 + ldi r22, 10 + clr r23 + push r25 + bigcall Utils_Divu16_16_16 ; R17:R16=result, R19:R18=remainder (R25) + pop r25 + ; move result for next loop + mov r20, r16 + mov r21, r17 + ; store digit of remainder (can only be 0-9 when dividing by 10) + ldi r19, '0' + add r19, r18 + st -X, r19 + dec r25 + breq IntToAscii_ret + ; maybe insert komma + tst r24 + breq IntToAscii_next + dec r24 + brne IntToAscii_next + ldi r19, ',' + st -X, r19 + dec r25 + breq IntToAscii_ret +IntToAscii_next: + mov r16, r20 ; result == 0? + or r16, r21 + breq IntToAscii_ret ; yes, done + rjmp IntToAscii_loop +IntToAscii_ret: + ret +; @end + + + + +#endif + +