0 like 0 dislike
7 views
Hello! Features ATMega128 8-bit register r16, which is a number from 0 to 255. You need to registers r17, r18, r19, which will contain figures for the number of r16. How to do better, in what direction to dig? Assuming you need to use the modulo operator. First, r16 divide by 100, record the first digit, then the remainder to divide by 10 to record the second category, and finally the final balance to record the third digit. But here arises another question — how to divide with remainder? Thanks in advance.
| 7 views

0 like 0 dislike
On the AVR've never programmed, therefore, limited to the algorithm.
\r
1 way short.
\r
`while (r16 >= 100) {++r17; r16 -= 100; } while (r16 >= 10) {++r18; r16 -= 10; } r19 = r16; `

2 way, fast.
\r
`if (r16 >= 200) { r17 = 2; r16 -= 200; } else if (r16 >= 100) { r17 = 1; r16 -= 100; } if (r16 >= 50) { r18 = 5; r16 -= 50; } if (r16 >= 20) { r18 += 2; r16 -= 20; } if (r16 >= 20) { r18 += 2; r16 -= 20; } if (r16 >= 10) {++r18; r16 -= 10; } r19 = r16; `

3 method, if the chip supports the instruction MUL.
Use the fact that (N / 10) = Hi(N * 0xCD) >> 3;
by
0 like 0 dislike
Sorry, this assembler was not working... But understand that if there is a division without a remainder, the remainder can get something like this: num — (num div 10) * 10
by
0 like 0 dislike
If you have the number 255, you can use conditions and bit shifts. A bit long, but surely. And faster, than for 10(100) to share.
by
0 like 0 dislike
Thanks for the interesting apangin algorithm.
\r
Today I asked this question of the teacher. Received in response to a similar algorithm:
\r
\r`while (r16 > 0) {if (r19 == 9) {r18++;r19 = 0;}if (r18 == 9) {r17++;r18 = 0;}r19++;r16--; }`
by

0 like 0 dislike