Conditional Loop Instructions, Conditional Structures

Slides:



Advertisements
Similar presentations
Jump Condition.
Advertisements

Departemen Ilmu Komputer FMIPA IPB Conditional Jump Instruction A conditional jump instruction transfer control to a destination address when a.
CDP ECE Spring 2000 ECE 291 Spring 2000 Lecture 4: The 80x86 Instruction Set Architecture Registers-Instructions Constantine D. Polychronopoulos.
CSC 221 Computer Organization and Assembly Language Lecture 21: Conditional and Block Structures: Assembly Programs.
Conditional Jumps Jcond destination  Cond refers to a flag condition  Destination should be a local label  Destination must be within –128 to +127 bytes.
Deeper Assembly: Addressing, Conditions, Branching, and Loops
NEG Instruction Change operand content into two’s complement (negative value) and stored back into its operand mov bl, b neg bl; bl = mov.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Conditional Loop Instructions LOOPZ and LOOPE LOOPNZ.
ACOE2511 Assembly Language Arithmetic and Logic Instructions.
Conditional Processing
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 Chapter 5: Procedures Kip R. Irvine.
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
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
Conditional Processing If … then … else While … do; Repeat … until.
1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
CS2422 Assembly Language & System Programming October 19, 2006.
Prof. Muhammad Saeed III. 1/27/2015Computer Architecture & Assembly Language2 Assembly Language Instructions.
Quiz #2 Topics Character codes Intel IA-32 architecture Mostly MASM
Sahar Mosleh California State University San MarcosPage 1 Applications of Shift and Rotate Instructions.
Sahar Mosleh California State University San MarcosPage 1 JMP and Loops Memory Operand Move Instruction Array Data Related Operation and Directives.
Conditional Processing Computer Organization & Assembly Language Programming Dr Adnan Gutub aagutub ‘at’ uqu.edu.sa [Adapted from slides of Dr. Kip Irvine:
1 Flow Control Instructions and Addressing Modes Chapter 3.
Lecture 3 Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU.
Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures.
Sahar Mosleh California State University San MarcosPage 1 Review.
Wednesday Feb 1 Project #1 Due Sunday, February 3 Quiz #2 Wednesday, February 6, in class Programming Project #2 is posted Due Sunday, February 10.
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
(Flow Control Instructions)
Sahar Mosleh California State University San MarcosPage 1 Nested Procedure calls and Flowcharts.
1 ICS 51 Introductory Computer Organization Fall 2009.
Decision Structures – Code Generation Lecture 24 Mon, Apr 18, 2005.
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.
EEL 3801 Part V Conditional Processing. This section explains how to implement conditional processing in Assembly Language for the 8086/8088 processors.
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.
CSC 221 Computer Organization and Assembly Language Lecture 20: Conditional and Block Structures.
Selection and Iteration Chapter 8 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Assembly Language for Intel-Based Computers, 4 th Edition Lecture 22: Conditional Loops (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for Intel-Based Computers, 4 th Edition Week 10: Conditional Processing Slides modified by Dr. Osama Younes.
K.K. Leung Fall 2008Introductory Pentium Programming1 Pentium Architecture: Introductory Programming Kin K. Leung
CS2422 Assembly Language and System Programming 0 Week 13 & 14 Codes in Assembly Language.
CS-401 Computer Architecture & Assembly Language Programming
1 Conditional Processing Chapter 6 Adapted with many modifications from slides prepared by Professor Mario Marchand Computer Science Dept. University.
Assembly Language for x86 Processors 6th Edition
CSC 221 Computer Organization and Assembly Language
Assembly Language for Intel-Based Computers, 5th Edition
Chapter 6: Conditional Processing
Homework Reading Labs PAL, pp
Assembly Language for x86 Processors 7th Edition
Assembly Language for Intel-Based Computers, 4th Edition
Assembly IA-32.
Assembly Language Lab (4).
Assembly Language Programming Part 2
Microprocessor and Assembly Language
Computer Organization and Assembly Language
فصل پنجم انشعاب و حلقه.
Homework Reading Machine Projects Labs PAL, pp
Flow Control Instructions
X86 Assembly Review.
EECE.3170 Microprocessor Systems Design I
High-level language structures
Conditional Processing
Computer Architecture and Assembly Language
Presentation transcript:

Conditional Loop Instructions, Conditional Structures Conditional Jump, Conditional Loop Instructions, and Conditional Structures

Type of conditional jump instruction Based on specific flag Based on equality between operands, or value of (E)cx. Based on comparisons of unsigned operands. Based on comparison of signed operands. Mnemonic Description Flags JZ Jump if zero ZF = 1 JNZ Jump if not zreo ZF = 0 JC Jump if carry CF = 1 JNC Jump if not carry CF = 0 JO Jump if overflow OF = 1 JNO Jump if not overflow OF = 0 JS Jump if signed SF = 1 JNS Jump if not signed SF = 0 JP Jump if parity (even) PF = 1 JNP Jump if not parity (odd) PF = 0

Jump if destination = source Equality comparisons Based on equality between operands, or value of (E)cx. CMP destination, Source Mnemonic Description JE Jump if destination = source JNE Jump if not equal JCXZ Jump if CX=0 JECXZ Jump if ECX = 0

CMP destination, Source Unsigned Comparisons Jumps based on comparisons of unsigned integers are useful when comparing unsigned values, such as 7FFh and 8000h, where 7FFh is smaller than 8000h latter. CMP destination, Source Mnemonic Description JA Jump if above (destination > source) JNBE Jump if not below or equal (same as JA) JAE Jump if above or equal (destination >= source) JNB Jump if not below ( same as JAE) JB Jump if below JNAE Jump if not above or equal ( same as JB) JBE Jump if below or equal ( destination <= source) JNA Jump if not above ( same as JBE)

Signed Comparison It is used when the numbers you are comparing can be interpreted as signed values. CMP destination, Source Example: mov al,7Fh ; (7Fh or +127) Cmp al,80h ; (80h or -128 ) Ja Isabove ; no: 7Fh not> 80 h Jg isGreater ; yes: + 127 > -128 Mnemonic Description JG Jump if Greater (destination > source) JNLE Jump if not less than or equal (same as JG) JGE Jump if Greater than or equal (destination >= source) JNL Jump if not Less than ( same as JGE) JL Jump if less ( destination < source ) JNGE Jump if not Greater or equal ( same as JL) JLE Jump if Less than or equal ( destination <= source) JNG Jump if not greater ( same as JLE)

Application: Larger of two integers. The following code compares the unsigned integers in Ax and Bx and moves the larger of the two to DX: mov dx,ax ; assume Ax is larger cmp ax,bx ; if Ax is .= BX then jae L1 ; jump if AX>=BX to L1 mov dx,bx ; else move BX to DX L1:

Application: The following instructions compare the unsigned values in the three variables V1, V2, V3 and move the smallest of the three to Eax: .data V1 Dword ? V2 Dword ? V3 Dword ? .code mov eax, V1 ; assumes V1 is smallest cmp eax,V2 ; if eax <= V2 then jbe L1 ; jump to L1 mov eax, V2 ; else move V2 to ax L1: cmp eax,V3 ; if eax <= v3 then jbe L2 ; jump to L2 mov eax, V3 ; else move to eax L2:

Scanning array for first non zero value. Title Scanning an Array .data intArray dword 0,0,0,0,1,20,35,12,66,4,0 noneMsg byte “A non-Zero value was not found”,0 .code Main proc Mov ebx,offset intArray Mov ecx, lenghtof intArray L1: Cmp [ebx],0 Jnz Found Add ebx,4 Loop L1 Jmp Notfound Found: Mov eax,[ebx] Call writeint Jmp Quit Notfound: Mov edx,offset nonMsg Call writestring Quit: Call crlf Exit Main endp End main

Conditional Loop instructions. Loopz and Loope instructions. The LOOPZ instruction permits a loop to continue while the Zero flag is set and the unsigned value of ECX is greater than zero. The destination label must be between -128 byte and +127 from the location of the following instruction. LOOPZ destination LOOPE instruction is equal to LOOPZ because they share the same circuitry, this is the execution logic of loopz and loope: ECX = ECX-1 If ECX>0 and ZF=1 jump to destination

LOOPNZ and LOOPNE instructions The LOOPNZ ( loop if not zero) instruction is the counter part of LOOPZ. The loop continues while the unsigned value of ECX is greater than Zero and the Zero flag is clear. The syntax is: LOOPNZ destination The LOOPNE (loop if not equal) instruction is equivalent to LOOPNZ. They share the same circuitry. This is the execution logic of LOOPNZ and LOOPNE ECX = ECX - 1 If ECX > 0 and ZF = 0, jump to destination Otherwise no jump occurs and control passes to the next instruction

TITLE Scanning for a Positive Value (Loopnz.asm) ; Scan an array for the first positive value. If positive value found ESI is left pointing at it. If The ;loop fails to find a positive number, it stops when ecx equal to zero. In this case JNZ jumps to quit ;and ESI points to sentinel value (0) stored immediately after the array. INCLUDE Irvine32.inc .data array SWORD -3,-6,-1,-10,10,30,40,4 sentinel SWORD 0 .code main PROC mov esi,OFFSET array mov ecx,LENGTHOF array next: mov ebx,[esi] ; move element of array to ebx test ebx,10000000b ; test highest bit of ebx pushfd ; push flags on stack add esi, 2 popfd ; pop flags from stack loopnz next ; continue loop jnz quit ; none found sub esi, 2 ; SI points to value quit: call crlf exit main ENDP END main

Conditional Structure Conditional structures are conditional expressions that trigger a choice between different logical branches. Each branch causes a different sequence of instructions to execute

Block-Structured IF Statement In most high level languages an IF statement implies that a boolean expression is followed by two lists of statements. One performed when the expression is true and another performed when the expression is false. If (expression) Statement list 1 Else Statement list 2 The else portion of the statement is optional. The following flowchart shows the two branching path in the conditional if structure, labeled true and false.

start Boolean expression TRUE FALSE Statement list 1 Statement list 2 end

Compile the following C++ if statement to assembly code Example Compile the following C++ if statement to assembly code If (op1 == op2) { X = 1; Y = 2; } Answer: mov eax, op1 cmp eax, op2 ; compare EAX to op2 je L1 ; jump if equal to L1 jmp L2 ; otherwise, jump to L2 L1: mov X, 1 mov Y, 2 L2:

Compound expressions Logical AND operator You can implement a boolean expression that uses the logical AND operator in at least two ways. Consider the following compound expression written in Pseudo code. If (al > bl) AND (bl > cl) { X = 1 } For any given compound expression there are at least several ways to implemented it in assembly. We are implementing two ways of the above compound expression.

cmp al, bl ;first expression ja L1 jmp Next L1: If (al > bl) AND (bl > cl) { X = 1 } cmp al, bl ;first expression ja L1 jmp Next L1: cmp bl, cl ; second expression ja L2 L2: ; both are true mov X, 1 ; set X to 1 Next: We will assume that the values are unsigned. The implementation using JA (jump if above) cmp al, bl ; first expression jbe Next ; quit if false cmp bl, cl ; second expression jbe next ; quit if false mov X, 1 ; both are true Next: We can simplify the code if we reverse the JA condition and use JBE instead

Logical OR operator When multiple expression occur in a compound expression using the logical OR operator the expression is automatically true as soon as one expression is true Example: If (al > bl) OR (bl > cl) X = 1 In the following implementation, the code branches to L1 if the first expression is true; otherwise it falls through the second CMP instruction. The second expression reverse the > operator and uses JBE instead. cmp al, bl ja L1 cmp bl, cl jbe next L1: mov X, 1 next:

While Loops The WHILE structure test a condition first before performing a block of statements. As long as the loop condition remains true, the statement are repeated. while (val1 < val2) { Val1++; Val2--; } When coding this structure in assembly language, it is convenient to reverse the loop condition and jump to endwhile when the condition becomes true

Assuming that val1 and val2 are variables mov eax, val1 ; copy variable to eax while: cmp eax, val2 ; if not (val1 < val2) jnl endwhile ; exit the loop inc eax ; val1++ dec val2 ; val2-- jmp while ; repeat the loop endwhile: mov val1, eax ; save new value for val1

Example: IF statement Nested in a Loop High-level structure languages are particularly good at representing nested control structure. In the following C++ example, an IF statement is nested inside a WHILE loop. While (op1 < op2) { Op1 ++; If (op2 == op3) X = 2; Else X = 3; } To simplify the translation, in the following flowchart, the registers have been substituted for variables (EAX = op1, EBX = op2, and ECX = op3)

eax = op1 ebx = op2 ecx = op3 op1 =eax end begin false inc eax X = 2 X = 3 L7: L6: L5: L4: L2: true L3: ebx = = eax? L1: eax < ebx?

cmp eax, ebx ; EAX < EBX? jl L2 ; true jmp L7 ; false L2 inc eax Assembly code mov eax, op1 mov ebx, op2 mov ecx, op3 L1: cmp eax, ebx ; EAX < EBX? jl L2 ; true jmp L7 ; false L2 inc eax L3: cmp ebx, ecx ; EBX = = ECX? je L4 ; true jmp L5 ; false L4: mov X, 2 ; X = 2 jmp L6 L5: mov X, 3 ; X = 3 L6: Jmp L1 ; repeat the loop L7: mov op1, eax ; update op1 ;While (op1 < op2) ;{ ;Op1 ++ ;If (op2 == op3) ;X = 2 ;Else ;X = 3 ; }