Integer Arithmetic Part2

Similar documents
Chapter 7 Integer Arithmetic

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

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.

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

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

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

Instructions moving data

Introduction to 8086 Assembly

Arithmetic Instructions

Defining and Using Simple Data Types

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

INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI

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

Computer Architecture and System Programming Laboratory. TA Session 3

Basic Pentium Instructions. October 18

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

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

Signed number Arithmetic. Negative number is represented as

EC 333 Microprocessor and Interfacing Techniques (3+1)

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

Assembly Language LAB

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

16.317: Microprocessor Systems Design I Spring 2014

8088/8086 Programming Integer Instructions and Computations

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

Chapter 6 (Part a) Conditional Processing

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

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

3.1 DATA MOVEMENT INSTRUCTIONS 45

Basic Assembly Instructions

LAB 5 Arithmetic Operations Simple Calculator

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

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

Intel 8086: Instruction Set

ELEC 242 Time Delay Procedure

EECE.3170: Microprocessor Systems Design I Spring 2016

Computer Architecture and Assembly Language. Practical Session 3

16.317: Microprocessor Systems Design I Fall 2013

Lecture (07) x86 programming 6

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

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

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

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

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

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.

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

Assembly Language LAB

MODE (mod) FIELD CODES. mod MEMORY MODE: 8-BIT DISPLACEMENT MEMORY MODE: 16- OR 32- BIT DISPLACEMENT REGISTER MODE

ADDRESSING MODES. Operands specify the data to be used by an instruction

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

Module 3 Instruction Set Architecture (ISA)

EEM336 Microprocessors I. Arithmetic and Logic Instructions

Summer 2003 Lecture 4 06/14/03

Computer Science Final Examination Wednesday December 13 th 2006

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 6: Conditional Processing

COMPUTER ENGINEERING DEPARTMENT

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

Data Transfers, Addressing, and Arithmetic. Part 2

Chapter 3: Addressing Modes

/ 30 Q3: Arithmetic instructions / 30 Q4: Logical instructions / 20 TOTAL SCORE / 100 Q5: EXTRA CREDIT / 10

Inline Assembler. Willi-Hans Steeb and Yorick Hardy. International School for Scientific Computing

Assembly Language Programming

Step 1: Add the least significant digits together: produces with carry 1.

APPENDIX C INSTRUCTION SET DESCRIPTIONS

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

Addressing Modes on the x86

COMPUTER ENGINEERING DEPARTMENT

Basic Execution Environment

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

Assembly Language: IA-32 Instructions

Basic Assembly SYSC-3006

Scott M. Lewandowski CS295-2: Advanced Topics in Debugging September 21, 1998

EEM336 Microprocessors I. Addressing Modes

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

SYSC3601 Microprocessor Systems. Unit 2: The Intel 8086 Architecture and Programming Model

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

Lesson 1. Fundamentals of assembly language

Binghamton University. CS-220 Spring x86 Assembler. Computer Systems: Sections

PESIT Bangalore South Campus

Credits and Disclaimers

ASSEMBLY - QUICK GUIDE ASSEMBLY - INTRODUCTION

from WRITE GREAT CODE Volume 2: Thinking Low-Level, Writing High-Level ONLINE APPENDIX A The Minimal 80x86 Instruction Set by Randall Hyde

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer

Week /8086 Microprocessor Programming

The x86 Architecture

UMBC. A register, an immediate or a memory address holding the values on. Stores a symbolic name for the memory location that it represents.

6/20/2011. Introduction. Chapter Objectives Upon completion of this chapter, you will be able to:

Shift and Rotate Instructions

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

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

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

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

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

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

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

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

Transcription:

Islamic University Of Gaza Assembly Language Faculty of Engineering Discussion Computer Department Chapter 7 Eng. Ahmed M. Ayash Date: 21/04/2013 Chapter 7 Integer Arithmetic Part2 7.4: Multiplication and Division Instructions MUL Instruction The MUL (unsigned multiply) instruction comes in three versions: o The first version multiplies an 8-bit operand by the AL register. o The second version multiplies a 16-bit operand by the AX register. o The third version multiplies a 32-bit operand by the EAX register. The multiplier and multiplicand must always be the same size, and the product is twice their size. The three formats accept register and memory operands, but not immediate operands: o MUL reg/mem8 o MUL reg/mem16 o MUL reg/mem32 MUL sets the Carry and Overflow flags if the upper half of the product is not equal to zero. IMUL Instruction The IMUL instruction is used for signed multiplication Preserves the sign of the product by sign-extending it. 1

One-Operand formats, as in MUL o IMUL r/m8 ; AX = AL * r/m8 o IMUL r/m16 ; DX:AX = AX * r/m16 o IMUL r/m32 ; EDX:EAX = EAX * r/m32 Two-Operand formats: o IMUL r16, r16/m16/imm8/imm16 o IMUL r32, r32/m32/imm8/imm32 Three-Operand formats: o IMUL r16, r16/m16, imm8/imm16 o IMUL r32, r32/m32, imm8/imm32 The Carry and Overflow flags are set if the upper half of the product is not a sign extension of the lower half. Example1: mov al,48 mov bl,4 imul bl ; AX = 00C0h (decimal +192), OF = CF = 1 Because AH is not a sign extension of AL, the Overflow flag is set to 1. Example2: mov ax, 48 mov bx, 4 imul bx ; DX:AX = 0000h:00C0h, OF = CF = 0 Here, DX is a sign extension of AX, so the Overflow flag is cleared. Example3: mov al,-4 mov bl,4 imul bl ; AX = FFF0h, CF = OF = 0 AH is a sign extension of AL, therefore the Overflow flag is cleared. DIV Instruction The DIV (unsigned divide) instruction performs 8-bit, 16-bit, and 32-bit unsigned integer division. The single register or memory operand is the divisor. The formats are o DIV reg/mem8 o DIV reg/mem16 o DIV reg/mem32 2

Signed Integer Division The dividend must be fully sign-extended before the division takes place. o Fill high byte, word, or double-word with a copy of the low byte/word/double word s sign bit. o Then we will apply them to the signed integer divide instruction, IDIV. o For Example: Sign Extension Instructions (CBW, CWD, CDQ) o Provide important sign-extension operations before division o CBW: Convert Byte to Word, sign-extends AL into AH o CWD: Convert Word to Double, sign-extends AX into DX o CDQ: Convert Double to Quad, sign-extends EAX into EDX IDIV Instruction The IDIV (signed divide) instruction performs signed integer division, using the same operands as DIV. Before executing 8-bit division, the dividend (AX) must be completely sign-extended. The remainder always has the same sign as the dividend. Divide Overflow Divide Overflow occurs when o Quotient cannot fit into the destination operand, or when o Dividing by Zero. Divide Overflow causes a CPU interrupt o The current program halts and an error dialog box is produced For Example: mov ax,1000h mov bl,10h div bl ; AL cannot hold 100h All status flags are undefined after executing DIV and IDIV 3

7.4.7 Section Review (7.4.7 p1) Explain why overflow cannot occur when the MUL and one-operand IMUL instructions execute. The product is stored in registers that are twice the size of the multiplier and multiplicand. If you multiply 0FFh by 0FFh, for example, the product (FE01h) easily fits within 16 bits. (7.4.7 p2) How is the one-operand IMUL instruction different from MUL in the way it generates a multiplication product? When the product fits completely within the lower register of the product, IMUL sign extends the product into the upper product register. MUL, on the other hand, zeroextends the product. (7.4.7 p3) What has to happen in order for the one-operand IMUL to set the Carry and Overflow flags? With IMUL, the Carry and Overflow flags are set when the upper half of the product is not a sign extension of the lower half of the product. (7.4.7 p4) When EBX is the operand in a DIV instruction, which register holds the quotient? EAX (7.4.7 p5) When BX is the operand in a DIV instruction, which register holds the quotient? AX (7.4.7 p6) When BL is the operand in a MUL instruction, which registers hold the product? AX. (7.4.7 p7) Show an example of sign extension before calling the IDIV instruction with a 16-bit operand. Code example: 4

mov ax,dividendlow cwd mov bx,divisor idiv bx ; sign-extend dividend (7.4.7 p8) What will be the contents of AX and DX after the following operation? mov dx,0 mov ax,222h mov cx,100h mul cx DX = 0002h, AX = 2200h. (7.4.7 p9) What will be the contents of AX after the following operation? mov ax,63h mov bl,10h div bl AX = 0306h. (7.4.7 p10) What will be the contents of EAX and EDX after the following operation? mov eax,123400h mov edx,0 mov ebx,10h div ebx EDX = 0, EAX = 00012340h. (7.4.7 p11) What will be the contents of AX and DX after the following operation? mov ax,4000h mov dx,500h mov bx,10h div bx The DIV will cause a divide overflow, so the values of AX and DX cannot be determined. (7.4.7 p12) Write instructions that multiply -5 by 3 and store the result in a 16-bit variable val1. mov al,3 mov bl,-5 imul bl mov val1,ax ; product 5

(7.4.7 p13) Write instructions that divide -276 by 10 and store the result in a 16-bit variable val1. mov ax,-276 cwd ; sign-extend AX into DX mov bx,10 idiv bx mov val1,ax ; quotient (7.4.7 p14) Implement the following C++ expression in assembly language, using 32-bit unsigned operands: val1 = (val2 * val3) / (val4-3) mov eax,val2 mul val3 mov ebx,val4 sub ebx,3 div ebx mov val1,eax (7.4.7 p15) Implement the following C++ expression in assembly language, using 32-bit signed operands: val1 = (val2 / val3) * (val1 + val2) mov eax,val2 cdq ; extend EAX into EDX idiv val3 ; EAX = quotient mov ebx,val1 add ebx,val2 imul ebx mov val1,eax ; lower 32 bits of product Extended Addition and Subtraction Extended precision addition and subtraction is adding and subtracting numbers having an almost unlimited size. ADC Instruction o ADC (add with carry) instruction adds both a source operand and the contents of the Carry flag to a destination operand. o The instruction formats are the same as for the ADD instruction, and the operands must be the same size: ADC reg,reg ADC mem,reg ADC reg,mem ADC mem,imm ADC reg,imm 6

o Example: Add two 32-bit integers (FFFFFFFFh + FFFFFFFFh), producing a 64-bit sum in EDX:EAX: mov edx,0 mov eax,0ffffffffh add eax,0ffffffffh adc edx,0 ;EDX:EAX = 00000001FFFFFFFEh o Task: Add 1 to EDX:EAX Starting value of EDX:EAX: 00000000FFFFFFFFh Add the lower 32 bits first, setting the Carry flag. Add the upper 32 bits, and include the Carry flag. mov edx,0 ; set upper half mov eax,0ffffffffh ; set lower half add eax,1 ; add lower half adc edx,0 ; add upper half ;EDX:EAX = 00000001 00000000 SBB Instruction o The SBB (subtract with borrow) instruction subtracts both a source operand and the value of the Carry flag from a destination operand. o Operand syntax: Same as for the ADC instruction. o Task: Subtract 1 from EDX:EAX Starting value of EDX:EAX: 0000000100000000h Subtract the lower 32 bits first, setting the Carry flag. Subtract the upper 32 bits, and include the Carry flag. mov edx,1 mov eax,0 sub eax,1 sbb edx,0 ; set upper half ; set lower half ; subtract lower half ; subtract upper half ;EDX:EAX = 00000000 FFFFFFFF 7.5.4 Section Review (7.5.4 p1) Describe the ADC instruction. The ADC instruction adds both a source operand and the Carry flag to a destination operand. 7

(7.5.4 p2) Describe the SBB instruction. The SBB instruction subtracts both a source operand and the Carry flag from a destination operand. (7.5.4 p3) What will be the values of EDX:EAX after the following instructions execute? mov edx,10h mov eax,0a0000000h add eax,20000000h adc edx,0 EAX = C0000000h, EDX = 00000010h. (7.5.4 p4) What will be the values of EDX:EAX after the following instructions execute? mov edx,100h mov eax,80000000h sub eax,90000000h sbb edx,0 EAX = F0000000h, EDX = 000000FFh. (7.5.4 p5) What will be the contents of DX after the following instructions execute (STC sets the Carry flag)? mov dx,5 stc ; set Carry flag mov ax,10h adc dx,ax DX = 0016h. (7.5.4 p6) The following program is supposed to subtract val2 from val1. Find and correct all logic errors (CLC clears the Carry flag):.data val1 QWORD 20403004362047A1h val2 QWORD 055210304A2630B2h result QWORD 0.code mov cx,8 ; loop counter mov esi,val1 ; set index to start mov edi,val2 clc ; clear Carry flag top: mov al,byte PTR[esi] ; get first number 8

sbb al,byte PTR[edi] ; subtract second mov BYTE PTR[esi],al ; store the result dec esi dec edi loop top In correcting this example, it is easiest to reduce the number of instructions. You can use a single register (ESI) to index into all three variables. ESI should be set to zero before the loop because the integers are stored in little endian order with their loworder bytes occurring first: mov ecx,8 ; loop counter mov esi,0 ; use the same index reg clc ; clear Carry flag top: mov al,byte ptr val1[esi] ; get first number sbb al,byte ptr val2[esi] ; subtract second mov byte ptr result[esi],al ; store the result inc esi ; move to next pair loop top Of course, you could easily reduce the number of loop iterations by adding doublewords rather than bytes. Quiz Next Week 9