CE 302 Microprocessors Week 6 v5 Control Transfer
CE 302 Microprocessors Week 6 v5 Control Transfer
Assembly Language
These lecture notes are based on the book by Muhammed Ali Mazidi,
Janice Gillispie Mazidi, Danny Causey; «The x86 PC assembly language,
design, ad interfacing», 5the Ed., Prentice Hall
Control Transfer Instructions
CONTROL TRANSFER INSTRUCTIONS
FAR and NEAR
u In the sequence of instructions, it is often necessary to
transfer program control to a different location.
u If control is transferred to a memory location within the
current code segment, it is NEAR.
u Sometimes called intrasegment. (within segment)
u If control is transferred outside the current code segment, it
is a FAR jump.
u Or intersegment. (between segments)
CONTROL TRANSFER INSTRUCTIONS
FAR and NEAR
F9 is 2’s complement of 7,
(15H-0EH)
15 is the offset of next
instruction after the “jmp”
Example program
CONTROL TRANSFER INSTRUCTIONS
short jumps
u After the program was assembled and linked, using debug
c>debug prog2-1.exe
-u cs:0 19 The IP value of MOV, at 0013, is
1067:0000 B86610 MOVE AX, 1066
added to FA to calculate the address of
1067:0003 8ED8 MOVE DS, AX
1067:0005 B90500 MOVE CX, 0005 label AGAIN, and the carry is dropped.
1067:0008 BB0000 MOVE BX, 0000
1067:000B B000 MOVE AL, 00
1067:000D 0207 ADD AL, [BX] FA is 2’s complement of 6 (-6): 0000 0110
1067:000F 43 INC BX (1111 1010)
1067:0010 49 DEC CX When you add FA + 13 = 0D (carry is
1067:0011 75FA JNZ 000D dropped)
1067:0013 A20500 MOVE [500], AL
1067:0016 B44C MOV AH, 4CH FA
1067:0008 CD21 INT 21H 13
10D
-"JNZ AGAIN" was assembled as "JNZ 000D", and 000D is the address of the
instruction with the label AGAIN.
"JNZ 000D" has the opcode 75 and the target address FA.
CONTROL TRANSFER INSTRUCTIONS
short jumps
u Calculate a forward jump target address by adding the IP of the following instruction
to the operand.
u The displacement value is positive, as shown.
– Memory indirect JMP - target address is the contents of two memory locations,
pointed at by the register.
• "JMP [DI]" will replace the IP with the contents of memory locations pointed
at by DI and DI+1.
– FAR JUMP - in the format "JMP FAR PTR label". A jump out of the current code
segment
• IP and CS are both replaced with new values.
CONTROL TRANSFER INSTRUCTIONS
CALL statements
u The CALL instruction is used to call a procedure, to perform tasks
that need to be performed frequently.
u The target address could be in the current segment, in which case it will
be a NEAR call or outside the current CS segment, which is a FAR call.
u The microprocessor saves the address of the instruction following
the call on the stack.
u To know where to return, after executing the subroutine.
u In the NEAR call only the IP is saved on the stack.
u In a FAR call both CS and IP are saved.
CONTROL TRANSFER INSTRUCTIONS
CALL statements
u For control to be transferred back to the caller, the last subroutine instruction
must be RET (return).
u For NEAR calls, the IP is restored.
u For FAR calls, CS & IP are restored.
u Assume SP = FFFEH: