Selection and Iteration. Chapter 7 S. Dandamudi

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

LABORATORY WORK NO. 7 FLOW CONTROL INSTRUCTIONS

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

Lab 6: Conditional Processing

Introduction to 8086 Assembly

IFE: Course in Low Level Programing. Lecture 6

Conditional Processing

Assembler lecture 5 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.

8086 INSTRUCTION SET

Lecture (08) x86 programming 7

Week /8086 Microprocessor Programming

Branching and Looping

Intel Instruction Set (gas)

Basic Assembly Instructions

Selected Pentium Instructions. Chapter 12 S. Dandamudi

CMSC 313 Lecture 05 [draft]

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

Lecture 5 Program Logic and Control

Branching and Looping

Assembly Language Lab # 6

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

EC 333 Microprocessor and Interfacing Techniques (3+1)

CHAPTER SEVENTEEN Assemblers versus Compilers. Intel 80x86 Assembly Language

APPENDIX C INSTRUCTION SET DESCRIPTIONS

PESIT Bangalore South Campus

Chapter 6 (Part a) Conditional Processing

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

Chapter 12. Selected Pentium Instructions

TUTORIAL. Emulador Emu8086 do. Microprocessador 8086

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

Computer Systems C S Cynthia Lee

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

Overview of Assembly Language

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

CS-202 Microprocessor and Assembly Language

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

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

7 LOW-LEVEL CONTROL STRUCTURES

Summer 2003 Lecture 4 06/14/03

Intel 8086: Instruction Set

Section 002. Read this before starting!

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

x64 Cheat Sheet Fall 2014

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

Section 001. Read this before starting!

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

9/25/ Software & Hardware Architecture

Program Control Instructions

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

Am186 and Am188 Family Instruction Set Manual. February, 1997

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

Program controlled semiconductor device (IC) which fetches (from memory), decodes and executes instructions.

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

Branching and Looping

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

Read this before starting!

Assembly Language Tutorial

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

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

Assembly Language II: Addressing Modes & Control Flow

An Introduction to x86 ASM

Chapter Four Instructions Set

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

Computer Architecture and Assembly Language Programming CS401 Lecture No: 1 Address, Data, and Control Buses A computer system comprises of a

LAB 3: Programming in Assembly Language

Assembly Language LAB

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

Arithmetic and Logic Instructions And Programs

ECE 498 Linux Assembly Language Lecture 3

Machine Programming 2: Control flow

ORG ; TWO. Assembly Language Programming

Lab 3. The Art of Assembly Language (II)

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.

Arithmetic Instructions

Basic Assembly SYSC-3006

UNIT III 8086 Microprocessor. Lecture 1

Section 001. Read this before starting!

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

Code segment Stack segment

CS61 Section Solutions 3

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

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

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

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

Assembly Language: IA-32 Instructions

COMP211 ASSEMBLY PROGRAMMING

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

ADVANCE MICROPROCESSOR & INTERFACING

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 5

SOEN228, Winter Revision 1.2 Date: October 25,

M80C286 HIGH PERFORMANCE CHMOS MICROPROCESSOR WITH MEMORY MANAGEMENT AND PROTECTION

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

Sungkyunkwan University

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

Section 001 & 002. Read this before starting!

Module 3 Instruction Set Architecture (ISA)

Transcription:

Selection and Iteration Chapter 7 S. Dandamudi

Outline Unconditional jump Compare instruction Conditional jumps Single flags Unsigned comparisons Signed comparisons Loop instructions Implementing high-level language decision structures Selection structures Iteration structures Illustrative examples Indirect jumps Multiway conditional statements Logical expression evaluation Full evaluation Partial evaluation Performance: Logical expression evaluation Partial vs. full evaluation S. Dandamudi Selection: Page 2

Unconditional Jump Unconditional jump transfers control to the instruction at the target address Format jmp target Specification of target Direct» Target address is specified as a part of the instruction Indirect» Target address is specified indirectly either through memory or a general-purpose register S. Dandamudi Selection: Page 3

Unconditional Jump (cont d) Example Two jump instructions Forward jump jmp CX_init_done Backward jump jmp repeat1 Programmer specifies target by a label Assembler computes the offset using the symbol table... mov CX,10 jmp CX_init_done init_cx_20: mov CX,20 CX_init_done: mov AX,CX repeat1: dec CX... jmp repeat1... S. Dandamudi Selection: Page 4

Unconditional Jump (cont d) Address specified in the jump instruction is not the absolute address Uses relative address» Specifies relative byte displacement between the target instruction and the instruction following the jump instruction» Displacement is w.r.t the instruction following jmp Reason: IP is already pointing to this instruction Execution of jmp involves adding the displacement value to current IP Displacement is a signed 16-bit number» Negative value for backward jumps» Positive value for forward jumps S. Dandamudi Selection: Page 5

Target Location Inter-segment jump Target is in another segment CS := target-segment (2 bytes) IP := target-offset (2 bytes)» Called far jumps (needs five bytes to encode jmp) Intra-segment jumps Target is in the same segment IP := IP + relative-displacement (1 or 2 bytes) Uses 1-byte displacement if target is within 128 to +127» Called short jumps (needs two bytes to encode jmp) If target is outside this range, uses 2-byte displacement» Called near jumps (needs three bytes to encode jmp) S. Dandamudi Selection: Page 6

Target Location (cont d) In most cases, the assembler can figure out the type of jump For backward jumps, assembler can decide whether to use the short jump form or not For forward jumps, it needs a hint from the programmer Use SHORT prefix to the target label If such a hint is not given» Assembler reserves three bytes for jmp instruction» If short jump can be used, leaves one byte of rogue data See the next example for details S. Dandamudi Selection: Page 7

Example... 8 0005 EB 0C jmp SHORT CX_init_done 0013-0007 = 0C 9 0007 B9 000A mov CX,10 10 000A EB 07 90 jmp CX_init_done rogue byte 0013-000D = 07 11 init_cx_20: 12 000D B9 0014 mov CX,20 13 0010 E9 00D0 jmp near_jump 00E3-0013 = D0 14 CX_init_done: 15 0013 8B C1 mov AX,CX S. Dandamudi Selection: Page 8

Example (cont d) 16 repeat1: 17 0015 49 dec CX 18 0016 EB FD jmp repeat1 0015-0018 = -3 = FDH... 84 00DB EB 03 jmp SHORT short_jump 00E0-00DD = 3 85 00DD B9 FF00 mov CX, 0FF00H 86 short_jump: 87 00E0 BA 0020 mov DX, 20H 88 near_jump: 89 00E3 E9 FF27 jmp init_cx_20 000D - 00E6 = -217 = FF27H S. Dandamudi Selection: Page 9

Compare Instruction Compare instruction can be used to test the conditions Format cmp destination, source Updates the arithmetic flags by performing destination - source The flags can be tested by a subsequent conditional jump instruction S. Dandamudi Selection: Page 10

Conditional Jumps Three types of conditional jumps Jumps based on the value of a single flag» Arithmetic flags such as zero, carry can be tested using these instructions Jumps based on unsigned comparisons» The operands of cmp instruction are treated as unsigned numbers Jumps based on signed comparisons» The operands of cmp instruction are treated as signed numbers S. Dandamudi Selection: Page 11

Jumps Based on Single Flags Testing for zero jz jump if zero jumps if ZF = 1 je jump if equal jumps if ZF = 1 jnz jump if not zero jumps if ZF = 0 jne jump if not equal jumps if ZF = 0 jcxz jump if CX = 0 jumps if CX = 0 (Flags are not tested) S. Dandamudi Selection: Page 12

Jumps Based on Single Flags (cont d) Testing for carry jc jump if carry jumps if CF = 1 jnc jump if no carry jumps if CF = 0 Testing for overflow jo jump if overflow jumps if OF = 1 jno jump if no overflow jumps if OF = 0 Testing for sign js jump if negative jumps if SF = 1 jns jump if not negative jumps if SF = 0 S. Dandamudi Selection: Page 13

Jumps Based on Single Flags (cont d) Testing for parity jp jump if parity jumps if PF = 1 jpe jump if parity jumps if PF = 1 is even jnp jump if not parity jumps if PF = 0 jpo jump if parity jumps if PF = 0 is odd S. Dandamudi Selection: Page 14

Jumps Based on Unsigned Comparisons Mnemonic Meaning Condition je jump if equal ZF = 1 jz jump if zero ZF = 1 jne jump if not equal ZF = 0 jnz jump if not zero ZF = 0 ja jump if above CF = ZF = 0 jnbe jump if not below CF = ZF = 0 or equal S. Dandamudi Selection: Page 15

Jumps Based on Unsigned Comparisons Mnemonic Meaning Condition jae jump if above CF = 0 or equal jnb jump if not below CF = 0 jb jump if below CF = 1 jnae jump if not above CF = 1 or equal jbe jump if below CF=1 or ZF=1 or equal jna jump if not above CF=1 or ZF=1 S. Dandamudi Selection: Page 16

Jumps Based on Signed Comparisons Mnemonic Meaning Condition je jump if equal ZF = 1 jz jump if zero ZF = 1 jne jump if not equal ZF = 0 jnz jump if not zero ZF = 0 jg jump if greater ZF=0 & SF=OF jnle jump if not less ZF=0 & SF=OF or equal S. Dandamudi Selection: Page 17

Jumps Based on Signed Comparisons (cont d) Mnemonic Meaning Condition jge jump if greater SF = OF or equal jnl jump if not less SF = OF jl jump if less SF OF jnge jump if not greater SF OF or equal jle jump if less ZF=1 or SF OF or equal jng jump if not greater ZF=1 or SF OF S. Dandamudi Selection: Page 18

A Note on Conditional Jumps All conditional jumps are encoded using 2 bytes Treated as short jumps What if the target is outside this range? target:... cmp AX,BX je target mov CX,10... traget is out of range for a short jump Use this code to get around target:... cmp AX,BX jne skip1 jmp target skip1: mov CX,10... S. Dandamudi Selection: Page 19

Loop Instructions Loop instructions use CX/ECX to maintain the count value target should be within the range of a short jump as in conditional jump instructions Three loop instructions loop target Action: CX := CX-1 Jump to target if CX 0 S. Dandamudi Selection: Page 20

Loop Instructions (cont d) The following two loop instructions also test the zero flag status loope/loopz target Action: CX := CX-1 Jump to target if (CX 0 and ZF = 1) loopne/loopnz target Action: CX := CX-1 Jump to target if (CX 0 and ZF = 0) S. Dandamudi Selection: Page 21

Instruction Execution Times Functionally, loop instruction can be replaced by dec CX jnz target loop instruction is slower than dec/jnz version loop requires 5/6 clocks whereas dec/jnz takes only 2 clocks jcxz also takes 5/6 clocks Equivalent code (shown below) takes only 2 clocks cmp CX,0 jz target S. Dandamudi Selection: Page 22

Implementing HLL Decision Structures High-level language decision structures can be implemented in a straightforward way See Section 7.5 (page 272) for examples that implement if-then-else if-then-else with a relational operator if-then-else with logical operators AND and OR while loop repeat-until loop for loop S. Dandamudi Selection: Page 23

Two example programs Linear search Illustrative Examples» LIN_SRCH.ASM» Searches an array of non-negative numbers for a given input number Selection sort» SEL_SORT.ASM» Uses selection sort algorithm to sort an integer array in ascending order S. Dandamudi Selection: Page 24

Indirect Jumps Jump target address is not specified directly as a part of the jump instruction With indirect jump, we can specify target via a general-purpose register or memory Example: Assuming CX has the offset value jmp CX Note: The offset value in indirect jump is the absolute value (not relative value as in direct jumps) Program example IJUMP.ASM» Uses a jump table to direct the jump S. Dandamudi Selection: Page 25

Indirect Jumps (cont d) Another example Implementing multiway jumps»we use switch statement of C We can use a table with appropriate target pointers for the indirect jump Segment override is needed» jump_table is in the code segment (not in the data segment) switch (ch) { case '0': count[0]++; break; case '1': count[1]++; break; case '2': count[2]++; break; case '3': count[3]++; break; default: count[4]++; } S. Dandamudi Selection: Page 26

Indirect Jumps (cont d) _main PROC NEAR... mov AL,ch cbw sub AX,48 ;48 = 0 mov BX,AX cmp BX,3 ja default shl BX,1 ;BX:= BX*2 jmp WORD PTR CS:jump_table[BX] case_0: inc WORD PTR [BP-10] jmp SHORT end_switch case_1: inc WORD PTR [BP-8] jmp SHORT end_switch case_2: inc WORD PTR [BP-6] jmp SHORT end_switch case_3: inc WORD PTR [BP-4] jmp SHORT end_switch default: inc WORD PTR [BP-2] end_switch:... _main ENDP jump_table LABEL WORD DW case_0 DW case_1 DW case_2 DW case_3... S. Dandamudi Selection: Page 27

Evaluation of Logical Expressions Two basic ways Full evaluation» Entire expression is evaluated before assigning a value» PASCAL uses full evaluation Partial evaluation» Assigns as soon as the final outcome is known without blindly evaluating the entire logical expression» Two rules help: cond1 AND cond2 If cond1 is false, no need to evaluate cond2 cond1 OR cond2 If cond1 is true, no need to evaluate cond2 S. Dandamudi Selection: Page 28

Evaluation of Logical Expressions (cont d) Partial evaluation Used by C Useful in certain cases to avoid run-time errors Example if ((X > 0) AND (Y/X > 100)) If x is 0, full evaluation results in divide error Partial evaluation will not evaluate (Y/X > 100) if X = 0 Partial evaluation is used to test if a pointer value is NULL before accessing the data it points to S. Dandamudi Selection: Page 29

Performance: Full vs. Partial Evaluation 8 Execution time (seconds) 6 4 2 full evaluation partial evaluation AL version 0 10 20 30 40 50 60 70 80 90 100 Number of calls (in thousands) S. Dandamudi Selection: Page 30