Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do the assembly instructions 'seta' and 'setb' do after repz cmpsb?

I'm having trouble understanding what the following lines of assembly do:

0x401810:    repz cmps BYTE PTR ds:[rsi],BYTE PTR es:[rdi]
0x401812:    seta   dl
0x401815:    setb   al

I understand after debugging, the first instruction compares the bytes in registers rsi and rdi, byte by byte.

Then it sets the lower bytes of rdx and rax according based on that instruction.

My confusion is, when I looked up this instruction online, it said seta sets the lower byte to 0x01 if its above a certain value, otherwise its 0x00. Similar for setb, which sets the byte to 0x01 if its below a certain value.

My question is what value, and how is it related to the above instruction?

like image 584
Varun Iyer Avatar asked Jun 19 '17 12:06

Varun Iyer


People also ask

What does Seta do in assembly?

Subscripted SETA symbolsThe assembler assigns the value of the expression in the operand field to the position in the declared array given by the value of the subscript. The subscript expression must not be 0 or have a negative value.

What does JB do in assembly?

The JB instruction branches to the address specified in the second operand if the value of the bit specified in the first operand is 1. The bit that is tested is not modified. No flags are affected by this instruction.

What is the difference between JBE and JLE?

JBE, Jump if Below or Equal, should be used when comparing unsigned numbers. JLE, Jump if Less Than or Equal, should be used when comparing signed numbers.

What is x86 assembly used for?

It is used to produce object code for the x86 class of processors. Regarded as a programming language, assembly is machine-specific and low-level. Like all assembly languages, x86 assembly uses mnemonics to represent fundamental CPU instructions, or machine code.


1 Answers

The cmps instruction compares [rsi] and [rdi]. The repz prefix (alternately spelled repe) means to increment rsi and rdi then repeat cmps as long as [rsi] and [rdi] compare equal. The rflags register will be set on each iteration; the final iteration where [rsi][rdi] is what will be used by seta (set if above) and setb (set if below).

In other words, the C pseudocode for those 3 instructions would look like this:

// Initial values
uint8_t *rsi = (...);
uint8_t *rdi = (...);
uint64_t rcx = (...);

// repz cmps BYTE PTR [rsi], BYTE PTR [rdi]
while (*rsi == *rdi && rcx > 0) {
    rsi++;
    rdi++;
    rcx--;
}

uint8_t dl = *rsi > *rdi;   // seta dl
uint8_t al = *rsi < *rdi;   // setb al

See the documentation for all of the setCC instructions here.

like image 103
Nayuki Avatar answered Sep 19 '22 02:09

Nayuki