Module2 - Logic Operations
Module2 - Logic Operations
• One application area the 8051 is designed to fill is that of machine control.
• A large part of machine control concerns sensing the on-off states of external switches,
making decisions based on the switch states, and then turning external circuits on or off.
• Single point sensing and control implies a need for byte and bit opcodes that operate on data
using Boolean operators.
• All 8051 RAM areas, both data and SFRs, may be manipulated using byte opcodes.
• Many of the SFRs, and a unique internal RAM area that is bit addressable, may be operated
upon at the individual bit level.
• Bit operators are notably efficient when speed of response is needed. Bit operators yield
compact program code that enhances program execution speed.
Byte-Level Logical Operations
• The byte-level logical operations use all four addressing modes for the source of a data
byte. The A register or a direct address in internal RAM is the destination of the logical
operation result.
• Keep in mind that all such operations are done using each individual bit of the desti
nation and source bytes. These operations, called byte-level Boolean operations because
tlfe entire byte is affected,
• The normal source of data from a port is the port pins; the normal destination for port
data is the port latch.
When the destination of a logical operation is the direct address of a port, the latch
register not the pins, is used both as the source for the original data and then the
destination for the altered byte of data.
Bit-Level Logical Operations
• The ability to operate on individual bits creates the need for an area of RAM that contains
data addresses that hold a single bit. Internal RAM byte addresses 20h to 2Fh serve this need
and are both byte and bit addressable. The bit addresses are numbered from OOh to 7Fh to
represent the 12Sd bit addresses
For example, bit 0E3h is bit 3 of the A register.
The carry flag, which is bit 7 of the PSW, is bit addressable as 0D7h.
The assembler can also "understand" more descriptive mnemonics, such as P0.5 for bit 5
of port 0, which is more formally addressed as 85h.
Bit-Level Boolean Operations
• The bit-level Boolean logical opcodes operate on any addressable RAM or SFR bit.
The carry flag (C) in the PSW special-function register is the destination for most of
the opcodes because the flag can be tested and the program flow changed using
instructions
Rotate and Swap Operations
• The A register can be rotated one bit position to the left or right with or
without including the C flag in the rotation.
• If the C flag is not included, then the rotation involves the eight bits of the A
register. If the C flag is included, then nine bits are involved in the rotation.
Including the C flag enables the programmer to construct rotate operations in
volving any number of bytes.
Arithmetic operations
• Applications of microcontrollers often involve performing mathematical calculations on
data in order to alter program flow and modify program actions.
• Flags
• The 8051 has four arithmetic flags: the carry (C), auxiliary carry (AC), overflow
(OV), and parity (P).
• Instructions Affecting Flags
• The C, AC, and OV flags are arithmetic flags. They are set to 1 or cleared to 0
automatically, depending upon the outcomes of the following instructions.
•
• For example, the C flag indicates a carry out of the lower byte position during
addition and indicates a borrow during sub traction.
• The instruction that last affects a flag determines the use of that flag.
• The simplest arithmetic operations involve adding or subtracting a binary 1 and a number.
• These simple operations become very powerful when coupled with the ability to repeat
the operation-that is, to "INCrement" or “DECrement"-until a desired result is reached.’
• All addition is done with the A register as the destination of the result. All addressing
modes may be used for the source: an immediate number, a register, a direct address, and
an indirect address
The following table lists the add with carry mnemonics:
Subtraction
• Subtraction can be done by taking the 2's complement of the number to be subtracted,
the subtrahend, and adding it to another number, the minuend.
• The 8051, however, has commands to perform direct subtraction of two signed or
unsigned numbers. Register A is the destination address for subtraction.
• The 8051 has the capability to perform 8-bit integer multiplication and division using the A
and B registers
• . The A register holds one byte of data before a multiply or divide operation, and one of the
result bytes after a multiply or divide operation.
• Multiplication operations use registers A and B as both source and destination addresses for
the operation. The unsigned number in register A is multiplied by the unsigned number
in register B.
Division
• Division operations use registers A and B as both source and destination addresses for
the operation.
The OV flag is cleared to 0 unless B holds OOh before the DIV. Then the OV flag is
set to 1 to show division by 0. The contents of A and B, when division by 0 is
attempted, are undefined.
Decimal Arithmetic
• Four bits are required to represent the decimal numbers from Oto 9 (0000 to 1001)
and the numbers are often called Binary coded decimal (BCD) numbers.
• The C flag is set to 1 if the adjusted number exceeds 99BCD and set to 0
otherwise.
• The DA A instruction makes use of the AC flag and the binary sums of the
individual binary nibbles to adjust the answer to BCD