7.4 Unconditional Transfer of Control (JMP)


7.4 Unconditional Transfer of Control (JMP)

The jmp (jump) instruction unconditionally transfers control to another point in the program. There are three forms of this instruction: a direct jump, and two indirect jumps. These instructions take one of the following three forms:

      jmp label;      jmp( reg32 );      jmp( mem32 ); 

For the first (direct) jump above, you normally specify the target address using a statement label (see the previous section for a discussion of statement labels). The statement label is usually on the same line as an executable machine instruction or appears by itself on a line preceding an executable machine instruction. The direct jump instruction is the most common of these three forms. It is completely equivalent to a GOTO statement in a high level language.[1] Example:

          << statements >>          jmp laterInPgm;               .               .               . laterInPgm:          << statements >> 

The second form of the jmp instruction above, "jmp( reg32 );", is a register indirect jump instruction. This instruction transfers control to the instruction whose address appears in the specified 32-bit general purpose register. To use this form of the jmp instruction you must load a 32-bit register with the address of some machine instruction prior to the execution of the jmp. You could use this instruction to implement a state machine by loading a register with the address of some label at various points throughout your program; then a single indirect jump at a common point in the program can transfer control to the label whose address you've loaded into the register. The short sample program in Listing 7-3 demonstrates how you could use the jmp in this manner.

Listing 7-3: Using Register Indirect JMP Instructions.

start example
 program regIndJmp; #include( "stdlib.hhf" ); static     i:int32; begin regIndJmp;     // Read an integer from the user and set EBX to     // denote the success or failure of the input.     try         stdout.put( "Enter an integer value between 1 and 10: " );         stdin.get( i );         mov( i, eax );         if( eax in 1..10 ) then             mov( &GoodInput, ebx );         else             mov( &valRange, ebx );         endif;       exception( ex.ConversionError )         mov( &convError, ebx );       exception( ex.ValueOutOfRange )         mov( &valRange, ebx );     endtry;     // Okay, transfer control to the appropriate     // section of the program that deals with     // the input.     jmp( ebx );     valRange:         stdout.put( "You entered a value outside the range 1..10" nl );         jmp Done;     convError:         stdout.put( "Your input contained illegal characters" nl );         jmp Done;     GoodInput:         stdout.put( "You entered the value ", i, nl );     Done: end regIndJmp; 
end example

The third form of the jmp instruction is a memory indirect jmp. This form of the jmp instruction fetches a double word value from the specified memory location and transfers control to the instruction at the address specified by the contents of the memory location. This is similar to the register indirect jmp except the address appears in a memory location rather than in a register. Listing 7-4 demonstrates a rather trivial use of this form of the jmp instruction:

Listing 7-4: Using Memory Indirect JMP Instructions.

start example
 program memIndJmp; #include( "stdlib.hhf" ); static     LabelPtr:dword := &stmtLabel; begin memIndJmp;     stdout.put( "Before the JMP instruction" nl );     jmp( LabelPtr );         stdout.put( "This should not execute" nl );     stmtLabel:         stdout.put( "After the LabelPtr label in the program" nl ); end memIndJmp; 
end example

Caution

Unlike the HLA high level control structures, the low level jmp instructions can get you into a lot of trouble. In particular, if you do not initialize a register with the address of a valid instruction and you jump indirect through that register, the results are undefined (though this will usually cause a general protection fault). Similarly, if you do not initialize a double word variable with the address of a legal instruction, jumping indirect through that memory location will probably crash your program.

[1]Unlike high level languages, where your instructors usually forbid you to use GOTO statements, you will find that the use of the jmp instruction in assembly language is absolutely essential.




The Art of Assembly Language
The Art of Assembly Language
ISBN: 1593272073
EAN: 2147483647
Year: 2005
Pages: 246
Authors: Randall Hyde

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net