L10 Logical & Arithmetic ENE334
L10 Logical & Arithmetic ENE334
L10 Logical & Arithmetic ENE334
Lecture 10:
MCS-51: Logical and Arithmetic
http://webstaff.kmutt.ac.th/~dejwoot.kha/
Page 1
Logical: Objectives
Logical: Intro
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 on 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.
ENE 334 MCS-51 Logical & Arithmetic Page 3 Week #10
Logical: Intro
The two data levels, byte or bit, at which the Boolean instructions operate are shown in the following table:
Page 4
Week #10
Logical: Intro
There are also rotate opcodes that operate only on a byte, or a byte and the Carry flag, to permit limited 8and 9-bit shift-register operations. The following table shows the rotate opcodes:
Page 5
Week #10
Keep in mind that all such operations are done using each individual bit of the destination and source bytes. These operations, called byte-level Boolean operations because the entire byte is affected, are listed in the following table: Note: that no flags are affected by the byte-level logical operations unless the direct RAM address is the PSW
ENE 334 MCS-51 Logical & Arithmetic Page 6 Week #10
Page 7
Week #10
Many of these byte-level operations use a direct address, which can include the port SFR addresses, as a destination. The normal source of data from a port are the port pin; 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.
ENE 334 MCS-51 Logical & Arithmetic Page 8 Week #10
Any port operation that must first read the source data, logically operate on it, and then write it back to the source (now the destination) must use the latch. Logical operations that use the port as a source, but not as a destination, use the pins of the port as the source of the data.
Page 9
Week #10
For example, the port 0 latch contains FFh, but the pins are all driving transistor bases and are close to ground level. The logical operation ANL P0,#0Fh which is designed to turn the upper nibble transistors off, reads FFh from the latch, ANDs it with 0Fh to produce 0Fh as a result, and then writes it back to the latch to turn these transistors off.
ENE 334 MCS-51 Logical & Arithmetic Page 10 Week #10
Reading the pins produces the result 00h, turning all transistors off, in error. But, the operation ANL A,P0 produces A = 00h by using the port 0 pin data, which is 00h.
Page 11
Week #10
Page 12
Week #10
Note that instructions that can use the SFR port latches as destinations are ANL, ORL, and XRL.
Page 13
Week #10
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
Page 14
Week #10
The following table lists the Boolean bit-level operations: Note: that no flags, other than the C flag, are affected, unless the flag is an addressed bit.
Page 15
Week #10
As is the case for byte-logical operations when addressing ports as destinations, a port bit used as a destination for a logical operation is part of the SFR latch, not the pin. A port bit used as a source only is a pin, not the latch. The bit instructions that can use a SFR latch bit are: CLR, CPL, MOV, and SETB.
Page 16
Week #10
Page 17
Week #10
Page 18
Week #10
The ability to rotate data is useful for inspecting bits of a byte without using individual bit opcodes. 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 involving any number of bytes.
ENE 334 MCS-51 Logical & Arithmetic Page 19 Week #10
The SWAP instruction can be thought of as a rotation of nibbles in the A register. Figure diagrams the rotate and swap operations, which are given in the following table:
Page 20
Week #10
Page 21
Week #10
operations.
Page 22
Week #10
Page 23
Week #10
Logical: Example
Page 24
Week #10
Double the number in register R2, and put the result in registers R3 (high byte) and R4 (low byte).
Thoughts on the Problem: The largest number in R2 is FFh; the largest result is 1FEh. There are at least three ways to solve this problem: Use the MUL instruction (multiply, covered in Chapter 7), add R2 to itself, or shift R2 left one time. The solution that shifts R2 left is as follows:
ENE 334 MCS-51 Logical & Arithmetic Page 25 Week #10
Page 26
Week #10
OR the contents of ports 1 and 2; put the result in external RAM location 0100h.
Thoughts on the Problem The ports should be input ports for this problem to make any physical sense; otherwise, we would not know whether to use the pin data or the port SFR latch data.
The solution is as follows:
ENE 334 MCS-51 Logical & Arithmetic Page 27 Week #10
Page 28
Week #10
Find a number that, when XORed to the A register, results in the number 3Fh in A. Thoughts on the Problem Any number can be in A, so we will work backwards:
Page 30
Week #10
Logical: Summary
Page 31
Week #10
Logical: Summary
ANL destination, source ORL destination, source XRL destination, source CLRA CPLA RRA RLA RRCA RLCA SWAP A
Page 32
Week #10
Logical: Summary
Page 33
Logical: Summary
ANL bit ORL bit CLR bit CPL bit SETB bit MOV C, source bit MOV destination bit, C
Page 34
Week #10
Page 35
Week #10
Arithmetic: Objectives
Page 36
Week #10
Arithmetic: Intro
following types:
Page 37
Week #10
Arithmetic: Intro
are immediate, register, direct, and indirect. The 8051 has four arithmetic flags: the Carry (C),
Auxiliary Carry (AC), Overflow (OV), and Parity (P).
Page 38
Week #10
Arithmetic: Intro
Arithmetic: Intro
Page 40
Week #10
Arithmetic: Intro
1s is even.
Page 41
Week #10
Register, direct, and indirect addresses may be INCremented or DECremented. No math flags (C, AC, OV) are affected. The following table lists the increment and decrement mnemonics: Note: that increment and decrement instructions that operate on a port direct address alter the latch for that port.
ENE 334 MCS-51 Logical & Arithmetic Page 42 Week #10
Page 43
Week #10
Page 44
Week #10
Page 45
Week #10
Arithmetic: Addition
Page 46
Week #10
Arithmetic: Addition
Note: that the C flag is set to 1 if there is a carry out of bit position 7; it is cleared to 0 otherwise. The AC flag is set to 1 if there is a carry out of bit position 3, it is cleared otherwise. The OV flag is set to 1 if there is a carry out of bit position 7, but not bit position 6 or if there is a carry out of bit position 6 but not bit position 7, which may be expressed as the logical operation OV = C7 XOR C6
ENE 334 MCS-51 Logical & Arithmetic Page 47 Week #10
Unsigned Addition
Signed Addition
Page 48
Week #10
If positive numbers are added, there is the possibility that the sum will exceed + 127d, as demonstrated in the
following example:
Page 49
Week #10
Page 50
An example of adding two positive numbers that do not exceed the positive limit is this:
Note: that there are no carries from bits 6 or 7 of the sum; the Carry and OV flags are both 0.
ENE 334 MCS-51 Logical & Arithmetic Page 51 Week #10
The result of adding two negative numbers together for a sum that does not exceed the negative limit is shown in this example:
Here, there is a carry from bit 7 and the Carry flag is 1; there is a carry from bit 6 and the OV flag is 0. These are the same flags as the case for adding unlike numbers; no corrections are needed for the sum.
ENE 334 MCS-51 Logical & Arithmetic Page 52 Week #10
When adding two negative numbers whose sum does exceed -128d, we have:
Or, the magnitude can be interpreted as -12d, which is the remainder after a carry out of -128d. In this example, there is a carry from bit position 7, and no carry from bit position 6, so the Carry and the OV flags are set to 1. The magnitude of the sum is correct the sign bit must be changed to a 1.
ENE 334 MCS-51 Logical & Arithmetic Page 53 Week #10
From these examples the programming actions needed for the C and OV flags are as follows:
Page 54
Week #10
A general rule is that if the OV flag is set, then complement the sign. The OV flag also signals that the sum exceeds the largest positive or negative numbers thought to be needed in the program.
Page 55
Week #10
Page 57
Week #10
Page 58
Week #10
Arithmetic: Subtraction
Page 59
Week #10
Arithmetic: Subtraction
Arithmetic: Unsigned
Arithmetic: Signed
Signed Subtraction
Arithmetic: Signed
Arithmetic: Signed
An overflow is possible when subtracting numbers of opposite sign because the situation becomes one of
Page 65
Week #10
Arithmetic: Signed
Here, there is a borrow into bit position 6 but not into bit
Arithmetic: Signed
There is a borrow from bit position 7, and no borrow from bit position 6; the OV flag and the Carry flag are both set to 1. Again the answer must be adjusted because the OV flag is set to 1. The magnitude can be interpreted as a +011d, the remainder from a carry out of 128d. The sign must be changed to a binary 0 and the OV condition dealt with.
ENE 334 MCS-51 Logical & Arithmetic Page 67 Week #10
Arithmetic: Signed
The general rule is that if the OV flag is set to 1, then complement the sign bit. The OV flag also signals that the result is greater than -128d or + 127d. Note that for all the examples in this section, it is assumed that the Carry flag = 0 before the SUBB. The Carry flag must be 0 before any SUBB operation that depends on C = 0 is done.
ENE 334 MCS-51 Logical & Arithmetic Page 68 Week #10
Arithmetic: Signed
The following table lists examples of SUBB multiple-byte signed arithmetic operations:
Page 69
Week #10
Arithmetic: Signed
Page 70
Week #10
The 8051 has the capability to perform 8-bit integer multiplication and division using the A and B registers. Multiplication and division treat the numbers in registers A and B as unsigned. The programmer must devise ways to handle signed numbers.
Page 71
Week #10
Arithmetic: Multiplication
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, as indicated in the following table:
Page 72
Week #10
Arithmetic: Multiplication
The OV flag will be set if A x B > FFh. Setting the OV flag does not mean that an error has occurred. Rather,
Page 73
Week #10
Arithmetic: Multiplication
Page 74
Week #10
Arithmetic: Division
Division operations use registers A and B as both source and destination addresses for the operation. The unsigned
Page 75
Week #10
Arithmetic: Division
The OV flag is cleared to 0 unless B holds 00h 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
Page 76
Week #10
Arithmetic: Division
Division always results in integer quotients and remainders, as shown in the following example:
Page 77
Week #10
Arithmetic: Division
Page 78
Week #10
Arithmetic: Decimal
The opcode that adjusts the result of BCD addition is the decimal adjust A for addition (DA A) command, as shown in the following table:
Page 79
Week #10
Arithmetic: Decimal
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. The AC flag has no other use to the programmer and no instructions other than a MOV or a direct bit operation to the PSW affect the AC flag.
Page 80
Week #10
Arithmetic: Decimal
Page 81
Week #10
Arithmetic: Decimal
Page 82
Week #10
Page 84
Week #10
Page 85
Week #10
Page 86
Week #10
Repeat problem 7.1 using BCD numbers. Thoughts on the Problem The numbers in the RAM locations must be in BCD before the problem begins. The largest number possible is 99d + 99d = 198d + 99d = 297d, so that up to two carries can be added to the MSB. The solution to this problem is identical to that for unsigned numbers, except a DA A must be added after each ADD instruction. If more bytes were added so that the MSB could exceed 09d, then a DA A would also be necessary after the ADDC opcodes.
ENE 334 MCS-51 Logical & Arithmetic Page 87 Week #10
Page 88
Week #10
Page 89
Week #10
Page 91
Week #10
Page 92
Week #10
Arithmetic: Summary
The 8051 can perform all four arithmetic operations: addition, subtraction, multiplication, and division. Signed and unsigned numbers may be used in addition and subtraction; an OV flag is provided to signal programmer errors in estimating signed number magnitudes needed and to adjust signed number results. Multiplication and division use unsigned numbers. BCD arithmetic may be done using the DA A and ADD or ADDC instructions.
ENE 334 MCS-51 Logical & Arithmetic Page 93 Week #10
Arithmetic: Summary
Page 94
Week #10
Hw:
Page 95
Week #10