Chapter 6 Arithmetic Instructions and Programs

Slides:



Advertisements
Similar presentations
Week 3. Assembly Language Programming  Difficult when starting assembly programming  Have to work at low level  Use processor instructions >Requires.
Advertisements

MOV Instruction MOV destination, source ; copy source to dest. MOV A,#55H ;load value 55H into reg. A MOV R0,A ;copy contents of A into R0 ;(now A=R0=55H)
Chapter Programming in C
ICS312 Set 2 Representation of Numbers and Characters.
Msc. Ivan A. Escobar Broitman Microprocessors 1 1 The 8051 Instruction Set.
HEXADECIMAL NUMBERS Code
ARITHMETIC, LOGIC INSTRUCTIONS, AND PROGRAMS
Chapter 6 Arithmetic, Logic Instructions, and Programs
TK 2633 Microprocessor & Interfacing Lecture 3: Introduction to 8085 Assembly Language Programming (2) 1 Prepared By: Associate Prof. Dr Masri Ayob.
Chapter 4 Operations on Bits
Chapter 7 Logic Instructions and Programs
Room: E-3-31 Phone: Dr Masri Ayob TK 2633 Microprocessor & Interfacing Lecture 1: Introduction to 8085 Assembly Language.
Dr Masri Ayob TK 2633: Microprocessor & Interfacing Lecture 5: Arithmetic and Logic Instructions.
8051 ASSEMBLY LANGUAGE PROGRAMMING
DIGITAL SYSTEMS TCE1111 Representation and Arithmetic Operations with Signed Numbers Week 6 and 7 (Lecture 1 of 2)
Microcontroller Intel 8051
1 Lecture 2: Number Systems Binary numbers Base conversion Arithmetic Number systems  Sign and magnitude  Ones-complement  Twos-complement Binary-coded.
The M68HC11 Basic Instruction Set Basic Arithmetic Instructions
Dr. Bernard Chen Ph.D. University of Central Arkansas
Arithmetic for Computers
©2010 Cengage Learning SLIDES FOR CHAPTER 1 INTRODUCTION NUMBER SYSTEMS AND CONVERSION Click the mouse to move to the next page. Use the ESC key to exit.
Computer Arithmetic Nizamettin AYDIN
The 8051 Microcontroller and Embedded Systems
CoE3DJ4 Digital Systems Design Chapter 3: instruction set summary.
Summer 2014 Chapter 1: Basic Concepts. Irvine, Kip R. Assembly Language for Intel-Based Computers 6/e, Chapter Overview Welcome to Assembly Language.
Khaled A. Al-Utaibi  Introduction  Arithmetic Instructions  Basic Logical Instructions  Shift Instructions  Rotate Instructions.
IT253: Computer Organization
ECEN2102 Digital Logic Design Lecture 1 Numbers Systems Abdullah Said Alkalbani University of Buraimi.
ICS312 Set 1 Representation of Numbers and Characters.
10-Sep Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept Representing Information in Computers:  numbers: counting numbers,
ASCII and BCD Arithmetic Chapter 11 S. Dandamudi.
Embedded System Spring, 2011 Lecture 10: Arithmetic, Logic Instruction and Programs Eng. Wazen M. Shbair.
Chapter four – The 80x86 Instruction Set Principles of Microcomputers 2015年10月19日 2015年10月19日 2015年10月19日 2015年10月19日 2015年10月19日 2015年10月19日 1 Chapter.
1 EENG 2710 Chapter 1 Number Systems and Codes. 2 Chapter 1 Homework 1.1c, 1.2c, 1.3c, 1.4e, 1.5e, 1.6c, 1.7e, 1.8a, 1.9a, 1.10b, 1.13a, 1.19.
Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.
University Of Engineering And Technology Taxila REF::NATIONAL TAIWANOCEAN UNIVERSITY 國立台灣海洋大學 Chapter 3 JUMP, LOOP and CALL Instructions.
Operations on Bits Arithmetic Operations Logic Operations
Microprocessors Monday, Apr. 13 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.
Chapter 19 Number Systems. Irvine, Kip R. Assembly Language for Intel-Based Computers, Translating Languages English: Display the sum of A times.
Computer Math CPS120 Introduction to Computer Science Lecture 4.
AEEE2031 Data Representation and Numbering Systems.
Ass. Prof. Dr Masri Ayob Lecture 5: Arithmetic and Logic Instructions TK 2633: Microprocessor & Interfacing.
Introduction to Microprocessors Chapter 2. Decimal or Base 10 Numbers  Have ten different digits (0-9)  It is a weighted number system. Each position.
1 Logic, Shift, and Rotate Instructions Read Sections 6.2, 7.2 and 7.3 of textbook.
Introduction To Number Systems Binary System M. AL-Towaileb1.
CHAPTER 6 ARITHMETIC, LOGIC INSTRUCTIONS, AND PROGRAMS.
Microprocessor & Assembly Language Arithmetic and logical Instructions.
Number Systems. The position of each digit in a weighted number system is assigned a weight based on the base or radix of the system. The radix of decimal.
Arithmetic Instructions and Programs. Outlines Range of numbers in 8051 unsigned data Range of numbers in 8051 unsigned data Addition & subtraction instructions.
Cosc 2150: Computer Organization
Classification of Instruction Set of 8051
Assembly Language Programming of 8085
Lecture Set 5 The 8051 Instruction Set.
Data Processing Instructions
Arithmetic and Logic Chapter 5
Instruction Groups The 8051 has 255 instructions.
Memory Organisation Source: under
Arithmetic operations Programming
The 8051 Assembly Language Arithmetic & Logic Instructions
Number Systems and Codes
Introduction to Micro Controllers & Embedded System Design Instruction set Department of Electrical & Computer Engineering Missouri University of Science.
Digital Logic & Design Lecture 03.
Introduction to Micro Controllers & Embedded System Design Instruction set Department of Electrical & Computer Engineering Missouri University of Science.
DMT 245 Introduction to Microcontroller
2’s Complement form 1’s complement form 2’s complement form
Arithmetic and Logic Chapter 5
Chapter 5 Arithmetic and Logic Instructions
8051 ASSEMBLY LANGUAGE PROGRAMMING
ARITHMETIC, LOGIC INSTRUCTIONS, AND PROGRAMS
Presentation transcript:

Chapter 6 Arithmetic Instructions and Programs

Sections 6.1 Unsigned Addition and Subtraction 6.2 Unsigned Multiplication and Division 6.3 Signed Number Concepts and Arithmetic Operations

Objective 介紹關於加減乘除的指令。 8051 內部運算都是以 byte 為單位,所以當資料量會有機會超過 one byte 時,programmer 自己要小心。 由於 8051 內部運算其實是很簡單的,你可想成都是 unsigned number 的運算。所以當我們想要做 signed number 或 BCD 的運算時,就必需靠 programmer 自己觀察 PSW 的變化與增加許多的檢查。

Section 6.1 Unsigned Addition and Subtraction

Unsigned Numbers Unsigned numbers are defined as data in which all the bits are used to represent data, and no bits are set aside for the positive or negative sign. For a 8-bit data, the operand can be between 00 and FFH.

Addition of Unsigned Numbers Add the source operand to register A and put the result in A. ADD A, source A + source  A MOV A,#25H ;load 25H into A ADD A,#34H ;add 34H to A,now A=59H The destination operation is always in A. The instruction could change CY,AC,OV and P.

Example 6-1 Show how the flag register is affected by the following instructions. MOV A,#0F5H ;A=F5 hex ADD A,#0BH ;A=F5+0B=00 Solution: F5H 1111 0101 +0BH +0000 1011 100H 1 0000 0000 After the addition, register A = 00H and the flags are as follows: CY = 1 since there is a carry out from D7. P = 1 because the number of 1s is 0 (an even number), P is set to 1. AC = 1 since there is a carry from D3 to D4.

Addition of Individual Bytes To calculate the sum of any number of operands, the carry flag should be checked after the addition of each operation. A 1111 0101 + R0 0000 1011 1 0000 0000 CY=1 Add 1 to R7 Result : 0000 0001 0000 0000 R7 A

Example 6-2 Assume that RAM locations 40-44 have the following values. Write a program to find the sum of the values. At the end of the program, register A should contain the low byte and R7 the high byte. All values are in hex. 40=(7D) 41=(EB) 42=(C5) 43=(5B) 44=(30) Solution: MOV R0,#40H ;load pointer MOV R2,#5 ;load counter CLR A ;A=0 MOV R7,A ;clear R7 AGAIN:ADD A,@R0 ;add (R0) JNC NEXT ;jump to carry INC R7 ;keep track of carries NEXT: INC R0 ;increment pointer DJNZ R2,AGAIN ;repeat until R2 is zero In the first iteration of the loop, 7DH is added to A with CY=0 and R7=00, and the counter R2=04. In the second iteration of the loop, EBH is added to A with CY=1. Since a carry occurred, R7 is incremented. No the counter R2=03H, R7=1, A=68H. C5H is added to A, which makes CY=1, A=2DH, R7=02H, R2=02H. 5BH is added to A, which makes CY=0, A=88H, R7=02H, R2=01H. 30H is added to A, which makes CY=1, A=B8H, R7=02H. DJNZ R2, AGAIN, R2=00H and not jump to AGAIN

ADDC ADD with carry To add two 16-bit data operands ADDC A, source ; A=A+source+CY MOV A,#E7H 3C E7 1559110 ADD A,#8DH + 3B 8D 1524510 MOV R6,A 78 74 3083610 MOV A,#3CH CY=1 ADDC A,#3BH ; A=A+operand+CY MOV R7,A ; R7=78H R6=74H high byte low byte To add two 16-bit data operands, we need to be concerned with the propagation of a carry from the lower byte to the higher byte.

Example 6-3 Write a program to add two 16-bit numbers. The numbers are 3CE7H and 3B8DH. Place that sum in R7 and R6; R6 should have the lower byte. Solution: CLR C ;make CY=0 MOV A,#0E7H ;load the low byte now A=E7H ADD A,#8DH ;add the low byte,A=74H and CY=1 MOV R6,A ;save the low byte in R6 MOV A,#3CH ;load the high byte ADDC A,#3BH ;add with the carry MOV R7,A ;save the high byte of the sum

BCD Number System Digit BCD 1 2 3 4 5 6 7 8 9 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 Binary Coded Decimal : use binary to represent 0-9 only. See Table 6.1 BCD Code Two terms for BCD number: Unpacked BCD Packed BCD

Unpacked / Packed BCD Numbers In Unpacked BCD, a byte is used to contain a BCD number. 0000 0101 is unpacked BCD for 5 0000 1001 is unpacked BCD for 9 In Packed BCD, a byte has two BCD numbers. 0101 1001 is packed BCD for 59. It is twice as efficient in storing data. When we get two BCD numbers, we want to add them directly in form of packed BCD. However, ADD and ADDC are just for binary!

Addition of Packed BCD Numbers Use ADD and ADDC to add two packed BCD numbers: To calculate 1710+1810=3510. MOV A,#17H ADD A,#18H The programmer must add 6 to the low digital. Then we get the correct packed BCD sum (35H). 1 7 + 1 8 2 F + 0 6 3 5

DA Decimal adjust for addition DA instruction will add 6 to the lower nibble or higher nibble if needed. DA A MOV A,#47H 4 7 MOV B,#55H + 5 5 ADD A,B 9 C DA A A 2 ;check CY here CY=1 0 2 1. Lower nibble 2. Upper nibble

Example 6-4 Assume that 5 packed BCD data items are stored in RAM locations starting at 40H, as shown below. Write a program to find the sum of all the numbers. The result must be in BCD. 40=(71) 41=(11) 42=(65) 43=(59) 44=(37) Solution: MOV R0,#40H ;load pointer MOV R2,#5 ;load counter CLR A ;A=0 MOV R7,A ;clear R7 AGAIN: ADD A,@R0 ;addition DA A ;adjust for BCD JNC NEXT ;jump no carry INC R7 ;keep track of carries NEXT: INC R0 ;increment pointer DUNZ R2,AGAIN ;

SUBB Subtraction with borrow SUBB A, source ; A = A – source – CY CLR C MOV A,#3FH 3 F MOV R3,#23H - 2 3 SUBB A,R3 1 C After the subtraction, CY=0: positive result CY=1: negative result

Example 6-7 Analyze the following program: CLR C MOV A,#62H 27 62 1008210 SUBB A,#96H - 12 96 373410 MOV R7,A 14 CC 634810 MOV A,#27H SUBB A,#12H MOV R6,A Solution: After the SUBB, A = 62H-96H = CCH and CY=1indicating there is a borrow. Since CY = 1, when SUBB is executed the second time A = 27H-12H-1 =14H. Therefore, we have 2762H-1296H =14CCH. low byte high byte

Subtraction of Unsigned Numbers (1/2) The 8051 use adder circuitry to perform the subtraction command. In subtraction, the 8051 use the 2’s complement method. A-B = A+(100H-B)-100 = A+(2’s complement of B)-100 = A + (2’s complement of B) + (toggle CY) 3F 0011 1111 - 23 + 1101 1101 2’s complemet of 23H 1C 1 0001 1100 borrow 100H and carry  positive  toggle CY=0 A

Subtraction of Unsigned Numbers (2/2) The steps of the hardware of the CPU in executing the SUBB instruction: A minus the CY value. Take the 2’s complement of the subtrahend. Add it to the minuend (A). Invert the carry. After the subtraction, CY=0: positive result CY=1: negative result 3FH–23H=1CH A=A-CY=3FH 23H  DDH (2’s complement) 3FH+DDH=11CH, where CY=1,A=1CH. Toggle CY=0

Example 6-5 Show the steps involved in the following. CLR C MOV A,#3FH MOV R3,#23H SUBB A,R3 Solution: A = 3F 0011 1111 0011 1111 (A=A-CY) R3= 23 0010 0011 + 1101 1101 (2’s complement) 1C 1 0001 1100 CY=0 (Toggle CY) The flags would be set as follows: CY = 0, AC = 0 The programmer must look at the carry flag to determine if the result is positive or negative.

Example 6-6 Analyze the following program: CLR C MOV A,#4C SUBB A,#6EH JNC NEXT ;jump not carry (CY=0) CPL A ;get 2’s complement by INC A ; yourself NEXT:MOV R1,A ;save A in R1 Solution: 4C 0100 1100 - 6E + 1001 0010 2’s complemet of 6EH -22 1101 1110(A) Borrow 100H and no carry  negative  toggle CY  CY=1, the result is negative and get the 2’s complement of A =0010 0010=22.

Section 6.2 Unsigned Multiplication and Division

MUL AB The 8051 supports byte by byte multiplication only. Multiple A * B, result: B=high byte, A=low byte. MUL AB MOV A,#25H 25 MOV B,#65H + 65 MUL AB 0E 99 Multiplication of operands larger than 8-bit takes some manipulation. Programmer need to multiple byte by byte and sum them together. B=0EH: high byte; A=99H: low byte

Table 6-1: Unsigned Multiplication Summary (MUL AB) Operand 1 Operand 2 Result byte × byte A B A = low byte B = high byte

DIV AB The 8051 supports byte by byte division only. Divide A by B, result: B=remainder, A=quotient. DIV AB MOV A,#95H MOV B,#10H DIV AB ;A=09H (quotient) ;B=05H (remainder) IF the numerator=B=0, then OV=1 indicates an error and CY=0. numerator:分子(除數) denominator:分母(被除數) quotient:商 remainder:餘數

Table 6-2: Unsigned Division Summary (DIV AB) Numerator Denominator Quotient Remainder byte/byte A B

An Application for DIV There are times when an analog-to-digital converter is connected to a port. The ADC represents some quantity such as temperature or pressure. The 8-bit ADC provides data in hex. This hex data must be converted to decimal for display. We divide it by 10 repeatedly until the quotient is less than 10. See Example 6-8.

Example 6-8 (1/2) Write a program to get hex data in the range of 00 – FFH from port 1 and convert it to decimal. Save the digits in R7, R6 and R5, where the least significant digit is in R7. Solution of (a): MOV A,#0FFH MOV P1,A ;make P1 an input port MOV A,P1 ;read data from P1 MOV B,#10 ;divide by 10 DIV AB ;P1 has max value 255 MOV R7,B ;3 bytes to save 3 decimals MOV B,#10 DIV AB MOV R6,B MOV R5,A R5 R6 R7

Example 6-8 (2/2) (b) Analyze the program, assuming that P1 has a value of FDH for data. Solution of (b): In the case of an 8-bit binary = FDH = 253 in decimal. Q(A) R(B) (1) FD  0A= 19 3 (2) 19  0A= 2 5 Therefore, we have FDH = 253. In order to display this data it must be converted to ASCII, which is described in the next chapter. 2 5 3 R5 R6 R7

Section 6.3 Concepts and Arithmetic Operations

Signed Numbers In everyday life, numbers are used that could be positive or negative. Usually, 2’s complement is used for signed numbers.

Figure 6-2. 8-Bit Signed Operand magnitude The most significant bit (MSB) is used for the sign. The rest is used for the magnitude which is represented in its 2’s complement.

The Range of Sign Number for a Single Byte Decimal Binary Hexadecimal -128 1000 0000 80H = 100H-80H -127 1000 0001 81H = 100H-7FH : -2 1111 1110 FEH = 100H –2H -1 1111 1111 FFH = 100H –1H 0 0000 0000 00H +1 0000 0001 01H +2 0000 0010 02H +127 1111 1111 7FH 2‘s complement

Example 6-9 Show how the 8051 would represent -5. Solution: Observe the following steps: 0000 0101 5 in 8-bit binary 1111 1010 invert each bit 1111 1011 add 1 Therefore -5 = FBH, the signed number representation in 2’s complement for -5. We can get FBH=100H-05H=FBH, too.

Example 6-10 Show how the 8051 would represent -34H. Solution: Observe the following steps. 0011 0100 128 in 8-bit binary 0111 1111 invert each bit 1000 0000 add 1 (which becomes 80 in hex) Therefore -34 = CCH, the signed number representation in 2’s complement for -34H. We can get FBH=100H-34H=CCH, too.

Example 6-11 Show how the 8051 would represent –128. Solution: Observe the following steps. 1000 0000 128 in 8-bit binary 80H 0111 1111 invert each bit 1000 0000 add 1 (which becomes 80 in hex) Therefore -128 = 80H, the signed number representation in 2’s complement for -128. We can get FBH=100H-80H=FBH, too.

Overflow Problem The CPU understands only 0s and 1s and ignores the human convention of positive and negative numbers. The overflow flag (OV) is designed to indicate an overflow of the operations for the signed numbers. The accept range –128 to 127 in decimal. When is an overflow? If the result of an operation on signed numbers is too large for the 8-bit register, an overflow has occurred and the programmer must be notified.

Example 6-12 Examine the following code and analyze the result. MOV A,#+96 ;A=60H MOV R1,#+70 ;R1=46H ADD A,R1 ;A=A6H=-90, INVALID!! Solution: 96 0110 0000 + 70 0100 0110 + 166 1010 0110 (CY=0, OV=1) The signed value in the register A=A6H=-90 is wrong. Programmer must check it by themselves. Note: There is a carry from D6 to D7 but no carry out of D7 Simlab_8051 does not know #+96 and #-128.

When is an Overflow? In 8-bit signed number operations, OV is set to 1 if either of the following two conditions occurs: There is a carry from D6 to D7 but no carry out of D7. There is a carry from D7 out but no carry from D6 to D7. In both above cases, the overflow flag is set to 1.

Example 6-13 Observe the following, noting the role of the OV flag. MOV A,#-128 ;A=1000 0000(A=80H) MOV R4,#-2 ;R1=1111 1110(R4=FEH) ADD A,R4 ;A=0111 1110(A=7EH=126) Solution: -128 1000 0000 + -2 1111 1110 - 130 0111 1110 (CY=1,OV=1) There is a carry from D7 out but no carry from D6 to D7. According to the CPU, there is an overflow (OV = 1). The sign number is wrong.

Example 6-14 Observe the following, noting the OV flag. MOV A,#-2 ;A=1111 1110 (A=FEH) MOV R1,#-5 ;R1=1111 1011(R1=FBH) ADD A,R1 ;A=1111 1001 (A=F9H=-7,correct) Solution: -2 1111 1110 + -5 1111 1011 - 7 1111 1001 (CY=1,OV=0) There are carries from D7 out and from D6 to D7. According to the CPU, the result is -7, which is correct (OV = 0).

Example 6-15 Examine the following, noting the role of OV. MOV A,#+7 ;A=0000 0111 (A=07H) MOV R1,#+18 ;R1=0001 0010(R1=12H) ADD A,R1 ;A=0001 1001 (A=19H=+25,correct) Solution: 7 0000 0111 + 18 0001 0010 25 0001 1001 (CY=0,OV=0) No carry occurs. According to the CPU, the result is +25, which is correct (OV = 0).

You are able to(1/2) Define the range of numbers possible in 8051 unsigned data Code addition and subtraction instructions for unsigned data Explain the BCD(binary coded decimal)system of data representation Contrast and compare packed and unpacked BCD data Perform addition and subtraction on BCD data

Code 8051 unsigned data multiplication and division instructions You are able to(2/2) Code 8051 unsigned data multiplication and division instructions Define the range of numbers possible in 8051 signed data Code 8051 signed data arithmetic instructions Explain carry and overflow problems and their corrections

Homework Chapter 6 Problems:1,3,4,11,13,15 Note: In the problem, please find the CY, AC and OV flags for each the following. You can do it by the simulation tools. Please write and compile the program of Problems 3,4,11,13,15