Arithmetic Instructions

Similar documents
EC 333 Microprocessor and Interfacing Techniques (3+1)

Signed number Arithmetic. Negative number is represented as

8086 INTERNAL ARCHITECTURE

Week /8086 Microprocessor Programming I

INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI

Code segment Stack segment

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

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

Intel 8086: Instruction Set

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

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

Lecture (07) x86 programming 6

8088/8086 Programming Integer Instructions and Computations

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

EEM336 Microprocessors I. Arithmetic and Logic Instructions

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.

Intel 8086 MICROPROCESSOR ARCHITECTURE

Intel 8086 MICROPROCESSOR. By Y V S Murthy

Instructions moving data

Arithmetic and Logic Instructions And Programs

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

9/25/ Software & Hardware Architecture

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

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

ADVANCE MICROPROCESSOR & INTERFACING

Integer Arithmetic Part2

Lesson 1. Fundamentals 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

8086 INSTRUCTION SET

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

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

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

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

Summer 2003 Lecture 4 06/14/03

EC-333 Microprocessor and Interfacing Techniques

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.

Chapter Four Instructions Set

3.1 DATA MOVEMENT INSTRUCTIONS 45

ASSEMBLY LANGUAGE PROGRAMMING OF THE MICROCOMPUTER

Ex : Write an ALP to evaluate x(y + z) where x = 10H, y = 20H and z = 30H and store the result in a memory location 54000H.

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

Internal architecture of 8086

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

Q1: Multiple choice / 20 Q2: Memory addressing / 40 Q3: Assembly language / 40 TOTAL SCORE / 100

MICROPROCESSOR PROGRAMMING AND SYSTEM DESIGN

CS401 Assembly Language Solved MCQS From Midterm Papers

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

Basic Execution Environment

Defining and Using Simple Data Types

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

CC411: Introduction To Microprocessors

PESIT Bangalore South Campus

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

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

Module 3 Instruction Set Architecture (ISA)

Introduction to IA-32. Jo, Heeseung

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

INTRODUCTION TO IA-32. Jo, Heeseung

Program controlled semiconductor device (IC) which fetches (from memory), decodes and executes instructions.

Computer Architecture 1 ح 303

A Presentation created By Ramesh.K Press Ctrl+l for full screen view

Basic Assembly Instructions

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

We can study computer architectures by starting with the basic building blocks. Adders, decoders, multiplexors, flip-flops, registers,...

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

Chapter 3: Addressing Modes

Lecture 5: Computer Organization Instruction Execution. Computer Organization Block Diagram. Components. General Purpose Registers.

ENE 334 Microprocessors

APPENDIX C INSTRUCTION SET DESCRIPTIONS

CS-202 Microprocessor and Assembly Language

Introduction to Microprocessor

16.317: Microprocessor Systems Design I Spring 2014

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

ET355 Microprocessors Thursday 6:00 pm 10:20 pm

MPID MICROPROCESSOR AND ITS INTERFACING DEVICES EEE III II SEMESTER OBJECTIVE QUESTIONS

Complex Instruction Set Computer (CISC)

16.317: Microprocessor Systems Design I Fall 2013

Week /8086 Microprocessor Programming

EEM336 Microprocessors I. Data Movement Instructions

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

icroprocessor istory of Microprocessor ntel 8086:

Basic Assembly SYSC-3006

Week /8086 Microprocessor Programming II

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

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

VARDHAMAN COLLEGE OF ENGINEERING (AUTONOMOUS) Shamshabad, Hyderabad

Assembly Language: IA-32 Instructions

Computer Architecture and System Programming Laboratory. TA Session 3

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

Q1: Multiple choice / 20 Q2: Data transfers and memory addressing

Lecture 9. INC and DEC. INC/DEC Examples ADD. ADD Examples SUB. INC adds one to a single operand DEC decrements one from a single operand

Assembly Language Each statement in an assembly language program consists of four parts or fields.

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

1. Introduction to Assembly Language

Addressing Modes on the x86

Mr. Sapan Naik 1. Babu Madhav Institute of Information Technology, UTU

LAB 5 Arithmetic Operations Simple Calculator

Assembler Programming. Lecture 2

Chapter 2 COMPUTER SYSTEM HARDWARE

Transcription:

Segment 3C Arithmetic Instructions This topic covers the following instructions: Addition (ADD, INC, ADC) Subtraction (SUB, DEC, SBB,CMP) Multiplication (MUL, IMUL) Division (DIV, IDIV) BCD Arithmetic (DAA, DAS) Course Instructor Mohammed Abdul kader Assistant Professor, EEE, IIUC

Addition appears in many forms in the microprocessor. Register addition Immediate addition Memory to register addition Increment addition Addition-with-carry N.B: The only types of addition not allowed are memory-to-memory and segment register. Example: ADD CS, DS ; not allowed ADD [AX], [BX] ; Not allowed Register Addition Addition Instructions Register addition instructions are used to add the contents of registers. Example: ADD AL, BL ; AL=AL+BL ADD CX, DI ; CX=CX+DI 2

Addition Instructions (Continued) 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 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. N.B. * 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. 3

Addition Instructions (Continued) 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. 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. 4

5 Addition Instructions (Continued) 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) Z=1(result zero) C=0 (no carry) C=1(carry exist) A=0 (no half carry) A= 1 (half carry exist) S=0 (result positive) S=1 (result negative) P=0 (Odd parity) P=1 (even parity) O=0 (no overflow) O= 1 (overflow occur) Problem: 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 058F H. 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.

Many forms of subtraction appear in the instruction set. Register Subtraction Immediate Subtraction Decrement Subtraction Subtraction with Borrow Comparison N.B: The only types of subtraction not allowed are memory-to-memory and segment register subtractions. Example: SUB CS, DS ; not allowed SUB [AX], [BX] ; Not allowed Register Subtraction Subtraction Instructions Register subtraction instructions are used to subtract the contents of registers. Example: SUB AL, BL ; AL=AL-BL SUB CX, DI ; CX=CX-DI 6

Subtraction Instructions (Continued) 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 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. N.B. * 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. 7

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. 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. N.B. 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 8 Subtraction Instructions (Continued)

Subtraction Instructions (Continued) 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 2 nd value (No borrow) 1 0 1 2 nd value is greater than 1 st value (Needs borrow) Conditional jump instructions that often followed by CMP instruction are JA (jump above) JB (jump below) JAE (jump above or equal) JBE (jump below or equal) Example: CMP AL, 10H; compare AL against 10H JAE EEE ; if AL is 10H or above program jump to EEE 9

Multiplication Instructions Multiplication 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. 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. 10

Multiplication Instructions (Continued) 16 bit multiplication 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. Problems: a) Write a short sequence of instructions that multiply 4 with 10 and the result is stored in DX. 11

Division 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. 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: Division Instructions 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 12

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 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 13 Division Instructions (Continued)

Division Instructions (Continued) 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 Problems: 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] 14

15 BCD Arithmetic 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) DAA 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. Example: (a) Let, AL=0101 1001 = 59 BCD and BL=0011 0101 = 35 BCD ADD AL, BL ; AL= 1000 1110 = 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

DAS BCD Arithmetic (Continued) 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. Example: 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) 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. 16

(a) Decimal up counter- BCD Arithmetic (Continued) Solution of problems MOV COUNT, 63H ; initialize 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. (b) Decimal down counter- MOV COUNT, 63H ; initialize 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. 17