博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Defining Bytes in GCC Inline Assembly in Dev-C++(.
阅读量:5881 次
发布时间:2019-06-19

本文共 3936 字,大约阅读时间需要 13 分钟。

hot3.png

6 down vote
2

The code below is just showing a Message Box on the screen.

The addresses are hardcoded to facilitate:

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"    );}

(I didn't comment all the code because my question is not really about the code)

My question is: Is there a way to write the string "user32.dll" in assembly inline without pushing manually to the stack? I mean like this in NASM: db 'Hello'

I know that in AT&T syntax I could do .ascii 'Hello' or .string 'Hello' but how about in gcc inline?

Please note that I'm using Dev-C++ on Windows XP SP3

Thanks!

|
add comment

1 Answer

up vote 7 down vote accepted

Yes, by making use of assembler directives inside your inline assembler.  The trick is in putting the string in the right place (the data section), which you can do by switching using .section .data, and then switching back again with .section .text.

You must give the data a label so that you can refer to it; I would recommend using the local label syntax here (where the label is a number, e.g. 1:, and you reference it as either 1b for the first 1: label backwards, or 1f for the first 1: label forwards - see the for more details).

Like this:

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;}

I don't have a Windows system handy to test this on, but it compiles OK and looks like it should be doing the right thing using a MinGW cross-compiler on Linux (I believe Dev-C++ is based on MinGW).

Note: this technique is generally applicable when using a GNU toolchain.  If you're building ELF binaries (e.g. native Linux), there is a neater way to switch back to the text section, which is to use .previous, which means "whatever the section before the previous .section was".  (The above example works on Linux if you change _puts to puts to account for different symbol prefixing conventions.)

|
Cool! Does "1:" means it's a label to the address of the string? Is the instruction "pushl $1b" pushing the address of "Hello"? What ".previous" do? Thanks!–                    
": Does ".previous" works on Windows? I'm not able to.. I'm using Dev-Cpp Portable–                    
1
@jyzuz: no, it seems not; sorry about that.  I've updated my answer with a way round that.  
1: is indeed a label, and
1b refers to it (see the assembler documentation I linked to in my updated answer); so yes,
pushl $1b pushes the value of the label - which is the address of the string - as a constant onto the stack.–                    

转载于:https://my.oschina.net/zhuzihasablog/blog/266505

你可能感兴趣的文章
20款绝佳的HTML5应用程序示例
查看>>
string::c_str()、string::c_data()及string与char *的正确转换
查看>>
11G数据的hive初测试
查看>>
如何使用Core Text计算一段文本绘制在屏幕上之后的高度
查看>>
==和equals区别
查看>>
2010技术应用计划
查看>>
XML 节点类型
查看>>
驯服 Tiger: 并发集合 超越 Map、Collection、List 和 Set
查看>>
Winform开发框架之权限管理系统改进的经验总结(3)-系统登录黑白名单的实现...
查看>>
Template Method Design Pattern in Java
查看>>
MVC输出字符串常用四个方式
查看>>
LeetCode – LRU Cache (Java)
查看>>
JavaScript高级程序设计--对象,数组(栈方法,队列方法,重排序方法,迭代方法)...
查看>>
【转】 学习ios(必看经典)牛人40天精通iOS开发的学习方法【2015.12.2
查看>>
在 ASP.NET MVC 中使用异步控制器
查看>>
SQL语句的执行过程
查看>>
Silverlight开发历程—动画(线性动画)
查看>>
详解Linux中Load average负载
查看>>
HTTP 协议 Cache-Control 头——性能啊~~~
查看>>
PHP遍历文件夹及子文件夹所有文件
查看>>