EC 333 Microprocessor and Interfacing Techniques (3+1)

Similar documents
Arithmetic Instructions

CS-202 Microprocessor and Assembly Language

Kingdom of Saudi Arabia Ministry of Higher Education. Taif University. Faculty of Computers & Information Systems

Intel 8086: Instruction Set

Ex: Write a piece of code that transfers a block of 256 bytes stored at locations starting at 34000H to locations starting at 36000H. Ans.

Week /8086 Microprocessor Programming I

Lecture 9. INC and DEC. INC/DEC Examples ADD. Arithmetic Operations Overflow Multiply and Divide

Signed number Arithmetic. Negative number is represented as

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

We will first study the basic instructions for doing multiplications and divisions

Code segment Stack segment

8086 INSTRUCTION SET

EXPERIMENT WRITE UP. LEARNING OBJECTIVES: 1. Get hands on experience with Assembly Language Programming 2. Write and debug programs in TASM/MASM

INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI

Summer 2003 Lecture 4 06/14/03

EC-333 Microprocessor and Interfacing Techniques

EC-333 Microprocessor and Interfacing Techniques

Lecture (08) x86 programming 7

Lecture (07) x86 programming 6

Mnem. Meaning Format Operation Flags affected ADD Addition ADD D,S (D) (S)+(D) (CF) Carry ADC Add with ADC D,C (D) (S)+(D)+(CF) O,S,Z,A,P,C

IBM PC Hardware CPU 8088, Pentium... ALU (Arithmetic and Logic Unit) Registers. CU (Control Unit) IP.

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

EEM336 Microprocessors I. Arithmetic and Logic Instructions

Week /8086 Microprocessor Programming

8086 INTERNAL ARCHITECTURE

Arithmetic and Logic Instructions And Programs

8088/8086 Programming Integer Instructions and Computations

9/25/ Software & Hardware Architecture

Marking Scheme. Examination Paper Department of CE. Module: Microprocessors (630313)

8086 Programming. Multiplication Instructions. Multiplication can be performed on signed and unsigned numbers.

BAHAR DÖNEMİ MİKROİŞLEMCİLER LAB4 FÖYÜ

Marking Scheme. Examination Paper. Module: Microprocessors (630313)

Instructions moving data

ADVANCE MICROPROCESSOR & INTERFACING

db "Please enter up to 256 characters (press Enter Key to finish): ",0dh,0ah,'$'

LABORATORY WORK NO. 7 FLOW CONTROL INSTRUCTIONS

Microprocessor and Assembly Language Week-5. System Programming, BCS 6th, IBMS (2017)

Intel 8086 MICROPROCESSOR ARCHITECTURE

1-Operand instruction types 1 INC/ DEC/ NOT/NEG R/M. 2 PUSH/ POP R16/M16/SR/F 2 x ( ) = 74 opcodes 3 MUL/ IMUL/ DIV/ DIV R/M

Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION 80x86 Instructions

PESIT Bangalore South Campus

Selection and Iteration. Chapter 7 S. Dandamudi

Defining and Using Simple Data Types

Week /8086 Microprocessor Programming II

ORG ; TWO. Assembly Language Programming

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Electronics and Communication

Microprocessor. By Mrs. R.P.Chaudhari Mrs.P.S.Patil

Intel 8086 MICROPROCESSOR. By Y V S Murthy

APPENDIX C INSTRUCTION SET DESCRIPTIONS

Lesson 1. Fundamentals of assembly language

Chapter 3: Addressing Modes

Assignment no:4 on chapter no :3 : Instruction set of 8086

EEM336 Microprocessors I. Data Movement Instructions

Logic Instructions. Basic Logic Instructions (AND, OR, XOR, TEST, NOT, NEG) Shift and Rotate instructions (SHL, SAL, SHR, SAR) Segment 4A

Basic Assembly SYSC-3006

Assembler lecture 4 S.Šimoňák, DCI FEEI TU of Košice

Basic Assembly Instructions

X86 Addressing Modes Chapter 3" Review: Instructions to Recognize"

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB

Lecture 16: Passing Parameters on the Stack. Push Examples. Pop Examples. CALL and RET

CS401 Assembly Language Solved MCQS From Midterm Papers

Integer Arithmetic Part2

.code. lea dx,msg2. Page 1/8. Problem 1: Programming in Assembly [25 Points]

complement) Multiply Unsigned: MUL (all operands are nonnegative) AX = BH * AL IMUL BH IMUL CX (DX,AX) = CX * AX Arithmetic MUL DWORD PTR [0x10]

ASSEMBLY LANGUAGE PROGRAMMING OF THE MICROCOMPUTER

SHEET-2 ANSWERS. [1] Rewrite Program 2-3 to transfer one word at a time instead of one byte.

Chapter Four Instructions Set

Assembling, Linking and Executing 1) Assembling: .obj obj .obj.lst .crf Assembler Types: a) One pass assembler:

3.1 DATA MOVEMENT INSTRUCTIONS 45

SRI VENKATESWARA COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF ECE EC6504 MICROPROCESSOR AND MICROCONTROLLER (REGULATION 2013)

UNIT 4. Modular Programming

Module 3 Instruction Set Architecture (ISA)

Conditional Processing

UNIT III MICROPROCESSORS AND MICROCONTROLLERS MATERIAL OVERVIEW: Addressing Modes of Assembler Directives. Procedures and Macros

EC 333 Microprocessor and Interfacing Techniques (3+1)

Assembly Language: IA-32 Instructions

CC411: Introduction To Microprocessors

Lecture 8: Control Structures. Comparing Values. Flags Set by CMP. Example. What can we compare? CMP Examples

8086 Programming ADD BX,245FH BX BX + 245FH ADD [BX],AL [DS:BX] [DS:BX] + AL ADD CL,[BP] CL CL + [SS:BP] ADD BX,[SI + 2] BX BX + [DS:(SI + 3:SI + 2)]

UNIT 2 PROCESSORS ORGANIZATION CONT.

Experiment 3. TITLE Optional: Write here the Title of your program.model SMALL This directive defines the memory model used in the program.

Assembly Language. Dr. Esam Al_Qaralleh CE Department Princess Sumaya University for Technology. Overview of Assembly Language

EXPERIMENT WRITE UP. LEARNING OBJECTIVES: 1. Get hands on experience with Assembly Language Programming 2. Write and debug programs in TASM/MASM

22 Assembly Language for Intel-Based Computers, 4th Edition. 3. Each edge is a transition from one state to another, caused by some input.

IFE: Course in Low Level Programing. Lecture 6

CONTENTS. 1. Display a Message Display a one Digit Number Accept a Character from keyboard and display the character 4

TUTORIAL. Emulador Emu8086 do. Microprocessador 8086

ECOM Computer Organization and Assembly Language. Computer Engineering Department CHAPTER 7. Integer Arithmetic

Integer Arithmetic. Pu-Jen Cheng. Adapted from the slides prepared by Kip Irvine for the book, Assembly Language for Intel-Based Computers, 5th Ed.

CSC 2400: Computer Systems. Towards the Hardware: Machine-Level Representation of Programs

CSC 8400: Computer Systems. Machine-Level Representation of Programs

Computer Architecture..Second Year (Sem.2).Lecture(4) مدرس المادة : م. سندس العزاوي... قسم / الحاسبات

CS/ECE/EEE/INSTR F241 MICROPROCESSOR PROGRAMMING & INTERFACING MODULE 4: 80X86 INSTRUCTION SET QUESTIONS ANUPAMA KR BITS, PILANI KK BIRLA GOA CAMPUS

Computer Architecture and System Software Lecture 04: Floating Points & Intro to Assembly

Q1: Multiple choice / 20 Q2: Protected mode memory accesses

PESIT Bangalore South Campus

Q1: Define a character string named CO_NAME containing "Internet Services" as a constant?

Computer Architecture and System Programming Laboratory. TA Session 3

Lecture 5:8086 Outline: 1. introduction 2. execution unit 3. bus interface unit

Introduction to IA-32. Jo, Heeseung

Transcription:

EC 333 Microprocessor and Interfacing Techniques (3+1) Lecture 6 8086/88 Microprocessor Programming (Arithmetic Instructions) Dr Hashim Ali Fall 2018 Department of Computer Science and Engineering HITEC University Taxila

Assemble, Link and Run a Program Steps in creating an executable Assembly Language Program 2

Assembly Instructions 3

Assembly Language Basics Character or String Constants - ABC - X - This isn t a test - 4096 Numeric Literals - 26-1Ah - 1101b - 2BH - 47d 4

Statements longarraydefinition dw 1000h,1020h,1030h \,1040h, 1050h, 1060h, 1070h Lines may break with \ character Identifier name limit of max 247 characters Case insensitive Variable - Count1 db 50 ; a variable (memory allocation) Label: - If a name appears in the code area of the program it is a label. LABEL1: mov ax,0 mov bx,1 LABEL2: jmp Label1 ;jump to label1 5

Assembler Directives Model: Selects the size of the memory model. -.MODEL SMALL : Reserves 64KB memory for Code Segment (CS) and 64KB for Data Segment (DS). -.MODEL MEDIUM : CS can exceed 64KB but DS must fit in 64KB of memory. -.MODEL COMPACT : CS is of fixed 64KB size but DS can vary. -.MODEL LARGE : Both CS & DS can exceed 64KB but only one of them. -.MODEL HUGE : Both can exceed its 64KB memory size. -.MODEL TINY : Used with compact (com) files in which CS & DS must fit into 64KB. 6

Assembly Code Template.MODEL SMALL.STACK ; beginning of the stack segment.data ; beginning of the data segment.code ; beginning of the code segment Ex:.DATA DATAW DW 213FH DATAB DB 52H SUM DB? ;nothing stored but storage is assigned Ex:.CODE ProgramName PROC ProgramName ENDP END ProgramName ; name of the program ; program statements 7

Data Types and Data Definition ORG (Origin) Used to set the beginning of offset address. DB (Define Byte) Reserve or allocate memory in byte size. Example: Data1 DB 25H Data2 DB 10001100B DQ (Define Quad-Word) Reserve or allocate 8 bytes of memory. DUP (Duplicate) Used to duplicate a given data Example: Data1 DB 6 DUP(?) Data2 DB 2 DUP(12H) DW or DWORD (Define Word) Reserve or allocate 2 bytes of memory. DD (Define Double-Word) Reserve or allocate 4 bytes of memory. 8

Data Types and Data Definition Data1 DB 25 Data2 DB 10001001b Data3 DB 12h ORG 0010h ; indicates distance from initial location Data4 DB 2591 ORG 0018h Data5 DB? 9

DB DW DD.data msg1 DB 1234567 msg2 DW 6667H data1 DB 1,2,3 DB 45h DB a DB 11110000b data2 DW 12,13 DW 2345H DD 300H ; How it looks like in memory 31 32 33 34 35 36 37 67 67 1 2 3 45 61 F0 0C 00 0D 00 45 23 00 03 00 00 10

More Examples DB 6 DUP(FFh); fill 6 bytes with ffh DW 954 DW 253Fh ; allocates two bytes DD 5C2A57F2h ; allocates eight bytes DQ 12h ; allocates four bytes COUNTER1 DB COUNT COUNTER2 DB COUNT 11

More Assembly OFFSET - The offset operator returns the distance of a label or variable from the beginning of its segment. The destination must be 16 bits. mov bx, offset count SEG - The segment operator returns the segment part of a label or variable s address. push ds mov ax, seg array mov ds, ax mov bx, offset array pop ds DUP operator only appears after a storage allocation directive. db 20 dup(?) EQU directive assigns a symbolic name to a string or constant. Maxint equ 0ffffh COUNT EQU 2 12

Simple Assembly Program Addition of two numbers.model small.stack 64.data data1 DB 52H data2 DB 29H sum DB?.code main proc mov ax, @data mov ds, ax mov al, data1 mov bl, data2 add al, bl mov sum, al mov ax, 4C00H int 21H main endp end main 13

Arithmetic Instructions Topic covers the following instructions:- - Addition (ADD, INC, ADC) - Subtraction (SUB, DEC, SBB, CMP) - Multiplication (MUL, IMUL) - Division (DIV, IDIV) - BCD Arithmetic (DAA, DAS) 14

Addition Instructions Addition appears in many forms in the microprocessor. - Register addition - Immediate addition - Memory to register addition - Increment addition - Addition-with-carry Note: - The only types of addition not allowed are memory-tomemory and segment register. Example: ADD CS, DS ; Not allowed ADD [AX], [BX] ; Not allowed 15

Register Addition Register addition instructions are used to add the contents of registers. - Example: ADD AL, BL ; AL = AL+BL ADD CX, DI ; CX = CX+DI 16

Immediate Addition Immediate addition is employed whenever constants or known data are added. - Example: ADD CL, 44H ; CL=CL+44H ADD BX, 245FH ; BX=BX+245FH 17

Memory to Register Addition Memory data can be added with register content. - Example: ADD CL, [BP] The byte content of the stack segment memory location addressed by BP add to CL with the sum stored in CL. ADD [BX], AL AL adds to the byte contents of the data segment memory location addressed by BX with the sum stored in the same memory location. Note: - Data Segment is used by default when BX, DI or SI is used to address memory. - If the BP register addresses memory, the stack segment is used by default. 18

Increment Addition Increment addition (INC) adds 1 to any register or memory location except a segment register. With indirect memory increments, the size of the data must be described by using the BYTE PTR, WORD PTR or, DWORD PTR assembler directives. Increment instructions affect the flag bits, as do most of the arithmetic and logic operations. The difference is that increment instructions do not affect the carry flag bit. - Example: INC BL ; BL = BL+1 INC SP ; SP = SP+1 INC BYTE PTR[BX] ; Add 1 to the byte contents of the data segment memory location addressed by BX. 19

Addition with Carry (ADC) An addition with carry (ADC) instruction adds the bit in the carry flag (C) to the operand data. - Example: ADC AL, AH ; AL=AL+AH+carry ADC DH, [BX] ; The byte content of the data segment memory location addressed by BP add to DH with the sum stored in DH. 20

Example Addition with Carry (ADC) - ADD AX, CX - ADC BX, DX 21

Changes of FLAG Bits after Addition Changes bits by any Add instruction: Sign, Zero, Carry, Auxiliary carry, parity and overflow flag. Z = 0 (Result not zero) C = 0 (No carry) A = 0 (No half carry) S = 0 (Positive result) P = 0 (Odd parity) O = 0 (No overflow) Z = 1 (Result zero) C = 1 (Carry exists) A = 1 (Half carry exists) S = 1 (Negative result) P = 1 (Even parity) O = 1 (Overflow occur) 22

Problem (Assignment) a) Develop a short sequence of instructions that adds two thirty two bit numbers (FE432211H and D1234EF1H) with the sum appearing in BX-AX b) What is wrong with the following instructions: - ADD AL, AX - ADC CS, DS - INC [BX] - ADD [AX], [BX] - INC SS c) Suppose, present content of flag register is 058FH. Find the content of AX and Flag register after executing the following instructions: - MOV AX, 1001H - MOV DX, 20FFH - ADD AX, DX d) Develop a short sequence of instructions that adds AL, BL, CL, DL and AH. Save the sum in the DH register. 23

Subtraction Instructions Many forms of subtraction appear in the instruction set. - Register Subtraction - Immediate Subtraction - Decrement Subtraction - Subtraction with Borrow - Comparison Note: - The only types of subtraction not allowed are memory-tomemory and segment register subtractions. - Example: SUB CS, DS ; Not allowed SUB [AX], [BX] ; Not allowed 24

Register Subtraction Register subtraction instructions are used to subtract the contents of registers. - Example: SUB AL, BL ; AL = AL-BL SUB CX, DI ; CX = CX-DI 25

Immediate Subtraction Immediate subtraction is employed whenever constants or known data are subtracted. - Example: SUB CL, 44H ; CL = CL-44H SUB BX, 245FH ; BX = BX-245FH 26

Memory to Register Subtraction Memory data can be subtracted from register content. - Example: SUB [DI], CH Subtract CH from the byte content of data segment memory addressed by DI and stores the result in same location. SUB CH, [BP] Subtract the byte contents of the stack segment memory location addressed by BP from CH and the result stored in CH. Note: - Data Segment is used by default when BX, DI or SI is used to address memory. - If the BP register addresses memory, the stack segment is used by default. 27

Decrement Subtraction Decrement subtraction (DEC) subtract 1 from a register or memory location except a segment register. With indirect memory increments, the size of the data must be described by using the BYTE PTR, WORD PTR or, DWORD PTR assembler directives. - Example: DEC BL ; BL = BL-1 DEC SP ; SP = SP-1 DEC BYTE PTR[BX] ; Subtracts 1 from the byte contents of the data segment memory location addressed by BX. DEC WORD PTR[BP] ; Subtracts 1 from the word content of the stack segment memory location addressed by BP. 28

Subtraction with Borrow (SBB) A subtraction with borrow (SBB) instruction functions as a regular subtraction, except that the carry flag (C), which hold the borrow, also subtracts from the difference. The most common use for this instruction is for subtractions that are wider than 16-bits. - Example: SBB AL, AH ; AL = AL-AH-Carry SBB DH, [BX] ; The byte content of the data segment memory location addressed by BP subtracted from DH and result stored in DH. Note: - Immediate subtraction from a memory location requires BYTE PTR,WORD PTR directives. - Example: SBB BYTE PTR[DI], 3 ; Both 3 and carry subtract from data segment memory location addressed by DI. 29

Comparison The comparison instruction (CMP) is a subtraction that changes only the flag bits, the destination operand never changes. A comparison is useful for checking the entire contents of a register or a memory location against another value. A CMP is normally followed by a conditional jump instruction, which test condition of the flag bits. - Example: CMP CL, BL ; CL-BL - The result of comparison depends on CF, ZF and SF. CF ZF SF Result 0 1 0 Result of subtraction is zero (the values are equal) 0 0 0 1 st value is greater than 2nd value (No borrow) 1 0 1 2 nd value is greater than 1st value (Needs borrow) 30

Conditional jump instructions that often followed by CMP instruction are: Nmeonic Condition Tested Operation JA ZF = 0 & CF = 0 Jump if above For JAE CF = 0 Jump above or equal UNSIGEND numbers JB CF = 1 Jump if below JBE ZF = 1 & CF = 1 Jump below or equal JC CF = 1 Jump if carry JE or JZ ZF = 1 Jump if equal, Jump if zero For SIGNED numbers Example: JG ZF = 0 & SF = 0 Jump if greater then JGE SF = 0 Jump greater then or equal JL SF!= 0 Jump if less then JLE ZF = 1 or SF!= 0 Jump less then or equal JNC CF = 0 Jump if no carry JNE or JNZ ZF = 0 Jump if not equal to, Jump if not zero - CMP AL, 10H ; Compare AL against 10H - JAE EEE ; If AL is 10H or above program jump to EEE 31

Multiplication Instructions In 8086 multiplication is performed on bytes (8-bit) and words (16-bit) and can be signed integer (IMUL) or unsigned integer (MUL). If two 8-bit numbers are multiplied, they generate 16-bit product; if two 16-bit numbers are multiplied they generate 32-bit product. Some flag bits, O (overflow) and C (carry) changes when multiply instruction executes and produce predictable outcomes. The other flag also change, but their results are unpredictable and therefore are unused. 32

8-bit multiplication (signed and unsigned) Multiplicand is always in AL register Multiplier can be any 8-bit register or any memory location. Product is placed in AX register. (For signed multiplication, the product is in binary form, if positive, and in 2 s complement form, if negative. For unsigned multiplication result is always in binary form). Immediate multiplication is not allowed. Unsigned multiplication uses MUL instruction and signed multiplication uses IMUL instruction. - Example: MUL CL ; AL is multiplied by CL, the unsigned product is in AX IMUL DH ; AL is multiplied by DH, the signed product is in AX IMUL BYTE PTR[BX] ; AL is multiplied by byte contents of the data segment memory location addressed by BX, the signed product is in AX. 33

16-bit multiplication (signed and unsigned) Multiplicand stay in AX. Multiplier can stay any 16-bit register or memory location. 32-bit product appear in DX-AX. The DX register always contains the most significant 16 bits and AX contains the least significant bits of the product. - Example: MUL CX ; AX is multiplied by CX, the unsigned product is in DX-AX. IMUL DI ; AX is multiplied by DI, the signed product is in DX- AX. MUL WORD PTR[SI]; AX is multiplied by the word content of data segment memory location addressed by SI, the unsigned product is in DX-AX. 34

Point to Remember!! Multiplication Operand 1 Operand 2 Result Byte x Byte AL Register or Memory AX Word x Word AX Register or Memory DX AX Byte x Word AL=Byte, AH=0 Register or Memory DX AX 35

Problem (Assignment) Write a short sequence of instructions that multiply 4 with 10 and the result is stored in DX. 36

Division Instructions Division occurs on 8-bit or 16-bit numbers in the 8086-80286 microprocessors, and on 32-bit numbers in the 80386-Pentium 4 microprocessor. Unsigned division use DIV instruction and signed division use IDIV instruction. The dividend is always a double-width dividend that is divided by the operand. This means that an 8-bit division divides a 16-bit number by an 8-bit number; a 16-bit division divides a 32-bit number by a 16-bit number. There is no immediate division instruction available to any microprocessor. None of the flag bits change predictably for a division. 37

8-bit Division Dividend (which is divided by a value) always stored in AX. The dividing content is stored in any 8-bit register or memory location. The quotient moves into AL division. Reminder stored in AH register. - Example: DIV CL ; AX is divided by CL, the unsigned quotient is in AL and the unsigned reminder is in AH. IDIV BL ; AX is divided by BL, the signed quotient is in AL and the signed reminder is in AH. DIV BYTE PTR[BP] ; AX is divided by the byte content of the stack segment memory location addressed by BP, the unsigned quotient is in AL and unsigned remainder is in AH. 38

16-bit division Dividend (which is divided by a value) always stored in DX-AX. The dividing content is stored in any 16-bit register or memory location. The quotient appears in AX. Reminder appears in DX register. - Example: - DIV CX ; DX-AX is divided by CX, the unsigned quotient is in AX and the unsigned remainder is in DX. - IDIV SI ; DX-AX is divided by SI, the signed quotient is in AX and the signed remainder is in DX. 39

Point to Remember!! Divission Dividend Divisor Quotient Reminder Byte / Byte Word / Word Word / Byte Double-Word / Word Example: - DATA1 DB 45H - DATA2 DB 10H - QOU DB? - REM DB? - MOV AL, DATA1 AL=Byte, AH=0 AX=Word, DX=0 AX=Word DXAX=Double-Word Register or Memory Register or Memory Register or Memory Register or Memory - SUB AH, AH - DIV DATA2 - MOV QOU, AL - MOV REM, AH AL AX AL AX AH DX AH DX 40

CBW (Convert signed byte to signed word) This instruction copies the sign of a byte in AL to all the bits in AH. AH is then said to be sign extension of AL. The CBW operation must be done before a signed byte in AL can be divided by another signed byte with the IDIV instruction. CBW effects no flag. - Example: Suppose we want to divide -38 by +3. AL = 11011010 = -26H = -38 decimal CH = 0000 0011 = +3H = +3 decimal - MOV AL, -26H - MOV CH, 03H - CBW ; Extended sign of AL through AH. AX=11111111 11011010 - IDIV CH; Divide AX by CH AL=11110100 = -OCH = -12 decimal AH = 11111110 = -2 decimal 41

CWD (Convert signed Word to signed Double word) CWD copies the sign of a word in AX to all the bits of the DX register. In other words, it extends the sign of AX into all of DX. The CWD operation must be done before a signed word in AX can be divided by another signed word with the IDIV instruction. CWD affects no flag. - Example: Divide -3897 decimal by +250 decimal. AX=11110000 11000111 = -3897 decimal CX = 00000000 1111 1010 = +250 decimal DX = 0000000000000000 CWD DX= 1111111111111111 AX= 11110000 11000111 IDIV CX 42

Problem (Assignment) a) Write a short sequence of instruction to divide 800 by 100. The result should be stored in CL register. b) Write a short sequence of instructions that divide the number in BL by the number in CL and then multiplies the result by 2. The final answer must be a 16-bit number stored in the DX register. c) [{130-(300/50)+6}*9] 43

BCD Arithmetic Instructions The addition or subtraction of two BCD numbers result a binary number. To adjust this number into BCD the following instructions are used:- - DAA (Decimal adjust AL after BCD addition) - DAS (Decimal adjust AL after BCD subtraction) 44

DAA (Decimal adjust AL after BCD addition) This instruction is used to make sure the result of adding two packed BCD numbers is adjusted to be a legal BCD number. The result must be in AL for DAA to work correctly. If the lower nibble in AL after addition is greater than 9 or AF was set by the addition, then the DAA instruction will add 6 to the lower nibble in AL. If the result in the upper nibble of AL is now greater than 9 or if the carry flag was set by the addition or correction, then DAA instruction will add 60H to AL. 45

Example: a) Let, AL = 0101 1001 = 59 BCD, and BL = 0011 0101 = 35 BCD - ADD AL,BL ; AL = 10001110 = 8EH - DAA ; Add 0110 because 1110 > 9 ; AL = 1001 0100 = 94 BCD b) Let, AL= 1000 1000 (88 BCD), and BL=0100 1001 (49 BCD) - ADD AL, BL ; AL = 1101 0001, AF=1 - DAA ; Add 0110 because AF=1 ; AL=1101 0111 (D7H) ; 1101>9 so add 0110 0000 ; AL=0011 0111 (37 BCD), CF=1 46

DAS (Decimal adjust AL after BCD subtraction) This instruction is used after subtracting two packed BCD numbers to make sure the result is correct packed BCD. The result of subtraction must be in AL for DAS to work correctly. If the lower nibble in AL after subtraction is greater than 9 or the AF was set by the subtraction then DAS instruction will subtract 6 from the lower nibble of AL. If the result in the upper nibble is now greater than 9 or if the carry flag was set, the DAS instruction will subtract 60 from AL. 47

Example: a) Let, AL = 1000 0110 (86 BCD), and BH= 0101 0111 (57 BCD) - SUB AL,BH ; AL = 0010 1111 (2FH), CF=0 - DAS ; Lower nibble of results is 1111>9, ;so DAS automatically subtracts 0000 0110 ; AL = 0010 1001 (29 BCD) 48

Problem a) Write an instruction set to implement a decimal up counter using DAA instruction. b) Write an instruction set to implement a decimal down counter using DAS instruction. 49

Solution of problem Decimal up counter:- - MOV COUNT, 63H ; initialise count in memory location as 1. - MOV AL, COUNT ; Bring COUNT into AL to work on. - ADD AL, 01H ; increment the value by 1. - DAA ; Decimal adjust the result. - MOV COUNT, AL ; Put decimal result back in memory. Decimal down counter:- - MOV COUNT, 63H ; initialise count in memory location as 99 decimal (63H). - MOV AL, COUNT ; Bring COUNT into AL to work on. - SUB AL, 01H ; Decrement the value by 1. - DAS ; Decimal adjust the result. - MOV COUNT, AL ; Put new count back in memory. 50