In the previous chapter, we used strings with implicit lengths, which means that these strings are terminated by a null byte. In this chapter, we will compare strings with implicit lengths and strings with explicit lengths.
Implicit Length
sse_string2_imp.asm
CFlag: Reset if IntRes2 is equal to zero; set otherwise.
ZFlag: Set if any byte/word of xmm2/mem128 is null; reset otherwise.
SFlag: Set if any byte/word of xmm1 is null; reset otherwise.
OFlag: IntRes2[0].
AFlag: Reset.
PFlag: Reset.
In the example, pcmpistri puts a 1 for every match into the corresponding position in IntRes1. When a differing byte is found, a zero is written in the corresponding position in IntRes1. Then IntRes2 is formed and applies negative polarity to IntRes1. IntRes2 will contain a 1 at the differing index (negative polarity), so IntRes2 will not be zero, and CF will be set to 1. The loop will then be interrupted, and pstrcmp will return with the position of the differing character in rax. If CF is not set but pcmpistri detects the terminating zero, the function will return with 0 in rax.
Explicit Length
sse_string3_exp.asm.
As you can see, using explicit length can sometimes complicate things. Then why use it? Many communication protocols use it, or your application may require that you use 0s in your data. One way or another we have to provide the length of the strings. In our case, we computed the length of the strings from the memory locations in section. data. However, printf expects zero-terminated strings. So, after we demonstrate how to compare strings with explicit lengths, we rebuild the strings in a buffer, add a newline and a terminating null in the buffer, and hand over the buffer to printf.
CFlag: Reset if IntRes2 is equal to zero; set otherwise.
ZFlag: Set if absolute value of EDX is less than 16 (8); reset otherwise.
SFlag: Set if absolute value of EAX is less than 16 (8); reset otherwise.
OFlag: IntRes2[0].
AFlag: Reset.
PFlag: Reset.
Note that pcmpestri and pcmpistri use ZF and SF differently. Instead of ZF signaling a terminating null, at every loop we decrease rax and rdx, and when one of them goes below 16, the loop is terminated.
Summary
Implicit and explicit string lengths
Negative polarity
Using flags