SlideShare uma empresa Scribd logo
1 de 78
Assembly Language for Intel-Based Computers, 5 th  Edition  Chapter 6: Conditional Processing (c) Pearson Education, 2006-2007. 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 R. Irvine
Chapter Overview ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Boolean and Comparison Instructions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Status Flags - Review ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
AND Instruction ,[object Object],[object Object],[object Object],[object Object],AND
OR Instruction ,[object Object],[object Object],[object Object],OR
XOR Instruction ,[object Object],[object Object],[object Object],XOR XOR is a useful way to toggle (invert) the bits in an operand.
NOT Instruction ,[object Object],[object Object],[object Object],NOT
Applications  (1 of 5) mov al,'a' ; AL = 01100001b and al,11011111b ; AL = 01000001b ,[object Object],[object Object]
Applications   (2 of 5) mov al,6 ; AL = 00000110b or  al,00110000b ; AL = 00110110b ,[object Object],[object Object],The ASCII digit '6' = 00110110b
Applications   (3 of 5) mov ax,40h ; BIOS segment mov ds,ax mov bx,17h ; keyboard flag byte or BYTE PTR [bx],01000000b ; CapsLock on ,[object Object],[object Object],This code only runs in Real-address mode, and it does not work under Windows NT, 2000, or XP.
Applications   (4 of 5) mov ax,wordVal and ax,1 ; low bit set? jz  EvenValue ; jump if Zero flag set ,[object Object],[object Object],JZ (jump if Zero) is covered in Section 6.3. Your turn: Write code that jumps to a label if an integer is negative.
Applications   (5 of 5) or  al,al jnz IsNotZero ; jump if not zero ,[object Object],[object Object],ORing any number with itself does not change its value.
TEST Instruction ,[object Object],[object Object],[object Object],test al,00000011b jnz  ValueFound ,[object Object],test al,00000011b jz  ValueNotFound
CMP Instruction  (1 of 3) ,[object Object],[object Object],[object Object],[object Object],mov al,5 cmp al,5 ; Zero flag set ,[object Object],mov al,4 cmp al,5 ; Carry flag set
CMP Instruction  (2 of 3) ,[object Object],mov al,6 cmp al,5 ; ZF = 0, CF = 0 (both the Zero and Carry flags are clear)
CMP Instruction  (3 of 3) ,[object Object],mov al,5 cmp al,-2 ; Sign flag == Overflow flag The comparisons shown here are performed with signed integers. ,[object Object],mov al,-1 cmp al,5 ; Sign flag != Overflow flag
What's Next ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Conditional Jumps ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
J cond  Instruction ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
J cond  Ranges ,[object Object],[object Object],[object Object],[object Object]
Jumps Based on Specific Flags
Jumps Based on Equality
Jumps Based on Unsigned Comparisons
Jumps Based on Signed Comparisons
Applications  (1 of 5) cmp eax,ebx ja  Larger ,[object Object],[object Object],cmp eax,ebx jg  Greater ,[object Object],[object Object]
Applications  (2 of 5) cmp eax,Val1 jbe L1 ; below or equal ,[object Object],cmp eax,Val1 jle L1 ,[object Object]
Applications  (3 of 5) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Applications  (4 of 5) ,[object Object],[object Object],[object Object],test DWORD PTR [edi],1 jz  L2 ,[object Object]
Applications  (5 of 5) and al,00001011b ; clear unwanted bits cmp al,00001011b ; check remaining bits je  L1 ; all set? jump to L1 ,[object Object],[object Object]
Your turn . . . ,[object Object],[object Object],[object Object],[object Object],[object Object]
Encrypting a String ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],The following loop uses the XOR instruction to transform every character in a string into a new value.
String Encryption Program ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],View the  Encrypt.asm  program's source code. Sample output: Enter the plain text:  Attack at dawn. Cipher text:   «¢¢Äîä-Ä¢-ïÄÿü-Gs Decrypted: Attack at dawn.
BT (Bit Test) Instruction ,[object Object],[object Object],[object Object],[object Object],[object Object],bt AX,9 ; CF = bit 9 jc L1 ; jump if Carry
What's Next ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Conditional Loop Instructions ,[object Object],[object Object]
LOOPZ and LOOPE ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
LOOPNZ and LOOPNE ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
LOOPNZ Example ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],The following code finds the first positive value in an array:
Your turn . . . ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Locate the first nonzero value in the array. If none is found, let ESI point to the sentinel value:
. . . (solution) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
What's Next ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Conditional Structures ,[object Object],[object Object],[object Object],[object Object],[object Object]
Block-Structured IF Statements ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],if( op1 == op2 ) X = 1; else X = 2;
Your turn . . . ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],if( ebx <= ecx ) { eax = 5; edx = 6; } (There are multiple correct solutions to this problem.)
Your turn . . . ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],if( var1 <= var2 ) var3 = 10; else { var3 = 6; var4 = 7; } (There are multiple correct solutions to this problem.)
Compound Expression with AND   (1 of 3) ,[object Object],[object Object],if (al > bl) AND (bl > cl) X = 1;
Compound Expression with AND   (2 of 3) cmp al,bl ; first expression... ja  L1 jmp next L1: cmp bl,cl ; second expression... ja  L2 jmp next L2: ; both are true mov X,1 ; set X to 1 next: if (al > bl) AND (bl > cl) X = 1; This is one possible implementation . . .
Compound Expression with AND   (3 of 3) 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: if (al > bl) AND (bl > cl) X = 1; But the following implementation uses  29% less code by reversing the first relational operator. We allow the program to &quot;fall through&quot; to the second expression:
Your turn . . . ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],if( ebx <= ecx  && ecx > edx ) { eax = 5; edx = 6; } (There are multiple correct solutions to this problem.)
Compound Expression with OR   (1 of 2) ,[object Object],[object Object],if (al > bl) OR (bl > cl) X = 1;
Compound Expression with OR   (1 of 2) cmp al,bl ; is AL > BL? ja  L1 ; yes cmp bl,cl ; no: is BL > CL? jbe next ; no: skip next statement L1: mov X,1 ; set X to 1 next: if (al > bl) OR (bl > cl) X = 1; We can use &quot;fall-through&quot; logic to keep the code as short as possible:
WHILE Loops while( eax < ebx) eax = eax + 1; 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. Consider the following example: top: cmp eax,ebx ; check loop condition jae next ; false? exit loop inc eax ; body of loop jmp top ; repeat the loop next: This is a possible implementation:
Your turn . . . top: cmp ebx,val1 ; check loop condition ja  next ; false? exit loop add ebx,5 ; body of loop dec val1 jmp top ; repeat the loop next: while( ebx <= val1) { ebx = ebx + 5; val1 = val1 - 1 } Implement the following loop, using unsigned 32-bit integers:
Table-Driven Selection   (1 of 3) ,[object Object],[object Object],[object Object],[object Object]
Table-Driven Selection   (2 of 3) .data CaseTable BYTE 'A' ; lookup value DWORD Process_A ; address of procedure EntrySize = ($ - CaseTable) BYTE 'B' DWORD Process_B BYTE 'C' DWORD Process_C BYTE 'D' DWORD Process_D NumberOfEntries = ($ - CaseTable) / EntrySize Step 1: create a table containing lookup values and procedure offsets:
Table-Driven Selection   (3 of 3) mov ebx,OFFSET CaseTable ; point EBX to the table mov ecx,NumberOfEntries ; loop counter L1: cmp al,[ebx] ; match found? jne L2 ; no: continue call NEAR PTR [ebx + 1] ; yes: call the procedure jmp L3 ; and exit the loop L2: add ebx,EntrySize ; point to next entry loop L1 ; repeat until ECX = 0 L3: Step 2: Use a loop to search the table. When a match is found, we call the procedure offset stored in the current table entry: required for procedure pointers
What's Next ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Application: Finite-State Machines ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Finite-State Machine ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
FSM Examples ,[object Object],[object Object]
Your turn . . . ,[object Object]
Implementing an FSM StateA: call Getnext ; read next char into AL cmp al,'+' ; leading + sign? je StateB ; go to State B cmp al,'-' ; leading - sign? je StateB ; go to State B call IsDigit ; ZF = 1 if AL = digit jz StateC ; go to State C call DisplayErrorMsg ; invalid input found jmp Quit The following is code from State A in the Integer FSM: View the  Finite.asm source code .
IsDigit Procedure IsDigit PROC   cmp  al,'0' ; ZF = 0   jb  ID1   cmp  al,'9' ; ZF = 0   ja  ID1   test  ax,0  ; ZF = 1 ID1: ret IsDigit ENDP Receives a character in AL. Sets the Zero flag if the character is a decimal digit.
Flowchart of State A State A accepts a plus or minus sign, or a decimal digit.
Your turn . . . ,[object Object],[object Object],[object Object]
What's Next ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Using the .IF Directive ,[object Object],[object Object],[object Object],[object Object],[object Object]
Runtime Expressions .IF eax > ebx mov edx,1 .ELSE mov edx,2 .ENDIF ,[object Object],[object Object],[object Object],.IF eax > ebx && eax > ecx mov edx,1 .ELSE mov edx,2 .ENDIF
Relational and Logical Operators
MASM-Generated Code mov eax,6 cmp eax,val1 jbe @C0001  mov result,1 @C0001: .data val1  DWORD 5 result DWORD ? .code mov eax,6 .IF eax > val1 mov result,1 .ENDIF Generated code: MASM automatically generates an unsigned jump (JBE) because  val1  is unsigned.
MASM-Generated Code mov eax,6 cmp eax,val1 jle @C0001  mov result,1 @C0001: .data val1  SDWORD  5 result SDWORD ? .code mov eax,6 .IF eax > val1 mov result,1 .ENDIF Generated code: MASM automatically generates a signed jump (JLE) because  val1  is signed.
MASM-Generated Code mov ebx,5 mov eax,6 cmp eax,ebx jbe @C0001  mov result,1 @C0001: .data result DWORD ? .code mov ebx,5 mov eax,6 .IF eax > ebx mov result,1 .ENDIF Generated code: MASM automatically generates an unsigned jump (JBE) when both operands are registers . . .
MASM-Generated Code mov ebx,5 mov eax,6 cmp eax,ebx jle @C0001  mov result,1 @C0001: .data result SDWORD ? .code mov ebx,5 mov eax,6 .IF SDWORD PTR eax > ebx mov result,1 .ENDIF Generated code: . . . unless you prefix one of the register operands with the SDWORD PTR operator. Then a signed jump is generated.
.REPEAT Directive ; Display integers 1 – 10: mov eax,0 .REPEAT inc eax call WriteDec call Crlf .UNTIL eax == 10 Executes the loop body before testing the loop condition associated with the .UNTIL directive.  Example:
.WHILE Directive ; Display integers 1 – 10: mov eax,0 .WHILE eax < 10 inc eax call WriteDec call Crlf .ENDW Tests the loop condition before executing the loop body The .ENDW directive marks the end of the loop.  Example:
Summary ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
The End

Mais conteúdo relacionado

Mais procurados

Push down automata
Push down automataPush down automata
Push down automata
Somya Bagai
 

Mais procurados (20)

[ASM]Lab6
[ASM]Lab6[ASM]Lab6
[ASM]Lab6
 
Assembly language (coal)
Assembly language (coal)Assembly language (coal)
Assembly language (coal)
 
Lecture6
Lecture6Lecture6
Lecture6
 
Unit 4 assembly language programming
Unit 4   assembly language programmingUnit 4   assembly language programming
Unit 4 assembly language programming
 
Logical instructions (and, or, xor, not, test)
Logical instructions (and, or, xor, not, test)Logical instructions (and, or, xor, not, test)
Logical instructions (and, or, xor, not, test)
 
Microprocessor Week 7: Branch Instruction
Microprocessor Week 7: Branch InstructionMicroprocessor Week 7: Branch Instruction
Microprocessor Week 7: Branch Instruction
 
Microprocessor Week 8: Advance programming
Microprocessor Week 8: Advance programmingMicroprocessor Week 8: Advance programming
Microprocessor Week 8: Advance programming
 
Assembly language (addition and subtraction)
Assembly language (addition and subtraction)Assembly language (addition and subtraction)
Assembly language (addition and subtraction)
 
Push down automata
Push down automataPush down automata
Push down automata
 
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
 
chapter 7 Logic, shift and rotate instructions
chapter 7 Logic, shift and rotate instructionschapter 7 Logic, shift and rotate instructions
chapter 7 Logic, shift and rotate instructions
 
Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...
 
Assembly language programming_fundamentals 8086
Assembly language programming_fundamentals 8086Assembly language programming_fundamentals 8086
Assembly language programming_fundamentals 8086
 
Alp 05
Alp 05Alp 05
Alp 05
 
push down automata
push down automatapush down automata
push down automata
 
Chap 8 The stack and introduction to procedures & Chapter 9 multiplication an...
Chap 8 The stack and introduction to procedures & Chapter 9 multiplication an...Chap 8 The stack and introduction to procedures & Chapter 9 multiplication an...
Chap 8 The stack and introduction to procedures & Chapter 9 multiplication an...
 
Introduction to ibm pc assembly language
Introduction to ibm pc assembly languageIntroduction to ibm pc assembly language
Introduction to ibm pc assembly language
 
Lecture5(1)
Lecture5(1)Lecture5(1)
Lecture5(1)
 
Pda
PdaPda
Pda
 
Automata Theory - Turing machine
Automata Theory - Turing machineAutomata Theory - Turing machine
Automata Theory - Turing machine
 

Semelhante a Chapt 06

Semelhante a Chapt 06 (20)

Al2ed chapter7
Al2ed chapter7Al2ed chapter7
Al2ed chapter7
 
Lecture no 15
Lecture no 15Lecture no 15
Lecture no 15
 
X86 operation types
X86 operation typesX86 operation types
X86 operation types
 
[ASM]Lab5
[ASM]Lab5[ASM]Lab5
[ASM]Lab5
 
[ASM]Lab4
[ASM]Lab4[ASM]Lab4
[ASM]Lab4
 
Assembly language.pptx
Assembly language.pptxAssembly language.pptx
Assembly language.pptx
 
String_manipulations.pdf
String_manipulations.pdfString_manipulations.pdf
String_manipulations.pdf
 
Computer Architecture Assignment Help
Computer Architecture Assignment HelpComputer Architecture Assignment Help
Computer Architecture Assignment Help
 
Al2ed chapter8
Al2ed chapter8Al2ed chapter8
Al2ed chapter8
 
Assembly language programs
Assembly language programsAssembly language programs
Assembly language programs
 
Loop instruction, controlling the flow of progam
Loop instruction, controlling the flow of progamLoop instruction, controlling the flow of progam
Loop instruction, controlling the flow of progam
 
8051 data type and directives
8051 data type and directives8051 data type and directives
8051 data type and directives
 
8051 data types and directives
8051 data types and directives8051 data types and directives
8051 data types and directives
 
COMPILER_DESIGN_CLASS 2.ppt
COMPILER_DESIGN_CLASS 2.pptCOMPILER_DESIGN_CLASS 2.ppt
COMPILER_DESIGN_CLASS 2.ppt
 
COMPILER_DESIGN_CLASS 1.pptx
COMPILER_DESIGN_CLASS 1.pptxCOMPILER_DESIGN_CLASS 1.pptx
COMPILER_DESIGN_CLASS 1.pptx
 
Arm Cortex material Arm Cortex material3222886.ppt
Arm Cortex material Arm Cortex material3222886.pptArm Cortex material Arm Cortex material3222886.ppt
Arm Cortex material Arm Cortex material3222886.ppt
 
Reversing malware analysis training part4 assembly programming basics
Reversing malware analysis training part4 assembly programming basicsReversing malware analysis training part4 assembly programming basics
Reversing malware analysis training part4 assembly programming basics
 
Al2ed chapter9
Al2ed chapter9Al2ed chapter9
Al2ed chapter9
 
Control structures pyhton
Control structures  pyhtonControl structures  pyhton
Control structures pyhton
 
Reversing & Malware Analysis Training Part 4 - Assembly Programming Basics
Reversing & Malware Analysis Training Part 4 - Assembly Programming BasicsReversing & Malware Analysis Training Part 4 - Assembly Programming Basics
Reversing & Malware Analysis Training Part 4 - Assembly Programming Basics
 

Último

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Último (20)

Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 

Chapt 06

  • 1. Assembly Language for Intel-Based Computers, 5 th Edition Chapter 6: Conditional Processing (c) Pearson Education, 2006-2007. 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 R. Irvine
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22. Jumps Based on Specific Flags
  • 23. Jumps Based on Equality
  • 24. Jumps Based on Unsigned Comparisons
  • 25. Jumps Based on Signed Comparisons
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48. Compound Expression with AND (2 of 3) cmp al,bl ; first expression... ja L1 jmp next L1: cmp bl,cl ; second expression... ja L2 jmp next L2: ; both are true mov X,1 ; set X to 1 next: if (al > bl) AND (bl > cl) X = 1; This is one possible implementation . . .
  • 49. Compound Expression with AND (3 of 3) 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: if (al > bl) AND (bl > cl) X = 1; But the following implementation uses 29% less code by reversing the first relational operator. We allow the program to &quot;fall through&quot; to the second expression:
  • 50.
  • 51.
  • 52. Compound Expression with OR (1 of 2) cmp al,bl ; is AL > BL? ja L1 ; yes cmp bl,cl ; no: is BL > CL? jbe next ; no: skip next statement L1: mov X,1 ; set X to 1 next: if (al > bl) OR (bl > cl) X = 1; We can use &quot;fall-through&quot; logic to keep the code as short as possible:
  • 53. WHILE Loops while( eax < ebx) eax = eax + 1; 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. Consider the following example: top: cmp eax,ebx ; check loop condition jae next ; false? exit loop inc eax ; body of loop jmp top ; repeat the loop next: This is a possible implementation:
  • 54. Your turn . . . top: cmp ebx,val1 ; check loop condition ja next ; false? exit loop add ebx,5 ; body of loop dec val1 jmp top ; repeat the loop next: while( ebx <= val1) { ebx = ebx + 5; val1 = val1 - 1 } Implement the following loop, using unsigned 32-bit integers:
  • 55.
  • 56. Table-Driven Selection (2 of 3) .data CaseTable BYTE 'A' ; lookup value DWORD Process_A ; address of procedure EntrySize = ($ - CaseTable) BYTE 'B' DWORD Process_B BYTE 'C' DWORD Process_C BYTE 'D' DWORD Process_D NumberOfEntries = ($ - CaseTable) / EntrySize Step 1: create a table containing lookup values and procedure offsets:
  • 57. Table-Driven Selection (3 of 3) mov ebx,OFFSET CaseTable ; point EBX to the table mov ecx,NumberOfEntries ; loop counter L1: cmp al,[ebx] ; match found? jne L2 ; no: continue call NEAR PTR [ebx + 1] ; yes: call the procedure jmp L3 ; and exit the loop L2: add ebx,EntrySize ; point to next entry loop L1 ; repeat until ECX = 0 L3: Step 2: Use a loop to search the table. When a match is found, we call the procedure offset stored in the current table entry: required for procedure pointers
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63. Implementing an FSM StateA: call Getnext ; read next char into AL cmp al,'+' ; leading + sign? je StateB ; go to State B cmp al,'-' ; leading - sign? je StateB ; go to State B call IsDigit ; ZF = 1 if AL = digit jz StateC ; go to State C call DisplayErrorMsg ; invalid input found jmp Quit The following is code from State A in the Integer FSM: View the Finite.asm source code .
  • 64. IsDigit Procedure IsDigit PROC cmp al,'0' ; ZF = 0 jb ID1 cmp al,'9' ; ZF = 0 ja ID1 test ax,0 ; ZF = 1 ID1: ret IsDigit ENDP Receives a character in AL. Sets the Zero flag if the character is a decimal digit.
  • 65. Flowchart of State A State A accepts a plus or minus sign, or a decimal digit.
  • 66.
  • 67.
  • 68.
  • 69.
  • 71. MASM-Generated Code mov eax,6 cmp eax,val1 jbe @C0001 mov result,1 @C0001: .data val1 DWORD 5 result DWORD ? .code mov eax,6 .IF eax > val1 mov result,1 .ENDIF Generated code: MASM automatically generates an unsigned jump (JBE) because val1 is unsigned.
  • 72. MASM-Generated Code mov eax,6 cmp eax,val1 jle @C0001 mov result,1 @C0001: .data val1 SDWORD 5 result SDWORD ? .code mov eax,6 .IF eax > val1 mov result,1 .ENDIF Generated code: MASM automatically generates a signed jump (JLE) because val1 is signed.
  • 73. MASM-Generated Code mov ebx,5 mov eax,6 cmp eax,ebx jbe @C0001 mov result,1 @C0001: .data result DWORD ? .code mov ebx,5 mov eax,6 .IF eax > ebx mov result,1 .ENDIF Generated code: MASM automatically generates an unsigned jump (JBE) when both operands are registers . . .
  • 74. MASM-Generated Code mov ebx,5 mov eax,6 cmp eax,ebx jle @C0001 mov result,1 @C0001: .data result SDWORD ? .code mov ebx,5 mov eax,6 .IF SDWORD PTR eax > ebx mov result,1 .ENDIF Generated code: . . . unless you prefix one of the register operands with the SDWORD PTR operator. Then a signed jump is generated.
  • 75. .REPEAT Directive ; Display integers 1 – 10: mov eax,0 .REPEAT inc eax call WriteDec call Crlf .UNTIL eax == 10 Executes the loop body before testing the loop condition associated with the .UNTIL directive. Example:
  • 76. .WHILE Directive ; Display integers 1 – 10: mov eax,0 .WHILE eax < 10 inc eax call WriteDec call Crlf .ENDW Tests the loop condition before executing the loop body The .ENDW directive marks the end of the loop. Example:
  • 77.