Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.

Slides:



Advertisements
Similar presentations
COMP 2003: Assembly Language and Digital Logic
Advertisements

Department of Computer Science and Software Engineering
Assembly Language Lecture 9
1 Multiplication, Division, and Numerical Conversions Chapter 6.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, MUL Instruction The MUL (unsigned multiply) instruction.
1 Lecture 7 Integer Arithmetic Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1.
Gursharan Singh Tatla 21-Nov-20101www.eazynotes.com.
© 2006 Pearson Education, Upper Saddle River, NJ All Rights Reserved.Brey: The Intel Microprocessors, 7e Chapter 5 Arithmetic and Logic Instructions.
9-1 ECE 424 Design of Microprocessor-Based Systems Haibo Wang ECE Department Southern Illinois University Carbondale, IL x86 Instructions Part.
Shift and Rotate Instructions
CE302 Outline Multiplication Division Program Segment Prefix Command Line Parameters.
MUL Instruction (Unsigned Multiply) Multiplies an 8-, 16-, or 32-bit operand by either AL, AX or EAX. MUL r/m8 MUL r/m16 MUL r/m32.
Assembly Language – Lab 5
Khaled A. Al-Utaibi  Introduction  Arithmetic Instructions  Basic Logical Instructions  Shift Instructions  Rotate Instructions.
Chapter 3 Elements of Assembly Language. 3.1 Assembly Language Statements.
Flag Control instructions CLC clear carry flag CF = 0 STC set carry flag CF= 1 CMC complement carry flag [CF] CF.
Chapter four – The 80x86 Instruction Set Principles of Microcomputers 2015年10月19日 2015年10月19日 2015年10月19日 2015年10月19日 2015年10月19日 2015年10月19日 1 Chapter.
Multiplication and Division Instructions & the 0Ah function.
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
Microprocessors Monday, Apr. 13 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.
Copyright 2000ELEC 242 Arithmetic Instructions1 Arithmetic Instructions Arithmetic and logical instructions modify the contents of the Flag (Status) register.
Arithmetic Flags and Instructions
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 4 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 05.b: Arithmetic Operations Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,
Integer Arithmetic Computer Organization and Assembly Languages Yung-Yu Chuang 2007/12/24 with slides by Kip Irvine.
Assembly Language for Intel-Based Computers Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may modify and copy.
Assembly Language. Symbol Table Variables.DATA var DW 0 sum DD 0 array TIMES 10 DW 0 message DB ’ Welcome ’,0 char1 DB ? Symbol Table Name Offset var.
Arithmetic and Logic Instructions
3.4 Addressing modes Specify the operand to be used. To generate an address, a segment register is used also. Immediate addressing: the operand is a number.
Arithmetic Flags and Instructions Chapter 7 S. Dandamudi.
ICS 312 SET 10 Multiplication & Division & input using function 0Ah.
The Assemble, Unassemble commands of the debugger: U Command for converting machine code language source Equivalent machine code instructions Equivalent.
Introduction Arithmetic instructions are used to perform arithmetic operation such as Addition Subtraction Multiplication Division These operations can.
Chapter 7 Bit Manipulation. 7.1 Logical Operations.
Microprocessor & Assembly Language Arithmetic and logical Instructions.
K.K. Leung Fall 2008Introductory Pentium Programming1 Pentium Architecture: Introductory Programming Kin K. Leung
Multiplication and Division instructions Dr.Hadi AL Saadi.
Data Transfers, Addressing, and Arithmetic
Microprocessor Systems Design I
16.317: Microprocessor System Design I
Microprocessor Systems Design I
Microprocessor Systems Design I
Basic Assembly Language
Multiplication and Division Instructions
Assembly Language Programming Part 2
# include < stdio.h > v oid main(void) { long NUM1[5]; long SUM; long N; NUM1[0] = 17; NUM1[1] = 3; NUM1[2] =  51; NUM1[3] = 242; NUM1[4] = 113; SUM =
CS-401 Computer Architecture & Assembly Language Programming
4.2 Arithmetic Instructions
INSTRUCTION SET OF 8086 PAWAN KUMAR SINGH.
Arithmetic Instructions
X86’s instruction sets.
Chapter 4: Instructions
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Microprocessor and Assembly Language
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Multiplication and Division Instructions
More IA32 (AKA Pentium) Instructions
Assembly Language for Intel-Based Computers, 4th Edition
Assembly Language for Intel-Based Computers, 5th Edition
Computer Architecture and System Programming Laboratory
EECE.3170 Microprocessor Systems Design I
Chapter 5 Arithmetic and Logic Instructions
Chapter 5: Arithmetic and Logic Instructions
Multiplication and Division Instructions
Multiplication and Division Instructions
Chapter 8: Instruction Set 8086 CPU Architecture
Division instruction.
Computer Architecture and System Programming Laboratory
Presentation transcript:

Chapter 4 Basic Instructions

4.1 Copying Data

mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements in a high-level language Format: mov destination, source register or memory register, memory or immediate

Operand Restrictions Operands must be same size Can’t move from memory to memory –mov nbr1, nbr2 illegal if nbr1 and nbr2 reference doublewords in memory –Instead use a register mov eax, nbr2 mov nbr1, eax Can only move one byte, word or doubleword at a time

Effect on Flags In general, an instruction may have one of three effects: –no flags are altered –specific flags are given values depending on the results of the instruction –some flags may be altered, but their settings cannot be predicted No mov instruction changes any flag

Machine Code Depends on operand type(s), with several different opcodes used for mov instructions Word-size and doubleword-size instructions use same opcodes, but word- size instructions have 66 prefix byte Object and source code from listing file B0 9B mov al, | B8 009B mov ax, 155 B B mov eax, 155

ModR/M Byte Part of the object code for many instructions Used to encode specific registers Used to distinguish between instructions that share the same opcode Used to specify memory modes

ModR/M Fields mod (mode), 2 bits reg (register), 3 bits r/m (register/memory), 3 bits Examples of encodings –mod = 00 and r/m = 101 combined always means direct memory addressing –reg = 011 means the EBX register in a 32-bit instruction

xchg Instruction Swaps the values referenced by its two operands –Can’t have both operands in memory Does not alter any flag

4.2 Integer Addition and Subtraction Instructions

add Instruction Format: add destination, source The integer at source is added to the integer at destination and the sum replaces the old value at destination SF, ZF, OF, CF, PF and AF flags are set according to the value of the result of the operation –Example: CF = 1 if there is a carry out of the sum

Addition Example Before EAX: ECX: A2 Instruction add eax, ecx After EAX: ECX: A2 SF=0 ZF=0 CF=0 OF=0

sub Instruction Format: sub destination, source The integer at source is subtracted from the integer at destination and the difference replaces the old value at destination SF, ZF, OF, CF, PF and AF flags are set according to the value of the result of the operation –Example: ZF = 1 if the difference is zero

Subtraction Example Before doubleword at Dbl: Instruction sub Dbl, 2 After Dbl: FE SF=0 ZF=0 CF=0 OF=0

Instruction Encoding Opcode depends on operand types The ModR/M byte distinguishes –Between operand types –Between add, sub and other operations for certain operand types An small immediate operand is sometimes encoded as a byte even in a 32-bit instruction

Increment and Decrement Instructions inc destination –Adds 1 to destination dec destination –Subtracts 1 from destination Each sets same flags as add or sub except for CF which isn’t changed

neg Instruction neg destination Negates (takes the 2's complement of) its operand –A positive value gives a negative result –A negative value will become positive –Zero remains 0 Affects same flags as add and sub

Programming in Assembly Language Start with a design Plan register usage –Decide what registers will be used for what variables in the design –There are only a few available registers Plan memory usage

4.3 Multiplication Instructions

Multiplication Instruction Mnemonics mul for unsigned multiplication –Operands treated as unsigned numbers imul for signed multiplication –Operands treated as signed numbers and result is positive or negative depending on the signs of the operands

mul Instruction Format mul source Single operand may be byte, word, doubleword or quadword in register or memory (not immediate) and specifies one factor Location of other factor is implied –AL for byte-size source –AX for word source –EAX for doubleword source –RAX for quadword source

mul Instruction Operation When a byte source is multiplied by the value in AL, the product is put in AX When a word source is multiplied by the value in AX, the product is put in DX:AX –The high-order 16 bits in DX and the low- order 16 bits in AX When a doubleword source is multiplied by the value in EAX, the product is put in EDX:EAX Product of two quadwords in RAX:DAX

Double-Length Product The “double-length” product ensures that the result will always fit in the destination location If significant bits of the product actually “spill over” into the high-order half (AH, DX or EDX), then CF and OF are both set to 1 If the high-order half is not significant, then CF and OF are both cleared to 0 –For unsigned multiplication, this is when the high-order half is all 0’s

mul Instruction Example Before EAX: EBX: EDX: ???????? Instruction mul ebx After EAX: A EBX: EDX: CF=OF=0

imul Instruction Formats imul source imul register, source imul register, source, immediate

imul source “Single-operand format” Similar to mul source except for signed operands CF=OF=0 if each bit in the high-order half is the same as the sign bit in the low-order half CF=OF=1 otherwise (the bits in the high- order half are significant)

Single-Operand Example Before AX: ??05 byte at Factor: FF Instruction imul Factor After AX: FFFB CF=OF=0

imul register,source “Two-operand format” Source operand can be in a register, in memory, or immediate Register contains other factor, and also specifies the destination Both operands must be word-size or doubleword-size, not byte-size Product must “fit” in destination register –CF and OF are cleared to 0 if result fits –CF and OF are set to 1 if it doesn’t fit

Two-operand Example Before EBX: A Instruction imul ebx, 10 After EBX: CF=OF=0

imul register,source,immediate “Three-operand format” The two factors are given by source (register or memory) and the immediate value The first operand, a register, specifies the destination for the product Operands register and source are the same size, both 16-bit or both 32-bit (not 8-bit) If the product will fit in the destination register, then CF and OF are cleared to 0; if not, they are set to 1

Three-Operand Example Before word at Value: 08F2 BX: ???? Instruction imul bx, Value, 1000 After BX: F150 CF=OF=1

4.4 Division Instructions

Division Instruction Formats idiv source for signed operands div source for unsigned operands source identifies the divisor –Byte, word, doubleword or quadword –In memory or register, but not immediate

Implicit Dividend for div and idiv Byte source divided into word in AX Word source divided into doubleword in DX:AX Doubleword source divided into quadword in EDX:EAX Quadword source divided into RDX:RAX

Results of div and idiv Byte-size divisor: quotient in AL and remainder in AH Word-size divisor: quotient in AX and remainder in DX Doubleword-size divisor: quotient in EAX and remainder in EDX Quadword-size divisor: quotient in RAX and remainder in RDX

Results of div and idiv All division operations satisfy the relation dividend = quotient*divisor + remainder –For signed division, the remainder will have same sign as dividend

Flag Settings Division instructions do not set flags to any meaningful values They may change previously set values of AF, CF, OF, PF, SF or ZF

Unsigned Division Example Before EDX: EAX: EBX: D Instruction div ebx ; 100/13 After EDX: EAX: = 7 *

Signed Division Example Before EDX: FF FF FF FF EAX: FF FF FF 9C ECX: D Instruction idiv ecx ; -100/13 After EDX: FFFFFFF7 EAX: FFFFFFF9 –100 = (–7) * 13 + (–9)

Errors in Division Caused by –Dividing by 0, or –Quotient too large to fit in destination Triggers an exception –The interrupt handler routine that services this exception may vary from system to system –When a division error occurs for a program running under Visual Studio, an error window pops up

Preparing for Division Dividend must be extended to double length Example –Copy a doubleword dividend to EAX –Extend dividend to EDX:EAX For unsigned division, use mov edx, 0 For signed division, use cdq instruction –Finally use div or idiv instruction

Convert Instructions No operand cbw sign extends the byte in AL to the word in AX cwd sign extends the word in AX to the doubleword in DX:AX cdq sign extends the doubleword in EAX to the quadword in EDX:EAX cqo sign extends the quadword in RAX to RDX:RAX