使用YASM编程 - 04
2017-09-04 17:01
513 查看
在linux下使用yasm
先介绍 64位的汇编开发
再介绍32位的开发:
先介绍 64位的汇编开发
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;test.asm ;在Linux下使用yasm ;安装: yasm sudo apt install yasm ;编译: yasm -f elf64 -o test64.o test.asm ;链接 ld -e start -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o test64 -lc test64.o ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; bits 64 section .data hello: db "hello",0x0a,0 format: db "msg:%s %d %d %d %d %d %d ",0x0a,0 tmp1 equ 1 tmp2 equ 2 tmp3 equ 3 tmp4 equ 4 tmp5 equ 5 tmp6 equ 6 extern printf extern exit ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ;实现在x64 下使用的 invoke ;自动将参数压入 rdi rsi rdx rcx r8 r9 ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;%ifidn 两个标识符是否相等 ;%ifid 是否id;%ifnum 是否数字%ifstr 是否字符串 %macro invoke 1-* %define jtmp %1 %define jcount %0 %if %0>1 %if %0>=2 mov rdi,%2 %endif %if %0>=3 mov rsi,%3 %endif %if %0>=4 mov rdx,%4 %endif %if %0>=5 mov rcx,%5 %endif %if %0>=6 mov r8,%6 %endif %if %0>=7 mov r9,%7 %endif %if %0-7>0 %rep %0-7 ;参数旋转,最后一个参数为1 %rotate -1 ;将 %1 参数入栈 push %1 %endrep %endif %endif call jtmp %if jcount>6 add rsp,(jcount-7)*8 %endif %endmacro section .text global start start: ; 在Linux x64 调用约定为 ; rdi ; rsi ; rcx ; rdx ; r8 ; r9 ; 多余的参数使用栈来传递 invoke printf,format,hello,tmp1,tmp2,tmp3,tmp4,tmp5,tmp6 ; 如果不调用exit ,退出的时候会崩溃 invoke exit,0 ret
再介绍32位的开发:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;test32.asm ;在Linux下使用yasm ;安装yasm: sudo apt install yasm ;编译: yasm -f elf32 -o test32.o test32.asm ;链接 ld -m elf_i386 -e start -dynamic-linker /lib32/ld-linux.so.2 -o test32 -lc test32.o ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; section .data hello: db "hello yasm",0x0a,0 %macro invoke 1-* %define jtmp %1 %define jcount %0 %if %0>0 %rep %0-1 ;参数旋转,最后一个参数为1 %rotate -1 ;将 %1 参数入栈 push %1 %endrep %endif call jtmp %if jcount>1 add esp,(jcount-1)*4 %endif %endmacro extern printf extern exit section .text global start start: invoke printf,hello ;这里不调用exit会崩溃 invoke exit,0 ret
相关文章推荐
- 【VBA编程】04.使用自定义数据类型
- .天轰穿C# -vs2010 - 04面向对象的编程之类的使用【原创】 2012-12-17 10:55阅读(0).
- 使用YASM编程 - 02
- 使用YASM编程 - 06
- 使用YASM编程 - 03
- 使用YASM编程 -07 模拟导入表
- 04-JDBC学习手册:JDBC中使用transaction(事务)编程和Javabean定义
- 使用YASM编程 - 01
- UC编程04-io读写write/read系统函数的使用
- 使用YASM编程 - 05
- linux下RTP编程(使用JRTPLIB)
- Kafka.net使用编程入门(四)
- 使用pthread库进行多线程编程1 - UNIX环境高级编程第11章读书笔记
- Liunx 命令行与shell脚本编程大全 第五章学习笔记(Vim 编辑器使用详解 解释+图示)
- iOS开发之网络编程--使用NSURLConnection实现大文件断点续传下载+使用输出流代替文件句柄
- linux使用tmux打造酷炫编程环境
- 高级UNIX编程 的源文件使用
- IOS多线程编程之NSOperation和NSOperationQueue的使用
- 使用ClearType使经典编程字体Courier New赏心悦目
- Android编程:使用toolbar