user4385532
user4385532

Reputation:

What does the instruction sete do in assembly?

My uni course requires some basic assembly knowledge... even though I have NONE. However they gave us this sample assembly code:

080485fa <check_pin>:
 80485fa:       55                      push   ebp
 80485fb:       89 e5                   mov    ebp,esp
 80485fd:       81 7d 08 bf 07 00 00    cmp    DWORD PTR [ebp+0x8],0x7bf
 8048604:       0f 94 c0                sete   al
 8048607:       0f b6 c0                movzx  eax,al
 804860a:       5d                      pop    ebp
 804860b:       c3                      ret

It is supposed to be more or less equivalent to the following C code:

int check_pin(int pin) {
        return pin == 0x7bf;
}

I'm trying to figure out what exactly this assembly code do and I'm dumbfounded by this sete instruction. What does this instruction do?

Wikibooks has a course on x86 assembly, but I was not able to find anything about sete in the chapter devoted to assembly instructions.

Upvotes: 18

Views: 35947

Answers (2)

phuclv
phuclv

Reputation: 41794

It might be a little bit difficult to find sete in many manuals, since they don't list it directly, just like cmove. One trick is to use the documentation feature on Godbolt's Compiler Explorer

Just write the instruction inline like this

__asm("sete %al");

sete al will also work, since we don't care what the operands are, the only important thing is the mnemonic. Then if you hover the mouse on the word sete you'll see the documentation tooltip appears. Now put the cursor on that word and press Ctrl+F8. Another popup will appear

SETE help

Sets the destination operand to 0 or 1 depending on the settings of the status flags (CF, SF, OF, ZF, and PF) in the EFLAGS register. The destination operand points to a byte register or a byte in memory. The condition code suffix (cc) indicates the condition being tested for.

At the end of the popup you'll also see the link to the documentation for that instruction where you can see this

0F 94           SETE r/m8   M   Valid   Valid   Set byte if equal (ZF=1).
REX + 0F 94     SETE r/m8*  M   Valid   N.E.    Set byte if equal (ZF=1).

ZF will be set when the result is zero, which also indicates an "equal" condition

Upvotes: 9

fuz
fuz

Reputation: 93014

The sete instruction (and its equivalent, setz) sets its argument to 1 if the zero flag is set or to 0 otherwise. The zero flag is set if the last comparison or arithmetic instruction yielded equality or a result of zero. Thus in your case, sete sets al to 0 or 1 according to the result of the preceeding cmp instruction.

Upvotes: 32

Related Questions