如何让strcmp在汇编中返回0
我希望对 strcmp 函数的调用返回 0,这意味着
int strncmp(const char *s1, const char *s2, size_t n);
const char *s1 和 const char *s2 应包含相同的字符串。如果s2
指向字符串“hello”并且n
是4,我如何将一个也对应于<的十进制值传递给s1
代码>你好?
8049e87: c7 44 24 08 04 00 00 movl $0x4,0x8(%esp) // 4
8049e8e: 00
8049e8f: c7 44 24 04 80 bd 04 movl $0x804bd80,0x4(%esp) // the constant is "hello"
8049e96: 08
8049e97: 89 04 24 mov %eax,(%esp) // The contents of %eax are a decimal (%d)
8049e9a: e8 61 ec ff ff call 8048b00 <strncmp@plt>
8049e9f: 85 c0 test %eax,%eax // I want this to be 0!
我尝试以 ASCII 形式传递“h”的十进制值,这似乎是正确的方向,但不完全正确。
I want the call to the strcmp function to return 0, which means
int strncmp(const char *s1, const char *s2, size_t n);
const char *s1
and const char *s2
should contain the same string. If s2
points to the string "hello" and n
is 4, how can I pass to s1
a decimal value that will also correspond to hello
?
8049e87: c7 44 24 08 04 00 00 movl $0x4,0x8(%esp) // 4
8049e8e: 00
8049e8f: c7 44 24 04 80 bd 04 movl $0x804bd80,0x4(%esp) // the constant is "hello"
8049e96: 08
8049e97: 89 04 24 mov %eax,(%esp) // The contents of %eax are a decimal (%d)
8049e9a: e8 61 ec ff ff call 8048b00 <strncmp@plt>
8049e9f: 85 c0 test %eax,%eax // I want this to be 0!
I tried passing in the decimal value for "h" in ASCII, and it seemed to be the right direction, but not fully.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据定义,对于大小写和长度相同的两个字符串,
strncmp
的返回值为零。查看您的汇编代码,行:
不是
strncmp
函数的一部分。使用调试器,在此指令处放置一个断点。检查
EAX
寄存器,它应该为零(取决于strncmp
函数是否在EAX
寄存器中返回其结果)。test
汇编指令将根据参数的值设置条件代码。流行的条件代码位是零位,指示表达式为零。如果条件代码为零,则下一条指令可能是跳转。如果您在数学语句或表达式中使用
strncmp
函数的结果,编译器可能会生成不同的代码。试试这个片段:
您需要编译器保存
strncmp
中的值是否有原因?您需要编译器将该值与常量数字零进行比较是否有原因?
By definition, the return value of
strncmp
is zero for two strings that are the same in case and length.Looking at your assembly code, the line:
is not part of the
strncmp
function.Using a debugger, put a breakpoint at this instruction. Examine the
EAX
register, it should be zero (depending if thestrncmp
function returns its result in theEAX
register).The
test
assembly instruction will set condition codes depending on the value of the parameters. A popular condition code bit is the zero bit indicating an expression is zero. The next instruction may be a jump if condition code is zero.If you use the result of the
strncmp
function in a mathematical statement or expression, the compiler may generate different code.Try this fragment:
Is there a reason you need the compiler to save the value from
strncmp
?Is there a reason you need the compiler to compare the value to constant numeric zero?