使用Intel语法将汇编代码实现字符串比较,并将其嵌入到C或C++程序中。以下示例代码使用了“jne”和“loop”指令来比较字符串的每个字符。
section .data
str1 db 'Hello World!',0
str1len equ $-str1
str2 db 'Hello',0
str2len equ $-str2
section .text
global _start
_start:
; load string pointers and lengths
mov esi, str1
mov edi, str2
mov ecx, [str1len]
cmp ecx, [str2len]
jne .not_equal ; strings not equal if lengths differ
; compare string characters
repe cmpsb
je .equal ; strings are equal if zero flag is set
.not_equal:
; strings are not equal
mov eax, 1 ; return 1 to indicate non-equality
mov ebx, 0 ; return 0 as the exit code
int 0x80
.equal:
; strings are equal
mov eax, 0 ; return 0 to indicate equality
mov ebx, 0 ; return 0 as the exit code
int 0x80