Path: Home => AVR-overview => Binary calculations => Conversions    (Diese Seite in Deutsch: Flag DE) Logo
AT90S8515

Number format conversion in AVR assembler


Converting numbers is very often used in assembler, because processors prefer calculating in binary, while people on a terminal prefer decimal numbers. If one has to communicate from within an assembler routine with one of the human species, number conversion is a must. This page provides some basic know how on number conversion. Learn a bit more in detail, how conversion between different number systems is done.

For those who prefer going directly to the source code, here is the link to the HTML version and the link to the asm version of the source code.

Number format basics

The number systems discussed here ar: So far the number format basics.

General rules of the assembler routines

The AVR assembler software for conversion has to be somehow flexible to apply in different situations. So it is better to spent some thinking before writing it. I have adopted some rules in writing this source code. Here are the rules:

From ASCII to binary

The routine AscToBin2 is suitable for converting ASCII-coded numbers in buffers. It reads over leading blanks and zeros and stops conversion on the first non-decimal digit read. The length of the number is limited to the 16-bit long result registers. Numbers that exceed 65,535 cause an error with the T-flag set and Z placed on the first digit that exceeds the 16-bit limit.

If the length of the ASCII number is exactly five digits, you can use the routine Asc5ToBin2. Every illegal character causes an error, except leading blanks and zeros.

Conversion is done from the left to the right of the number. Each additional digit causes a multiplication of the result by 10, followed by adding the digit to the result. The routine is somewhat slow due to the multiplication by 10. There sure are quicker methods to do the conversion job. But this one is easy to understand.

From BCD to binary

Conversion of BCDs to binary is similiar to ASCII conversion, so we do not discuss this routine in detail. See the source code of this routine for additional comments.

Binary multiplication by 10

This routine Bin1Mul10 multiplies a 16-bit-binary in rBin1H:L by 10. To do that, the binary is copied to another location, added two times with itself, then the copy is added, and another addition with itself yields the tenfold. As during each step of adding an overflow of the 16-bit-binary can occur, the detection of the overflow and the following reaction cause the most time and code of that routine. If you are fine without that level of error detection and if you don't care if your result is ok or if you prefer error detection a different way, you can skip the parts of the routine that branch around. The code will be much shorter and faster then.

From binary to ASCII

Conversion from binary to ASCII is done in the routine Bin2ToAsc5. The result is generally five digits long. The conversion is done by a call to the subroutine Bin2ToBcd5. This is discussed more in detail below.

If you use the routine Bin2ToAsc instead, the routine returns with the pointer Z on the first non-zero digit and the number of remaining digits in rBin2L. This is convenient if you have to send the result via SIO to the man behind the terminal without boring him with leading blanks.

From binary to BCD

Bin2ToBcd5 converts the binary in rBin1H:L to decimal BCD. This conversion is a bit long in code and required time, but sure easier to understand than others. The conversion is done by repeated subtraction of binary representations of the decimal numbers 10,000, 1,000, 100 and 10. Only the remaining one's are treated different!

From binary to Hex

The routine Bin2ToHex4 produces a four digit Hex-ASCII number from a 16-bit binary in rBin1H:L. Such hex numbers are somewhat easier to read and remember than the binary original. How would you spell 1011011001011110? Hex B65E is a little bit more convenient to remember, nearly like the decimal equivalent 46686.

The routine converts the hex digits A to F in capital letters. If you more like a to f, change the source code accordingly.

From hex to binary

With Hex4ToBin2 the conversion goes the other way round. Because illegal (non-hex) digits can occur, some code is necessary to detect those errors and signal them in the T-flag.

Zum Seitenanfang

©2002 by http://www.avr-asm-tutorial.net