CS2422 Assembly Language and System Programming Conditional Processing Department of Computer Science National Tsing Hua University.

Slides:



Advertisements
Similar presentations
CSC 221 Computer Organization and Assembly Language Lecture 21: Conditional and Block Structures: Assembly Programs.
Advertisements

Assembly Language for x86 Processors 6th Edition
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, 4 th Edition Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for Intel-Based Computers
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You.
CS2422 Assembly Language & System Programming October 17, 2006.
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, 4 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
Flow Control Instructions
Shift and Rotate Instructions
Assembly Language for Intel-Based Computers, 4 th Edition Lecture 23: Finite State Machines, WHILE operator (c) Pearson Education, All rights reserved.
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.
Quiz #2 Topics Character codes Intel IA-32 architecture Mostly MASM
Conditional Processing Computer Organization & Assembly Language Programming Dr Adnan Gutub aagutub ‘at’ uqu.edu.sa [Adapted from slides of Dr. Kip Irvine:
Assembly Language for x86 Processors 7th Edition
ICS312 Set 9 Logic & Shift Instructions. Logic & Shift Instructions Logic and Shift Instructions can be used to change the bit values in an operand. The.
Web siteWeb site ExamplesExamples ASSEMBLY LANGUAGE FOR INTEL- BASED COMPUTERS, 5 TH EDITION Chapter 6: Conditional Processing Kip R. Irvine.
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.
Fall 2013 Chapter 6: Conditional Processing. Questions Answered by this Chapter How can I use the boolean operations introduced in Chapter 1 (AND, OR,
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You.
Assembly Language for x86 Processors 6th Edition
Logic Conditional Processing. Status flags - review The Zero flag is set when the result of an operation equals zero. The Carry flag is set when an instruction.
Chapter 6: Conditional Processing. 2 Chapter Overview Boolean and Comparison Instructions Conditional Jumps Conditional Loop Instructions Conditional.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You.
Assembly Language for Intel-Based Computers Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may modify and copy.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You.
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.
EEL 3801 Part V Conditional Processing. This section explains how to implement conditional processing in Assembly Language for the 8086/8088 processors.
Conditional Loop Instructions, Conditional Structures
Lecture 12 Integer Arithmetic Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
COMP 2003: Assembly Language and Digital Logic Chapter 2: Flags and Instructions Notes by Neil Dickson.
Conditional Processing Computer Organization and Assembly Languages Yung-Yu Chuang 2005/11/03 with slides by Kip Irvine.
CSC 221 Computer Organization and Assembly Language Lecture 20: Conditional and Block Structures.
Computer and Information Sciences College / Computer Science Department CS 206 D Computer Organization and Assembly Language.
Assembly Language for Intel-Based Computers, 4 th Edition Lecture 22: Conditional Loops (c) Pearson Education, All rights reserved. You may modify.
Boolean, Shift and Rotate instructions Dr.Hadi AL Saadi.
Assembly Language for Intel-Based Computers, 4 th Edition Week 10: Conditional Processing Slides modified by Dr. Osama Younes.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
CS2422 Assembly Language and System Programming 0 Week 13 & 14 Codes in Assembly Language.
Assembly Language for x86 Processors 6th Edition
CSC 221 Computer Organization and Assembly Language
Assembly Language for x86 Processors 7th Edition
Assembly Language for Intel-Based Computers, 5th Edition
Chapter 6: Conditional Processing
Practical Session 2.
CSC 221 Computer Organization and Assembly Language
Assembly Language for x86 Processors 7th Edition
Assembly Language for Intel-Based Computers, 4th Edition
EE3541 Introduction to Microprocessors
Morgan Kaufmann Publishers Computer Organization and Assembly Language
More on logical instruction and
Assembly Language Programming Part 2
Computer Organization and Assembly Language
Shift & Rotate Instructions)
Flow Control Instructions
Assembly Language for Intel-Based Computers, 4th Edition
Assembly Language for Intel-Based Computers, 5th Edition
Shift, Multiply, and Divide
Conditional Processing
Computer Organization and Assembly Language
Computer Architecture and Assembly Language
Presentation transcript:

CS2422 Assembly Language and System Programming Conditional Processing Department of Computer Science National Tsing Hua University

CS2422 Assembly Language and System Programming Assembly Language for Intel- Based Computers, 5 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify and copy this slide show for your personal use, or for use in the classroom, as long as this copyright statement, the author's name, and the title are not changed. Slides prepared by the author Revision date: June 4, 2006 Kip Irvine

2 Chapter Overview  Boolean and Comparison Instructions AND, OR, XOR, NOT, TEST, CMP  Conditional Jumps  Conditional Loop Instructions  Conditional Structures  Application: Finite-State Machines (skipped)  Decision Directives  Brief Overview of Chapter 7

3 AND Instruction  Perform a Boolean AND operation between each pair of matching bits in two operands  Syntax: AND destination, source (same operand types as MOV) AND

4 OR Instruction  Performs a Boolean OR operation between each pair of matching bits in two operands  Syntax: OR destination, source OR

5 XOR Instruction  Performs a Boolean exclusive-OR operation between each pair of matching bits in two operands  Syntax: XOR destination, source XOR XOR is a useful way to toggle (invert) the bits in an operand.

6 NOT Instruction  Performs a Boolean NOT operation on a single destination operand  Syntax: NOT destination NOT

7 Application – An Example  Task: Convert character in AL to upper case  Solution: Use AND instruction to clear bit 5 mov al,'a‘ ; AL = b and al, b ; AL = b ASCII code of ‘A’ = 41H ASCII code of ‘a’ = 61H

8 TEST Instruction  Performs a nondestructive AND between each pair of matching bits in two operands  No operands are modified, but the Zero flag is affected.  Example: jump to a label if either bit 0 or bit 1 in AL is set. test al, b jnz ValueFound

9 CMP Instruction (1/2)  Compare destination operand to source operand Nondestructive subtraction of source from destination (destination operand is not changed)  Syntax: CMP destination, source  Example: destination < source  Example: destination > source mov al,4 cmp al,5; Carry flag set mov al,6 cmp al,5; ZF = 0, CF = 0 (both the Zero and Carry flags are clear)

10 CMP Instruction (2/2)  The comparisons shown here are performed with signed integers.  Example: destination > source Example: destination < source mov al,5 cmp al,-2 ; Sign flag == Overflow flag mov al,-1 cmp al,5 ; Sign flag != Overflow flag

11 What's Next  Boolean and Comparison Instructions  Conditional Jumps  Conditional Loop Instructions  Conditional Structures  Application: Finite-State Machines (skipped)  Decision Directives  Brief Overview of Chapter 7

12 CMP and Jcond Instruction  The IF statement in C and PASCAL is converted into CMP and Jcond instructions in x86 Assembly: CMP X, op1 JNG EndIf EndIf: If (X > op1) Then End If

13 Jcond Instruction  A conditional jump instruction branches to a label when specific register or flag conditions are met  Examples: JB, JC jump to a label if the Carry flag is set JE, JZ jump to a label if the Zero flag is set JS jumps to a label if the Sign flag is set JNE, JNZ jump to a label if the Zero flag is clear JECXZ jumps to a label if ECX equals 0

14 J cond Ranges  Jump destinations usually confined within same procedure  Prior to the 386: jump must be within –128 to +127 bytes from current location counter  IA-32 processors: 32-bit offset permits jump anywhere in segment

15 Jumps Based on Specific Flags

16 Jumps Based on Equality

17 Jumps Using Unsigned Comparison

18 Jumps Using Signed Comparisons

19 More Frequently Used Jcond  JE (Equal)  JNE (Not Equal)  JG or JGE (Greater Than or Equal)  JL or JLE (Less Than or Equal) Note: JG=JNLE, JGE=JNL, … etc.

20 Simple IF  If (op1=op2) then end if  Two different approaches: CMP op1, op2 JE True JMP EndIf True: EndIf CMP op1, op2 JNE False False:

21 IF … AND … CMP X, op1 JNG EndIf CMP Y, op2 JNLE EndIf CMP … … EndIf: If (X > op1)and (Y <=op2)and … Then End If

22 IF … OR … CMP X, op1 JG True CMP Y, op2 JLE True CMP … … JMP EndIf True: EndIf: If (X > op1) or (Y <=op2) or … Then End If

23 Applications  Task: Jump to a label if unsigned EAX is greater than EBX  Solution: Use CMP, followed by JA  Task: Jump to a label if signed EAX is greater than EBX  Solution: Use CMP, followed by JG cmp eax,ebx ja Larger cmp eax,ebx jg Greater

24 What's Next  Boolean and Comparison Instructions  Conditional Jumps  Conditional Loop Instructions  Conditional Structures  Application: Finite-State Machines (skipped)  Decision Directives  Brief Overview of Chapter 7

25 LOOPZ and LOOPE  Syntax: LOOPE destination; loop if equal LOOPZ destination; loop if zero Destination label must be in -128 ~ +127  Logic: ECX  ECX – 1 if ECX > 0 and ZF=1, jump to destination  Useful when scanning an array for the first element that does not match a given value.

26 LOOPNZ and LOOPNE  Syntax: LOOPNZ destination LOOPNE destination  Logic: ECX  ECX – 1; if ECX > 0 and ZF=0, jump to destination  Useful when scanning an array for the first element that matches a given value.

27 LOOPNZ Example  Find the first positive value in an array:.data array SWORD -3,-6,-1,-10,10,30,40,4.code mov esi,OFFSET array mov ecx,LENGTHOF array sub esi,TYPE array next: add esi, TYPE array test WORD PTR [esi],8000h ; test sign bit loopnz next; continue loop jnz quit; none found … ; ESI points to value quit:

28 What's Next  Boolean and Comparison Instructions  Conditional Jumps  Conditional Loop Instructions  Conditional Structures  Application: Finite-State Machines (skipped)  Decision Directives  Brief overview of Chapter 7

29 Block-Structured IF Statements  Assembly language programmers can easily translate logical statements written in C++/Java into assembly language: mov eax,op1 cmp eax,op2 jne L1 mov X,1 jmp L2 L1: mov X,2 L2: if( op1 == op2 ) X = 1; else X = 2;

30 WHILE  A WHILE loop is really an IF statement followed by the body of the loop, followed by an unconditional jump to the top of the loop. While: CMP op1, op2 JNL EndDo JMP While EndDo: DO WHILE(op1<op2) END DO

31 REPEAT UNTIL repeat: CMP X, op1 JE EndIf CMP Y, op2 JNG repeat EndIf: REPEAT UNTIL(X == op1) or (Y > op2)

32 What's Next  Boolean and Comparison Instructions  Conditional Jumps  Conditional Loop Instructions  Conditional Structures  Application: Finite-State Machines (skipped)  Decision Directives.IF,.WHILE, and.REPEAT directives (note: not instructions)  Brief Overview of Chapter 7

33 Runtime Expressions .IF,.ELSE,.ELSEIF, and.ENDIF can be used to instruct the assembler to create block-structured IF statements MASM generates "hidden" code for you, consisting of code labels, CMP and conditional jump instructions.IF eax>ebx mov edx,1.ELSE mov edx,2.ENDIF.IF eax>ebx && eax>ecx mov edx,1.ELSE mov edx,2.ENDIF

34 Relational and Logical Operators

35 MASM-Generated Code  MASM automatically generates an unsigned jump, because val1 is unsigned JBE: jump if below or equal mov eax,6 cmp eax,val1 mov val1 DWORD 5 result DWORD ?.code mov eax,6.IF eax > val1 mov result,1.ENDIF Generated code:

36 MASM-Generated Code  MASM automatically generates a signed jump (JLE) mov eax,6 cmp eax,val1 mov val1 SDWORD 5 result SDWORD ?.code mov eax,6.IF eax > val1 mov result,1.ENDIF Generated code:

37.REPEAT Directive  Executes the loop body before testing the loop condition associated with the.UNTIL directive.  Example: ; Display integers 1 ~ 10: mov eax,0.REPEAT inc eax call WriteDec call Crlf.UNTIL eax == 10

38.WHILE Directive  Tests the loop condition before executing the loop body. The.ENDW directive marks the end of the loop.  Example: ; Display integers 1 ~ 10: mov eax,0.WHILE eax < 10 inc eax call WriteDec call Crlf.ENDW

39 When to Use or Not to Use Directives?  Directives make assembly language easier to write and to understand, by hiding tedious work. (Food for thought: Wouldn ’ t it be even better to use C language?)  Don ’ t use directives if you want to have total control.

40 What's Next  Boolean and Comparison Instructions  Conditional Jumps  Conditional Loop Instructions  Conditional Structures  Application: Finite-State Machines (skipped)  Decision Directives  Brief Overview of Chapter 7

41 Logical vs Arithmetic Shifts  A logical shift fills the newly created bit position with zero:  An arithmetic shift fills the newly created bit position with the sign bit:

42 SHL Instruction  Shift left: performs a logical left shift on the destination operand, filling the lowest bit with 0.  Operand types for SHL: SHL reg,imm8 SHL mem,imm8 SHL reg,CL SHL mem,CL (Same for all shift and rotate instructions)

43 SHR Instruction  Shift right: performs a logical right shift on the destination operand. The highest bit position is filled with a zero  Shifting right n bits divides the operand by 2 n mov dl,80 shr dl,1; DL = 40 shr dl,2; DL = 10

44 SAL and SAR Instructions  Shift arithmetic left: identical to SHL  Shift arithmetic right: performs a right arithmetic shift on the destination operand  An arithmetic shift preserves the number's sign mov dl,-80 sar dl,1; DL = -40 sar dl,2; DL = -10

45 MUL Instruction  The MUL (unsigned multiply) instruction multiplies an 8-, 16-, or 32-bit operand by either AL, AX, or EAX.  The instruction formats are: MUL r/m8 MUL r/m16 MUL r/m32 Implied operands:

46 DIV Instruction  Unsigned divide: performs 8-bit, 16-bit, and 32-bit division on unsigned integers  A single operand is supplied (register or memory operand), which is assumed to be the divisor  Instruction formats: DIV r/m8 DIV r/m16 DIV r/m32 Default Operands:

47 Summary  Bitwise inst. (AND, OR, XOR, NOT, TEST) manipulate individual bits in operands  CMP: implied sub for comparing operands sets condition flags  Conditional Jumps & Loops equality: JE, JNE flag values: JC, JZ, JNC, JP,... signed: JG, JL, JNG,... unsigned: JA, JB, JNA,... LOOPZ, LOOPNZ, LOOPE, LOOPNE  Shift: SHL, SHR, SAL, SAR  Multiplication and division: MUL, DIV