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

Similar documents
Lecture 5 Program Logic and Control

Selection and Iteration. Chapter 7 S. Dandamudi

LABORATORY WORK NO. 7 FLOW CONTROL INSTRUCTIONS

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

Language of x86 processor family

Lab 6: Conditional Processing

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

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

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.

CMSC 313 Lecture 05 [draft]

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

Conditional Processing

Introduction to 8086 Assembly

Assembly Language Lab # 6

IFE: Course in Low Level Programing. Lecture 6

Lecture (08) x86 programming 7

Intel Instruction Set (gas)

COMPUTER ARCHITECTURE AND ORGANIZATION SEM-4 TH SECTION-B (NOTES)

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

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

8086 INSTRUCTION SET

PRACTICAL WORKBOOK. Department of Computer Engineering University of Lahore. Designed and Compiled by : Engineer Zahid Muneer. Batch.

Week /8086 Microprocessor Programming

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

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

PESIT Bangalore South Campus

Intel 8086: Instruction Set

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

Chapter 12. Selected Pentium Instructions

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

ASSEMBLY LANGUAGE PROGRAMMING OF THE MICROCOMPUTER

Basic Assembly Instructions

The x86 Architecture

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

SOEN228, Winter Revision 1.2 Date: October 25,

Introduction to IA-32. Jo, Heeseung

INTRODUCTION TO IA-32. Jo, Heeseung

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

Chapter Four Instructions Set

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

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

APPENDIX C INSTRUCTION SET DESCRIPTIONS

Branching and Looping

Logical and bit operations

EC 333 Microprocessor and Interfacing Techniques (3+1)

Code segment Stack segment

Complex Instruction Set Computer (CISC)

3.1 DATA MOVEMENT INSTRUCTIONS 45

Lab 3. The Art of Assembly Language (II)

Summer 2003 Lecture 4 06/14/03

Program Control Instructions

Assembly Language LAB

Module 3 Instruction Set Architecture (ISA)

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

9/25/ Software & Hardware Architecture

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

Practical Malware Analysis

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

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

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

An Introduction to x86 ASM

Basic Execution Environment

CS-202 Microprocessor and Assembly Language

CS61 Section Solutions 3

Objectives. ICT106 Fundamentals of Computer Systems Topic 8. Procedures, Calling and Exit conventions, Run-time Stack Ref: Irvine, Ch 5 & 8

Branching and Looping

CS401 Assembly Language Solved MCQS From Midterm Papers

Computer Systems C S Cynthia Lee

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

PHY4635/5635 Spring Lecture 8: Program Control Instructions

Selected Pentium Instructions. Chapter 12 S. Dandamudi

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

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

SRI VENKATESWARA COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF ECE EC6504 MICROPROCESSOR AND MICROCONTROLLER (REGULATION 2013)

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

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

Machine Programming 2: Control flow

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

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

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

Computer Organization & Assembly Language Programming. CSE 2312 Lecture 15 Addressing and Subroutine

Assembler Programming. Lecture 8

if 2 16bit operands multiplied the result will be

INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI

Assembly basics CS 2XA3. Term I, 2017/18

x86 architecture et similia

Section 001. Read this before starting!

EEM336 Microprocessors I. Data Movement Instructions

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.

ORG ; TWO. Assembly Language Programming

Assembly Language: IA-32 Instructions

Assembly Language Programming

1. Required Knowledge to Write in Assembly. 4. Assembler used (gas/masm): (a) Assembler directives (prefixed

The Instruction Set. Chapter 5

The Hardware/Software Interface CSE351 Spring 2013

Microprocessor. By Mrs. R.P.Chaudhari Mrs.P.S.Patil

Process Layout and Function Calls

Overview of Assembly Language

Experiment 3 3 Basic Input Output

Transcription:

Assembler lecture 5 S.Šimoňák, DCI FEEI TU of Košice Jumps and iterations conditional and unconditional jumps iterations (loop) implementation of HLL control structures Unconditional jump unconditional transfer of control to the destination specified, syntax: jmp dest Destination specification destination address specified directly (part of instruction, forward/backward) indirectly (register/memory contains the address) direct jumps address specified within instruction relative offset between the destination and instruction following the jmp (!) after the jmp is fetched, EIP updated automatically offset signed number (positive forward jump) relative addresses suitable for dynamically relocatable code (position-independent code) jump destination within a segment destination in the same segment like the jmp instruction (intrasegment jump) till now we considered this type of jump EIP EIP + rel. offset

in different segment (intersegment jump, far jump) CS dest. segment EIP dest. offset segment and offset specified within an instruction (for 32-bit. segment instruction size 7B) most of jumps intrasegment, 2 ways of specifying the destination according to the size of rel. offset short jump (2B, 1B op. code + 1B rel. offset, signed number from range -128/+127) near jump (3/5B, 1B op. code + 2/4B rel. offset) 2B rel. offset for 16-bit. segments, 4B for 32-bit segments specification of short jumps (SHORT) we want to use a short jump information for compiler: jmp SHORT ECX_init_done if the destination is farther error message assembler will automatically supply SHORT for backward jumps (if the destination is in valid range) forward jumps assembler doesn't know the destination distance, help of programmer welcome

Example: short/near jumps encoding [1] r.167 (2B) specified like a SHORT, op. code EBH, offset 14H r.169 (5B) not specified like a SHORT, assembler assumes NEAR version (op. code E9H, offset 0000000AH) r.177 (2B) backward jump, assembler can decide, that NEAR is enough (FDH = -3) r.172 (5B) little endian, offset 00000652H

Comparison instruction (cmp) setting flags, next conditional jump instruction tests them implementation of HLL construction IF-THEN-ELSE in assembly in two steps arithmetic/compare instruction conditional jump Conditional jumps can be subdivided into three groups according to the value of a single flag according to results of unsigned comparisons according to results of signed comparisons Jumps with single flag test two instructions (0/1) for each status flag except the AF two names available (alias) for ZF, PF zero flag (ZF) jz, je (ZF = 1) jnz, jne (ZF = 0) jecxz (jump if ECX = 0, without testing the flags), jcxz (if CX = 0) carry (CF) jc (CF = 1), jnc (CF = 0) overflow (OF) jo (OF = 1), jno (OF = 0) sign (SF) js (SF = 1), jns (SF = 0) parity (PF) jp, jpe (PF = 1) jnp, jpo (PF = 0)

Jumps according to results of unsigned comparisons when comparing two numbers (cmp num1, num2) signed or unsigned numbers? Example: AL = 10110111 (183/-73) 10, DL = 01101110 (110) 10 cmp AL, DL AL > DL (unsigned interpretation) AL < DL (signed interpretation) comparison order (cmp num1, num2) always the relation num1 to num2, possible relations (6): num1 = num2, num1 num2 num1 > num2, num1 num2 num1 < num2, num1 num2 for unsigned numbers CF and ZF relevant, aliases available mnemonics je/jz jne/jnz ja/jnbe jae/jnb jb/jnae jbe/jna meaning equal/ zero not equal/ not zero above/ not below or equal above or equal/ not below below/ not above or equal below or equal/ not above condition ZF = 1 ZF = 0 CF = 0 AND ZF = 0 CF = 0 CF = 1 CF = 1 OR ZF = 1

Jumps according to results of signed comparisons comparisons =, work in the same way on signed/unsigned numbers for signed numbers relevant flags: SF, OF, ZF mnemonics je/jz jne/jnz jg/jnle jge/jnl jl/jnge jle/jng meaning equal/ zero not equal/ not zero greater/ not less or equal greater or equal/ not less less/ not greater or equal less or equal/ not greater condition ZF = 1 ZF = 0 ZF = 0 AND SF = OF SF = OF SF OF ZF = 1 OR SF OF suppose instruction cmp snum1, snum2: conditions for snum1 > snum2 (jg) conditions for snum1 < snum2 (jl, ZF redundant, ZF=1 SF = OF = 0)

Destination distance and conditional jumps conditional jumps SHORT/NEAR (most efficient if encoded like 2B instructions, SHORT) range -128/127 B (SHORT) when this range is not sufficient (condition negation + unconditional jump) Example: LHS code replaced by RHS one [1] Iterations iteration instructions use CX/ECX register (repeat count) according to the operand size (we suppose 32-bit in most cases) decrements the register before the test for zero (without affecting flags) destination range -128/127 B (1B offset) Instructions loop, loope/loopz, loopne/loopnz synonyms (aliases), syntax loop loope loopne dest dest dest instructions loope/loopz, loopne/loopnz support of loops with two termination conditions

mnemonics loop loope/loopz loopne/loopnz meaning loop loop while equal/ loop while zero condition ECX = ECX 1 IF ECX 0 jump to dest ECX = ECX 1 IF (ECX 0 AND ZF = 1) jump to dest loop while not equal/ loop while not zero ECX = ECX 1 IF (ECX 0 AND ZF = 0) jump to dest Example: program reads integers from the keyboard, terminates after specified number of integers (SIZE), or zero read %include "asm_io.inc" SIZE EQU 10 segment.bss buffer resd SIZE segment.text global _asm_main _asm_main: enter 0,0 pusha mov EBX,buffer mov ECX,SIZE read_more: call mov add cmp loopne popa mov leave ret read_int [EBX],EAX EBX,4 EAX,0 read_more EAX,0 problem: if at the beginning ECX = 0 (FFFFFFFFH repeats or zero entered) solution: instruction jecxz before entering the loop execution speed of instructions loop and jcxz (optimization purposes) [3] two instructions [2 clock ticks] executed faster than corresponding (loop dest) (5/6 clocks) dec ECX jnz dest two instructions [2 clock ticks] executed faster than corresponding (jecxz dest) (5/6 clocks) cmp ECX,0 jz dest

Implementation of HLL control structures using jumping and iteration instructions Construction if-then-else Example: construction if and relational operator (C code assigns bigger of two values (int) to variable bigger) [1] a) C code b) after the translation (Turbo C) condition tested by cmp/jle redundant code generated

Example: construction if and logical operator and (test for lower case letter and eventual translation to capital) [1] a) C code b) after the translation (Turbo C, variable ch in DL) combined condition two pairs of cmp/jx instructions redundant code generated (sub DL,32) Iterative constructions constructions like while, repeat-until, for Loop while test of condition before executing the loop (entry-test loop) loop body executed repeatedly, until the condition holds

a) C code b) after the translation (Turbo C, variable total in BX) [1] unconditional control transfer at the beginning (condition test) Loop repeat-until condition tested after the loop body execution (exit-test loop) commands in the body executed once at least a) C code b) after the translation (Turbo C, variable number in DI) [1] test realized using or (instead of cmp instruction)

Loop for number of iterations fixed (counting loop) a) C code b) after the translation (Turbo C, variable i in SI) [1] unconditional jump at the beginning (condition test) incrementing i (decrementing similarly) Indirect jumps till now instructions of direct jumps (destination address) encoded within the instruction itself we suppose intersegment jumps address of destination specified in R/M specified absolute offset size (offset in direct jumps relative) usage jmp [ECX]

Multiway conditional execution for greater number of branches, using if construction often not effective, error prone [1] Construction switch a) C code b) after the translation (Turbo C) jump table in code segment (jump_table) segment prefix CS: (line 11) BX index into table, table items 2B (shl (line 10)) BP counters, local variables

Study literature: [1] Dandamudi,S.,P.: Introduction to Assembly Language Programming, Springer Science+Business Media, Inc., 2005. [2] Carter, A., P.: PC Assembly Language, 2006, http://www.drpaulcarter.com/pcasm/ [3] Rafiquzzaman, M.: Microprocessor Theory and Applications with 68000/68020 and Pentium, John Wiley & Sons, Inc., 2008, http://onlinelibrary.wiley.com/doi/10.1002/9780470391396.app6/pdf