cmps usage

A little bit of ASM and it’s 3.42 am now. What a waste of life! But anyways, I am currently working on a mini project on username and password authentication. This is the hardest login form I have ever made! I am constantly looking for higher level instructions to use to simplify my code, and one of them I happen to come across is a string comparison instruction, CMPS.

The problem with the HELPPC is that the documentation does not include example snippets and it’s extremely hard to guess what the instruction is going to use for its operations. Then with a little bit more effort, this is what I have come to learn when comparing strings.

First, you must understand that CMPSB compares two strings, where one is from ES and the other DS. You need to specify te DI and SI respectively before the CMPSB instruction. Another trick here, which is not clearly mentioned in the HELPPC is that you need to use a REP instruction to “loop” through the bytes pointed by DI and SI. And finally, as with all loops, the CX must be initialised to specify the number of “loops” which is also the number of bytes to compare. Note that there is also a CMPSW and CMPSD for word and double word.

XOR cx, cx ; exclusive or
MOV cl, 12 ; 12 bytes to compare
LEA di, STRING ; Load effective address
REPE CMPBS ; repeat while equal
je ACTION_LABEL ; jump to ACTION_LABEL if equal