ASCII and BCD Arithmetic Chapter 11 S. Dandamudi.

Slides:



Advertisements
Similar presentations
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)
Advertisements

Introduction to Computer Engineering by Richard E. Haskell BCD Arithmetic Module M16.5 Section 10.4.
Introduction to Computer Engineering by Richard E. Haskell Multiplication and Division Instructions Module M16.4 Section 10.4.
ICS312 Set 2 Representation of Numbers and Characters.
1 x86’s instruction sets. 2 Instruction Set Classification  Transfer Move  Arithmetic Add / Subtract Mul / Div, etc.  Control Jump Call / Return, etc.
Assembly Language for x86 Processors 6th Edition
Department of Computer Science and Software Engineering
ACOE2511 Assembly Language Arithmetic and Logic Instructions.
Computer Organization & Assembly Language
Assembly Language Lecture 9
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, MUL Instruction The MUL (unsigned multiply) instruction.
Gursharan Singh Tatla 21-Nov-20101www.eazynotes.com.
8086 : INSTRUCTION SET By, Pramod Sunagar Assistant Professor
© 2006 Pearson Education, Upper Saddle River, NJ All Rights Reserved.Brey: The Intel Microprocessors, 7e Chapter 5 Arithmetic and Logic Instructions.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may.
Shift and Rotate Instructions
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may modify.
MUL Instruction (Unsigned Multiply) Multiplies an 8-, 16-, or 32-bit operand by either AL, AX or EAX. MUL r/m8 MUL r/m16 MUL r/m32.
Lecture – 5 Assembly Language Programming
Data Representation in Computers. Data Representation in Computers/Session 3 / 2 of 33 Number systems  The additive approach – Number earlier consisted.
Assembly Language – Lab 5
The 8051 Microcontroller and Embedded Systems
Khaled A. Al-Utaibi  Introduction  Arithmetic Instructions  Basic Logical Instructions  Shift Instructions  Rotate Instructions.
Lecture 05: Assembly Language Programming (2). The 80x86 IBM PC and Compatible Computers Chapter 3 Arithmetic & Logic Instructions and Programs Chapter.
Flag Control instructions CLC clear carry flag CF = 0 STC set carry flag CF= 1 CMC complement carry flag [CF] CF.
ICS312 Set 1 Representation of Numbers and Characters.
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.
CIS 020 Assembly Programming Chapter 12 - RR-Format Instructions & more RX-Format Instructions © John Urrutia 2012, All Rights Reserved.5/27/20121.
1 Lecture 3 ENGRE 254 1/14/09. 2 Lecture 1 review Digital signals assume two values represented by “0” and “1”. Typically a “0” represents a voltage near.
Microprocessors Monday, Apr. 13 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.
Arithmetic Flags and Instructions
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 4 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 05.b: Arithmetic Operations Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,
CT215: Assembly Language Programming
ECE 353 Introduction to Microprocessor Systems Michael G. Morrow, P.E. Week 6.
Assembly Language for Intel-Based Computers, 4 th Edition Unpacked and Packed Integers (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for x86 Processors 6th Edition Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may modify and copy.
Arithmetic and Logic Instructions
Chapter 7: Integer Arithmetic. 2 Chapter Overview Shift and Rotate Instructions Shift and Rotate Applications Multiplication and Division Instructions.
Lecture 12 Integer Arithmetic Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Arithmetic Flags and Instructions Chapter 7 S. Dandamudi.
Irvine, Kip R. Assembly Language for Intel-Based Computers. Chapter 7: Integer Arithmetic Slides to Accompany Assembly Language for Intel-Based Computers,
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may.
Assembly Language for x86 Processors 7th Edition
Introduction Arithmetic instructions are used to perform arithmetic operation such as Addition Subtraction Multiplication Division These operations can.
Microprocessor & Assembly Language Arithmetic and logical Instructions.
ECE 353 Introduction to Microprocessor Systems Michael J. Schulte Week 6.
Computer and Information Sciences College / Computer Science Department CS 206 D Computer Organization and Assembly Language.
Multiplication and Division instructions Dr.Hadi AL Saadi.
Lecture – 5 Assembly Language Programming
Integer Multiplication and Division
Assembly Language for Intel-Based Computers, 5th Edition
Assembly Language for x86 Processors 7th Edition
Multiplication and Division Instructions
4.2 Arithmetic Instructions
INSTRUCTION SET OF 8086 PAWAN KUMAR SINGH.
X86’s instruction sets.
Chapter 4: Instructions
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Microprocessor and Assembly Language
Shift & Rotate Instructions)
Multiplication and Division Instructions
ADDITION Register Addition. ADD AX,BX AX=AX+BX 2. Immediate Addition.
Assembly Language for Intel-Based Computers, 4th Edition
Chapter 5 Arithmetic and Logic Instructions
Multiplication and Division Instructions
Multiplication and Division Instructions
Introduction To Number Systems
Presentation transcript:

ASCII and BCD Arithmetic Chapter 11 S. Dandamudi

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 11: Page 2 Outline Representation of Numbers  ASCII representation  BCD representation »Unpacked BCD »Packed BCD Processing ASCII numbers »ASCII addition »ASCII subtraction »ASCII multiplication »ASCII division  Example: Multidigit ASCII addition Processing packed BCD numbers  Packed BCD addition  Packed BCD subtraction  Example: Multibyte packed BCD addition Performance: Decimal versus binary arithmetic

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 11: Page 3 Representation of Numbers Numbers are in ASCII form  when received from keyboard  when sending to the display Binary form is efficient to process numbers internally

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 11: Page 4 Representation of Numbers (cont’d) Requires conversion between these two number representations »We have used GetInt / GetLint and PutInt / PutLint to perform these two conversions In some applications, processing of numbers is simple (e.g. a simple addition) »Does not justify the input and output conversion overheads »In this case, it is better to process numbers in the decimal form Decimal numbers can be represented in »ASCII »BCD

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 11: Page 5 Representation of Numbers (cont’d) ASCII representation  Numbers are stored as a string of ASCII characters »Example: 1234 is stored as H  ASCII for 1 is 31H, for 2 is 32H, etc. BCD representation  Unpacked BCD »Example: 1234 is stored as H –Additional byte is used for sign  Sign byte: 00H for + and 80H for   Packed BCD »Saves space by packing two digits into a byte –Example: 1234 is stored as 12 34H

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 11: Page 6 Processing ASCII Numbers Pentium provides four instructions aaa  ASCII adjust after addition aas  ASCII adjust after subtraction aam  ASCII adjust after multiplication aad  ASCII adjust before division  These instructions do not take any operands »Operand is assumed to be in AL

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 11: Page 7 Processing ASCII Numbers (cont’d) ASCII addition Example 1Example 2 34H = B36H = B 35H = B37H = B 69H = B6DH = B Should be 09HShould be 13H Ignore 6Ignore 6 and add 9 to D The aaa instruction performs these adjustments to the byte in AL register

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 11: Page 8 Processing ASCII Numbers (cont’d) The aaa instruction works as follows:  If the least significant four bits in AL are > 9 or if AF =1, it adds 6 to AL and 1 to AH. –Both CF and AF are set  In all cases, the most significant four bits in AL are cleared  Example: sub AH,AH ; clear AH mov AL,'6' ; AL = 36H add AL,'7' ; AL = 36H+37H = 6DH aaa ; AX = 0103H or AL,30H ; AL = 33H

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 11: Page 9 Processing ASCII Numbers (cont’d) ASCII subtraction The aas instruction works as follows:  If the least significant four bits in AL are > 9 or if AF =1, it subtracts 6 from AL and 1 from AH. –Both CF and AF are set  In all cases, the most significant four bits in AL are cleared This adjustment is needed only if the result is negative

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 11: Page 10 Processing ASCII Numbers (cont’d) Example 1: Positive result sub AH,AH ; clear AH mov AL,'9' ; AL = 39H sub AL,'3' ; AL = 39H-33H = 6H aas ; AX = 0006H or AL,30H ; AL = 36H Example 2: Negative result sub AH,AH ; clear AH mov AL,'3' ; AL = 33H sub AL,'9' ; AL = 33H-39H = FAH aas ; AX = FF04H or AL,30H ; AL = 34H

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 11: Page 11 Processing ASCII Numbers (cont’d) ASCII multiplication The aam instruction adjusts the result of a mul instruction  Multiplication should not be performed on ASCII »Can be done on unpacked BCD The aam instruction works as follows  AL is divided by 10  Quotient is stored in AH  Remainder in AL aam does not work with imul instruction

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 11: Page 12 Processing ASCII Numbers (cont’d) Example 1 mov AL,3 ; multiplier in unpacked BCD form mov BL,9 ; multiplicand in unpacked BCD form mul BL ; result 001BH is in AX aam ; AX = 0207H or AX,3030H ; AX = 3237H Example 2 mov AL,'3' ; multiplier in ASCII mov BL,'9' ; multiplicand in ASCII and AL,0FH ; multiplier in unpacked BCD form and BL,0FH ; multiplicand in unpacked BCD form mul BL ; result 001BH is in AX aam ; AX = 0207H or AL,30H ; AL = 37H

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 11: Page 13 Processing ASCII Numbers (cont’d) ASCII division The aad instruction adjusts the numerator in AX before dividing two unpacked decimal numbers  The denominator is a single unpacked byte The aad instruction works as follows  Multiplies AH by 10 and adds it to AL and sets AH to 0  Example: »If AX is 0207H before aad »AX is changed to 001BH after aad aad instruction reverses the changes done by aam

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 11: Page 14 Processing ASCII Numbers (cont’d) Example: Divide 27 by 5 mov AX,0207H ; dividend in unpacked BCD form mov BL,05H ; divisor in unpacked BCD form aad ; AX = 001BH div BL ; AX = 0205H aad converts the unpacked BCD number in AX to binary form so that div can be used Example: Multidigit ASCII addition  ASCIIADD.ASM  Adds two 10-digit numbers »Adds one digit at a time starting with the rightmost digit

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 11: Page 15 Processing Packed BCD Numbers Two instructions to process packed BCD numbers daa  Decimal adjust after addition  Used after add or adc instruction das  Decimal adjust after subtraction  Used after sub or sbb instruction  No support for multiplication or division »For these operations –Unpack the numbers –Perform the operation –Repack them

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 11: Page 16 Processing Packed BCD Numbers (cont’d) Packed BCD addition Example 1Example 2 29H = B27H = B 69H = B34H = B 92H = B5BH = B Should be 98H (add 6)Should be 61H (add 6) Example 3 52H = B 61H = B B3H = B Should be 13H (add 60H)

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 11: Page 17 Processing Packed BCD Numbers (cont’d) The daa instruction works as follows:  If the least significant four bits in AL are > 9 or if AF =1, it adds 6 to AL and sets AF  If the most significant four bits in AL are > 9 or if CF =1, it adds 60H to AL and sets CF Example: mov AL,71H add AL,43H ; AL = B4H daa ; AL = 14H and CF = 1  The result including the carry (i.e., 114H) is the correct answer

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 11: Page 18 Processing Packed BCD Numbers (cont’d) Packed BCD subtraction The das instruction works as follows:  If the least significant four bits in AL are > 9 or if AF =1, it subtracts 6 from AL and sets AF  If the most significant four bits in AL are > 9 or if CF =1, it subtracts 60H from AL and sets CF Example: mov AL,71H sub AL,43H ; AL = 2EH das ; AL = 28H

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 11: Page 19 Processing Packed BCD Numbers (cont’d) Example: Multibyte packed BCD addition Adds two 10-digit numbers  Adds two digits at a time starting from the rightmost pair

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 11: Page 20 Performance: Decimal vs. Binary Arithmetic Tradeoffs associated with the three representations