DOC

Chapter 5 Braching and Looping

By Floyd Harris,2014-07-01 06:47
6 views 0
Chapter 5 Braching and Looping

Chapter 5 Braching and Looping

    Introduction

    ? Master jump instructions

    ? Master loop instructions

    ? Master the method of implementing loop structures and selective

    structures

    ? Master the method of programming arrays

    Program structure

Contents

1. Jump instructions

    2. Loop instructions

    3. Implementing loop structures

    4. Application : Arrays programming

1. Jump instructions

    Jump instructions

1.1 Unconditional Jumps

    Target address>CS:EIP

    ? Statement lable : address of other assembly language statement

    ? Similar to GOTO

1.1.1 Jump direction

    ? Backward reference

    ? Transfer control to a point that precedes the jmp statement itself.

    ? Example 5.1

    ? Forward reference

    ? Transfer control to a point that is behind the jmp statement itself.

    ? Jmp quit

    Example :5.0

    JMP quit

    ……………………..

    quit: INVOKE ExitProcess , 0

    Example:5.1

    output explain ; initial instructions

     mov sum ,0 ; sum := 0

     mov ebx ,0 ; count := 0

    forever:

     output prompt ; prompt for number

     input number,16 ; read ASCII characters

     atod number ; convert to integer

add sum,eax ; add number to sum

    inc ebx ; add 1 to count

     dtoa value,ebx ; convert count to ASCII

     output countLabel ; display label for count

     output value ; display count

dtoa value ,sum ; convert sum to ASCII

    output sumLabel ; display label for sum

    output value ; display sum

mov eax,sum ; get sum

    cdq ; extend sum to 64 bits

    idiv ebx ; sum / count

    dtoa value,eax ; convert average to ASCII

    output avgLabel ; display label for average

    output value ; output average

output nextPrompt ; skip down

     ; start next prompt

    jmp forever ; repeat

1.1.2 About JMP instructions

    ? Intersegment jump : Change CS register

    ? Intersegment jump : Not change CS register

1.1.3 Relative jump/direct jump

    Contains the sign offset of the target from the JMP statement itself .

    ? Positive for a forward reference

    ? Negative for a backward reference

    ? Target addr = offset +addr of next instruction

    ? Target addr label is contained in jump instruction

    ? Example : Jmp forever

1.1.4 Offset size

1.1.5 Indirect jump

     Use a 32-bit address for the target.

     Address is stored in a register or in a memory double word.

     Example:

    Jmp edx ;edx->EIP

    Target dword 98098912h

    Jmp Target ;[target]->EIP

    Jmp DWORD PTR [ebx] ;[ebx]->EIP

1.2 Conditional Jumps

     J- target Statement ;target address->EIP

    ? Identifies the condition under which the jump is to be executed.

    ? If the condition holds, then the jump takes place;

    ? Otherwise, the next instruction is executed.

    ? Target Statement must be relative addressing

    Conditional jump instructions do not modify the flags : They only react to

    previously set flag values .

    Conditional jump instructions

    Example: 5.2

    Pseudo code

    Add value to balance ;

    If balance < 0

    Then

    ….{design for negative balance }

    Elseif balance = 0

    Then

    …..{design for zero balance}

    Else

    …..{design for positive balance}

    End if ; Code fragment

    Add balance , eax

    Ins elseifZero

    …..

    Jmp endBalanceCheck

    ElseifZero : jnz elsePos

    ….

    Jmp endBanlanceCheck

    elsePos : ……

    endBanlanceCheck :

1.2.1 Set or unset flag

1.2.2 Compare instruction

    CMP operand 1 , operand 2

     Calculating operand 1 minus operand2, like a SUB instruction

     Set flags but do not change operand 1

     Addressing mode is the same as SUB

Example: 5.3

If Structure --first

    Code fragment

    cmp ebx , 10; value <10

    jnl elseLarge

    inc smallCount

    jmp endValueCheck

    elaseLarge:

    inc lartgeCout endValueCheck: Pseudo code

    if value<10

     then

     add 1 to smallCount;

     else

     add 1 to largetCount;

     endif

If Structure second

    Pseudo code

    if (total>=100) or (count=10)

    then

     add value to total;

    endif Code fragment

    cmp total , 100; total <=100?

    jge addValue

    cmp cx, 10; count=100?

    jne endAddCheck

    addValue: mov ebx, value; copy value

    add total , ebx ; add value to total

    endAddCheck:

If Structure third

    Pseudo code

    if(count>0)and (ch=backspace)

    then

     subtract 1 from count;

    endif Code fragment

    cmp cx , 0 ; count > 0 ?

    jng endCheckCh

    cmp al , backspace ; ch a backspace ?

    jne endCheckCh

    dec count ; subtract 1 from count

    endCheckCh:

2. Loop instructions

    2.1 Loop statement label

     Statement label is the label of a statement that is a short offset from

    the loop instruction.

     ECX -1->ECX

     If ECX =0, then execute the statement following the loop

    instruction

     If ECX !=0, then a jump to the instruction at statement label takes

    place

Example:5.4

    Pseudo code

    for count : = 20 downto 1 loop

    ….{ body of loop }

    end for Code fragment

    mov ecx , 20 ; number of iterations

    forCount : …. ; body of loop

    loop forCount ; repeat body 20 times

    mov ecx , number ;number of iterations

    cmp ecx , 0

    je/jecxz endFor ; skip loop if number = 0

    forIndes : …. ; body of loop

    loop forIndes ; repeat body number times

Example:5.5

    Pseudo code

    for index : = 1 to 50 loop

    ….{ body of loop }

    end for

    Code fragment

    mov ecx , 50 ; number of iterations

    mov ebx , 1 ; index : = 1

    forCount : …… ; body of loop

     inc ebx

     loop forCount ; repeat body 20 times

2.2 Conditional loop

    Loopz / loope

    If ECX ! = 0 and ZF = 1

     then loop again .

    Loopnz / loopne

    If ECX ! = 0 and ZF = 0

     then loop again.

2.3 For loop structure

    Pseudo code

    for year : = downto 1 until balance = 0 loop

    …..{ body of loop }

    end for Code fragment

    mov ecx , 10 ; maximum number of iterations

    forYear : …..

     ; body of loop

     cmp ebx , 0 ; balance = 0 ?

     loopne forYear ; repeat body 20 times

2.4 Other instructions

    Lea destination , source

    ? Destination will normally be a 32-bit register ;

    ? Source is any reference to memory ;

    ? The address of the source is loaded into the register ;

    ? Mov destination , OFFSET source ;

     Jecxz targetstatement

    Jump if ecx = 0 ;

3. Implementing loop structures

    Loop structures include While, Until and For.

    ? Use jump instructions

    ? Use loop instructions

    While

     while continuation condition loop

     ....{body of loop}

     end while ;

    Until

     until termination condition loop

     ....{body of loop}

     end until ;

    For

     for index :=

     initialValue to finalValue loop

     ....{body of loop}

     end for ;

3.1 While loop structure

    Example 5.6

    Pseudo code

    while (sum<1000) loop

    …{body of loop}

    end while; Code fragment

    whileSum:

     cmp sum , 1000 ; sum<1000?

     jnl endWhileSum ;exit loop if not

    

    jmp whileSum ; go check condition again

    endWhileSum:

Example 5.7

    Pseudo code

    X : = 0 ; twoToX : = 1 ;

    while

    twoToX<=number

    multiply twoToX by 2 ;

    add 1 to x ;

    end while ;

    subtract 1 from x Code fragment

    mov cx , 0

    mov eax , 1

    whileLe : cmp eax , number

     jnle endWhileLE

    Body : add eax , eax

     inc cx

     jmp whileLE

    endWhileLE :

     dec cx

Example 5.8

    Pseudo code

    while (sum<1000) and ( count <=24 ) loop

    …{ body of loop }

    end while;

Code fragment

    whileSum:

     cmp sum , 1000 ; sum<1000?

     jnl endWhileSum ;exit loop if not

     cmp cx , 24 ;count <=24

     ;body of loop

     jmp whileSum ;go check condition again

    endWhileSum:

3.2 For loop structure

    For loop structure

    for index:=initialValue to finalValue loop

    …. ..{body of loop}

    end for; While loop structure

    Index:=initialValue

    while index <=finalValue loop

    …..{body of loop}

Report this document

For any questions or suggestions please email
cust-service@docsford.com