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

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

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.

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

Chapter 12. Selected Pentium Instructions

Arithmetic and Logic Instructions And Programs

Shift and Rotate Instructions

Lecture (08) x86 programming 7

Lab Session 08. To understand the use of Shift and Rotate instructions. To be able to differentiate between Arithmetic shift and Logical shift.

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

Chapter 5. Real-Mode 80386DX Microprocessor Programming 1 Part 2. The 80386, 80486, and Prentium Processors,Triebel Prof. Yan Luo, UMass Lowell 1

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

Instructions moving data

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

Logical and Bit Operations. Chapter 8 S. Dandamudi

Microprocessor & Computer Architecture / Lecture

Basic Assembly SYSC-3006

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

Computer Department Chapter 7. Created By: Eng. Ahmed M. Ayash Modified and Presented by: Eng. Eihab S. El-Radie. Chapter 7

8088/8086 Programming Integer Instructions and Computations

Introduction to 8086 Assembly

Week /8086 Microprocessor Programming I

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

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

Basic Pentium Instructions. October 18

3.1 DATA MOVEMENT INSTRUCTIONS 45

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

Chapter 7 Integer Arithmetic

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

Bit Operations. Ned Nedialkov. McMaster University Canada. SE 3F03 February 2014

Week /8086 Microprocessor Programming II

EECE.3170: Microprocessor Systems Design I Spring 2016

Signed number Arithmetic. Negative number is represented as

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

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

16.317: Microprocessor Systems Design I Fall 2013

Computer Architecture and System Programming Laboratory. TA Session 3

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

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

Arithmetic Instructions

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

CMSC 313 Lecture 07. Short vs Near Jumps Logical (bit manipulation) Instructions AND, OR, NOT, SHL, SHR, SAL, SAR, ROL, ROR, RCL, RCR

Code segment Stack segment

Logical and bit operations

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: Instruction Set

Defining and Using Simple Data Types

8086 INSTRUCTION SET

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

CC411: Introduction To Microprocessors

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

Assembly Language LAB

INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI

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

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

EC 333 Microprocessor and Interfacing Techniques (3+1)

Arithmetic Logic Unit

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

16.317: Microprocessor Systems Design I Spring 2014

CMSC 313 Lecture 05 [draft]

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

Intel 8086 MICROPROCESSOR ARCHITECTURE

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

Section 001. Read this before starting!

Week /8086 Microprocessor Programming

Section 001 & 002. Read this before starting!

UNIT 2 PROCESSORS ORGANIZATION CONT.

CS401 Assembly Language Solved MCQS From Midterm Papers

APPENDIX C INSTRUCTION SET DESCRIPTIONS

8086 INTERNAL ARCHITECTURE

Read this before starting!

Computer Architecture and Assembly Language. Practical Session 3

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

International Islamic University Chittagong (IIUC) Department of Electrical and Electronic Engineering (EEE)

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

Chapter Four Instructions Set

Section 001. Read this before starting!

icroprocessor istory of Microprocessor ntel 8086:

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

SOEN228, Winter Revision 1.2 Date: October 25,

Computer Architecture and Organization: L04: Micro-operations

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

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

9/25/ Software & Hardware Architecture

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

Lecture 2 Assembly Language

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

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

TYPES OF INTERRUPTS: -

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

Intel 8086 MICROPROCESSOR. By Y V S Murthy

CS241 Computer Organization Spring 2015 IA

PESIT Bangalore South Campus

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 5

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

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

Lab 3. The Art of Assembly Language (II)

10-1 C D Pearson Education, Inc. M. Morris Mano & Charles R. Kime LOGIC AND COMPUTER DESIGN FUNDAMENTALS, 4e

UNIT II 16 BIT MICROPROCESSOR INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING. The Intel 8086 Instruction Set

Transcription:

Lecture 9 INC and DEC Arithmetic Operations Shift Instructions Next week s homework! 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 dec membyte ; decrement 8-bit inc memword ;increment 16-bit dec memword ;decrement 16-bit 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! 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 1

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 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, = 1 (4F + B1 = 1FFh 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: 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. 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. 2

Unsigned 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 = 127. In this case, the carry flag is set and overflow has occurred. More on Unsigned 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 overflow. Yes, this is a bit counter-intuitive. Signed 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 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 More Signed Shift and Rotate 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 Shift and rotate instructions provide a way to move bits around in an operand. SHL shift left SHLD double-precision shift left SHR shift right SHRD double-precision shift right SAL shift arithmetic left SAR shift arithmetic right ROL rotate left ROR rotate right RCL rotate carry left RCR rotate carry right 3

SHL shift left Each bit in the destination operand is shifted to the left, filling the lowest bit with zero. The high bit is moved to the carry bit, the bit that was in the carry bit is discarded. SHL dest,1 ;1 bit to left SHL dest, CL ;CL holds # of bits SHL dest, immed8 ; shift immed8 ;# of bits SHL continued shl bl, 1 ;shift bl 1 bit to the left ;bl = 05h, new bl = 0Ah shl wordval, 1 ;16-bit memory operand ;wordval = 50A7h, new ;wordval = A14Eh shl al, cl ;shift using count in cl ;al = 4Bh, cl = 4, new al = B0h shl bx, 5 ;shift left 5 ;bx = 50A7h, new bx = 14E0h 0 Uses for SHL SHR shift right The most common use is highspeed multiplication. mov dl, 1 ;dl = 1 shl dl, 1 ;dl = 2 shl dl, 1 ;dl = 4 shl dl, 1 ;dl = 8 etc. Each shift left multiplies by a power of 2! Shifting is much faster than multiplication. Each bit in the destination operand is shifted to the right, replacing the highest bit with a zero. The low bit is copied into the carry flag, and the bit that was in the carry flag is lost. Instruction formats are the same as in SHL. 0 SHR - continued shr bl, 1 ;shift bl 1 bit to the right ;bl = 05h, new bl = 02h shr wordval, 1 ;16-bit memory operand ;wordval = 50A7h, new ;wordval =2853h shr al, cl ;shift using count in cl ;al = 4Bh, cl = 4, new al = 04h shr bx, 5 ;shift right 5 ;bx = 50A7h, new bx = 0285h Uses for SHR You can use SHR to divide a number by 2: mov dl, 32 ;00100000b shr dl, 1 ;00010000b (dl = 16) You can use multiple shifts to divide by larger powers of 2: mov al, 01000000b ;al = 64 shr al, 3 ;divide by 8 ; al = 00001000b = 8 what numbers can t you divide this way? -> signed numbers! (sign is lost) 4

SAR SAR cont. So how do you divide signed numbers? SAR and SAL (shift arithmetic right and shift arithmetic left). SAL is identical to SHL and is included in the instruction set for completeness SAR shifts each bit to the right and makes a copy of the sign bit, preserving the sign of the number. mov al, 0F0h ;al = 11110000b ;(-16) sar al, 1 ;al = 11111000b ;(-8), = 0 mov dx, 80000h ;dx = ;100000000000000b ;(-32768) sar dx, 5 ;dx = ;1111110000000000 ;(-1024) shifting right 5 times is the same as dividing by 2**5. ROL ROL cont. ROL, rotate left, moves each bit to the left. The highest bit is copied into the carry flag and the lowest bit. In rotate instructions, bits are never lost as they rotate off one end, they rotate back on to the other. mov al, 40h rol al, 1 ;al = 01000000b ;al = 10000000b rol al, 1 ;al = 00000001b ;cf = 1 rol al, 1 ;al = 00000010b Example Using ROL ROR Example 1, p233 of Irvine Like ROL, except rotating right. mov al, 01h ;al = 00000001b ror al, 1 ;al = 10000000 ;cf = 1 ror al, 1 ;al = 01000000b 5