1.14. CONDITIONAL JUMPS
Here we see a new instruction:BLTZ(“Branch if Less Than Zero”).
There is also theNEGUpseudo instruction, which just does subtraction from zero. The “U” suffix in both
SUBUandNEGUimplies that no exception to be raised in case of integer overflow.
Branchless version?
You could have also a branchless version of this code. This we will review later:3.13 on page 518.
1.14.3 Ternary conditional operator
The ternary conditional operator in C/C++ has the following syntax:
expression? expression : expression
Here is an example:
const char* f (int a)
{
return a==10? "it is ten" : "it is not ten";
};
x86
Old and non-optimizing compilers generate assembly code just as if anif/elsestatement was used:
Listing 1.123: Non-optimizing MSVC 2008
$SG746 DB 'it is ten', 00H
$SG747 DB 'it is not ten', 00H
tv65 = -4 ; this will be used as a temporary variable
_a$ = 8
_f PROC
push ebp
mov ebp, esp
push ecx
; compare input value with 10
cmp DWORD PTR _a$[ebp], 10
; jump to $LN3@f if not equal
jne SHORT $LN3@f
; store pointer to the string into temporary variable:
mov DWORD PTR tv65[ebp], OFFSET $SG746 ; 'it is ten'
; jump to exit
jmp SHORT $LN4@f
$LN3@f:
; store pointer to the string into temporary variable:
mov DWORD PTR tv65[ebp], OFFSET $SG747 ; 'it is not ten'
$LN4@f:
; this is exit. copy pointer to the string from temporary variable to EAX.
mov eax, DWORD PTR tv65[ebp]
mov esp, ebp
pop ebp
ret 0
_f ENDP
Listing 1.124: Optimizing MSVC 2008
$SG792 DB 'it is ten', 00H
$SG793 DB 'it is not ten', 00H
_a$ = 8 ; size = 4
_f PROC
; compare input value with 10
cmp DWORD PTR _a$[esp-4], 10