Assembly Language for Beginners

(Jeff_L) #1

.1. X86


CMPSB/CMPSW/CMPSD/CMPSQ(M) compare byte/ 16-bit word/ 32-bit word/ 64-bit word from the ad-
dress which is in SI/ESI/RSI with the variable at the address stored in DI/EDI/RDI. Set flags asCMP
does.


Together with the REP prefix, it is to be repeated in a loop, the counter is stored in the CX/ECX/RCX
register, the process will run until the ZF flag is zero (e.g., until the compared values are equal to
each other, hence ā€œEā€ in REPE).

It works like memcmp() in C.

Example from the Windows NT kernel (WRKv1.2):

Listing 3: base\ntos\rtl\i386\movemem.asm
; ULONG
; RtlCompareMemory (
; IN PVOID Source1,
; IN PVOID Source2,
; IN ULONG Length
; )
;
; Routine Description:
;
; This function compares two blocks of memory and returns the number
; of bytes that compared equal.
;
; Arguments:
;
; Source1 (esp+4) - Supplies a pointer to the first block of memory to
; compare.
;
; Source2 (esp+8) - Supplies a pointer to the second block of memory to
; compare.
;
; Length (esp+12) - Supplies the Length, in bytes, of the memory to be
; compared.
;
; Return Value:
;
; The number of bytes that compared equal is returned as the function
; value. If all bytes compared equal, then the length of the original
; block of memory is returned.
;
;--

RcmSource1 equ [esp+12]
RcmSource2 equ [esp+16]
RcmLength equ [esp+20]

CODE_ALIGNMENT
cPublicProc _RtlCompareMemory,3
cPublicFpo 3,0

push esi ; save registers
push edi ;
cld ; clear direction
mov esi,RcmSource1 ; (esi) -> first block to compare
mov edi,RcmSource2 ; (edi) -> second block to compare

;
; Compare dwords, if any.
;

rcm10: mov ecx,RcmLength ; (ecx) = length in bytes
shr ecx,2 ; (ecx) = length in dwords
jz rcm20 ; no dwords, try bytes
repe cmpsd ; compare dwords
jnz rcm40 ; mismatch, go find byte

;
; Compare residual bytes, if any.
;
Free download pdf