Yes, it is possible to have multiple conditional jumps following a single comparison instruction. The comparison instruction (in this case, CMP EAX, ECX
) sets status bits in the EFLAGS
status register which are used by following conditional branches when deciding whether or not to take a jump.
Take this code for example:
MOV EAX, 5 ; set EAX to 5
MOV ECX, 3 ; set ECX to 3
CMP EAX, ECX ; sets comparison bits
JL _target1
JG _target2
In this code section, 5 is greater than 3, so the code will jump to _target2.
There are 4 standard flags, ZNCV (Zero flag, Negative flag, Carry flag, Overflow flag), which are set by different instructions at different times. For example, addition (ADD EAX, ECX
) would set the Overflow flag if the numbers added were very large and caused integer overflow.
For CMP
, the Carry flag is used to show if the first number is greater or less than the second number. The Zero flag is set to 1 if both numbers are equal.
As far as different ways to go about this, if you are branching to many different places based on a single value (equivalent to a switch statement in C), this will often be written with a jump table in assembly. A jump table is a simple table with all the possible targets you might jump to. If you were switching on a number, you would use the number to index into the jump table and find your target.