avr: added itoa

This commit is contained in:
Martin Preuss
2026-01-18 14:25:21 +01:00
parent 31cce1a609
commit e9f1aec122
2 changed files with 97 additions and 0 deletions

View File

@@ -33,6 +33,7 @@
eeprom-r.asm
eeprom-w.asm
ressource.asm
itoa.asm
</extradist>
</gwbuild>

96
avr/common/itoa.asm Normal file
View File

@@ -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