| Table of Contents |
Jumping instructions are a unique set. They are the ones that reset the IP register or the CS:IP register pair. By doing so, they cause an alteration of the standard next instruction program sequence. The CS:IP register pair is used by the CPU to keep track of where the next instruction to execute is located.
Standard Jumps
In the standard 80X86 instruction set, there are three different size offset values that can be used for jumping. They are the 8-bit short jumps, the 16-bit near jumps, and the 32-bit far jumps. The 32-bit far jumps use two 16-bit words combined to make up a 20-bit segment:offset address. The following are some common examples of jump instruction statements.
;goto near procedure next_task
jmp next_task
;goto far procedure next_task
jmpf next_task
;the programmer does not need to use the jmpf mnemonic
; because the compiler will decide and use when needed
load BX with location of code routine
mov BX,offset next_task
jmp [BX] ;goto where BX is pointing
;define data word with address of code routine
next dw offset next_task
jmp [next] ;goto next code routine
Conditional Jumps
A standard conditional jump instruction checks the contents of the Flag register bits relating to the jump condition. If the associated bits are set correctly for the jump condition, then the IP register is altered and program execution continues at the new address indicated by the instruction data. All conditional jump instructions use an eight-bit jump offset. To the programmer, this means that you can only jump over a few instructions. You can usually jump over 30 to 40 instructions; beyond that, you take a chance with compiling errors.
The following is a list of jump mnemonics with conditions and explanatory notes:
Real-time programmers note that the logical branch instructions execute several times faster if the jump is not taken. If possible, write your code such that the jumps are not taken under most conditions.
Subroutine Jumps
When you call a subroutine, the CPU saves the current contents of the CS:IP register pair or the IP register in the stack area so that when the subroutine is finished, it can load the return address from the stack area into the IP register or CS:IP register pair for continuation at the next instruction after the call statement.
;call procedure, compiler will decide if near or far
call subroutine
;call using address in memory location
call [indirect]
;call using address in register BX
call [BX]
A subroutine call is terminated by a matching return instruction. The return instruction is a special case load instruction that loads the IP register or the CS:IP register pair with data indexed by the stack pointer.
Interrupt Jumps
These are normally used for calling system functions.
Because the conditional jumps are only eight bit offsets, problems will occur if you attempt to jump over more than a few instructions. One standard way to overcome this problem is to translate the jump as follows:
original
jc carry_overflow
nop
carry_overflow:
translates to
jnc no_overflow
jmp carry_overflow
no_overflow:
carry_overflow:
Table Lookup Jumping
If you have a data variable and you want to execute one of several different functional routines depending on the data, then the table lookup jump is a good method for branching control. There are many ways that table lookup jumping can be performed, depending on the nature of the data that is used to select a particular subroutine from a set of possible subroutines.
The following is an example to select a jump given a positive binary integer between 0 and 32000:
function_table dw function_0_routine
dw function_1_routine
dw function_2_routine
;load BX with number
mov bx,function_code
;point DI to index start of jump table
lea di,function_table
;adjust BX to word offset into jump table
shl bx,1
;perform call to routine indexed in table
call [di+bx]
The following is an example of keyboard table lookup jumping.
keyboard_key dw 0031
function_key_table dw 0031
dw function_1_routine
table_end:
;
;load AX with function code
mov ax,keyboard_key
;ind
ex jump table with SI
lea si,function_key_table
function_loop:
cmp si,table_end ;?? end of table ??
jae no_find_function ;branch if end
;check for match of function codes
cmp ax,[si]
je function_find ;branch if find code
add si,4 ;else point to next entry
jmp function_loop
function_find:
jmp [si+2]
no_find_function:
| Table of Contents |