Introduction to 8086 Assembly

Similar documents
Introduction to 8086 Assembly

Computer Architecture and System Programming Laboratory. TA Session 3

Basic Assembly Instructions

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

Integer Arithmetic Part2

Instructions moving data

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

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

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

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

Basic Pentium Instructions. October 18

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

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

Chapter 7 Integer Arithmetic

Defining and Using Simple Data Types

Registers. Ray Seyfarth. September 8, Bit Intel Assembly Language c 2011 Ray Seyfarth

LAB 5 Arithmetic Operations Simple Calculator

EC 333 Microprocessor and Interfacing Techniques (3+1)

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

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

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

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

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 5

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

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

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

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

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

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

Computer Architecture and Assembly Language. Practical Session 3

Hardware and Representation

ELEC 242 Time Delay Procedure

16.317: Microprocessor Systems Design I Spring 2015

Credits and Disclaimers

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

16.317: Microprocessor Systems Design I Fall 2015

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

INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI

LAB 4 Extending Signed and Unsigned Numbers

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

Week /8086 Microprocessor Programming I

mith College Computer Science CSC231 Assembly Week #5 Fall 2017 Dominique Thiébaut

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

Assembly Language: IA-32 Instructions

Subprograms: Local Variables

CONTENTS. 1. Display a Message Display a one Digit Number Accept a Character from keyboard and display the character 4

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

Arithmetic 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.

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 12

Week /8086 Microprocessor Programming

Where We Are. Optimizations. Assembly code. generation. Lexical, Syntax, and Semantic Analysis IR Generation. Low-level IR code.

Introduction to 8086 Assembly

Language of x86 processor family

Credits and Disclaimers

Signed number Arithmetic. Negative number is represented as

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

Intel 8086: Instruction Set

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

Sungkyunkwan University

x64 Cheat Sheet Fall 2014

ASSEMBLY - QUICK GUIDE ASSEMBLY - INTRODUCTION

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer

Computer Architecture and System Programming Laboratory. TA Session 5

COMPUTER ENGINEERING DEPARTMENT

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

CSE351 Spring 2018, Midterm Exam April 27, 2018

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Part 1

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

A4 Sample Solution Ch3

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

X86-NASM STANDARD COMMANDS. Comment your code with a semicolon (;)! The assembler won t read anything after it.

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

Assembly I: Basic Operations. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Von Neumann Architecture. Machine Languages. Context of this Lecture. High-Level Languages. Assembly Language: Part 1. Princeton University.

COMPUTER ENGINEERING DEPARTMENT

Basic Assembly SYSC-3006

Stack, subprograms. procedures and modular programming role of stack while using procedures stack implementation (Pentium)

The Hardware/Software Interface CSE351 Spring 2013

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

mith College Computer Science CSC231 - Assembly Week #4 Dominique Thiébaut

Compiler construction. x86 architecture. This lecture. Lecture 6: Code generation for x86. x86: assembly for a real machine.

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

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 21: Generating Pentium Code 10 March 08

CS 33. Machine Programming (2) CS33 Intro to Computer Systems XII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

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

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

Summer 2003 Lecture 4 06/14/03

Basic Intel x86 Assembly Language Programming

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

EEM336 Microprocessors I. Addressing Modes

Branching and Looping

Instructions and Instruction Set Architecture

Subprograms: Arguments

Instruction Set Architecture (ISA) Data Types

3.1 DATA MOVEMENT INSTRUCTIONS 45

16.317: Microprocessor Systems Design I Spring 2014

ECE 498 Linux Assembly Language Lecture 3

Computer Processors. Part 2. Components of a Processor. Execution Unit The ALU. Execution Unit. The Brains of the Box. Processors. Execution Unit (EU)

Transcription:

Introduction to 8086 Assembly Lecture 7 Multiplication and Division

Multiplication commands: mul and imul mul source (source: register/memory)

Unsigned Integer Multiplication (mul) mul src (src: register/memory) src: 8 bits ax <- al * src src: 16 bits dx:ax <- ax * src src: 32 bits edx:eax <- eax * src src: 64 bits rdx:rax <- rax * src (x64 only)

Unsigned Integer multiplication (mul) mul src8 AH AL = AL * src8 8 bit mul src16 DX AX = AX * src16 16 bit mul src32 EDX EAX = EAX * src32 32 bit mul src64 RDX RAX = RAX * src64 64 bit (x64 only)

Example mul bl mul bx mul ebx mul rbx (x64 only)

Example l1: db 0xFF, 0x1A, 0x11, 0xE2 db 0x2A, 0x82, 0x1F, 0x74 mul [l1]

Example l1: db 0xFF, 0x1A, 0x11, 0xE2 db 0x2A, 0x82, 0x1F, 0x74 mul [l1] mul byte [l1] ; 8 bit AX = AL * [l1] mul word [l1] ; 16 bit DX:AX = AX * [l1] mul dword [l1] ; 32 bit EDX:EAX = EAX * [l1] mul qword [l1] ; 64 bit RDX:RAX = RAX * [l1]

Signed Integer Multiplication (imul) imul src (src: register/memory) src: 8 bits ax <- al * src src: 16 bits dx:ax <- ax * src src: 32 bits edx:eax <- eax * src src: 64 bits rdx:rax <- rax * src (x64 only)

Signed Integer multiplication (mul) imul src8 AH AL = AL * src8 8 bit imul src16 DX AX = AX * src16 16 bit imul src32 EDX EAX = EAX * src32 32 bit imul src64 RDX RAX = RAX * src64 64 bit (x64 only)

Question Why not have add and iadd just like mul and imul?

Other forms of imul imul src imul dest, src dest = dest * src imul dest, src1, src2 dest = src1 * src2

Other forms of imul imul src src: reg/mem imul dest, src dest: reg src: reg/mem/immed imul dest, src1, src2 dest: reg src1: reg/mem src2: immed

Practice: Factorial Write a program reading an integer and printing its factorial assume that the answer fits in 32 bits

Practice: Factorial call read_int mov ecx, eax fact.asm mov eax, 1 loop1: mul ecx loop loop1 call print_int call print_nl

Practice: Factorial Write a program reading an integer and printing its factorial print an error message if the answer is out of range

Practice: Factorial segment.data fact2.asm call print_int call print_nl msg: db "out of range!", 10, 0 jmp endl fact2.asm (cont.) segment.text : call read_int mov ecx, eax mov eax, 1 l1: mul ecx errlbl: mov eax, msg call print_string endl: cmp edx, 0 jne errlbl loop l1

Division div source (source: register/memory) idiv source (source: register/memory)

Integer division (div, idiv) div src8, idiv src8 (src8: 8 bits) al <- ax / src8 (quotient) ah <- ax % src8 (remainder) Example: div bh div byte [l1]

Integer division (div, idiv) div src16, idiv src16 (src16: 16 bits) ax <- dx:ax / src16 (quotient) dx <- dx:ax % src16 (remainder) Example: div cx div word [a]

Integer division (div, idiv) div src32, idiv src32 (src32: 32 bits) eax <- edx:eax / src32 (quotient) edx <- edx:eax % src32 (remainder) Example: div esi div dword [num1]

Integer division (div, idiv) div src64, idiv src64 (src64: 64 bits, x64 only) rax <- rdx:rax / src64 (quotient) rdx <- rdx:rax % src64 (remainder) Example: div rdi div qword [sum]

Integer Division div src8 remainder quotient AX = / AH AL AH AL src8 8 bit div src16 remainder quotient = / DX AX DX AX src16 16 bit div src32 remainder quotient = / EDX EAX EDX EAX src32 32 bit div src64 remainder quotient = / RDX RAX RDX RAX src64 64 bit (x64 only)

Further reading https://www.tutorialspoint.com/assembly_programming/assembly_arithmetic_instructions.htm https://en.wikibooks.org/wiki/x86_assembly/arithmetic https://www.csie.ntu.edu.tw/~acpang/course/asm_2004/slides/chapt_07_partiisolve.pdf

Errors can happen in division

Errors can happen in division

Usually dividend and divisor are of the same size! Unsigned: mov edx, 0 div esi

Usually dividend and divisor are of the same size! Unsigned: mov edx, 0 div esi Signed: CDQ idiv ebx

Remember: Extending bit size - signed AX <- AL CBW (convert Byte to Word) EAX <- AX CWDE (convert Word to double word extended) RAX <- EAX CDQE (convert Double to Quad extended, x64) DX:AX <- AX CWD (convert Word to Double word) EDX:EAX <- EAX CDQ (convert Double word to Quad word) RDX:RAX <- RAX CQO (convert Quad word to Oct Word, x64)

Practice: Prime Numbers Write a program reading an integer and printing if it is prime assume that input is larger than 1

Practice: Prime Numbers segment.data prime_msg: db "Prime!", 10, 0 notprime_msg: db "Not prime!", 10, 0 segment.text : call read_int mov ebx, eax mov ecx, 2 prime.asm startloop: cmp ecx, ebx jge endloop mov eax, ebx mov edx, 0 div ecx cmp edx, 0 je notprime_lbl inc ecx jmp startloop endloop: mov eax, prime_msg call print_string jmp endl notprime_lbl: mov eax, notprime_msg call print_string endl: prime.asm (cont.)

Code on the right also correct? startloop: cmp ecx, ebx jge endloop mov eax, ebx mov edx, 0 div ecx cmp edx, 0 je notprime_lbl inc ecx jmp startloop endloop: mov eax, prime_msg call print_string jmp endl notprime_lbl: mov eax, notprime_msg call print_string endl: startloop: cmp ecx, eax jge endloop mov eax, ebx mov edx, 0 div ecx cmp edx, 0 je notprime_lbl inc ecx jmp startloop endloop: mov eax, prime_msg call print_string jmp endl notprime_lbl: mov eax, notprime_msg call print_string endl: