Selection and Iteration Chapter 8 S. Dandamudi. 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,

Slides:



Advertisements
Similar presentations
Jump Condition.
Advertisements

Flow of Control Instruction/Control structure Looping structure Looping structure Branching structure Branching structure For assembly language program.
Departemen Ilmu Komputer FMIPA IPB Conditional Jump Instruction A conditional jump instruction transfer control to a destination address when a.
Micro-Computer Applications: Jumping and Loop Dr. Eng. Amr T. Abdel-Hamid ELECT 707 Fall 2011.
ACOE2511 Assembly Language Arithmetic and Logic Instructions.
Assembly Language :CSC 225 (Lec#4: Flag Register and Conditional Statements) By Dr. Syed Noman.
Gursharan Singh Tatla 21-Nov-20101www.eazynotes.com.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for Intel-Based Computers
CS2422 Assembly Language & System Programming October 17, 2006.
CS2422 Assembly Language and System Programming Conditional Processing Department of Computer Science National Tsing Hua University.
Flow Control Instructions
9-1 ECE 424 Design of Microprocessor-Based Systems Haibo Wang ECE Department Southern Illinois University Carbondale, IL x86 Instructions Part.
Addressing Modes Chapter 11 S. Dandamudi To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer,  S.
Khaled A. Al-Utaibi  Introduction  Arithmetic Instructions  Basic Logical Instructions  Shift Instructions  Rotate Instructions.
Overview of Assembly Language Chapter 9 S. Dandamudi.
Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures.
Chapter 4 - Implementing Standard Program Structures in 8086 Assembly Language from Microprocessors and Interfacing by Douglas Hall.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#5) By Dr. Syed Noman.
Dr. José M. Reyes Álamo 1.  Review: ◦ of Comparisons ◦ of Set on Condition  Statement Labels  Unconditional Jumps  Conditional Jumps.
Selected Pentium Instructions Chapter 12 S. Dandamudi.
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
Arithmetic Flags and Instructions
(Flow Control Instructions)
Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice.
Decision Structures – Code Generation Lecture 24 Mon, Apr 18, 2005.
Addressing Modes Chapter 6 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 5 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Assembly Language. Symbol Table Variables.DATA var DW 0 sum DD 0 array TIMES 10 DW 0 message DB ’ Welcome ’,0 char1 DB ? Symbol Table Name Offset var.
Chapter 5 Branching and Looping.
1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 06: Control Structures Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,
EEL 3801 Part V Conditional Processing. This section explains how to implement conditional processing in Assembly Language for the 8086/8088 processors.
LEA instruction The LEA instruction can be used to get the offset address of a variable Example ORG 100h MOV AL, VAR1 ; check value of VAR1 by moving it.
Conditional Loop Instructions, Conditional Structures
3.4 Addressing modes Specify the operand to be used. To generate an address, a segment register is used also. Immediate addressing: the operand is a number.
Microprocessor MA Rahim Khan Computer Engineering and Networks Department.
Arithmetic Flags and Instructions Chapter 7 S. Dandamudi.
COMP 2003: Assembly Language and Digital Logic Chapter 2: Flags and Instructions Notes by Neil Dickson.
Assembly 06. Outline cmp (review) Jump commands test mnemonic bt mnemonic Addressing 1.
2/20/2016CAP 2211 Flow Control Instructions. 2/20/2016CAP 2212 Transfer of Control Flow control instructions are used to control the flow of a program.
MOV Instruction MOV destination,source  MOV AX,BX  MOV SUM,EAX  MOV EDX,ARRAY[EBX][ESI]  MOV CL,5  MOV DL,[BX]
Jumps, Loops and Branching. Unconditional Jumps Transfer the control flow of the program to a specified instruction, other than the next instruction in.
Assembly Language Wei Gao. Assembler language Instructions.
CS-401 Computer Architecture & Assembly Language Programming
Practical Session 2 Computer Architecture and Assembly Language.
Data Transfers, Addressing, and Arithmetic
Instruksi Set Prosesor 8088
Microprocessor and Assembly Language
EE3541 Introduction to Microprocessors
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Basic Assembly Language
INSTRUCTION SET.
Assembly IA-32.
INSTRUCTION SET.
More on logical instruction and
Assembly Language Programming Part 2
Microprocessor and Assembly Language
Computer Organization and Assembly Language
Flags Register & Jump Instruction
Program Logic and Control
Program Logic and Control
Flow Control Instructions
EECE.3170 Microprocessor Systems Design I
Assembly Language for Intel 8086 Jump Condition
Chapter 7 –Program Logic and Control
Carnegie Mellon Ithaca College
Chapter 8: Instruction Set 8086 CPU Architecture
Chapter 7 –Program Logic and Control
Computer Architecture and Assembly Language
Presentation transcript:

Selection and Iteration Chapter 8 S. Dandamudi

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 8: Page 2 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

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 8: Page 3 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

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 8: Page 4 Unconditional Jump (cont’d) Example Two jump instructions  Forward jump jmp ECX_init_done  Backward jump jmp repeat1 Programmer specifies target by a label Assembler computes the offset using the symbol table... mov ECX,10 jmp ECX_init_done init_CX_20: mov ECX,20 CX_init_done: mov EAX,ECX repeat1: dec ECX... jmp repeat1...

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 8: Page 5 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: EIP is already pointing to this instruction  Execution of jmp involves adding the displacement value to current EIP  Displacement is a signed number »Negative value for backward jumps »Positive value for forward jumps

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 8: Page 6 Target Location Inter-segment jump  Target is in another segment CS = target-segment (2 bytes) EIP = target-offset (4 bytes) »Called far jumps (needs five bytes to encode jmp ) Intra-segment jumps  Target is in the same segment EIP = EIP + relative-displacement  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/4-byte displacement »Called near jumps (needs 3 or 5 bytes to encode jmp )

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 8: Page 7 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

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 8: Page 8 Example …09 EB 14 jmp SHORT ECX_init_done 0…1F – 0…0B = 0… …0B B mov ECX, H 169 0…10 E9 0A jmp ECX_init_done 0…1F – 0…15 = 0…00A 170 init_ECX: 171 0…15 B9 12EFCDAB mov ECX,0ABCDEF12H 172 0…1A E jmp near_jump 0…671 – 0…01F = 0… ECX_init_done: 174 0…1F 89 C8 mov EAX,ECX

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 8: Page 9 Example (cont’d) 175 repeat1: 176 0…21 49 dec ECX 177 0…22 EB FD jmp repeat1 0…21 – 0…24 = -3 = FD …662 EB jmp short_jump 0…66C – 0…667 = …667 B9 FFFF00FF mov ECX, 0FF00FFFFH 559 short_jump: 560 0…66C BA mov EDX, H 561 near_jump: 562 0…671 E9 9FF9FFFF jmp init_ECX 0…015 – 0…676 = FFFFF99F

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 8: Page 10 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

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 8: Page 11 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

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 8: Page 12 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)

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 8: Page 13 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

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 8: Page 14 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

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 8: Page 15 Jumps Based on Unsigned Comparisons MnemonicMeaningCondition jejump if equalZF = 1 jzjump if zeroZF = 1 jnejump if not equalZF = 0 jnzjump if not zeroZF = 0 jajump if aboveCF = ZF = 0 jnbejump if not below CF = ZF = 0 or equal

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 8: Page 16 Jumps Based on Unsigned Comparisons MnemonicMeaningCondition jaejump if aboveCF = 0 or equal jnbjump if not belowCF = 0 jbjump if belowCF = 1 jnaejump if not aboveCF = 1 or equal jbejump if belowCF=1 or ZF=1 or equal jna jump if not above CF=1 or ZF=1

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 8: Page 17 Jumps Based on Signed Comparisons MnemonicMeaningCondition jejump if equalZF = 1 jzjump if zeroZF = 1 jnejump if not equalZF = 0 jnzjump if not zeroZF = 0 jgjump if greaterZF=0 & SF=OF jnle jump if not less ZF=0 & SF=OF or equal

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 8: Page 18 Jumps Based on Signed Comparisons (cont’d) MnemonicMeaningCondition jgejump if greater SF = OF or equal jnljump if not less SF = OF jljump if less SF  OF jngejump if not greater SF  OF or equal jlejump if lessZF=1 or SF  OF or equal jng jump if not greater ZF=1 or SF  OF

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 8: Page 19 A Note on Conditional Jumps 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... All conditional jumps are encoded using 2 bytes  Treated as short jumps What if the target is outside this range?

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 8: Page 20 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: ECX = ECX  1 Jump to target if ECX  0

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 8: Page 21 Loop Instructions (cont’d) The following two loop instructions also test the zero flag status loope/loopz target Action: ECX = ECX  1 Jump to target if (ECX  0 and ZF = 1) loopne/loopnz target Action: ECX = ECX  1 Jump to target if (ECX  0 and ZF = 0)

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 8: Page 22 Instruction Execution Times Functionally, loop instruction can be replaced by dec ECX 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 ECX,0 jz target

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 8: Page 23 Implementing HLL Decision Structures High-level language decision structures can be implemented in a straightforward way See Section 8.5 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

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 8: Page 24 Illustrative Examples Two example programs  Linear search »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

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 8: Page 25 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 ECX has the offset value jmp [ECX]  Note: The offset value in indirect jump is the absolute value (not relative value as in the direct jumps) Program example  IJUMP.ASM »Uses a jump table to direct the jump

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 8: Page 26 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]++; }

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 8: Page 27 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 ;end of main jump_table LABEL WORD dw case_0 dw case_1 dw case_2 dw case_3... Last slide