/*
* Copyright (C) 2022, Andrei Egorov
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
uint8_t __attribute__ ((noinline)) NumOneBits(uint8_t b) {
uint8_t result;
asm volatile(
"mov r18,%1\n\t"
"clr %0\n\t"
"tst r18\n\t"
"l:\n\t"
"breq exit\n\t"
"inc %0\n\t"
"mov r19,r18\n\t"
"dec r19\n\t"
"and r18,r19\n\t"
"rjmp l\n\t"
"exit:\n\t"
: "=r" (result)
: "r" (b)
: "r18", "r19"
);
return result;
}
void setup() {
Serial.begin(115200);
}
void loop() {
static byte i(0);
Serial.print(NumOneBits(i));
Serial.print(" ");
if (!(++i%16)) Serial.println();
if (!i) while (1);
}