【发布时间】:2014-05-04 23:30:01
【问题描述】:
我正在尝试调用sprintf 来格式化字符串并将结果存储在堆栈变量中。但是,我的尝试惨遭失败,并立即崩溃。
sub esp, 0x100 ;Allocate 256 bytes on the stack.
push dword[RequestedFile] ;push string2
push dword[Host] ;push string1
push dword[GetHeader] ;push format "String1: %s, String2: %s"
push dword[ebp - 0x04] ;push buffer/stack variable
call [sprintf] ;store string in buffer
add esp, 0x10 ;restore stack
push dword[ebp - 0x04] ;push the stack variable.
push StringFormat ;push the format
call [printf] ;print the new string.
add esp, 0x08 ;restore the stack
add esp, 0x100 ;destroy the stack variable.
任何想法我做错了什么?
【问题讨论】:
-
它在哪里崩溃?你的
add esp, 0x04不应该是add esp, 0x08吗?如果您可以访问 C 编译器,您可能希望查看 C 中的等效示例并查看它生成的内容。 -
它在调用
sprintf时崩溃是的,你是对的,它应该是0x08。已修复,但在调用sprintf时仍会崩溃。我尝试了 gcc explorer: gcc.godbolt.org 和-m32标志,但我不明白 lea 和它使用的指令。我更喜欢使用push。它似乎也不叫sprintf。 -
lea是一个非常基本、重要的指令。如果您打算使用组装,您应该熟悉它。