From 5119c38ca6d2370fda8fc7b03147f97b8bbb5f53 Mon Sep 17 00:00:00 2001 From: Martin Preuss Date: Sun, 15 Mar 2026 20:20:33 +0100 Subject: [PATCH] avr: added divide8 --- avr/common/divide.asm | 5 ++++ avr/common/divide8.asm | 60 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 avr/common/divide8.asm diff --git a/avr/common/divide.asm b/avr/common/divide.asm index 0609b02..bf125ab 100644 --- a/avr/common/divide.asm +++ b/avr/common/divide.asm @@ -57,3 +57,8 @@ Utils_Divu16_16_16_loop_end: ; @end + +#endif + + + diff --git a/avr/common/divide8.asm b/avr/common/divide8.asm new file mode 100644 index 0000000..ff46e65 --- /dev/null +++ b/avr/common/divide8.asm @@ -0,0 +1,60 @@ +; *************************************************************************** +; copyright : (C) 2025 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 COMMON_DIVIDE8_H +#define COMMON_DIVIDE8_H + + + + + +; --------------------------------------------------------------------------- +; @routine Utils_Divu8_8_8 +; +; Divides two unsigned 8 bit values resulting in one 8 bit value. +; This is a rather simple but reasonable fast routine working just like you would +; when doing divisions with pen and paper. +; +; R16 = R18 / R19 +; R17 = R18 mod R19 + +; TODO: adapt for signed values +; +; @param R18 8 bit value A +; @param R19 8 bit value B +; @return R16 8 bit result +; @return R17 8 bit remainder +; @clobbers R25 + + +Utils_Divu8_8_8: + mov r16, r18 ; r16=result (intitially: dividend) + clr r17 ; r17=remainder + ldi r25, 8 ; 8 bits to divide +Utils_Divu8_8_8_loop: + lsl r16 ; shift 0 bit into result + rol r17 + + sub r17, r19 ; try to subtract divisor from value + brcs Utils_Divu8_8_8_nofit ; jmp if dividend < divisor + ori r16, 1 ; otherwise set bit in result + rjmp Utils_Divu8_8_8_loop_end +Utils_Divu8_8_8_nofit: + add r17, r19 ; undo subtraction + ; r16=result +Utils_Divu8_8_8_loop_end: + dec r25 + brne Utils_Divu8_8_8_loop + ret +; @end + + + +#endif +