A83: Re: daa


[Prev][Next][Index][Thread]

A83: Re: daa




I can't explain how it is used to check for vowel's in a text string (though
I had heard of that before the contest...was on the A86 list quite some time
ago).  However, DAA stands for Decimal Adjust Accumulator.  Most processors
have at least minimal support for BCD, or Binary Coded Decimal.  For those
of you without experience in digital electronics, BCD is a simple way of
storing base 10 numbers in binary.  Each digit of a base 10 number is stored
in a nibble, which is 4 bits, or put another way, a single hexadecimal (base
16) digit.  As is obvious, this takes up more space to store numbers, but
makes displaying them much, much easier.  If you print a number stored in
BCD as a hexadecimal value (which is much easier than printing a number
stored in binary) you will get a decimal number.

Of course, working with BCD numbers is different than working with binary
numbers.  The 6502, for example, has a flag that you set to make the
addition and subtraction instructions work on BCD values.  In Z80, there is
no such option.  This is what DAA is used for.  A flag that probably seems
worthless that probably goes unnoticed by the average coder is the half-cary
flag, which is bit 4 of the flag register.  This is just what it sounds
like, a carry flag set when the first nibble of the accumulator overflows.
The other flag used by DAA is the Add/Subtract flag, which is bit 1.  This
bit is set if the previous operation was a subtraction.  If you execute a
DAA after an addition or subtraction of two BCD numbers, the result will be
adjusted accordingly.  As an example:

 ld a,$34
 add a,$47
; a = $7B  -- before DAA -- like adding two hex numbers (what it does, of
course)
 daa
; a = $81  -- after DAA -- like adding two decimal numbers

The same holds true for subtraction.  BCD has many uses, not the least of
which involve the easy storage and printing of numbers for things like
scores.  If you store a number in BCD, you can print it using by merely
shifting to get the nibbles (or a "swap" on the gb).  BCD can also be used
to perform operations on very large numbers.  This is how the calculator's
ROM works internally.  However, BCD multiplication and division are quite
non-trivial, and can require lots of tempory space.

Hope this little rant shed some light on the subject :)

> Upon examining the source of the winners of Void's pig latin competition,
I
> noticed that the smallest ones all used the "daa" instruction.  I read
that
> this converts a into BCD format, which allows you to do floating point
> operations.  But beyond that, I really have no clue what it does.  Can
> anyone explain what it does to a?  Does it affect any other registers?


_____________________________________________
NetZero - Defenders of the Free World
Click here for FREE Internet Access and Email
http://www.netzero.net/download/index.html



Follow-Ups: References: