;; MASM syntax assembly language ;; ;; Module: string compare ;; Author: Lin Jensen ;; Date: 25 Nov. 2002 ;; ;; Class example for csc116 ;; C-convention, args pushed right to left, caller cleans up stack .386 .model flat ; sensible 32-bit code .code public strcmp ; make a global symbol strcmp: ; (char* str1, char* str2) push ebp ;save frame pointer mov ebp, esp mov esi, 8[ebp] ; esi = str1 (pointer) mov edi, 12[ebp] ; edi = str2 (pointer) ;; WHILE characters equal and not end of string, scloop: mov al, [esi] ; *str1 (char of first string) mov bl, [edi] ; *str2 sub al, bl ; difference, al - bl jne scdone ; if different, return difference cmp bl,0 ; if at end of (both) strings jz scdone ; al will be 0, return as meaning equal inc esi ; ELSE inc edi ; increment both pointers jmp scloop scdone: cbw cwd ; sign extend result to 32 bits (eax) mov esp, ebp pop ebp ; restore frame pointer ret ; return ; eax < 0 means str1 < str2 ; eax = 0 means str1 = str2 ; eax > 0 means str1 > str2 end ;of assembling this file.