Ics312 set 8 Flow Control Instructions



Yüklə 112,5 Kb.
tarix07.11.2018
ölçüsü112,5 Kb.
#78726


ICS312 SET 8


Flow Control Instructions



Jump Instructions (1)

  • The unconditional jump instruction, branches to a new address in the program code.     JMP NEXT        ; jumps to address of "NEXT"

  • Conditional jump instructions test the condition of one or two FLAGS in the flags register, then "jump" to a new location in the program based on the result of the test.

  • Example:

  • TOP:

  • SUB AX, BX ;AX = AX - BX

  • JC EXIT: ;IF AX < BX then jump to EXIT

  • JMP TOP ;else, repeat until AX < BX

  • EXIT:

  • MOV AX, 4C00H

  • INT 21H



Jump Instructions (2)

  • CMP Instruction 

  • The CMP instruction sets the flags the same as if a subtraction had been performed, but the destination operand is NOT changed.

  • Example (simulating the LOOP instruction):

  • MOV CX, 5

  • TOP_2:

  • CMP CX, 0 ; CX = 0 ?

  • JE EXIT_: ; IF CX = 0, then EXIT_

  • SUB AX, BX ; AX = AX - BX

  • DEC CX

  • JMP TOP_2 ;else, repeat until CX = 0

  • EXIT_:

  • MOV AX, 4C00H

  • INT 21H

  • Note: the above code simulates the effect of the LOOP instruction



Signed versus Unsigned Conditional Jumps

  • Note:In this course we will only make use of signed conditional jumps.

  • Unsigned Conditional Jumps:  JA and JB

  • Signed Conditional Jumps: JG or JL

  • Example 1. Will the JG instruction in the following code result in a jump?

  • MOV AL, 80h

  • MOV BL, 7FH

  • CMP AL, BL

  • JG EXIT_

  • ...

  • Example 2. Will the JA instruction in the following code result in a jump?

  • MOV AL, 80h

  • MOV BL, 7FH

  • CMP AL, BL

  • JA EXIT_

  • ...



Examples of Flow Control Using Jump Instructions (1)



Examples of Flow Control Using Jump Instructions (2)



Examples of Flow Control Using Jump Instructions (3)



Examples of Flow Control Using Jump Instructions (4)



Examples of Flow Control Using Jump Instructions (5)



Examples of Flow Control Using Jump Instructions (6)



Examples of Flow Control Using Jump Instructions (7)



Examples of Flow Control Using Jump Instructions (8)



Loop Instruction

  • Used to implement "for loops" more conveniently than above.

  • Decrements CX register and jumps to specified label as long as CX != 0 after being decremented.

  • Example: Input a number, and display that number of asterisks

  • .586 ; required for MOVSX or MOVZX instructions

  • .CODE

  • START:

  • MOV DL, '*"

  • MOV AH, 1

  • INT 21H

  • SUB AL, 30H

  • MOVSX CX, AL ; sign extend into CX register

  • MOV AH, 2

  • TOP_:

  • INT 21H ; display asterisks

  • LOOP TOP_ ; repeats CX times

  • EXIT_:

  • MOV AX, 4C00H

  • INT 21H



MOVSX Instruction



JMP Instructions

  • For signed arithmetic

  • J[N] {G | L | Z } [E]

  • eg: JG, JGE, JNZ, JLE

  • For unsigned arithmetic

  • (not used in this course)

  • J[N] {A | B | Z } [E]

  • eg: JA, JAE, JNZ, JBE



Example on JE



Example on JGE



Example on JE & JL



Example on JCXZ



Textbook Reading (Jones):



Yüklə 112,5 Kb.

Dostları ilə paylaş:




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə