gpt4 book ai didi

gcc - 在 Dev-C++ 中的 GCC 内联汇编中定义字节(Windows 上 AT&T 语法中的 .ascii)

转载 作者:行者123 更新时间:2023-12-03 03:36:23 25 4
gpt4 key购买 nike

下面的代码只是在屏幕上显示一个消息框。
地址被硬编码以方便:

int main ()
{
asm("xorl %eax, %eax \n"
"xorl %ebx, %ebx \n"
"xorl %ecx, %ecx \n"
"xorl %edx, %edx \n"
"pushl %ecx \n" //$0x0
"pushl $0x20206c6c \n" //" ll"
"pushl $0x642e3233 \n" //"d.23"
"pushl $0x72657375 \n" //"resu"
"movl %esp, %ecx \n" //store "user32.dll" address in %ecx
"movl $0x7c801d7b, %ebx \n" //store address of LoadLibraryA in %ebx
"pushl %ecx \n"
"call *%ebx \n"
"movl $0xef30675e, %ecx \n"
"addl $0x11111111, %ecx \n"
"pushl %ecx \n"
"pushl $0x42656761 \n"
"pushl $0x7373654d \n"
"movl %esp, %ecx \n"
"pushl %ecx \n"
"pushl %eax \n"
"movl $0x7c80ae40, %ebx \n"
"call *%ebx \n"
"movl %esp, %ecx \n"
"xorl %edx, %edx \n"
"pushl %edx \n"
"pushl %ecx \n"
"pushl %ecx \n"
"pushl %edx \n"
"call *%eax \n"
"xorl %eax, %eax \n"
"pushl %eax \n"
"movl $0x7c81cb12, %eax \n"
"call *%eax \n"
);
}

(我没有评论所有代码,因为我的问题并不是真正关于代码)

我的问题是:有没有办法在程序集中内联写入字符串“user32.dll”,而无需手动推送到堆栈?我的意思是在 NASM 中这样:db 'Hello'

我知道在 AT&T 语法中我可以执行 .ascii 'Hello'.string 'Hello' 但在 gcc 内联中怎么样?

请注意,我在 Windows XP SP3 上使用 Dev-C++

谢谢!

最佳答案

是的,通过在内联汇编器中使用汇编器指令。诀窍在于将字符串放在正确的位置(数据部分),您可以通过使用 .section .data 进行切换来完成此操作。 ,然后再次切换回来 .section .text .

您必须给数据一个标签,以便您可以引用;我建议在这里使用本地标签语法(其中标签是一个数字,例如 1: ,您可以将其引用为 1b (第一个 1: 向后标签),或 1f (第一个 1: )标签转发 - 请参阅 GNU assembler documentation 了解更多详细信息)。

像这样:

int main(void)
{
asm(".section .data \n"
"1: .asciz \"Hello\" \n"
".section .text \n"
"pushl $1b \n"
"call _puts \n"
"add $4, %esp \n"
);
return 0;
}

我没有方便的 Windows 系统来测试它,但它编译正常,并且看起来应该在 Linux 上使用 MinGW 交叉编译器做正确的事情(我相信 Dev-C++ 基于 MinGW) .

注意:此技术通常适用于使用 GNU 工具链时。如果您正在构建 ELF 二进制文件(例如 native Linux),有一种更简洁的方法可以切换回文本部分,即使用 .previous ,这意味着“无论前一个 .section 之前的部分是什么”。 (如果将 _puts 更改为 puts 以考虑不同的符号前缀约定,则上述示例适用于 Linux。)

关于gcc - 在 Dev-C++ 中的 GCC 内联汇编中定义字节(Windows 上 AT&T 语法中的 .ascii),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3712062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com