减去两个字符
Subtracting two characters
我刚开始用汇编编程,所以我是初学者。
为了练习,我正在尝试用汇编重写一个基本的 libc(NASM Intel 语法)。
但我卡在 strcmp 函数上:
;; Compare two C-style NUL-terminated strings
;; Inputs : ESI = address of s1, EDI = address of s2
;; Outputs : EAX = return an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2
strcmp:
call strlen
mov ecx, eax ; ecx = length of the string in esi
repe cmpsb
sub esi, edi ; result = *esi - *edi
mov eax, esi
ret
对我来说,应该是这样的:
s1 db 'Hello World', 0
s2 db 'Hello Stack', 0
在repe cmpsb
指令之后,ESI
应该等于[s1 + 7]
,EDI
应该等于[s2 + 7]
。
所以我只需要做 EAX
= 'W' - 'S' = 87 - 83 = 4
问题是,它不起作用。我认为问题是当我执行这条指令时:
sub esi, edi ; result = *esi - *edi
我不认为这意味着:减去EDI
和ESI
指向的字符。
有人知道我该怎么做吗?
您的代码几乎是正确的。还剩下三个问题:
- 您不应该假定
strcmp
保留 esi
和 edi
的内容,除非您明确指定它这样做。以后很容易更改 strcmp
然后忘记需求,导致各种烦人的问题。
- 而不是 return 计算
*edi
和 *esi
之间的差异,您 return 计算 edi
和 esi
之间的差异。此外,随着 cmpsb
将 esi
和 edi
提前一个,最后比较的字符位于 edi[-1]
和 esi[-1]
.
- 你有一个差一错误:
strlen
returns NUL 字节之前的字符数,但你也需要比较 NUL 字节。否则,如果一个字符串是另一个字符串的前缀,您最终会发现两个字符串相等,因为您永远不会在第一个字符串结束时检查第二个字符串是否实际结束。
要解决第一个问题,我建议您在对 strlen
的调用周围保存和恢复 esi
和 edi
。最简单的方法是将它们压入堆栈:
push esi ; save ESI and EDI
push edi
call strlen ; compute the string length
pop edi ; restore ESI and EDI
pop esi
第二个问题已解决,方法是从内存中加载要比较的字符,计算差异,然后将结果存储到 eax
:
movzx eax, byte [esi-1] ; load byte from ESI[-1] and zero extend into EAX
movzx ecx, byte [edi-1] ; load byte from EDI[-1] and zero extend into ECX
sub eax, ecx ; compute the difference
这也通过立即使用正确的偏移量解决了第三个问题。注意这里需要movzx
而不是稍微简单的
mov al, [esi-1] ; load byte from ESI[-1] into AL
sub al, [edi-1] ; subtract EDI[-1] from AL
因为我们希望减法的结果正确符号扩展为 eax
。
我刚开始用汇编编程,所以我是初学者。
为了练习,我正在尝试用汇编重写一个基本的 libc(NASM Intel 语法)。
但我卡在 strcmp 函数上:
;; Compare two C-style NUL-terminated strings
;; Inputs : ESI = address of s1, EDI = address of s2
;; Outputs : EAX = return an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2
strcmp:
call strlen
mov ecx, eax ; ecx = length of the string in esi
repe cmpsb
sub esi, edi ; result = *esi - *edi
mov eax, esi
ret
对我来说,应该是这样的:
s1 db 'Hello World', 0
s2 db 'Hello Stack', 0
在repe cmpsb
指令之后,ESI
应该等于[s1 + 7]
,EDI
应该等于[s2 + 7]
。
所以我只需要做 EAX
= 'W' - 'S' = 87 - 83 = 4
问题是,它不起作用。我认为问题是当我执行这条指令时:
sub esi, edi ; result = *esi - *edi
我不认为这意味着:减去EDI
和ESI
指向的字符。
有人知道我该怎么做吗?
您的代码几乎是正确的。还剩下三个问题:
- 您不应该假定
strcmp
保留esi
和edi
的内容,除非您明确指定它这样做。以后很容易更改strcmp
然后忘记需求,导致各种烦人的问题。 - 而不是 return 计算
*edi
和*esi
之间的差异,您 return 计算edi
和esi
之间的差异。此外,随着cmpsb
将esi
和edi
提前一个,最后比较的字符位于edi[-1]
和esi[-1]
. - 你有一个差一错误:
strlen
returns NUL 字节之前的字符数,但你也需要比较 NUL 字节。否则,如果一个字符串是另一个字符串的前缀,您最终会发现两个字符串相等,因为您永远不会在第一个字符串结束时检查第二个字符串是否实际结束。
要解决第一个问题,我建议您在对 strlen
的调用周围保存和恢复 esi
和 edi
。最简单的方法是将它们压入堆栈:
push esi ; save ESI and EDI
push edi
call strlen ; compute the string length
pop edi ; restore ESI and EDI
pop esi
第二个问题已解决,方法是从内存中加载要比较的字符,计算差异,然后将结果存储到 eax
:
movzx eax, byte [esi-1] ; load byte from ESI[-1] and zero extend into EAX
movzx ecx, byte [edi-1] ; load byte from EDI[-1] and zero extend into ECX
sub eax, ecx ; compute the difference
这也通过立即使用正确的偏移量解决了第三个问题。注意这里需要movzx
而不是稍微简单的
mov al, [esi-1] ; load byte from ESI[-1] into AL
sub al, [edi-1] ; subtract EDI[-1] from AL
因为我们希望减法的结果正确符号扩展为 eax
。