Assembly Language for Beginners

(Jeff_L) #1

2.1 Integral datatypes.


2.1 Integral datatypes


Integral datatype is a type for a value which can be converted to number. These are numbers, enumera-
tions, booleans.


2.1.1 Bit


Obvious usage for bits are boolean values: 0 forfalseand 1 fortrue.


Set of booleans can be packed intoword: there will be 32 booleans in 32-bit word, etc. This way is called
bitmaporbitfield.


But it has obvious overhead: a bit jiggling, isolating, etc. While usingword(orinttype) for boolean
variable is not economic, but highly efficient.


In C/C++ environment, 0 is forfalseand any non-zero value is fortrue. For example:


if (1234)
printf ("this will always be executed\n");
else
printf ("this will never\n");

This is popular way of enumerating characters in a C-string:


char *input=...;

while(*input) // execute body if *input character is non-zero
{
// do something with *input
input++;
};

2.1.2 Nibble AKA nybble.


AKAhalf-byte, tetrade. Equals to 4 bits.


All these terms are still in use today.


Binary-coded decimal (BCD^1 )


4-bit nibbles were used in 4-bit CPUs like legendary Intel 4004 (used in calculators).


It’sinterestingtoknowthattherewasbinary-codeddecimal(BCD)wayofrepresentingdecimaldigitusing
4 bits. Decimal 0 is represented as 0b0000, decimal 9 as 0b1001 and higher values are not used. Decimal
1234 is represented as 0x1234. Of course, this way is not economical.


Nevertheless, it has one advantage: decimal toBCD-packed number conversion and back is extremely
easy. BCD-numbers can be added, subtracted, etc., but an additional correction is needed. x86 CPUs has
rare instructions for that: AAA/DAA(adjust after addition),AAS/DAS(adjust after subtraction),AAM(after
multiplication),AAD(after division).


The need for CPUs to supportBCDnumbers is a reason whyhalf-carry flag(on 8080/Z80) andauxiliary
flag(AFon x86) are exist: this is carry-flag generated after proceeding of lower 4 bits. The flag is then
used for adjustment instructions.


The fact of easy conversion had led to popularity of [Peter Abel,IBM PC assembly language and pro-
gramming(1987)] book. But aside of this book, the author of these notes never seenBCDnumbers in
practice, except formagic numbers(5.6.1 on page 712), like when someone’s birthday is encoded like
0x19791011—this is indeed packedBCDnumber.


BCDinstructions in x86 were often used for other purposes, especially in undocumented ways, for exam-
ple:


(^1) Binary-Coded Decimal

Free download pdf