You are on page 1of 27

Assembly Language

Conditional Processing
Lec-3

Rakhi Budhrani (lecturer SSCCS)

Overview
Flags Conditional Jumps Conditional Loop Instructions Conditional Structures

Rakhi Budhrani (lecturer SSCCS)

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.

Rakhi Budhrani (lecturer SSCCS)

CMP Instruction
Syntax: CMP destination, source Example: destination == source
mov al,5 cmp al,5

(1 of 3)

Compares the destination operand to the source operand


Nondestructive subtraction of source from destination (destination operand is not changed)

; Zero flag set

Example: destination < source


mov al,4 cmp al,5

; Carry flag set

Rakhi Budhrani (lecturer SSCCS)

CMP Instruction
Example: destination > source
mov al,6 cmp al,5

(2 of 3)

; ZF = 0, CF = 0

(both the Zero and Carry flags are clear)

Rakhi Budhrani (lecturer SSCCS)

CMP Instruction
Example: destination > source
mov al,5 cmp al,-2

(3 of 3)

The comparisons shown here are performed with signed integers.

; Sign flag == Overflow flag

Example: destination < source


mov al,-1 cmp al,5 ; Sign flag != Overflow flag

Rakhi Budhrani (lecturer SSCCS)

Conditional Jumps
Jumps Based On . . .
Specific flags Equality Unsigned comparisons Signed Comparisons

Applications

Rakhi Budhrani (lecturer SSCCS)

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 JcxZ jumps to a label if cx equals 0
Rakhi Budhrani 8 (lecturer SSCCS)

Jumps Based on Specific Flags

Rakhi Budhrani (lecturer SSCCS)

Jumps Based on Equality

Rakhi Budhrani (lecturer SSCCS)

10

Jumps Based on Unsigned Comparisons

Rakhi Budhrani (lecturer SSCCS)

11

Jumps Based on Signed Comparisons

Rakhi Budhrani (lecturer SSCCS)

12

Conditional Structures
Block-Structured IF Statements Compound Expressions with AND Compound Expressions with OR WHILE Loops Switch Selection

Rakhi Budhrani (lecturer SSCCS)

13

Your turn . . .
Implement the following pseudocode in assembly language. All values are unsigned:
If( bx <= cx ) { ax = 5; dx = 6; } Cmp ja Mov Mov next: bx,cx next ax,5 dx,6

Rakhi Budhrani (lecturer SSCCS)

14

Block-Structured IF Statements
Assembly language programmers can easily translate logical statements written in C++/Java into assembly language. For example:

if( op1 == op2 ) X = 1; else X = 2;

mov cmp jne mov jmp L1: mov L2:

ax,op1 ax,op2 L1 X,1 L2 X,2

Rakhi Budhrani (lecturer SSCCS)

15

Your turn . . .
Implement the following pseudocode in assembly language

if( var1 var3 = else { var3 = var4 = }

<= var2 ) 10;

6; 7;

mov cmp jle mov mov jmp L1: mov L2:

ax,var1 ax,var2 L1 var3,6 var4,7 L2 var3,10

Rakhi Budhrani (lecturer SSCCS)

16

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;

Rakhi Budhrani (lecturer SSCCS)

17

Compound Expression with AND (2 of 3)


if (al > bl) AND (bl > cl) X = 1; This is one possible implementation . . . cmp al,bl ja L1 jmp next L1: cmp bl,cl ja L2 jmp next L2: mov X,1 next: ; second expression... ; first expression...

; both are true ; set X to 1

Rakhi Budhrani (lecturer SSCCS)

18

Compound Expression with AND (3 of 3)


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 "fall through" to the second expression: cmp jbe cmp jbe mov next: al,bl next bl,cl next X,1 ; ; ; ; ; first expression... quit if false second expression... quit if false both are true

Rakhi Budhrani (lecturer SSCCS)

19

Your turn . . .
Implement the following pseudocode in assembly language. All values are unsigned:
if( bx <= cx && cx > dx ) { ax = 5; dx = 6; } cmp ja cmp jbe mov mov next: bx,cx next cx,dx next ax,5 dx,6

Rakhi Budhrani (lecturer SSCCS)

20

Compound Expression with OR (1 of 2)


When implementing the logical OR operator, consider that HLLs use shortcircuit evaluation In the following example, if the first expression is true, the second expression is skipped:

if (al > bl) OR (bl > cl) X = 1;

Rakhi Budhrani (lecturer SSCCS)

21

Compound Expression with OR (1 of 2)


if (al > bl) OR (bl > cl) X = 1;

We can use "fall-through" logic to keep the code as short as possible:

cmp ja cmp jbe L1: mov next:

al,bl L1 bl,cl next X,1

; ; ; ; ;

is AL > BL? yes no: is BL > CL? no: skip next statement set X to 1

Rakhi Budhrani (lecturer SSCCS)

22

Switch Selection
CMP BL, 30H JL @NEGATIVE JZ @ZERO JG @POSITIVE @NEGATIVE: MOV DL, 'N JMP @DISPLAY @ZERO: MOV DL, 'Z JMP @DISPLAY @POSITIVE: MOV DL, 'P JMP @DISPLAY @DISPLAY: MOV AH, 2 INT 21H ; compare input digit and 0 ; jump to label @NEGATIVE if digit<0 ; jump to label @ZERO if digit=0 ; jump to label @POSITIVE if digit>0 ; jump label ; jump to label @DISPLAY ; jump label ; jump to label @DISPLAY ; jump label ; jump to label @DISPLAY ; jump label ; print the character

Rakhi Budhrani (lecturer SSCCS)

23

WHILE Loops
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:

while( ax < bx) ax = ax + 1; This is a possible implementation: top: cmp jae inc jmp next: ax,bx next ax top ; ; ; ; check loop condition false? exit loop body of loop repeat the loop

Rakhi Budhrani (lecturer SSCCS)

24

Your turn . . .
Implement the following loop while( bx <= val1) { bx = bx + 5; val1 = val1 - 1 }

top: cmp ja add dec jmp next:

bx,val1 next bx,5 val1 top

; check loop condition ; false? exit loop ; body of loop ; repeat the loop

Rakhi Budhrani (lecturer SSCCS)

25

Operations: Branches
Conditional branches:
These must be preceded by an instruction which sets at least one status flag (this includes cmp operations)
the flag tested is based on which branch is used

Unconditional branches do not use a previous comparison or flag, just branch to given location
jmp location
jmp instructions are used to implement goto statements and procedure calls

je/jne location
branch if zero flag set/clear

jg/jge/jl/jle location
jump on > (positive flag set), >=, < (negative flag set), <=

loop location
decrement cx (or ecx) if cx (ecx) != 0 then branch to label location
used for for-loops since cx (or ecx) is used implicitly here, inside such a loop structure, we cannot use cx/ecx as a data register!
26

jc/jnc/jz/jnz/jp/jnp location
Jump on carry, no carry, zero, not zero, even parity, not even parity (odd parity)

Rakhi Budhrani (lecturer SSCCS)

DOS Function Calls (Using Int 21h)


AH = 01H : For reading a character from keyboard . The input value is put in AL register. (read.asm) AH = 02 H : This prints 8 bit data that is stored in DL register on the screen (e02.asm) AH = 08H : This is an input function for inputting one character. (dispstr.asm) AH = 09H : This program outputs a string whose offset is stored in DX register and that is terminated using a $ character. AH = 0AH : for input of string up to 255 characters. (Loop.asm)
Rakhi Budhrani (lecturer SSCCS) 27

You might also like