我从fasm (Version1.71.51)代码中调用memcmp,得到了奇怪的结果。
似乎memcmp只比较处于奇数位置的字符。代码:
format ELF64
section '.text' executable
public _start
extrn memcmp
extrn exit
_start:
push rbp ; Align the stack to 16 bytes
mov rdi, str1
mov rsi, str2
mov rdx, BUFF_LEN
call memcmp
mov rdi, rax
call exit
pop rbp
section '.data' writeable
str1 db '1509487271'
BUFF_LEN = $ - str1
str2 db '1509487273'我在运行Ubuntu:
$ uname -a
Linux freedom 4.13.0-43-generic #48~16.04.1-Ubuntu SMP Thu May 17 12:56:46 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux为了组装和运行上面的代码,我使用以下命令:
$ fasm strange_memcmp.asm && ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 -lc -melf_x86_64 strange_memcmp.o -o strange_memcmp.out && ./strange_memcmp.out
flat assembler version 1.71.51 (16384 kilobytes memory)
3 passes, 803 bytes.
$ echo $?
0我希望memcmp会返回一个不是0的值,因为缓冲区是不同的。
我试过换两个缓冲器。如果我设置:
str1 = '2509487271'
str2 = '1509487273'我得到的返回值为1。
如果我设置:
str1 = '1409487271'
str2 = '1509487273'我得到一个返回值为0。似乎memcmp只关心偶数位置的字符: 0,2,4,.等。
我有一种奇怪的感觉,我不正确地称呼memcmp。这可能与Linux有关,但我找不出问题所在。任何想法都是值得赞赏的!
编辑:根据雷蒙德的回答,进程的返回值带有一个掩码。
为了修复上面的代码,我添加了两个说明:
neg rax
sbb rax, rax就在打给memcmp之后。
发布于 2018-06-16 17:29:15
您正在将从memcmp返回的64位值直接传递给exit,但传递给 before being returned to the parent。memcmp可能会返回一个类似于512的非零值,但是系统会将其截断为0。
https://stackoverflow.com/questions/50889769
复制相似问题