Introduction to 8086 Assembly

Similar documents
BAHAR DÖNEMİ MİKROİŞLEMCİLER LAB4 FÖYÜ

Computer Architecture..Second Year (Sem.2).Lecture(4) مدرس المادة : م. سندس العزاوي... قسم / الحاسبات

Jump instructions. Unconditional jumps Direct jump. do not change flags. jmp label

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

IFE: Course in Low Level Programing. Lecture 6

mith College Computer Science CSC231 Assembly Week #9 Spring 2017 Dominique Thiébaut

LABORATORY WORK NO. 7 FLOW CONTROL INSTRUCTIONS

Lab 6: Conditional Processing

Basic Assembly Instructions

Conditional Processing

Selection and Iteration. Chapter 7 S. Dandamudi

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

Branching and Looping

Branching and Looping

Intel Instruction Set (gas)

Chapter 6 (Part a) Conditional Processing

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.

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

Assembly Language Lab # 6

8086 INSTRUCTION SET

Control. Young W. Lim Mon. Young W. Lim Control Mon 1 / 16

CS61 Section Solutions 3

Computer Systems C S Cynthia Lee

CMSC 313 Lecture 05 [draft]

An Introduction to x86 ASM

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

Week /8086 Microprocessor Programming

ECE 498 Linux Assembly Language Lecture 3

Branching and Looping

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

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

APPENDIX C INSTRUCTION SET DESCRIPTIONS

Lecture (08) x86 programming 7

x64 Cheat Sheet Fall 2014

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

Lecture 5 Program Logic and Control

7 LOW-LEVEL CONTROL STRUCTURES

It is possible to define a number using a character or multiple numbers (see instruction DB) by using a string.

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

2.1 Chapter Overview. 2.2 Low Level Control Structures. 2.3 Statement Labels. Low-Level Control Structures

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

Machine Programming 2: Control flow

TUTORIAL. Emulador Emu8086 do. Microprocessador 8086

Intel x86 Jump Instructions. Part 5. JMP address. Operations: Program Flow Control. Operations: Program Flow Control.

Assembly Language for Intel-Based Computers, 5 th Edition. Kip R. Irvine. Chapter 6: Conditional Processing

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

Assembly Language LAB

Intel x86 Jump Instructions. Part 5. JMP address. Operations: Program Flow Control. Operations: Program Flow Control.

1 if (a == b) 2 { 3 /* true stuff here */ 4 } 5 else 6 { 7 /* false stuff here */ 8 } 10 /* done either way (i.e., whether true.

COMP211 ASSEMBLY PROGRAMMING

Assembly Language II: Addressing Modes & Control Flow

SOEN228, Winter Revision 1.2 Date: October 25,

Assembly II: Control Flow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Credits and Disclaimers

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

Assembly II: Control Flow

CHAPTER SEVENTEEN Assemblers versus Compilers. Intel 80x86 Assembly Language

Assembly Language Tutorial

CS356 Unit 5. Translation to Assembly. Translating HLL to Assembly ASSEMBLY TRANSLATION EXAMPLE. x86 Control Flow

Assembly Language: IA-32 Instructions

Second Part of the Course

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

Lab 3. The Art of Assembly Language (II)

CS 290 Host-based Security and Malware. Christopher Kruegel

Introduction to 8086 Assembly

5.1. CS356 Unit 5. x86 Control Flow

Process Layout and Function Calls

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College February 9, 2016

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College September 25, 2018

Assembly Language: Part 2

Selected Pentium Instructions. Chapter 12 S. Dandamudi

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 6: Conditional Processing. Chapter Overview. Boolean and Comparison Instructions

Am186 and Am188 Family Instruction Set Manual. February, 1997

CS-202 Microprocessor and Assembly Language

EC 333 Microprocessor and Interfacing Techniques (3+1)

CSCI 2021: x86-64 Control Flow

Program Exploitation Intro

US06CCSC04: Introduction to Microprocessors and Assembly Language UNIT 3: Assembly Language Instructions II

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 8. Conditional Processing

CSC 405 Introduction to Computer Security. Reverse Engineering

ASSEMBLY II: CONTROL FLOW. Jo, Heeseung

Credits and Disclaimers

administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions?

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

CPS104 Recitation: Assembly Programming

Towards the Hardware"

MILITARY Intel386 TM HIGH PERFORMANCE 32-BIT MICROPROCESSOR WITH INTEGRATED MEMORY MANAGEMENT

The Hardware/Software Interface CSE351 Spring 2013

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

Assembly II: Control Flow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS 33: Week 3 Discussion. x86 Assembly (v1.0) Section 1G

AIMS Embedded Systems Programming MT 2017 Micro Architectures

Basic Intel x86 Assembly Language Programming

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

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

PESIT Bangalore South Campus

Credits and Disclaimers

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

Sungkyunkwan University

Transcription:

Introduction to 8086 Assembly Lecture 5 Jump, Conditional Jump, Looping, Compare instructions

Labels and jumping (the jmp instruction) mov eax, 1 add eax, eax jmp label1 xor eax, eax label1: sub eax, 303

Labels and jumping (the jmp instruction) mov eax, 1 add eax, eax jmp label1 xor eax, eax label1: sub eax, 303 address of sub eax, 303

Infinite loop mov eax, 0 loop1: call print_int call print_nl inc eax jmp loop1 infinite_loop.asm

Remember: the FLAGS Register 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 Overflow Flag (OF) Direction Flag (DF) Interrupt Flag (IF) Trap Flag (TF) Sign Flag (SF) Zero Flag (ZF) Auxiliary Carry Flag (AF) Parity Flag (PF) Carry Flag (CF) CF: carry flag OF: overflow flag SF: sign flag ZF: zero flag PF: parity flag DF: direction flag IF: interrupt flag

Conditional loops JZ JNZ JO JNO JS JNS JC JNC JP JNP Jump if ZF=1 Jump if ZF=0 Jump if OF=1 Jump if OF=0 Jump if SF=1 Jump if SF=0 Jump if CF=1 Jump if CF=0 Jump if PF=1 Jump if PF=0

Example unsigned integer: if (eax == ebx) esi = 0 JZ JNZ JO JNO JS JNS JC JNC JP JNP Jump if ZF=1 Jump if ZF=0 Jump if OF=1 Jump if OF=0 Jump if SF=1 Jump if SF=0 Jump if CF=1 Jump if CF=0 Jump if PF=1 Jump if PF=0

Example unsigned integer: if (eax == ebx) esi = 0 sub eax, ebx jnz next mov esi, 0 next: JZ JNZ JO JNO JS JNS JC JNC JP JNP Jump if ZF=1 Jump if ZF=0 Jump if OF=1 Jump if OF=0 Jump if SF=1 Jump if SF=0 Jump if CF=1 Jump if CF=0 Jump if PF=1 Jump if PF=0

Example signed integer: if (eax == ebx) esi = 0 sub eax, ebx jnz next mov esi, 0 next: JZ JNZ JO JNO JS JNS JC JNC JP JNP Jump if ZF=1 Jump if ZF=0 Jump if OF=1 Jump if OF=0 Jump if SF=1 Jump if SF=0 Jump if CF=1 Jump if CF=0 Jump if PF=1 Jump if PF=0

Example signed integer: if (eax == - ebx) edi = 4 JZ JNZ JO JNO JS JNS JC JNC JP JNP Jump if ZF=1 Jump if ZF=0 Jump if OF=1 Jump if OF=0 Jump if SF=1 Jump if SF=0 Jump if CF=1 Jump if CF=0 Jump if PF=1 Jump if PF=0

Example signed integer: if (eax == - ebx) edi = 4 add eax, ebx jnz next mov edi, 4 next: JZ JNZ JO JNO JS JNS JC JNC JP JNP Jump if ZF=1 Jump if ZF=0 Jump if OF=1 Jump if OF=0 Jump if SF=1 Jump if SF=0 Jump if CF=1 Jump if CF=0 Jump if PF=1 Jump if PF=0

Example unsigned integer: if (eax >= ebx) esp -= 4 JZ JNZ JO JNO JS JNS JC JNC JP JNP Jump if ZF=1 Jump if ZF=0 Jump if OF=1 Jump if OF=0 Jump if SF=1 Jump if SF=0 Jump if CF=1 Jump if CF=0 Jump if PF=1 Jump if PF=0

Example unsigned integer: if (eax >= ebx) esp -= 4 sub eax, ebx jc next sub esp, 4 next: JZ JNZ JO JNO JS JNS JC JNC JP JNP Jump if ZF=1 Jump if ZF=0 Jump if OF=1 Jump if OF=0 Jump if SF=1 Jump if SF=0 Jump if CF=1 Jump if CF=0 Jump if PF=1 Jump if PF=0

Example signed integer: if (eax < ebx) ebp += 8 x - y x < y => SF = 1 x >= y => SF = 0

Example signed integer: x - y if (eax < ebx) ebp += 8 OF=0 x < y => SF = 1 x >= y => SF = 0

Example signed integer: x - y if (eax < ebx) ebp += 8 OF=0 x < y => SF = 1 x >= y => SF = 0 OF=1 x < 0 < y => SF = 0 x > 0 > y => SF = 1

Example signed integer: if (eax < ebx) ebp += 8 sub eax, ebx jo ovflow jns endl if_cond: add ebp, 8 jmp endl ovflow: jns if_cond endl: OF=0 OF=1 x - y x < y => SF = 1 x >= y => SF = 0 x < 0 < y => SF = 0 x > 0 > y => SF = 1

Example signed integer: if (eax < ebx) ebp += 8 else ebp -= 8 OF=0 OF=1 x - y x < y => SF = 1 x >= y => SF = 0 x < 0 < y => SF = 0 x > 0 > y => SF = 1

Other conditional jump commands sub x, y unsigned signed JE label jump if x == y JE label jump if x == y JNE label jump if x!= y JNE label jump if x!= y JA label JNBE label jump if x > y JG label JNLE label jump if x > y JB label JNAE label jump if x < y JL label JNGE label jump if x < y JAE JNB label label jump if x >= y JGE JNL label label jump if x >= y JBE JNA label label jump if x <= y JLE JNG label label jump if x <= y

Example:

Example: rem.asm rem2.asm

Example: Practice: Also print quotient rem.asm

Example: rem.asm div.asm

Example:

Example:

The loop commands loop lbl ecx--; if (ecx!=0) goto lbl loopz lbl ecx--; if (ecx!=0 && ZF=1) goto lbl loopnz lbl ecx--; if (ecx!=0 && ZF=0) goto lbl

Example: Count up to N

Example: Count up to N

Example: Count up to N

using sub before jump; what's wrong?

the cmp instruction

The cmp instruction sub eax, ebx cmp eax, ebx cmp x, y subtracts y from x (like sub x,y) does not store the result (x is not changed) flags are set (as though a subtraction has taken place)

The cmp instruction cmp x, y unsigned signed JE label jump if x == y JE label jump if x == y JNE label jump if x!= y JNE label jump if x!= y JA label JNBE label jump if x > y JG label JNLE label jump if x > y JB label JNAE label jump if x < y JL label JNGE label jump if x < y JAE JNB label label jump if x >= y JGE JNL label label jump if x >= y JBE JNA label label jump if x <= y JLE JNG label label jump if x <= y

Practice if (eax > ebx) {edi=1} else {edi=2} (signed)

Practice if (eax > ebx) {edi=1} else {edi=2} (signed) cmp eax, ebx jle else_lbl mov edi, 1 jmp endif else_lbl: mov edi, 2 endif: