What do the assembly instructions "seta" and "setb" do after cmpsb reps?

I am having trouble understanding what the following assembly lines do:

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

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

Then it sets the lower bytes rdxand raxin accordance with this instruction.

My confusion, when I looked at this instruction online, I said that it setasets the low byte to 0x01 if its value is above a certain value, otherwise its 0x00. Similarly for setb, which sets the byte to 0x01, if its below a certain value.

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

+4
source share
2 answers

cmpsinstruction compares [rsi]and [rdi]. repzprefix (written alternately repe) means increasing rsiand rdithen repeat cmpsuntil [rsi]and [rdi]compare equal. The register rflagswill be set at each iteration; final iteration, where [rsi][rdi]is what will be used seta(if indicated above) and setb(if set below).

In other words, the C pseudo-code for these 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 instructions setCC here .

+8
source

Mnemonics of teams:

  • repz cmps ds:[esi], es:[edi]
    ,
  • seta dl
    dl 1, , dl 0,
  • setb al
    al 1, , al to 0,

repz ecx . (ecx .)

[EDIT], , , , , - :

  • : CF = 0 ZF = 0
  • : CF = 1
+5

Source: https://habr.com/ru/post/1679632/


All Articles