Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Unit 4 Arithmetic-Logic Instructions

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 38

The 8051 Microcontroller and Embedded Systems

CHAPTER 6 ARITHMETIC and LOGIC INSTRUCTIONS

CHAPTER 14 Interfacing to External Memory


1
10/20/11 Unit 4 Logic & Numerical methods

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

Unit 4 Logic & Numerical methods

Addition of unsigned numbers


The form of the ADD instruction is

ADD A, source

;A = A + source

10/20/11

Unit 4 Logic & Numerical methods

Addition of individual bytes

10/20/11

Unit 4 Logic & Numerical methods

ADDC and addition of 16-bit numbers

10/20/11

Unit 4 Logic & Numerical methods

BCD (binary coded decimal) number system

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

Unit 4 Logic & Numerical methods

ASCII numbers

Table 65

ASCII Code for Digits 09


Unit 4 Logic & Numerical methods

10/20/11

SECTION 6.1: ARITHMETIC INSTRUCTIONS

Unpacked BCD

Figure 61

BCD Code
10/20/11 Unit 4 Logic & Numerical methods

BCD (binary coded decimal) number system

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

Unit 4 Logic & Numerical methods

BCD (binary coded decimal) number system


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

Unit 4 Logic & Numerical methods

BCD (binary coded decimal) number system

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)

DA A must be used after the addition of BCD operands.

Adds 6 to lower/higher nibble as needed

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

Subtraction of unsigned numbers

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

If CY =0 after SUBB, result is positive If CY=1 after SUBB, result is negative

(2) with CY = l prior to SUBB (used for multibyte subtractions)

1 is subtracted form result


10/20/11 Unit 4 Logic & Numerical methods

12

Subtraction of unsigned numbers

13

Refer to example 6-7 (p.146) for CY=1 prior to SUBB


10/20/11 Unit 4 Logic & Numerical methods

Subtraction of unsigned numbers


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

Subtraction of unsigned numbers

SUBB (subtract with borrow) when CY = 1

15

10/20/11

Unit 4 Logic & Numerical methods

UNSIGNED MULTIPLICATION AND DIVISION

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

Unit 4 Logic & Numerical methods

Multiplication of unsigned numbers

The 8051 supports byte-by-byte multiplication only.

The bytes are assumed to be unsigned data.


MUL AB ;A x B, place 16-bit result in B and A

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

MOV A,#25H MOV B,#65H MUL AB

17

Division of unsigned numbers

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

Unit 4 Logic & Numerical methods

Division of unsigned numbers


MOV A,#95 MOV B,#10 DIV AB ;load 95 into A ;load 10 into B ;now A = 09 (quotient) and ;B = 05 (remainder)

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

Unit 4 Logic & Numerical methods

SECTION 6.2: SIGNED NUMBER CONCEPTS AND ARITHMETIC OPERATIONS

Concept of signed numbers in computers

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

Signed 8-bit operands

In signed byte operands

D7 (MSB) is the sign


If D7 = 0, the operand is positive if D7 = 1, it is negative.

D0 to D6 is the magnitude of the number.

21

10/20/11

Unit 4 Logic & Numerical methods

Positive numbers

The range of positive numbers (D7=0) that can be represented is 0 to +127.


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

Unit 4 Logic & Numerical methods

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

Unit 4 Logic & Numerical methods

Overflow problem in signed number operations

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

CJNE destination,source,relative address

25

10/20/11

Unit 4 Logic & Numerical methods

SECTION 6.3: LOGIC AND COMPARE INSTRUCTIONS

Compare instruction

Table 63

Carry Flag Setting For CJNE Instruction


10/20/11 Unit 4 Logic & Numerical methods

26

SECTION 6.4: ROTATE INSTRUCTION AND DATA SERIALIZATION

Rotating through the carry

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

Unit 4 Logic & Numerical methods

Serializing a byte of data

29

10/20/11

Unit 4 Logic & Numerical methods

Single-bit operations with CY


Instructions to modify CY directly

30

10/20/11

Unit 4 Logic & Numerical methods

Interfacing to External Memory

Three characteristics of semiconductor memory


1. 2.

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

Address Read activate Data on data pins


10/20/11 Unit 4 Logic & Numerical methods

31

ROM Memory types


ROM Read Only Memory non-volatile memory PROM Programmable ROM

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

EPROM Erasable PROM

EEPROM - Electrically Erasable PROM


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 Memory types

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

Must be refreshed regularly; data cant be accessed during refresh

NV-RAM Non-Volatile RAM can be Read/Write but holds data on power off
10/20/11 Unit 4 Logic & Numerical methods

33

Memory Address Decoding

Chips use chip select (CS) to activate memory content


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

Address lines of CPU to chip


34

Memory Address Decoding

Address decoding can be done with Simple logic gates 3-8 decoder (74LS138) Programmable logic requires burner to program

35

10/20/11

Unit 4 Logic & Numerical methods

8051 External Data ROM

36

10/20/11

Unit 4 Logic & Numerical methods

Accessing External Data


For external program code in ROM use

PSEN pin of CPU to OE of memory chip

For external data in ROM use

Read (RD) pin of CPU to OE of memory chip


MOV R2, #30 ;set counter for 30 bytes of data MOV DPTR, #1000H ;address of external data AGAIN: MOVX A, @DPTR MOV P1, A INC DPTR DJNZ R2, AGAIN

Use MOVX instruction to fetch memory data

37

10/20/11

Unit 4 Logic & Numerical methods

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

#1,6(b),12-14,16,24,30a,42, and 45 (pp. 174-178)


10/20/11 Unit 4 Logic & Numerical methods

38

You might also like