Flow of Control - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Flow of Control

Description:

... Language Programming. 2. Unconditional Jump. JMP [operator] destination ... Combine a conditional and unconditional jump to overcome this range limitation ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 19
Provided by: timma87
Category:

less

Transcript and Presenter's Notes

Title: Flow of Control


1
Flow of Control
  • Assembly Language Programming
  • University of Akron
  • Dr. Tim Margush

2
Unconditional Jump
  • JMP operator destination
  • Intrasegment direct E9 disp_16
  • Intrasegment direct short EB disp_8
  • Intrasegment indirect FF mod 100 r/m ??
  • Intersegment direct EA disp_16 seg_16
  • Intersegment indirect FF mod 101 r/m ??
  • operator SHORT, NEAR PTR, or FAR PTR
  • NEAR PTR is the usual default

3
Specifying the Jump Target
  • Instruction Label
  • A symbolic name defined to be an address in the
    code segment of a program
  • A label may be attached to any point in the code
    of a program
  • a_Label jmp a_Label
  • Labels definitions usually have a colon at the
    end signifying them as NEAR

4
Executing a Jump
  • Intrasegment jumps are caused by changing the IP
    register to a new value
  • Short jumps add a signed 8-bit displacement to IP
  • Near jumps add a signed 16-bit displacement to IP
  • Intersegment jumps change both the CS and IP
    registers
  • Far jumps simply assign new values to these
    registers

5
Sample Jump Encodings
  • 11060100 EB2A JMP 012C
  • 012C-0102002A
  • 11060102 EBFC JMP 0100
  • 0100-0104FFFC
  • 11060104 E97F00 JMP 0186
  • 0186-01060080 (too far for short!)
  • 0186-0107007F
  • 11060107 E9F5FE JMP FFFF
  • FFFF-010AFEF5

6
Conditional Jumps
  • Jxxx destination
  • There are 30 some variations that interrupt
    sequential flow based on various flag settings
  • JNZ - Jump if zero flag is clear (0) meaning the
    result of a previous operation was non-zero
  • JC - Jump if a previous operation caused the
    carry flag to be set (1)

7
Range of Conditional Jumps
  • All conditional jumps are SHORT
  • range is -128 to 127 bytes
  • 80386 allow larger distances
  • Combine a conditional and unconditional jump to
    overcome this range limitation
  • jz too_far ugh jnz is_close
  • use code at jmp near ptr too_far
  • right! is_close

8
Using Conditional Jumps
  • Conditional jumps typically follow an instruction
    that alters the flag bits
  • CMP destination, source
  • Computes (destination-source) and sets flag bits
  • result is not stored
  • flags allow us to decide lt, lt, gt, gt, , ltgt,
    etc
  • we can also interpret the results meaningfully
    for signed or unsigned data

9
Implementing an IF-THEN
  • unsigned int n
  • if (ngt7) do_it()
  • If n is a signed int, use jng (not greater)
  • unsigned
  • above, below
  • signed
  • less, greater
  • if (ngt7)
  • mov ax,n
  • cmp ax,7
  • jna skip_it
  • then-part
  • call do_it
  • end if
  • skip_it

10
Implementing an IF-ELSE
  • char n
  • if (n'7')
  • do_it()
  • else
  • do_that()
  • Document the control structures and keep the
    parts in the usual order
  • if (n'7')
  • cmp n,'7'
  • jne else_
  • then-part
  • call do_it
  • jmp short endif
  • else_
  • call do_that
  • endif

11
Implementing a WHILE
  • int n
  • while (ngt0) n-2
  • This loop could be optimized by keeping n in a
    register and storing to memory only at end of loop
  • while (ngt0)
  • while_
  • cmp n,0
  • jle end_while
  • loop-body
  • sub n,2
  • jmp while_
  • end_while

12
Compound Conditions
  • char n int w,x
  • if (ngt'A' wx)
  • whatever()
  • This example uses short-circuit evaluation
  • if the first condition is false it immediately
    skips past the then-part
  • if(ngt'A'wx)
  • cmp n,'A'
  • jl no_go
  • mov ax,w
  • cmp ax,x
  • jne no_go
  • then-part
  • call whatever
  • no_go

13
Compound Conditions - OR
  • char n,k unsigned int w
  • if (nltgtk wlt10)
  • whatever()
  • This example uses short-circuit evaluation
  • if the first condition is true it immediately
    skips to the then-part
  • if(nltgtkwlt10)
  • mov ah,n
  • cmp ah,k
  • jne then_
  • cmp w,10
  • ja end_if
  • then_
  • call whatever
  • end_if

14
LOOP
  • LOOP destination
  • decrements CX but does not change any flags
  • if CX is not zero after the decrement, control is
    transferred to the destination label
  • This is a SHORT jump only
  • for (x9xgt0x--) nx
  • for(x9xgt0x--)
  • mov cx,9
  • top_loop
  • add n,cx nnx
  • loop top_loop

15
JCXZ destination
  • Directly compares CX to 0 and jumps to the
    destination if equal
  • This instruction does not affect the flags
  • It is commonly used to bypass the first iteration
    of a loop if the count is already 0
  • while(xgt0)do_it()
  • mov cx,x
  • jcxz skip_it
  • top_loop
  • call do_it
  • loop top_loop
  • skip_it

16
LOOPZ/E and LOOPNZ/E
  • Enhancement of the LOOP instruction
  • The state of the ZERO Flag may also cause loop
    termination
  • Loop while ZF/equal CX!0
  • Loop while (NZ/ not equal) CX!0
  • Remember that LOOP decrements CX, but this does
    not affect the flags!
  • LOOPZ LOOPE
  • LOOPNZLOOPNE
  • Some action inside the loop should affect the
    zero flag (cmp ?)

17
LOOPZ Example
  • This program accepts at most 9 characters from
    the keyboard
  • When the 9th character is pressed (or the enter
    key is used) the number of keypresses is displayed
  • mov ah,1
  • mov cx,9
  • next_char
  • int 21h
  • cmp al,13
  • loopne next_char
  • determine count
  • mov ax, 0239h
  • sub al,cl
  • mov dl,al
  • int 21h

18
Homework
  • Page 113
  • 1, 3, 4, 10
Write a Comment
User Comments (0)
About PowerShow.com