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

Similar documents
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

Signed number Arithmetic. Negative number is represented as

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

Arithmetic Instructions

EC 333 Microprocessor and Interfacing Techniques (3+1)

INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI

Arithmetic and Logic Instructions And Programs

Instructions moving data

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

Adding Binary Integers. Part 4. Negative Binary. Integers. Adding Base 10 Numbers. Adding Binary Example = 10. Arithmetic Logic Unit

Intel 8086: Instruction Set

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

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

8088/8086 Programming Integer Instructions and Computations

Adding Binary Integers. Part 5. Adding Base 10 Numbers. Adding 2's Complement. Adding Binary Example = 10. Arithmetic Logic Unit

Week /8086 Microprocessor Programming I

Integer Arithmetic Part2

Basic Assembly SYSC-3006

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

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

Defining and Using Simple Data Types

Code segment Stack segment

Lesson 1. Fundamentals of assembly language

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

Computer Architecture and System Programming Laboratory. TA Session 3

Integer Arithmetic. Shift and rotate. Overview. Shift and Rotate Instructions

Basic Assembly Instructions

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

Summer 2003 Lecture 4 06/14/03

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

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

ELEC 242 Time Delay Procedure

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

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.

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

EEM336 Microprocessors I. Arithmetic and Logic Instructions

3.1 DATA MOVEMENT INSTRUCTIONS 45

8086 INTERNAL ARCHITECTURE

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

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

Week /8086 Microprocessor Programming

Introduction to 8086 Assembly

Computer Science Final Examination Wednesday December 13 th 2006

Lecture (07) x86 programming 6

Assembly Language Lab # 4 Data Transfer & Arithmetic (1)

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

Chapter 12. Selected Pentium Instructions

Transfer of Control. Lecture 10 JMP. JMP Formats. Jump Loop Homework 3 Outputting prompts Reading single characters

Microprocessors & Assembly Language Lab 1 (Introduction to 8086 Programming)

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.

Chapter 7 Integer Arithmetic

Integer Arithmetic. Computer Organization and Assembly Languages Yung-Yu Chuang 2005/11/17. with slides by Kip Irvine

Lecture 15 Intel Manual, Vol. 1, Chapter 3. Fri, Mar 6, Hampden-Sydney College. The x86 Architecture. Robb T. Koether. Overview of the x86

UNIT 2 PROCESSORS ORGANIZATION CONT.

EXPERIMENT NO.1. A Microcontroller is a complete computer system built on a single chip.

ET355 Microprocessors Thursday 6:00 pm 10:20 pm

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

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

LAB 5 Arithmetic Operations Simple Calculator

Faculty of Engineering Computer Engineering Department Islamic University of Gaza Assembly Language Lab # 2 Assembly Language Fundamentals

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

EXAMPLE PROGRAMS 8085

Chapter Four Instructions Set

CC411: Introduction To Microprocessors

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

Arithmetic and Bitwise Operations on Binary Data

COMPUTER ARITHMETIC (Part 1)

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

Chapter 2 COMPUTER SYSTEM HARDWARE

Lecture (08) x86 programming 7

Lecture 8: Addition, Multiplication & Division

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

8086 INSTRUCTION SET

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

Assembly Language: IA-32 Instructions

The x86 Architecture

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

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

16.317: Microprocessor Systems Design I Spring 2014

EC-333 Microprocessor and Interfacing Techniques

Math in MIPS. Subtracting a binary number from another binary number also bears an uncanny resemblance to the way it s done in decimal.

Basic Pentium Instructions. October 18

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

ADVANCE MICROPROCESSOR & INTERFACING

Week /8086 Microprocessor Programming II

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

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

Intel 8086 MICROPROCESSOR ARCHITECTURE

ENE 334 Microprocessors

Basic Execution Environment

CNIT 127: Exploit Development. Ch 1: Before you begin. Updated

EECE.3170: Microprocessor Systems Design I Spring 2016

APPENDIX C INSTRUCTION SET DESCRIPTIONS

Module 2: Computer Arithmetic

Assembly Language LAB

9/25/ Software & Hardware Architecture

SN8F5000 Family Instruction Set

16.317: Microprocessor Systems Design I Fall 2013

CS401 Assembly Language Solved MCQS From Midterm Papers

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

Transcription:

Lecture 9 INC and DEC Arithmetic Operations Overflow Multiply and Divide INC adds one to a single operand DEC decrements one from a single operand INC destination DEC destination where destination can be a register or a memory operand INC/DEC Examples ADD inc al ; increment 8-bit register dec bx ; decrement 16-bit register inc membyte ;increment 8-bit ;memory operand dec membyte ; decrement 8-bit ;memory operand inc memword ;increment 16-bit ;memory operand dec memword ;decrement 16-bit ;memory operand ADD adds a source operand to a destination operand of the same size. ADD dest, src ;dest = src+dest sizes must match only one operand (at most) can be a memory location all status flags are affected! 1

ADD Examples SUB add cl, al ;add 8-bit register to register ;cl = cl + al add bx, 1000h ;add immediate to 16-bit ; register bx = bx + 1000h add var1, ax ;add 16-bit register to memory ;var1 = var1 + ax add dx, var1 ;add 16-bit memory to register ;dx = dx + var1 add var, 10 ;add immediate value to ;memory var = var + 10 SUB subtracts a source operand from a destination operand. SUB dest, src ;dest = dest src sizes must match only one operand (at most) can be a memory location all status flags are affected inside the CPU, SUB is performed by negating the src operand (using 2 s complement) and added to the dest operand SUB Examples sub cl, al ;subtract 8-bit ; register from register ;cl = cl - al sub bx, 1000h ;subtract immediate ;value from 16-bit ;register ; bx = bx - 1000h sub var1, ax ;subtract 16-bit register ; from memory ;var1 = var1 - ax sub dx, var1 ;subtract 16-bit ;memory from register ;dx = dx var1 sub var1, 10 ;subtract immediate ;value from memory ;var1 = var1-10 Flags Affected: Zero Zero flag set when the result of an operation is zero. mov ax, 10 sub ax, 10 ;ax = 0, ZF = 1 mov bl, 4Fh add bl, 0B1h ;bl = 00, ZF = 1 mov ax, 0FFFFh ;ax = -1 inc ax ;ZF = 1 mov ax, 1 dec ax ;ZF = 1 2

Flags Affected: Sign Flags Affected: Carry Sign flag set when the result of an operation is negative. mov bx, 1 sub bx, 2 ;bx = FFFF, SF = 1 Carry flag set when there is a carry out of the left-most bit. mov bl, 4Fh add bl, 0B1h ;bl = 00, CF = 1 (4F + B1 = 100h since we only have eight bits, the 1 is discarded and the carry flag is set) Exception: INC and DEC do not set the carry flag! Flags Affected: Overflow Overflow flag set when an arithmetic operation generates a signed value that exceeds to storage size of the destination operand. This means that the value placed in the destination operand is incorrect. The CPU sets it by comparing the carry flag to the bit carried into the sign bit of the destination operand. If not equal, overflow is set. Overflow How you check for overflow depends on if you are using signed or unsigned operands. The programmer decides if they are using signed or unsigned numbers. The CPU updates both the carry and the overflow flags in order to cover both options. The programmer checks the appropriate flag depending on if they are doing signed or unsigned operations. 3

Unsigned Overflow During unsigned arithmetic, if the carry flag is set then overflow has occurred: addition: 0FFh + 1 = 100h. If you are only using 8 bits, then only the two lowest digits (00) fit into the destination. Carry is set and overflow has occurred (result will be 00, not 100) subtraction: 1 2 = 01 + FE = FF. If this is treated as unsigned, it is not correct! FF unsigned = 255. In this case, the carry flag is set and overflow has occurred. More on Unsigned Overflow Something looks strange about the subtraction: 01 + FE = FF -> where is the carry? For subtraction and negation, the carry flag is set if there is NO carry out of the most significant bit. 1 2 = 01 + FE = FF no carry, so carry bit is set to signify overflow. 2 1 = 02 + FF = 01 there is a carry, and the carry bit is cleared, signifying no overflow. Yes, this is a bit counter-intuitive. Signed Overflow During signed arithmetic, the overflow flag is set when an out of range value is generated. Of course, the computer doesn t know if your values are signed or unsigned, so what does this really mean? If the carry into the sign bit differs from the carry out of the sign bit, then the overflow flag is set. Signed Overflow Examples mov al, 126 add al, 2 01111110 + 00000010 10000000 = 80h carry into the sign bit is 1 carry out of sign bit is 0 80h = -128, not +128 -> overflow 4

More Signed Overflow mov al, -128 sub al, 2 10000000-00000010 01111110 al = 7Eh = 126, not -130 10000000 + 11111110 (2 in 2 s comp) 1 01111110 carry in = 0, carry out = 1 -> overflow Multiplication and Division Instructions for integer multiplication on 8, 16, and 32 bit operands MUL, DIV unsigned binary numbers IMUL, IDIV signed binary numbers For floating point? Special floating point instructions (Ch 15 in Irvine) MUL Multiplies an 8, 16, or 32 bit operand by,, or E respectively. Format: MUL multiplier multiplier register or memory (not immediate!) Registers used: Multiplicand Multiplier Product op-8 op-16 : MUL Examples bval db 10 mov al, 100 mul bval Before MUL: After MUL:? 64h 03 E8h 5

MUL Examples wval dw 1000 mov ax, 55555 ;D903h mul wval Before MUL: After MUL:? D903 034F B3B8 Multiplication result might need a high-order byte or word. We ll be using numbers that are small enough for the result to fit in so we can ignore. How do we know when we can do this? MUL sets CF and OF If result is small enough to fit into (bytes) or (words), CF = 0 and OF = 0 If result is big, CF = 1, OF = 1 Example: Checking Size IMUL ; multiply CX by. If result extends ; into, copy 2 words to result ; locations, else copy one word..data ResultLo dw? ResultHi dw? mul cx mov ResultLo, jnc L1 ;jump if carry not set mov ResultHi, L1: IMUL multiplies signed binary numbers. Why is this different? It sign extends the result when needed. Formats and use of registers are the same as in MUL. Carry and overflow flags are set the same. 6

IMUL Examples bval db 4 mov al, -4 imul bval IMUL Examples bval db 4 mov al, 48 imul bval Before IMUL:? FC After IMUL: Before IMUL:? 30h After IMUL: FF F0 00 C0 CF = 0, OF = 0 : Result fits into 8 bits, holds sign extension. CF = 1, OF = 1 : Result does not fit into 8 bits! (why? C0 is a negative number! 00C0 is positive) IMUL Examples wval dw 4 mov ax, 48 imul wval Before IMUL:? 0030 Warning Sixteen bit multiplication will wipe out whatever is in! It s easy to forget this if you are only using the result returned in. After IMUL: 0000 00C0 CF = 0, OF = 0: Result fits into 16 bits. 7

DIV DIV divides unsigned 8-bit, 16- bit, and 32-bit numbers. Format: DIV divisor divisor - register or memory (not immediate!) Registers used: Dividend Divisor Quotient Remainder op-8 : op-16 DIV Examples bval db 2 ;divisor mov ax, 0083h ;dividend div bval Before DIV: After DIV: 00 83h 01 41h 83h / 02h = 41h, R=1 DIV Examples wval dw 100h ;divisor mov dx, 0 ;clear dividend high! mov ax, 8003h ;dividend div wval Before DIV: After DIV: 0000 8003h 0003 0080h Warning If you don t remember to clear you will get unexpected results!!! If you had something you were using in, it will get destroyed by the division. 8003h / 0100h = 80h, R=3 8

IDIV IDIV works like DIV except it uses signed numbers. In 8-bit division, the dividend is in, so the sign is determined by bit 15. In 16-bit division, the sign is determined by bit 15 in. IDIV Examples bval db 5 ;divisor mov ax, -48 ;dividend idiv bval Before IDIV: FF After DIV: D0 FD F7 FFD0/5 = F7, R FD F7 = -9, FD = -3 What NOT to Do! bval db 5 ;divisor mov ah, 0 mov al, -48 ;dividend idiv bval Before IDIV: After DIV: 00 D0 (+208) Sign Extending You will need to sign extend your dividend. Intel provides instructions for this: CBW convert byte to word extends into CWD convert word to double word extends into : 03 29 00D0/5 = 29, R 3 29 = 41 9

IDIV Examples wval dw 256 ;divisor mov ax, -5000 ;: =????EC78h cwd ;: =FFFFEC78h idiv wval Before IDIV: FFFF After DIV: FF78 EC78 FFED FFED = -19 (quotient) FF78 = -136 (remainder) Division Problems Divide Overflow is produced by: a division result that is too large dividing by zero This is not handled gracefully by the processor: your program will die a horrible death. You ll need to prevent it: for large numbers, use larger operands (registers). for divide by zero, check for the zero yourself before you divide. or, you can write a special interrupt handler (see chapter 15 if you re interested). 10