Rev | Line | |
---|
[9] | 1 | ! memcmp() Author: Kees J. Bot
|
---|
| 2 | ! 27 Jan 1994
|
---|
| 3 | .sect .text; .sect .rom; .sect .data; .sect .bss
|
---|
| 4 |
|
---|
| 5 | ! int memcmp(const void *s1, const void *s2, size_t n)
|
---|
| 6 | ! Compare two chunks of memory.
|
---|
| 7 | !
|
---|
| 8 | .sect .text
|
---|
| 9 | .define _memcmp
|
---|
| 10 | _memcmp:
|
---|
| 11 | cld
|
---|
| 12 | push bp
|
---|
| 13 | mov bp, sp
|
---|
| 14 | push si
|
---|
| 15 | push di
|
---|
| 16 | xor ax, ax ! Prepare return value
|
---|
| 17 | mov si, 4(bp) ! String s1
|
---|
| 18 | mov di, 6(bp) ! String s2
|
---|
| 19 | mov cx, 8(bp) ! Length
|
---|
| 20 | cmp cx, #16
|
---|
| 21 | jb cbyte ! Don't bother being smart with short arrays
|
---|
| 22 | mov dx, si
|
---|
| 23 | or dx, di
|
---|
| 24 | andb dl, #1
|
---|
| 25 | jnz cbyte ! Bit 0 set, use byte compare
|
---|
| 26 | cword: sar cx, #1
|
---|
| 27 | adcb dl, dl ! Save carry
|
---|
| 28 | repe cmps ! Compare words
|
---|
| 29 | mov cx, #2 ! Recompare the last word
|
---|
| 30 | sub si, cx
|
---|
| 31 | sub di, cx
|
---|
| 32 | addb cl, dl ! One more byte?
|
---|
| 33 | cbyte: test cx, cx ! Set 'Z' flag if cx = 0
|
---|
| 34 | last:
|
---|
| 35 | repe cmpsb ! Look for the first differing byte
|
---|
| 36 | je equal
|
---|
| 37 | ja after
|
---|
| 38 | sub ax, #2 ! if (s1 < s2) ax -= 2;
|
---|
| 39 | after: inc ax ! ax++, now it's -1 or 1
|
---|
| 40 | equal: mov dx, si ! For bcmp() to play with
|
---|
| 41 | pop di
|
---|
| 42 | pop si
|
---|
| 43 | pop bp
|
---|
| 44 | ret
|
---|
Note:
See
TracBrowser
for help on using the repository browser.