Setxx指令提供了在一定情况下替换条件分支的方法。基于FLAGS寄存器的状态,这些指令将一个字节的寄存器或内存空间的值置为0或1.在SET后的字符与条件分支使用的是一样的。如果SETxx条件为真,那么储存的结果就是1,如果为假,则储存的结果就为0.例如
setz al ;AL=如果ZF标志置位了则为1,否则为0
例如,考虑查找两个数的最大数的问题。这个问题的标准解决方法是使用一条CMP指令再使用条件分支对最大值进行操作。下面这个例子展示了不使用分支如何找到最大值。
%include "asm_io.inc" segment .data message1 db "Enter a number: ",0 message2 db "Enter another number: ",0 message3 db "The larger number is: ",0 segment .bss input1 resd 1 segment .text global _asm_main _asm_main: enter0,0 pusha mov eax,message1 call print_string call read_int mov [input1],eax mov eax,message2 call print_string call print_nl xor ebx,ebx cmp eax,[input1] setg bl ;ebx=(input2>input1)?1:0 neg ebx ;ebx=(input2>input1)?0xFFFFFFFF:0 mov ecx,ebx ;ecx=(input2>input1)?0xFFFFFFFF;0 and ecx,eax ;ecx=(input2>input1)?input2:0 not ebx ;ebx=(input2>input1)?0:0xFFFFFFFF and ebx,[input1];ebx=(input2>input1)?0:input1 or ecx,ebx ;ecx=(input2>input1)?input2:input1 mov eax,message3 call print_string mov eax,ecx call print_int call print_nl popa mov eax,0 leave ret