lec_3 [compatibility mode]

Upload: rakhi-manglani

Post on 07-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/6/2019 Lec_3 [Compatibility Mode]

    1/27

    Assembly Language

    Conditional Processing

    Lec-3

    1Rakhi Budhrani (lecturer SSCCS)

  • 8/6/2019 Lec_3 [Compatibility Mode]

    2/27

    Overview

    Flags

    Conditional Jumps

    Conditional Loop Instructions Conditional Structures

    2Rakhi Budhrani (lecturer SSCCS)

  • 8/6/2019 Lec_3 [Compatibility Mode]

    3/27

    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 generatesan invalid signed result.

    3Rakhi Budhrani (lecturer SSCCS)

  • 8/6/2019 Lec_3 [Compatibility Mode]

    4/27

    CMP Instruction (1 of 3) Compares the destination operand to the source operand

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

    Syntax: CMP destination, source

    Example: destination == source

    mo v a l , 5c mp a l , 5 ; Z e r o f l a g s e t

    Example: destination < source

    mo v a l , 4c mp a l , 5 ; Ca r r y f l a g s e t

    4Rakhi Budhrani (lecturer SSCCS)

  • 8/6/2019 Lec_3 [Compatibility Mode]

    5/27

    CMP Instruction (2 of 3) Example: destination > source

    mo v a l , 6c mp a l , 5 ; Z F = 0 , CF = 0

    (both the Zero and Carry flags are clear)

    5Rakhi Budhrani (lecturer SSCCS)

  • 8/6/2019 Lec_3 [Compatibility Mode]

    6/27

    CMP Instruction (3 of 3)

    Example: destination > source

    mo v a l , 5c mp a l , - 2 ; S i g n f l a g = = Ov e r f l o w f l a g

    The comparisons shown here are performed with signed integers.

    Example: destination < source

    mo v a l , - 1

    c mp a l , 5 ; S i g n f l a g ! = Ov e r f l o w f l a g

    6Rakhi Budhrani (lecturer SSCCS)

  • 8/6/2019 Lec_3 [Compatibility Mode]

    7/27

    Conditional Jumps Jumps Based On . . .

    Specific flags

    Equality

    Unsigned comparisonsSigned Comparisons

    Applications

    7Rakhi Budhrani (lecturer SSCCS)

  • 8/6/2019 Lec_3 [Compatibility Mode]

    8/27

    8

    JcondInstruction 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 0Rakhi Budhrani (lecturer SSCCS)

  • 8/6/2019 Lec_3 [Compatibility Mode]

    9/27

    Jumps Based on Specific Flags

    9Rakhi Budhrani (lecturer SSCCS)

  • 8/6/2019 Lec_3 [Compatibility Mode]

    10/27

    Jumps Based on Equality

    10Rakhi Budhrani (lecturer SSCCS)

  • 8/6/2019 Lec_3 [Compatibility Mode]

    11/27

    Jumps Based on Unsigned

    Comparisons

    11Rakhi Budhrani (lecturer SSCCS)

  • 8/6/2019 Lec_3 [Compatibility Mode]

    12/27

    Jumps Based on Signed

    Comparisons

    12Rakhi Budhrani (lecturer SSCCS)

  • 8/6/2019 Lec_3 [Compatibility Mode]

    13/27

    Conditional Structures

    Block-Structured IF Statements

    Compound Expressions with AND

    Compound Expressions with OR WHILE Loops

    Switch Selection

    13Rakhi Budhrani (lecturer SSCCS)

  • 8/6/2019 Lec_3 [Compatibility Mode]

    14/27

    Your turn . . .Implement the following pseudocode in

    assembly language. All values are unsigned:

    Cmp b x , c x

    j a n e x tMo v a x , 5Mo v d x , 6

    n e x t :

    I f ( b x < = c x )

    {

    a x = 5 ;

    d x = 6 ;

    }

    14Rakhi Budhrani (lecturer SSCCS)

  • 8/6/2019 Lec_3 [Compatibility Mode]

    15/27

    Block-Structured IF StatementsAssembly language programmers can easily translate logical statements

    written in C++/Java into assembly language. For example:

    mo v a x , o p 1

    c mp a x , o p 2 j n e L 1mo v X , 1

    j mp L 2L 1 : mo v X, 2L 2 :

    i f ( o p 1 = = o p 2 )

    X = 1 ;

    e l s e

    X = 2 ;

    15Rakhi Budhrani (lecturer SSCCS)

  • 8/6/2019 Lec_3 [Compatibility Mode]

    16/27

    Your turn . . .Implement the following pseudocode in

    assembly language

    mo v a x , v a r 1c mp a x , v a r 2

    j l e L 1mo v v a r 3 , 6

    mo v v a r 4 , 7 j mp L 2

    L 1 : mo v v a r 3 , 1 0L 2 :

    i f ( v a r 1 < = v a r 2 )

    v a r 3 = 1 0 ;

    e l s e

    {v a r 3 = 6 ;

    v a r 4 = 7 ;

    }

    16Rakhi Budhrani (lecturer SSCCS)

  • 8/6/2019 Lec_3 [Compatibility Mode]

    17/27

    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:

    i f ( a l > b l ) A ND ( b l > c l )

    X = 1 ;

    17Rakhi Budhrani (lecturer SSCCS)

  • 8/6/2019 Lec_3 [Compatibility Mode]

    18/27

    Compound Expression with AND (2 of 3)

    c mp a l , b l ; f i r s t e x p r e s s i o n . . . j a L 1 j mp n e x t

    L 1 :c mp b l , c l ; s e c o n d e x p r e s s i o n . . .

    j a L 2

    j mp n e x tL 2 : ; b o t h a r e t r u e

    mo v X , 1 ; s e t X t o 1n e x t :

    i f ( a l > b l ) A ND ( b l > c l )

    X = 1 ;

    This is one possible implementation . . .

    18Rakhi Budhrani (lecturer SSCCS)

  • 8/6/2019 Lec_3 [Compatibility Mode]

    19/27

    Compound Expression with AND (3 of 3)

    c mp a l , b l ; f i r s t e x p r e s s i o n . . .j b e n e x t ; q u i t i f f a l s ec mp b l , c l ; s e c o n d e x p r e s s i o n . . .

    j b e n e x t ; q u i t i f f a l s emo v X , 1 ; b o t h a r e t r u e

    n e x t :

    i f ( a l > b l ) A ND ( b l > c l )

    X = 1 ;

    But the following implementation uses 29% less code by reversing the

    first relational operator. We allow the program to "fall through" to thesecond expression:

    19Rakhi Budhrani (lecturer SSCCS)

  • 8/6/2019 Lec_3 [Compatibility Mode]

    20/27

  • 8/6/2019 Lec_3 [Compatibility Mode]

    21/27

    Rakhi Budhrani (lecturer

    SSCCS)21

    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:

    i f ( a l > b l ) OR ( b l > c l )

    X = 1 ;

  • 8/6/2019 Lec_3 [Compatibility Mode]

    22/27

    Compound Expression with OR (1 of 2)

    c mp a l , b l ; i s A L > B L ?j a L 1 ; y e sc mp b l , c l ; n o : i s B L > CL ?

    j b e n e x t ; n o : s k i p n e x t s t a t e me n t

    L 1 : mo v X , 1 ; s e t X t o 1n e x t :

    i f ( a l > b l ) OR ( b l > c l )

    X = 1 ;

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

    22Rakhi Budhrani (lecturer SSCCS)

  • 8/6/2019 Lec_3 [Compatibility Mode]

    23/27

    Switch SelectionCMP BL, 30H ; compare input digit and 0

    JL @NEGATIVE ; jump to label @NEGATIVE if digit0

    @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

    Rakhi Budhrani (lecturer SSCCS) 23

  • 8/6/2019 Lec_3 [Compatibility Mode]

    24/27

  • 8/6/2019 Lec_3 [Compatibility Mode]

    25/27

    Your turn . . .

    t o p : c mp b x , v a l 1 ; c h e c k l o o p c o n d i t i o nj a n e x t ; f a l s e ? e x i t l o o pa d d b x , 5 ; b o d y o f l o o pd e c v a l 1

    j mp t o p ; r e p e a t t h e l o o pn e x t :

    wh i l e ( b x < = v a l 1 )

    {

    b x = b x + 5 ;

    v a l 1 = v a l 1 - 1

    }

    Implement the following loop

    25Rakhi Budhrani (lecturer SSCCS)

  • 8/6/2019 Lec_3 [Compatibility Mode]

    26/27

    Operations: Branches Conditional branches:

    These must be preceded by aninstruction which sets at least

    one status flag (this includes

    cmp operations)

    the flag tested is based onwhich branch is used

    je/jne location

    branch if zero flag set/clear

    jg/jge/jl/jle location

    jump on > (positive flag set),>=, < (negative flag set),

  • 8/6/2019 Lec_3 [Compatibility Mode]

    27/27

    DOS Function Calls (Using Int 21h)

    AH = 01H : For reading a character from keyboard . Theinput value is put in AL register. (read.asm)

    AH = 02 H : This prints 8 bit data that is stored in DLregister on the screen (e02.asm)

    AH = 08H : This is an input function for inputting onecharacter. (dispstr.asm)

    AH = 09H : This program outputs a string whose offset isstored in DX register and that is terminated using a $

    character.

    AH = 0AH : for input of string up to 255 characters.

    (Loop.asm)

    27Rakhi Budhrani (lecturer SSCCS)