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



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

ASCII and BCD Arithmetic. Chapter 11 S. Dandamudi

Complete 8086 instruction set

Faculty of Engineering Student Number:

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

Lecture 7: Machine-Level Programming I: Basics Mohamed Zahran (aka Z)

CSI 333 Lecture 1 Number Systems

Z80 Instruction Set. Z80 Assembly Language

Computer Organization and Architecture

Computer Science 281 Binary and Hexadecimal Review

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

Using Debug 1 INTRODUCING DEBUG

The 80x86 Instruction Set

CS61: Systems Programing and Machine Organization

The x86 PC: Assembly Language, Design, and Interfacing 5 th Edition

Assembly Language Tutorial

x64 Cheat Sheet Fall 2015

PC Assembly Language. Paul A. Carter

1 Classical Universal Computer 3

Machine Programming II: Instruc8ons

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

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer

Hacking Techniques & Intrusion Detection. Ali Al-Shemery arabnix [at] gmail

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

Systems Design & Programming Data Movement Instructions. Intel Assembly

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

TECHNICAL BULLETIN [ 1 / 5 ]

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

A Tiny Guide to Programming in 32-bit x86 Assembly Language

CHAPTER 6 TASK MANAGEMENT

Solution for Homework 2

Administration. Instruction scheduling. Modern processors. Examples. Simplified architecture model. CS 412 Introduction to Compilers

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

Systems I: Computer Organization and Architecture

Binary Representation

IA-32 Intel Architecture Software Developer s Manual

HOMEWORK # 2 SOLUTIO

Appendix C: Keyboard Scan Codes

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

MACHINE ARCHITECTURE & LANGUAGE

8085 INSTRUCTION SET

Instruction Set Architecture

PROBLEMS (Cap. 4 - Istruzioni macchina)

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

Chapter 4: Computer Codes

Assembly Language: Function Calls" Jennifer Rexford!

Computer Organization and Assembly Language

High-speed image processing algorithms using MMX hardware

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

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

Intel 8086 architecture

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

Compilers I - Chapter 4: Generating Better Code

1 Description of The Simpletron

Useful Number Systems

2) Write in detail the issues in the design of code generator.

1. Give the 16 bit signed (twos complement) representation of the following decimal numbers, and convert to hexadecimal:

CPEN Digital Logic Design Binary Systems

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

esrever gnireenigne tfosorcim seiranib

Outline. Lecture 3. Basics. Logical vs. physical memory physical memory. x86 byte ordering

=

Lecture 2. Binary and Hexadecimal Numbers

Intel Hexadecimal Object File Format Specification Revision A, 1/6/88

X86-64 Architecture Guide

ELEG3924 Microprocessor Ch.7 Programming In C

Chapter 5 Instructor's Manual

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

MICROPROCESSOR AND MICROCOMPUTER BASICS

64-Bit NASM Notes. Invoking 64-Bit NASM

Number Representation

MACHINE INSTRUCTIONS AND PROGRAMS

Counting in base 10, 2 and 16

Binary Adders: Half Adders and Full Adders

Software Fingerprinting for Automated Malicious Code Analysis

PART B QUESTIONS AND ANSWERS UNIT I

Lecture 11: Number Systems

This 3-digit ASCII string could also be calculated as n = (Data[2]-0x30) +10*((Data[1]-0x30)+10*(Data[0]-0x30));

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

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

PCI BIOS SPECIFICATION. Revision 2.1

CHAPTER 5 Round-off errors

CPU Organisation and Operation

MICROPROCESSOR BCA IV Sem MULTIPLE CHOICE QUESTIONS

Number Systems. Introduction / Number Systems

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

Hexadecimal Object File Format Specification

Binary, Hexadecimal, Octal, and BCD Numbers

Compiler Construction

Writing an 8086 emulator in Python

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

LABORATORY MANUAL EE0310 MICROPROCESSOR & MICROCONTROLLER LAB

CPU Organization and Assembly Language

Stack machines The MIPS assembly language A simple source language Stack-machine implementation of the simple language Readings:

Instruction Set Architecture (ISA)

8051 hardware summary

Programmer s Model = model of µc useful to view hardware during execution of software instructions

PES Institute of Technology-BSC QUESTION BANK

Binary Numbers. Binary Octal Hexadecimal

Return-oriented programming without returns

Transcription:

BCD (ASCII) Arithmetic The Intel Instruction set can handle both packed (two digits per byte) and unpacked BCD (one decimal digit per byte) We will first look at unpacked BCD Unpacked BCD can be either binary or ASCII. Consider the number 4567 Bytes then look like 34h 35h 36h 37h OR: 04h 05h 06h 07h In packed BCD where there are two decimal digits per byte 99h = 99d 4567d= 4567d Unpacked BCD Arithmetic With unpacked BCD we wish to add strings such as '989' and '486' and come up with the string '1475'. With BCD you can use the standard input and output routines for strings to get numbers into and out of memory without converting to binary BCD arithmetic uses the standard binary arithmetic instructions and then converts the result to BCD using BCD adjustment instructions. Where and Why is BCD used? BCD takes more space and more time than standard binary arithmetic From the SQL Server Manual Exact Numerics It is used extensively in applications that deal with currency because floating point representations are inherently inexact Database management systems offer a variety of numeric storage options; Decimal means that numbers are stored internally either as BCD or as fixed-point integers BCD offers a relatively easy way to get around size limitations on integer arithmetic bigint bit decimal int money float numeric smallint smallmoney tinyint Approximate Numerics real BCD Adjustment Instructions Four unpacked adjustment instructions are available: (ASCII Adjust After Addition) AAS (ASCII Adjust After Subtraction) AAM (ASCII Adjust After Multiplication) AAD (ASCII ADjust BEFORE Division) Packed BCD, ASCII, Unpacked BCD and AAS can be used with both ASCII and unpacked BCD 9701 in ASCII (hex) 39 39 30 31 9701 in unpacked BCD 09 07 00 01 Except for AAD the instructions are used to adjust results after performing a binary operation on ASCII or BCD data AAD (in spite of the mnemonic) is used after a DIV instruction Two Packed BCD operations are also available: DAA Decimal Adjust After Addition DAS Decimal Adjust After Subtraction 1

This instruction has very complex semantics that are rarely documented correctly. Here are two examples: IF AL > 9 OR AF = 1 THEN AL <- AL - 10 CF <- 1 AF <- 1 CF <- 0 AF <- 0 IF AL > 9 OR AF = 1 THEN AL <- AL + 6 Bits 4-7 of AL set to 0 AF and CF set AF and CF clear Bits 4-7 of AL set to 0 Example Let s see what happens if we try to add ASCII strings byte by byte. 989 > 39 38 39 486 > 34 38 36 1475 6D 70 6F As you can see the result in binary does not look like what we want. When adding, AF is set whenever there is a carry from the lowest-order nibble to the next lowest nibble. Recall that a NIBBLE is one hex digit or 4 bits. When adding 9 and 6 in hex we get F and no binary carry. Note that AF is clear after the addition of 39h and 36h, because there was no carry from the low-order nibble to the next one If adding 9 and 7 we would get 10h, and AF would be set. Semantics (ASCII Adjust for Addition) does the following things: IF AL > 9 THEN clear top nibble of AL AL <- AL - 10 CF <- 1 AF <- 1 CF <- 0 AF <- 0 clear top nibble of AL Notes 1. addition result must be in AL in order for to work 2. top nibble of AL always cleared so will adjust for ASCII as well as unpacked BCD 3. Either AH or CF can be used for decimal carries. Example with no Aux Carry 2BC5:0108 B039 MOV AL,39 AX=0039 BX=0000 CX=0012 DX=0000 SP=FFFE BP=0000 SI=0000 DI=0000 2BC5:010A 0436 ADD AL,36 AX=006F BX=0000 CX=0012 DX=0000 SP=FFFE BP=0000 SI=0000 DI=0000 DS=2BC5 ES=2BC5 SS=2BC5 CS=2BC5 IP=010C NV UP EI PL NZ NA PE NC AX=0105 BX=0000 CX=0012 DX=0000 SP=FFFE BP=0000 SI=0000 DI=0000 DS=2BC5 ES=2BC5 SS=2BC5 CS=2BC5 IP=010D NV UP EI PL NZ AC PO CY Example with Aux Carry Example with Unpacked BCD 2BC5:0108 B039 MOV AL,39 AX=0039 BX=0000 CX=0012 DX=0000 SP=FFFE BP=0000 SI=0000 DI=0000 2BC5:010A 0438 ADD AL,38 AX=0071 BX=0000 CX=0012 DX=0000 SP=FFFE BP=0000 SI=0000 DI=0000 DS=2BC5 ES=2BC5 SS=2BC5 CS=2BC5 IP=010C NV UP EI PL NZ AC PE NC AX=0107 BX=0000 CX=0012 DX=0000 SP=FFFE BP=0000 SI=0000 DI=0000 DS=2BC5 ES=2BC5 SS=2BC5 CS=2BC5 IP=010D NV UP EI PL NZ AC PE CY 2BC5:0108 B009 MOV AL,09 AX=0009 BX=0000 CX=0012 DX=0000 SP=FFFE BP=0000 SI=0000 DI=0000 2BC5:010A 0409 ADD AL,09 AX=0012 BX=0000 CX=0012 DX=0000 SP=FFFE BP=0000 SI=0000 DI=0000 DS=2BC5 ES=2BC5 SS=2BC5 CS=2BC5 IP=010C NV UP EI PL NZ AC PE NC AX=0108 BX=0000 CX=0012 DX=0000 SP=FFFE BP=0000 SI=0000 DI=0000 DS=2BC5 ES=2BC5 SS=2BC5 CS=2BC5 IP=010D NV UP EI PL NZ AC PE CY 2

Example with No Carries BCD Addition Program 2BC5:0108 B031 MOV AL,31 AX=0031 BX=0000 CX=0012 DX=0000 SP=FFFE BP=0000 SI=0000 DI=0000 2BC5:010A 0431 ADD AL,31 AX=0062 BX=0000 CX=0012 DX=0000 SP=FFFE BP=0000 SI=0000 DI=0000 DS=2BC5 ES=2BC5 SS=2BC5 CS=2BC5 IP=010C NV UP EI PL NZ NA PO NC AX=0002 BX=0000 CX=0012 DX=0000 SP=FFFE BP=0000 SI=0000 DI=0000 DS=2BC5 ES=2BC5 SS=2BC5 CS=2BC5 IP=010D NV UP EI PL NZ NA PO NC segment.data str1 db '04989' ; leading 0 allows easy processing str2 db '07486' ; in loop without concern for segment.bss sum resb 5 ; extra digit for carry out segment.text... mov ssi, str1+4 ;point to LSD mov ebx, str2+4 mov edi, sum +4 mov ecx, 5 ;digits to process ;ensure cf clear mov al,[esi] adc al,[ebx] mov [edi],al BCD Addition Program (2) mov ecx, 5 inc edi LP2: or byte [edi],30h inc edi loop LP2 ;get a digit from op1 ;add prev cf + op2 digit ;adjust ;save result ;advance all 3 pointers ;convert to ASCII ;adjust di back to MSD ;convert What s Wrong with This? ;this code tries to do it in a single loop ;but doesn t work correctly mov al,[esi] ;get a digit from op1 adc al,[ebx] ;add prev cf + op2 digit ;adjust or al, 30h ;convert to ASCII mov [edi],al ;save result ;advance all 3 pointers ;How can we fix without using a 2nd loop? AAS AAS (ASCII Adjust for Subtraction) works in a similar manner to Note that negative results are expressed in 10's complement. -a 1469:0100 mov al,31 1469:0102 sub al,39 1469:0104 aas 1469:0105 AX=0031 BX=0000 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000 DS=1469 ES=1469 SS=1469 CS=1469 IP=0102 1469:0102 2C39 SUB AL,39 NV UP EI PL NZ NA PO NC AX=00F8 BX=0000 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000 DS=1469 ES=1469 SS=1469 CS=1469 IP=0104 1469:0104 3F AAS NV UP EI NG NZ AC PO CY 10 s Complement The reason that 2 s complement is used in computers is that we can replace subtraction with addition We can apply the same principle to decimal arithmetic To obtain the 10 s complement of a number take the 9 s complement of each digit and then add 1 Example: 68 37 =? Compute 10 s complement of 37: 99 37 = 62 62 + 1 = 63 Compute 68 + 63, ignoring any carry: 63 + 68 = 131 = 31 AX=FF02 BX=0000 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000 DS=1469 ES=1469 SS=1469 CS=1469 IP=0105 NV UP EI NG NZ AC PO CY 3

10 s Complement (2) Complement notation is used with fixed size integers. For a given size n digits, you can compute the 10 s complement by subtracting from n 9 s and then add 1 Or you can subtract the number from 10 n+1 Example: what is 77 in 5 digit 10 s complement? 99999 00077 = 99922 + 1 = 99923 Or 100000 77 = 99923 Note that the leftmost digit is a sign digit. If it is >= 5 the result is negative Other Adjustment Instructions There are four other instructions used in BCD arithmetic: Unpacked BCD: AAM ASCII Adjust After Multiplication AAD ASCII Adjust before Division Packed BCD: DAA Decimal adjust after addition DAS Decimal adjust after subtraction Note that there are no multiplication or division instructions for packed BCD The BCD instructions can also be used for certain specialized conversions. We will take a brief look at AAM and AAD AAM (ASCII Adjust after Multiplication) Used after multiplication of two unpacked BCD numbers (note: NOT ASCII!). Semantics: AL <- AL mod 10, AH <- AL/10 SF <- high bit of AL ZF <- set if AL = 0 AX=0000 BX=0000 CX=0028 DX=0000 SP=FFFE BP=0000 SI=0000 DI=0000 DS=22E5 ES=22E5 SS=22E5 CS=22E5 IP=0105 NV UP EI PL ZR NA PE NC 22E5:0105 B007 MOV AL,07 AX=0007 BX=0000 CX=0028 DX=0000 SP=FFFE BP=0000 SI=0000 DI=0000 DS=22E5 ES=22E5 SS=22E5 CS=22E5 IP=0107 NV UP EI PL ZR NA PE NC 22E5:0107 B306 MOV BL,06 AX=0007 BX=0006 CX=0028 DX=0000 SP=FFFE BP=0000 SI=0000 DI=0000 DS=22E5 ES=22E5 SS=22E5 CS=22E5 IP=0109 NV UP EI PL ZR NA PE NC 22E5:0109 F6E3 MUL BL AX=002A BX=0006 CX=0028 DX=0000 SP=FFFE BP=0000 SI=0000 DI=0000 DS=22E5 ES=22E5 SS=22E5 CS=22E5 IP=010B NV UP EI PL ZR NA PE NC 22E5:010B D40A AAM DS=22E5 ES=22E5 SS=22E5 CS=22E5 IP=010D NV UP EI PL NZ NA PO NC AAD (ASCII Adjust before Division) Unlike other BCD instructions, AAD is performed before a division rather than after AL <- AH * 10 + AL AH <- 0 ZF set if AL = 0, SF <- high bit of AL DS=22E5 ES=22E5 SS=22E5 CS=22E5 IP=0110 NV UP EI PL NZ NA PO NC 22E5:0110 B306 MOV BL,06 DS=22E5 ES=22E5 SS=22E5 CS=22E5 IP=0112 NV UP EI PL NZ NA PO NC 22E5:0112 D50A AAD AX=002A BX=0006 CX=0028 DX=0000 SP=FFFE BP=0000 SI=0000 DI=0000 DS=22E5 ES=22E5 SS=22E5 CS=22E5 IP=0114 NV UP EI PL NZ NA PO NC 22E5:0114 F6F3 DIV BL AX=0007 BX=0006 CX=0028 DX=0000 SP=FFFE BP=0000 SI=0000 DI=0000 DS=22E5 ES=22E5 SS=22E5 CS=22E5 IP=0116 NV UP EI PL ZR NA PE NC Undocumented Operations with AAM and AAD In the original 8086, there was not enough room in the microcode for the constant 10d intended to be used for AAM and AAD. Consequently the 10d is actually placed as an immediate value in the machine code, even though the instruction was documented only as a 0-operand instruction AAM and AAD are assembled as 2-byte, one operand instructions AAD imm and AAM imm Values other than 10 can be used (if the assembler will do it or if you are willing to patch the assembled code manually) Both are particularly useful with the value 16 Because many programs came to rely on this behavior, Intel retained it but never documented it Other manufacturers reproduced the behavior Packing and Unpacking BCD AAM 16 will "unpack" packed BCD in AL into AH and AL 22E5:0116 B85600 MOV AX,0056 AX=0056 BX=0006 CX=0028 DX=0000 SP=FFFE BP=0000 SI=0000 DI=0000 DS=22E5 ES=22E5 SS=22E5 CS=22E5 IP=0119 NV UP EI PL ZR NA PE NC 22E5:0119 D410 AAM 10 AX=0506 BX=0006 CX=0028 DX=0000 SP=FFFE BP=0000 SI=0000 DI=0000 DS=22E5 ES=22E5 SS=22E5 CS=22E5 IP=011B NV UP EI PL NZ NA PE NC AAD 16 will "pack" AH and AL into AL AX=0000 BX=0006 CX=0028 DX=0000 SP=FFFE BP=0000 SI=0000 DI=0000 DS=22E5 ES=22E5 SS=22E5 CS=22E5 IP=011D NV UP EI PL ZR NA PE NC 22E5:011D B008 MOV AL,08 AX=0008 BX=0006 CX=0028 DX=0000 SP=FFFE BP=0000 SI=0000 DI=0000 DS=22E5 ES=22E5 SS=22E5 CS=22E5 IP=011F NV UP EI PL ZR NA PE NC 22E5:011F B405 MOV AH,05 AX=0508 BX=0006 CX=0028 DX=0000 SP=FFFE BP=0000 SI=0000 DI=0000 DS=22E5 ES=22E5 SS=22E5 CS=22E5 IP=0121 NV UP EI PL ZR NA PE NC 22E5:0121 D510 AAD 10 AX=0058 BX=0006 CX=0028 DX=0000 SP=FFFE BP=0000 SI=0000 DI=0000 DS=22E5 ES=22E5 SS=22E5 CS=22E5 IP=0123 NV UP EI PL NZ NA PO NC 4

2-Digit Decimal Conversions The zero-operand AAD and AAM with implied base 10 can be used for conversion of 2-digit decimal numbers Examples ; binary to ASCII mov ah, 2ch ; DOS get time function int 21h mov al, cl ; load minutes into AL aam ; al <- al mod 10, ah <- al/10 or ax, 3030h ; convert to ascii ;ASCII to binary mov al, lowdigit mov al, highdigit sub ax, 3030h aad mov binnum, al ASCII to Binary ; get ones ASCII digit ; get tens ASCII digit ; convert to binary ; AL <- AH * 10 + AL Generalized BCD Addition ; register parameter version BCDAdd: ; esi points to first digit of operand 1 ; edi points to first digit of operand 2 ; ebx points to first digit of storage for result ; ecx contains digit count (same for both operands!) ; result is one byte longer for carry pusha ; save regs ; adjust pointers to LSD of operands and result add ebx, ecx ; start + digit count add esi, ecx ; start + digit count ; minus one add edi, ecx ; start + digit count ; minus one Generalized BCD Addition:2 ; clear carry for adc in loop mov al,[esi] ; get digit from op1 adc al,[edi] ; add in corresponing digit from op2 ; adjust pushf ; save flags or al,30h ; convert to ascii popf ; restore CF mov [ebx],al ; and store ; adjust pointers (CF unchanged!) ; now we re done processing all digits, ; BUT we have to account for a possible carry mov byte [ebx], 0 ; clear MSD of result adc byte [ebx], 0 ; add in CF ret cdecl BCD Addition BCDAdd: ; this version gets a pointer to the START of the ; string instead of the of the string ; call from C with BCDAdd(op1, op2, result, cnt) %define dcount [ebp+20] %define result [ebp+16] %define operand2 [ebp+12] %define operand1 [ebp+8] push ebp ; save caller s frame pointer mov ebp, esp ; set up frame pointer pusha ; save regs for C mov esi, operand1 ; these params are pointers mov edi, operand2 mov ebx, result ; adjust to avoid 0 delimiter in C mov ecx, dcount ; this is a value cdecl BCD Addition:2 ; ensure CF clear ; use ecx with SIB addressing to count back mov al,[esi+ecx] ; get digit from op1 ; add in corresponding adc al,[edi+ecx] ; digit from op2 ; BCD adjust mov [ebx+ecx],al ; and store 5