Unit 4 Arithmetic-Logic Instructions
Unit 4 Arithmetic-Logic Instructions
Unit 4 Arithmetic-Logic Instructions
OBJECTIVES
Define the range of numbers possible in 8051 unsigned data Code addition and subtraction instructions for unsigned data Perform addition of BCD data Code 8051 unsigned data multiplication and division instructions Code 8051 Assembly language logic instructions AND, OR, and EX-OR Use 8051 logic instructions for bit manipulation Code 8051 rotate instruction and data serialization Explain the BCD (binary coded decimal) system of data representation Contrast and compare packed and unpacked BCD data
10/20/11
ADD A, source
;A = A + source
10/20/11
10/20/11
10/20/11
Unpacked BCD
The lower 4 bits of the number represent the BCD number. The rest of the bits are 0. For example, "0000 1001" and "0000 0101" are unpacked BCD for 9 and 5, respectively. Unpacked BCD requires 1 byte of memory or an 8-bit register to contain it.
10/20/11
ASCII numbers
Table 65
10/20/11
Unpacked BCD
Figure 61
BCD Code
10/20/11 Unit 4 Logic & Numerical methods
Packed BCD
A single byte has two BCD numbers in it, one in the lower 4 bits, and one in the upper 4 bits. For example, "0101 1001" is packed BCD for 59H. It takes only 1 byte of memory to store the packed BCD operands. Its more efficient than unpacked BCD.
10/20/11
There is a problem with adding BCD numbers. Adding two BCD numbers must give a BCD result. After adding packed BCD numbers, the result is no longer BCD.
MOV A, #17H ADD A,#28H ;packed BCD (0001 0111) ;packed BCD (0010 1000) ;A = 3F which is not BCD ;should be 17H + 28H = 45H as packed BCD "DA A" is designed to correct the BCD addition problem.
10
10/20/11
DA instruction
MOV MOV ADD DA A,#47H B,#25H A,B A ;A=47H first BCD operand ;B=25 second BCD operand ;hex (binary) addition (A=6CH) ;adjust for BCD addition (A=72H)
Important to note that DA A works only after an ADD instruction, it will not work after the INC instruction.
10/20/11 Unit 4 Logic & Numerical methods
11
SUBB A, source ;A = A - source CY In the 8051 we have only have subtract with borrow SUBB. There are two cases for the SUBB instruction: (1) with CY = 0 prior to SUBB
12
13
If the CY = 0 after the execution of SUBB, the result is positive. If CY = 1, the result is negative and the destination has the 2's complement of the result. Normally, the result is left in 2's complement, but the CPL (complement) and INC instructions can be used to change it.
The CPL instruction performs the 1's complement of the operand then the operand is incremented (INC adds 1)) to get the 2's complement.
10/20/11 Unit 4 Logic & Numerical methods
14
15
10/20/11
In multiplying or dividing two numbers in the 8051, the use of registers A and B is required.
The multiplication and division instructions work only with these two registers.
16
10/20/11
After multiplication, the result is in the A and B registers. The lower byte is in A, and the upper byte is in B. ;load 25H to reg. A ;load 65H in reg. B ;25H * 65H = E99 where ;B = 0EH and A = 99H
10/20/11 Unit 4 Logic & Numerical methods
17
In the division of unsigned numbers, the 8051 supports byte over byte only.
DIV AB ;divide A by B The numerator must be in register A and the denominator must be in B. After the DIV instruction is performed, the quotient is in A and the remainder is in B.
18
10/20/11
This instruction always makes CY = 0 and OV = 0 if the denominator is not 0. If the denominator is 0 (B = 0), OV = 1 indicates an error, and CY = 0.
The standard practice in all microprocessors when dividing a number by 0 is to indicate in some way the invalid result of infinity. In the 805I, the OV flag is set to 1.
19
10/20/11
Computers must be able to accommodate sign numbers. Computer scientists have devised the following arrangement for the representation of signed positive and negative numbers:
The most significant bit (MSB) is set aside for the sign (+ or -), while the rest of the bits are used for the magnitude. The sign is represented by 0 for positive (+) numbers 1 for negative (- ) numbers.
10/20/11 Unit 4 Logic & Numerical methods
20
21
10/20/11
Positive numbers
If a positive number is > +127 and < +255 the value is correct as unsigned positive number If positive number >255, a 16-bit size operand must be used. Refer to Example 6-13
22
10/20/11
Negative numbers
For negative numbers, D7 is1. The magnitude is represented in its 2's complement. To convert to negative number representation (2's complement):
1. Write the magnitude of the number in 8-bit binary (no sign). 2. Invert each bit. 3. Add 1 to it.
23
10/20/11
When using signed numbers, a serious problem arises that must be dealt with. This is the overflow problem.
The 8051 indicates the existence of an error by raising the OV (overflow) flag PSW.2 at RAM D2H. If the result of an operation on signed numbers is too large for the register (<-128 or >127), an overflow has occurred and the programmer must be notified.
10/20/11 Unit 4 Logic & Numerical methods
24
Compare instruction
25
10/20/11
Compare instruction
Table 63
26
In the 8051 the rotation instructions RL, RR, RLC, and RRC are designed to rotate the accumulator right or left. To rotate a byte the operand must be in register A. There are two type of rotations. One is a simple rotation of the bits of A, and the other is a rotation through the carry.
10/20/11 Unit 4 Logic & Numerical methods
27
Serializing data
Serializing data is a way of sending a byte of data one bit at a time through a single pin of microcontroller. There are two ways to transfer a byte of data serially:
1. Using the serial port. The details of serial port data transfer are discussed in Chapter 10. 2. The second method of serializing data is to transfer data one bit at a time and control the sequence of data and spaces in between them.
In many new devices such as LCD, ADC, and ROM, the serial versions of these devices are becoming popular since they take less space on a printed circuit board.
28
10/20/11
29
10/20/11
30
10/20/11
Memory capacity - number of bits chip can store Memory organization- number of stored locations equal to number of address lines
Each location can hold 1,4,8,16 bits or more Number of bits/location = number of data pins on chip
3.
Speed time for data to show on data pins called access time
31
Programmed by blowing fuses programmable once UV erasable erases entire ROM contents in 20 minutes
Can erase one byte instantly Must have circuitry to erase on system board
Flash memory EPROM similar to EEPROM but erasure is complete Mask ROM ROM programmed by manufacture
10/20/11 Unit 4 Logic & Numerical methods
32
RAM Random Access Memory - volatile memory can Read or Write SRAM Static RAM -uses flip-flops to store data Does NOT need refresh to maintain data DRAM Dynamic RAM uses capacitor to store data
NV-RAM Non-Volatile RAM can be Read/Write but holds data on power off
10/20/11 Unit 4 Logic & Numerical methods
33
Data bus of CPU connected to data pins on memory chip Control signals from CPU to memory chip
Read(RD) of CPU to Output Enable (OE) of chip Write (WR) of CPU to Write Enable (WE) of chip Upper address line to CS of chip Lower address lines to address lines of chip
10/20/11 Unit 4 Logic & Numerical methods
34
Address decoding can be done with Simple logic gates 3-8 decoder (74LS138) Programmable logic requires burner to program
35
10/20/11
36
10/20/11
37
10/20/11
Assignments
This week Demo of problems 6(a) p. 175 for SUBB 16a p. 175 Lab 4 activities 1 to 4 Next week Exam 1 chapters 0 to 6 open book; open notes Read chapter 17 (pp. 491-515) Lab 5 activities 1 to 4 Problems chapter 6
38