SlideShare a Scribd company logo
Assembly Language Fundamentals of Assembly language Conditional Processing Motaz K. Saad Spring 2007 Motaz K. Saad, Dept. of CS
Overview Boolean and Comparison Instructions Conditional Jumps Conditional Loop Instructions Conditional Structures Application: Finite-State Machines Decision Directives Motaz K. Saad, Dept. of CS
Status Flags - Review The  Zero  flag is set when the result of an operation equals zero. The  Carry  flag is set when an instruction generates a result that is too large for the destination operand. The  Sign  flag is set if the destination operand is negative, and it is clear if the destination operand is positive. The  Overflow  flag is set when an instruction generates an invalid signed result. Motaz K. Saad, Dept. of CS
AND Instruction Performs a Boolean AND operation between each pair of matching bits in two operands Syntax: AND  destination, source (same operand types as MOV) AND Motaz K. Saad, Dept. of CS
OR Instruction Performs a Boolean OR operation between each pair of matching bits in two operands Syntax: OR  destination, source OR Motaz K. Saad, Dept. of CS
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. Motaz K. Saad, Dept. of CS
NOT Instruction Performs a Boolean NOT operation on a single destination operand Syntax: NOT  destination NOT Motaz K. Saad, Dept. of CS
TEST Instruction Performs a nondestructive AND operation 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,00000011b jnz  ValueFound Example: jump to a label if neither bit 0 nor bit 1 in AL is set. test al,00000011b jz  ValueNotFound Motaz K. Saad, Dept. of CS
CMP Instruction  (1 of 3) Compares the destination operand to the source operand Nondestructive subtraction of source from destination (destination operand is not changed) Syntax:  CMP  destination, source Example: destination == source mov al,5 cmp al,5 ; Zero flag set Motaz K. Saad, Dept. of CS Example: destination < source mov al,4 cmp al,5 ; Carry flag set
CMP Instruction  (2 of 3) Example: destination > source mov al,6 cmp al,5 ; ZF = 0, CF = 0 (both the Zero and Carry flags are clear) Motaz K. Saad, Dept. of CS
CMP Instruction  (3 of 3) Example: destination > source mov al,5 cmp al,-2 ; Sign flag == Overflow flag The comparisons shown here are performed with signed integers. Motaz K. Saad, Dept. of CS Example: destination < source mov al,-1 cmp al,5 ; Sign flag != Overflow flag
Conditional Jumps Jumps Based On . . . Specific flags Equality Unsigned comparisons Signed Comparisons Applications Motaz K. Saad, Dept. of CS
J cond  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 JcxZ jumps to a label if cx equals 0 Motaz K. Saad, Dept. of CS
J cond  Ranges 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 memory Motaz K. Saad, Dept. of CS
Jumps Based on Specific Flags Motaz K. Saad, Dept. of CS
Jumps Based on Equality Motaz K. Saad, Dept. of CS
Jumps Based on Unsigned Comparisons Motaz K. Saad, Dept. of CS
Jumps Based on Signed Comparisons Motaz K. Saad, Dept. of CS
Conditional Structures Block-Structured IF Statements Compound Expressions with AND Compound Expressions with OR WHILE Loops Switch Selection Motaz K. Saad, Dept. of CS
Your turn . . . Implement the following pseudocode in assembly language. All values are unsigned: Cmp bx,cx ja  next Mov ax,5 Mov dx,6 next: If( bx <= cx ) { ax = 5; dx = 6; } Motaz K. Saad, Dept. of CS
Block-Structured IF Statements Assembly language programmers can easily translate logical statements written in C++/Java into assembly language. For example: mov ax,op1 cmp ax,op2 jne L1 mov X,1 jmp L2 L1: mov X,2 L2: if( op1 == op2 ) X = 1; else X = 2; Motaz K. Saad, Dept. of CS
Your turn . . . Implement the following pseudocode in assembly language mov ax,var1 cmp ax,var2 jle L1 mov var3,6 mov var4,7 jmp L2 L1: mov var3,10 L2: if( var1 <= var2 ) var3 = 10; else { var3 = 6; var4 = 7; } Motaz K. Saad, Dept. of CS
Compound Expression with AND   (1 of 3) When implementing the logical AND operator, consider that HLLs use short-circuit evaluation In the following example, if the first expression is false, the second expression is skipped: if (al > bl) AND (bl > cl) X = 1; Motaz K. Saad, Dept. of CS
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 . . . Motaz K. Saad, Dept. of CS
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: Motaz K. Saad, Dept. of CS
Your turn . . . Implement the following pseudocode in assembly language. All values are unsigned: cmp bx,cx ja  next cmp cx,dx jbe next mov ax,5 mov dx,6 next: if( bx <= cx  && cx > dx ) { ax = 5; dx = 6; } Motaz K. Saad, Dept. of CS
Compound Expression with OR   (1 of 2) When implementing the logical OR operator, consider that HLLs use short-circuit evaluation In the following example, if the first expression is true, the second expression is skipped: Motaz K. Saad, Dept. of CS 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: Motaz K. Saad, Dept. of CS
Switch Selection CMP BL, 30H  ; compare input digit and 0 JL @NEGATIVE  ; jump to label @NEGATIVE if digit<0 JZ @ZERO  ; jump to label @ZERO if digit=0 JG @POSITIVE  ; jump to label @POSITIVE if digit>0 @NEGATIVE:  ; jump label MOV DL, 'N‘ JMP @DISPLAY  ; jump to label @DISPLAY @ZERO:  ; jump label MOV DL, 'Z‘ JMP @DISPLAY  ; jump to label @DISPLAY @POSITIVE:  ; jump label MOV DL, 'P‘ JMP @DISPLAY  ; jump to label @DISPLAY @DISPLAY:  ; jump label MOV AH, 2  ; print the character INT 21H Motaz K. Saad, Dept. of CS
WHILE Loops while( ax < bx) ax = ax + 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: Motaz K. Saad, Dept. of CS top: cmp ax,bx ; check loop condition jae next ; false? exit loop inc ax ; body of loop jmp top ; repeat the loop next: This is a possible implementation:
Your turn . . . top: cmp bx,val1 ; check loop condition ja  next ; false? exit loop add bx,5 ; body of loop dec val1 jmp top ; repeat the loop next: while( bx <= val1) { bx = bx + 5; val1 = val1 - 1 } Implement the following loop Motaz K. Saad, Dept. of CS
Decision Directive Using the .IF Directive Runtime Expressions Relational and Logical Operators MASM-Generated Code .REPEAT Directive .WHILE Directive Motaz K. Saad, Dept. of CS
Runtime Expressions .IF ax > bx mov dx,1 .ELSE mov dx,2 .ENDIF .IF, .ELSE, .ELSEIF, and .ENDIF can be used to evaluate runtime expressions and create block-structured IF statements. Examples: MASM generates &quot;hidden&quot; code for you, consisting of code labels, CMP and conditional jump instructions. .IF ax > bx && ax > cx mov dx,1 .ELSE mov dx,2 .ENDIF Motaz K. Saad, Dept. of CS
Relational and Logical Operators Motaz K. Saad, Dept. of CS
MASM-Generated Code mov ax,6 cmp ax,val1 jbe @C0001  mov result,1 @C0001: .data val1  DWORD 5 result DWORD ? .code mov ax,6 .IF ax > val1 mov result,1 .ENDIF Generated code: MASM automatically generates an unsigned jump (JBE) because  val1  is unsigned. Motaz K. Saad, Dept. of CS
MASM-Generated Code mov ax,6 cmp ax,val1 jle @C0001  mov result,1 @C0001: .data val1  SDWORD  5 result SDWORD ? .code mov ax,6 .IF ax > val1 mov result,1 .ENDIF Generated code: MASM automatically generates a signed jump (JLE) because  val1  is signed. Motaz K. Saad, Dept. of CS
MASM-Generated Code mov bx,5 mov ax,6 cmp ax,bx jbe @C0001  mov result,1 @C0001: .data result DWORD ? .code mov bx,5 mov ax,6 .IF ax > bx mov result,1 .ENDIF Generated code: MASM automatically generates an unsigned jump (JBE) when both operands are registers . . . Motaz K. Saad, Dept. of CS
MASM-Generated Code mov bx,5 mov ax,6 cmp ax,bx jle @C0001  mov result,1 @C0001: .data result SDWORD ? .code mov bx,5 mov ax,6 .IF SDWORD PTR ax > bx 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. Motaz K. Saad, Dept. of CS
.REPEAT Directive ; Display integers 1 – 10: mov ax,0 .REPEAT inc ax call WriteDec call Crlf .UNTIL ax == 10 Executes the loop body before testing the loop condition associated with the .UNTIL directive.  Example: Motaz K. Saad, Dept. of CS
.WHILE Directive ; Display integers 1 – 10: mov ax,0 .WHILE ax < 10 inc ax call WriteDec call Crlf .ENDW Tests the loop condition before executing the loop body The .ENDW directive marks the end of the loop.  Example: Motaz K. Saad, Dept. of CS

More Related Content

PPTX
Flag Registers (Assembly Language)
PDF
Chapter 5The proessor status and the FLAGS registers
PDF
Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...
PPTX
Input output interface
PDF
chapter 7 Logic, shift and rotate instructions
PDF
Chap 8 The stack and introduction to procedures & Chapter 9 multiplication an...
PPTX
Loop instruction, controlling the flow of progam
PPTX
Input Output Organization
Flag Registers (Assembly Language)
Chapter 5The proessor status and the FLAGS registers
Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...
Input output interface
chapter 7 Logic, shift and rotate instructions
Chap 8 The stack and introduction to procedures & Chapter 9 multiplication an...
Loop instruction, controlling the flow of progam
Input Output Organization

What's hot (20)

PPTX
assembly flag resister
PDF
Instruction formats-in-8086
PDF
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
PDF
Chapter 6 Flow control Instructions
PPTX
Instruction sets of 8086
PDF
Assembly Language Programming By Ytha Yu, Charles Marut Chap 7 (Logic, Shift,...
PPT
Bus system
PPTX
Stack and its usage in assembly language
PPTX
Microprocessor 8086 instructions
PPTX
Moore and mealy machine
PPTX
Hardwired control
PPTX
bus and memory tranfer (computer organaization)
PPTX
Syntax Analysis - LR(0) Parsing in Compiler
PPTX
Register & flags
PPTX
Mealy and moore machine
PDF
Assembly Language Programming By Ytha Yu, Charles Marut Chap 6 (Flow Control ...
PDF
8086 String Instructions.pdf
PPTX
General register organization (computer organization)
PPT
Types of instructions
PPT
Assembly language programming_fundamentals 8086
assembly flag resister
Instruction formats-in-8086
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
Chapter 6 Flow control Instructions
Instruction sets of 8086
Assembly Language Programming By Ytha Yu, Charles Marut Chap 7 (Logic, Shift,...
Bus system
Stack and its usage in assembly language
Microprocessor 8086 instructions
Moore and mealy machine
Hardwired control
bus and memory tranfer (computer organaization)
Syntax Analysis - LR(0) Parsing in Compiler
Register & flags
Mealy and moore machine
Assembly Language Programming By Ytha Yu, Charles Marut Chap 6 (Flow Control ...
8086 String Instructions.pdf
General register organization (computer organization)
Types of instructions
Assembly language programming_fundamentals 8086
Ad

Viewers also liked (19)

PDF
Cross Language Concept Mining
PDF
مقدمة في تكنواوجيا المعلومات
PPT
3.7 outlier analysis
PDF
Browsing The Source Code of Linux Packages
PDF
Hewahi, saad 2006 - class outliers mining distance-based approach
PPT
Knowledge discovery thru data mining
PDF
Open Source Business Models
PDF
Class Outlier Mining
PPT
The x86 Family
PPT
Intel 64bit Architecture
PDF
Browsing Linux Kernel Source
PPT
OS Lab: Introduction to Linux
PPTX
Data Mining: Outlier analysis
PPT
Assembly Language Lecture 4
PDF
Data Mining and Business Intelligence Tools
PPT
Assembly Language Lecture 3
PPTX
Structured Vs, Object Oriented Analysis and Design
PPT
Data mining: Concepts and Techniques, Chapter12 outlier Analysis
PPT
Introduction to CLIPS Expert System
Cross Language Concept Mining
مقدمة في تكنواوجيا المعلومات
3.7 outlier analysis
Browsing The Source Code of Linux Packages
Hewahi, saad 2006 - class outliers mining distance-based approach
Knowledge discovery thru data mining
Open Source Business Models
Class Outlier Mining
The x86 Family
Intel 64bit Architecture
Browsing Linux Kernel Source
OS Lab: Introduction to Linux
Data Mining: Outlier analysis
Assembly Language Lecture 4
Data Mining and Business Intelligence Tools
Assembly Language Lecture 3
Structured Vs, Object Oriented Analysis and Design
Data mining: Concepts and Techniques, Chapter12 outlier Analysis
Introduction to CLIPS Expert System
Ad

Similar to Assembly Language Lecture 5 (20)

PPT
chapt5 and 06assemblylanguagecodesandmachinelanguage.ppt
PPT
Chapt 06
PPT
Chapt 06
PPTX
[ASM]Lab5
PPTX
LEC 15 BOLEAN OPERATORS in comsats university.pptx
PPTX
LEC 15 BOLEAN OPERATORS in comsats university.pptx
PPT
1344 Alp Of 8086
PDF
Assembly language 8086
PDF
Assembly language 8086 intermediate
PPTX
Assembly jmp presentation
PPTX
L12_ COA_COmputer architecurte and organization
PPT
Al2ed chapter8
PPT
Al2ed chapter7
PPTX
Assembly language programs
PPTX
lecture4 control flow instruction in assembly.pptx
PPTX
Assembly Language Compiler Implementation
PPT
Assignment on alp
PPT
Assignment on alp
PPT
Chapter6-mikroprocessor
chapt5 and 06assemblylanguagecodesandmachinelanguage.ppt
Chapt 06
Chapt 06
[ASM]Lab5
LEC 15 BOLEAN OPERATORS in comsats university.pptx
LEC 15 BOLEAN OPERATORS in comsats university.pptx
1344 Alp Of 8086
Assembly language 8086
Assembly language 8086 intermediate
Assembly jmp presentation
L12_ COA_COmputer architecurte and organization
Al2ed chapter8
Al2ed chapter7
Assembly language programs
lecture4 control flow instruction in assembly.pptx
Assembly Language Compiler Implementation
Assignment on alp
Assignment on alp
Chapter6-mikroprocessor

Recently uploaded (20)

PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
Spectroscopy.pptx food analysis technology
PPTX
A Presentation on Artificial Intelligence
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Machine Learning_overview_presentation.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Machine learning based COVID-19 study performance prediction
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PPT
Teaching material agriculture food technology
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Spectroscopy.pptx food analysis technology
A Presentation on Artificial Intelligence
Building Integrated photovoltaic BIPV_UPV.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Programs and apps: productivity, graphics, security and other tools
Machine Learning_overview_presentation.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
Review of recent advances in non-invasive hemoglobin estimation
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
MIND Revenue Release Quarter 2 2025 Press Release
Machine learning based COVID-19 study performance prediction
Encapsulation_ Review paper, used for researhc scholars
Network Security Unit 5.pdf for BCA BBA.
The Rise and Fall of 3GPP – Time for a Sabbatical?
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Teaching material agriculture food technology

Assembly Language Lecture 5

  • 1. Assembly Language Fundamentals of Assembly language Conditional Processing Motaz K. Saad Spring 2007 Motaz K. Saad, Dept. of CS
  • 2. Overview Boolean and Comparison Instructions Conditional Jumps Conditional Loop Instructions Conditional Structures Application: Finite-State Machines Decision Directives Motaz K. Saad, Dept. of CS
  • 3. Status Flags - Review The Zero flag is set when the result of an operation equals zero. The Carry flag is set when an instruction generates a result that is too large for the destination operand. The Sign flag is set if the destination operand is negative, and it is clear if the destination operand is positive. The Overflow flag is set when an instruction generates an invalid signed result. Motaz K. Saad, Dept. of CS
  • 4. AND Instruction Performs a Boolean AND operation between each pair of matching bits in two operands Syntax: AND destination, source (same operand types as MOV) AND Motaz K. Saad, Dept. of CS
  • 5. OR Instruction Performs a Boolean OR operation between each pair of matching bits in two operands Syntax: OR destination, source OR Motaz K. Saad, Dept. of CS
  • 6. 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. Motaz K. Saad, Dept. of CS
  • 7. NOT Instruction Performs a Boolean NOT operation on a single destination operand Syntax: NOT destination NOT Motaz K. Saad, Dept. of CS
  • 8. TEST Instruction Performs a nondestructive AND operation 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,00000011b jnz ValueFound Example: jump to a label if neither bit 0 nor bit 1 in AL is set. test al,00000011b jz ValueNotFound Motaz K. Saad, Dept. of CS
  • 9. CMP Instruction (1 of 3) Compares the destination operand to the source operand Nondestructive subtraction of source from destination (destination operand is not changed) Syntax: CMP destination, source Example: destination == source mov al,5 cmp al,5 ; Zero flag set Motaz K. Saad, Dept. of CS Example: destination < source mov al,4 cmp al,5 ; Carry flag set
  • 10. CMP Instruction (2 of 3) Example: destination > source mov al,6 cmp al,5 ; ZF = 0, CF = 0 (both the Zero and Carry flags are clear) Motaz K. Saad, Dept. of CS
  • 11. CMP Instruction (3 of 3) Example: destination > source mov al,5 cmp al,-2 ; Sign flag == Overflow flag The comparisons shown here are performed with signed integers. Motaz K. Saad, Dept. of CS Example: destination < source mov al,-1 cmp al,5 ; Sign flag != Overflow flag
  • 12. Conditional Jumps Jumps Based On . . . Specific flags Equality Unsigned comparisons Signed Comparisons Applications Motaz K. Saad, Dept. of CS
  • 13. J cond 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 JcxZ jumps to a label if cx equals 0 Motaz K. Saad, Dept. of CS
  • 14. J cond Ranges 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 memory Motaz K. Saad, Dept. of CS
  • 15. Jumps Based on Specific Flags Motaz K. Saad, Dept. of CS
  • 16. Jumps Based on Equality Motaz K. Saad, Dept. of CS
  • 17. Jumps Based on Unsigned Comparisons Motaz K. Saad, Dept. of CS
  • 18. Jumps Based on Signed Comparisons Motaz K. Saad, Dept. of CS
  • 19. Conditional Structures Block-Structured IF Statements Compound Expressions with AND Compound Expressions with OR WHILE Loops Switch Selection Motaz K. Saad, Dept. of CS
  • 20. Your turn . . . Implement the following pseudocode in assembly language. All values are unsigned: Cmp bx,cx ja next Mov ax,5 Mov dx,6 next: If( bx <= cx ) { ax = 5; dx = 6; } Motaz K. Saad, Dept. of CS
  • 21. Block-Structured IF Statements Assembly language programmers can easily translate logical statements written in C++/Java into assembly language. For example: mov ax,op1 cmp ax,op2 jne L1 mov X,1 jmp L2 L1: mov X,2 L2: if( op1 == op2 ) X = 1; else X = 2; Motaz K. Saad, Dept. of CS
  • 22. Your turn . . . Implement the following pseudocode in assembly language mov ax,var1 cmp ax,var2 jle L1 mov var3,6 mov var4,7 jmp L2 L1: mov var3,10 L2: if( var1 <= var2 ) var3 = 10; else { var3 = 6; var4 = 7; } Motaz K. Saad, Dept. of CS
  • 23. Compound Expression with AND (1 of 3) When implementing the logical AND operator, consider that HLLs use short-circuit evaluation In the following example, if the first expression is false, the second expression is skipped: if (al > bl) AND (bl > cl) X = 1; Motaz K. Saad, Dept. of CS
  • 24. 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 . . . Motaz K. Saad, Dept. of CS
  • 25. 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: Motaz K. Saad, Dept. of CS
  • 26. Your turn . . . Implement the following pseudocode in assembly language. All values are unsigned: cmp bx,cx ja next cmp cx,dx jbe next mov ax,5 mov dx,6 next: if( bx <= cx && cx > dx ) { ax = 5; dx = 6; } Motaz K. Saad, Dept. of CS
  • 27. Compound Expression with OR (1 of 2) When implementing the logical OR operator, consider that HLLs use short-circuit evaluation In the following example, if the first expression is true, the second expression is skipped: Motaz K. Saad, Dept. of CS if (al > bl) OR (bl > cl) X = 1;
  • 28. 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: Motaz K. Saad, Dept. of CS
  • 29. Switch Selection CMP BL, 30H ; compare input digit and 0 JL @NEGATIVE ; jump to label @NEGATIVE if digit<0 JZ @ZERO ; jump to label @ZERO if digit=0 JG @POSITIVE ; jump to label @POSITIVE if digit>0 @NEGATIVE: ; jump label MOV DL, 'N‘ JMP @DISPLAY ; jump to label @DISPLAY @ZERO: ; jump label MOV DL, 'Z‘ JMP @DISPLAY ; jump to label @DISPLAY @POSITIVE: ; jump label MOV DL, 'P‘ JMP @DISPLAY ; jump to label @DISPLAY @DISPLAY: ; jump label MOV AH, 2 ; print the character INT 21H Motaz K. Saad, Dept. of CS
  • 30. WHILE Loops while( ax < bx) ax = ax + 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: Motaz K. Saad, Dept. of CS top: cmp ax,bx ; check loop condition jae next ; false? exit loop inc ax ; body of loop jmp top ; repeat the loop next: This is a possible implementation:
  • 31. Your turn . . . top: cmp bx,val1 ; check loop condition ja next ; false? exit loop add bx,5 ; body of loop dec val1 jmp top ; repeat the loop next: while( bx <= val1) { bx = bx + 5; val1 = val1 - 1 } Implement the following loop Motaz K. Saad, Dept. of CS
  • 32. Decision Directive Using the .IF Directive Runtime Expressions Relational and Logical Operators MASM-Generated Code .REPEAT Directive .WHILE Directive Motaz K. Saad, Dept. of CS
  • 33. Runtime Expressions .IF ax > bx mov dx,1 .ELSE mov dx,2 .ENDIF .IF, .ELSE, .ELSEIF, and .ENDIF can be used to evaluate runtime expressions and create block-structured IF statements. Examples: MASM generates &quot;hidden&quot; code for you, consisting of code labels, CMP and conditional jump instructions. .IF ax > bx && ax > cx mov dx,1 .ELSE mov dx,2 .ENDIF Motaz K. Saad, Dept. of CS
  • 34. Relational and Logical Operators Motaz K. Saad, Dept. of CS
  • 35. MASM-Generated Code mov ax,6 cmp ax,val1 jbe @C0001 mov result,1 @C0001: .data val1 DWORD 5 result DWORD ? .code mov ax,6 .IF ax > val1 mov result,1 .ENDIF Generated code: MASM automatically generates an unsigned jump (JBE) because val1 is unsigned. Motaz K. Saad, Dept. of CS
  • 36. MASM-Generated Code mov ax,6 cmp ax,val1 jle @C0001 mov result,1 @C0001: .data val1 SDWORD 5 result SDWORD ? .code mov ax,6 .IF ax > val1 mov result,1 .ENDIF Generated code: MASM automatically generates a signed jump (JLE) because val1 is signed. Motaz K. Saad, Dept. of CS
  • 37. MASM-Generated Code mov bx,5 mov ax,6 cmp ax,bx jbe @C0001 mov result,1 @C0001: .data result DWORD ? .code mov bx,5 mov ax,6 .IF ax > bx mov result,1 .ENDIF Generated code: MASM automatically generates an unsigned jump (JBE) when both operands are registers . . . Motaz K. Saad, Dept. of CS
  • 38. MASM-Generated Code mov bx,5 mov ax,6 cmp ax,bx jle @C0001 mov result,1 @C0001: .data result SDWORD ? .code mov bx,5 mov ax,6 .IF SDWORD PTR ax > bx 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. Motaz K. Saad, Dept. of CS
  • 39. .REPEAT Directive ; Display integers 1 – 10: mov ax,0 .REPEAT inc ax call WriteDec call Crlf .UNTIL ax == 10 Executes the loop body before testing the loop condition associated with the .UNTIL directive. Example: Motaz K. Saad, Dept. of CS
  • 40. .WHILE Directive ; Display integers 1 – 10: mov ax,0 .WHILE ax < 10 inc ax call WriteDec call Crlf .ENDW Tests the loop condition before executing the loop body The .ENDW directive marks the end of the loop. Example: Motaz K. Saad, Dept. of CS