ASCII and BCD Arithmetic. Chapter 11 S. Dandamudi



Similar documents
BCD (ASCII) Arithmetic. Where and Why is BCD used? Packed BCD, ASCII, Unpacked BCD. BCD Adjustment Instructions AAA. Example

Unpacked BCD Arithmetic. BCD (ASCII) Arithmetic. Where and Why is BCD used? From the SQL Server Manual. Packed BCD, ASCII, Unpacked BCD

Complete 8086 instruction set

LSN 2 Number Systems. ECT 224 Digital Computer Fundamentals. Department of Engineering Technology

Computer Science 281 Binary and Hexadecimal Review

Systems I: Computer Organization and Architecture

Lecture 8: Binary Multiplication & Division

CDA 3200 Digital Systems. Instructor: Dr. Janusz Zalewski Developed by: Dr. Dahai Guo Spring 2012

Binary Representation. Number Systems. Base 10, Base 2, Base 16. Positional Notation. Conversion of Any Base to Decimal.

Useful Number Systems

CS201: Architecture and Assembly Language

Oct: 50 8 = 6 (r = 2) 6 8 = 0 (r = 6) Writing the remainders in reverse order we get: (50) 10 = (62) 8

Chapter 4: Computer Codes

Number Systems. Introduction / Number Systems

2011, The McGraw-Hill Companies, Inc. Chapter 3

The 80x86 Instruction Set

King Fahd University of Petroleum and Minerals. College of Computer Science and Engineering. Computer Engineering Department COE 205

COMPSCI 210. Binary Fractions. Agenda & Reading

EE 261 Introduction to Logic Circuits. Module #2 Number Systems

Solution for Homework 2

YOU MUST BE ABLE TO DO THE FOLLOWING PROBLEMS WITHOUT A CALCULATOR!

NUMBER SYSTEMS. William Stallings

Lecture 2. Binary and Hexadecimal Numbers

Decimals Adding and Subtracting

Decimal to Binary Conversion

=

COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012

To convert an arbitrary power of 2 into its English equivalent, remember the rules of exponential arithmetic:

Solve addition and subtraction word problems, and add and subtract within 10, e.g., by using objects or drawings to represent the problem.

Binary Representation

Number and codes in digital systems

Divide: Paper & Pencil. Computer Architecture ALU Design : Division and Floating Point. Divide algorithm. DIVIDE HARDWARE Version 1

Assembly Language Tutorial

Paramedic Program Pre-Admission Mathematics Test Study Guide

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer

HOMEWORK # 2 SOLUTIO

Z80 Instruction Set. Z80 Assembly Language

Numeral Systems. The number twenty-five can be represented in many ways: Decimal system (base 10): 25 Roman numerals:

3. Convert a number from one number system to another

Recall the process used for adding decimal numbers. 1. Place the numbers to be added in vertical format, aligning the decimal points.

PREPARATION FOR MATH TESTING at CityLab Academy

ND48-RS ASCII A2.04 Communication Protocol

Faculty of Engineering Student Number:

AN617. Fixed Point Routines FIXED POINT ARITHMETIC INTRODUCTION. Thi d t t d ith F M k Design Consultant

Accuplacer Arithmetic Study Guide

Addition Methods. Methods Jottings Expanded Compact Examples = 15

CSI 333 Lecture 1 Number Systems

Chapter 2. Binary Values and Number Systems

Counting in base 10, 2 and 16

A single register, called the accumulator, stores the. operand before the operation, and stores the result. Add y # add y from memory to the acc

Digital System Design Prof. D Roychoudhry Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

The Hexadecimal Number System and Memory Addressing

MATH-0910 Review Concepts (Haugen)

Number Representation

plc numbers Encoded values; BCD and ASCII Error detection; parity, gray code and checksums

The string of digits in the binary number system represents the quantity

PROBLEMS (Cap. 4 - Istruzioni macchina)

CPEN Digital Logic Design Binary Systems

Solution Guide Chapter 14 Mixing Fractions, Decimals, and Percents Together

Chapter 1: Digital Systems and Binary Numbers

Sheet 7 (Chapter 10)

M6800. Assembly Language Programming

CS101 Lecture 11: Number Systems and Binary Numbers. Aaron Stevens 14 February 2011

Section 1.4 Place Value Systems of Numeration in Other Bases

Today. Binary addition Representing negative numbers. Andrew H. Fagg: Embedded Real- Time Systems: Binary Arithmetic

Grade 5 Common Core State Standard

Digital Design. Assoc. Prof. Dr. Berna Örs Yalçın

Cyber Security Workshop Encryption Reference Manual

NUMBER SYSTEMS. 1.1 Introduction

Arithmetic in MIPS. Objectives. Instruction. Integer arithmetic. After completing this lab you will:

Appendix C: Keyboard Scan Codes

Lecture 11: Number Systems

Number of bits needed to address hosts 8

INTRODUCTION TO FRACTIONS

Sequential Skills. Strands and Major Topics

Welcome to Basic Math Skills!

THUMB Instruction Set

8051 hardware summary

How To Use A Computer With A Screen On It (For A Powerbook)

How do you compare numbers? On a number line, larger numbers are to the right and smaller numbers are to the left.

Goals. Unary Numbers. Decimal Numbers. 3,148 is s 100 s 10 s 1 s. Number Bases 1/12/2009. COMP370 Intro to Computer Architecture 1

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 20: Stack Frames 7 March 08

BINARY CODED DECIMAL: B.C.D.

JobTestPrep's Numeracy Review Decimals & Percentages

ELEG3924 Microprocessor Ch.7 Programming In C

Chapter 1: Order of Operations, Fractions & Percents

Levent EREN A-306 Office Phone: INTRODUCTION TO DIGITAL LOGIC

PART B QUESTIONS AND ANSWERS UNIT I

MEP Y9 Practice Book A

Decimal Notations for Fractions Number and Operations Fractions /4.NF

PAYCHEX, INC. BASIC BUSINESS MATH TRAINING MODULE

NCTM Curriculum Focal Points for Grade 5. Everyday Mathematics, Grade 5

1 Classical Universal Computer 3

Binary Number System. 16. Binary Numbers. Base 10 digits: Base 2 digits: 0 1

Binary, Hexadecimal, Octal, and BCD Numbers

Copy in your notebook: Add an example of each term with the symbols used in algebra 2 if there are any.

1. Convert the following base 10 numbers into 8-bit 2 s complement notation 0, -1, -12

Student Worksheet 1 TI-15 Explorer : Finding Patterns

Dr Brian Beaudrie pg. 1

COMMON CORE STATE STANDARDS FOR MATHEMATICS 3-5 DOMAIN PROGRESSIONS

Transcription:

ASCII and BCD Arithmetic Chapter 11 S. Dandamudi

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 S. Dandamudi BCD: Page 2

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 Input data (in ASCII) ASCII to binary conversion Process in binary Binary to ASCII conversion Output data (in ASCII) S. Dandamudi BCD: Page 3

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 S. Dandamudi BCD: Page 4

Representation of Numbers (cont d) ASCII representation Numbers are stored as a string of ASCII characters» Example: 1234 is stored as 31 32 33 34H ASCII for 1 is 31H, for 2 is 32H, etc. BCD representation Unpacked BCD» Example: 1234 is stored as 01 02 03 04H 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 S. Dandamudi BCD: Page 5

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 S. Dandamudi BCD: Page 6

Processing ASCII Numbers (cont d) ASCII addition Example 1 Example 2 34H = 00110100B 36H = 00110110B 35H = 00110101B 69H = 01101001B Should be 09H Ignore 6 37H = 00110111B 6DH = 01101101B Should be 13H Ignore 6 and add 9 to D The aaa instruction performs these adjustments to the byte in AL register S. Dandamudi BCD: Page 7

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 S. Dandamudi BCD: Page 8

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 S. Dandamudi BCD: Page 9

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 S. Dandamudi BCD: Page 10

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 S. Dandamudi BCD: Page 11

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 S. Dandamudi BCD: Page 12

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 S. Dandamudi BCD: Page 13

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 S. Dandamudi BCD: Page 14

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 S. Dandamudi BCD: Page 15

Processing Packed BCD Numbers (cont d) Packed BCD addition Example 1 Example 2 29H = 00101001B 27H = 00100111B 69H = 01101001B 34H = 00110100B 92H = 10010010B 5BH = 01011101B Should be 98H (add 6) Should be 61H (add 6) Example 3 52H = 01010010B 61H = 01100001B B3H = 10110010B Should be 13H (add 60H) S. Dandamudi BCD: Page 16

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 S. Dandamudi BCD: Page 17

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 S. Dandamudi BCD: Page 18

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 For storage of the two input numbers and the result, we can use DT (Define Ten-byte) directive DT stores in packed BCD form Example: DT 1234567890 is stored as 90 78 56 34 12H S. Dandamudi BCD: Page 19

Performance: Decimal vs Binary Arithmetic Tradeoffs associated with the three representations Representation Storage Conversion Processing overhead overhead overhead Binary Nil High Nil Packed BCD Medium Medium Medium ASCII High Nil High S. Dandamudi BCD: Page 20

Performance: Decimal vs Binary Arithmetic 2.0 Execution time (seconds) 1.5 1.0 0.5 Binary add BCD add ASCII add 0.0 0 50 100 150 200 Number of calls (in thousands) S. Dandamudi BCD: Page 21

Performance: Decimal vs Binary Arithmetic 6.0 Execution time (seconds) 5.0 4.0 3.0 2.0 1.0 ASCII add BCD add Binary add 0.0 0 50 100 150 200 Number of calls (in thousands) S. Dandamudi BCD: Page 22