使用YASM编程 - 02

Yasm 支持的GAS(Gnu asm)格式
实现:
1 基本框架
2 调用外部API
3 导出_start
4 mov xor push 等
5 使用 label 数字 寄存器
6 定义数字和定义字符串常量
7 编译
8 链接

/*********************************************************
 * Gnu asm 注释和字符串风格和C风格一样
 * 还支持单行注释#
 * gas                                                NASM
 * movl (movb movw)                           mov
 * pushl(pushb pushw)                        push
 * addl                                               add
 * xorl                                                xor
 * ...
 * ...
 * ...
 * 指令有(b w l 后缀)                              没有后缀
 * .global  _start                                 global  _start
 * hello: .string "hello\n"                      hello:  "hello",0x0a,0
 * %eax (%在寄存器使用的前缀)               eax(没有前缀)
 * $11                                               11(数字没有前缀)
 * .extern                                           extern
 * 
 * 如果是二元操作
 * 操作符 目的操作数,源操作数
 *********************************************************/
#testGas.asm
#os:windows
.data
hello: .string "hello GAS\n"
resultString: .string "Add1(%d,%d)=%d\n"
msg: .string "hello\n"
tlt: .string "msg\n"
one: .int 100
two: .int 200
.extern _MessageBoxA@16
.extern _printf
.global _start

#build.bat
/*****************************************************************************
vsyasm -p gas -w -f win32 testGAS.asm
link /entry:start /subsystem:console /libpath:C:\masm32\lib user32.lib msvcrt.lib testGAS.obj
*****************************************************************************/
.text
add1:
    pushl %ebp
    movl  %esp,%ebp
    movl  8(%ebp),%eax
    movl  12(%ebp),%edx
    addl   %edx,%eax
    popl   %ebp
    ret
callMessageBox:
    xorl %eax,%eax
    pushl %eax
    pushl $msg
    pushl $tlt
    pushl %eax
    call _MessageBoxA@16
    ret

_start:
    pushl one
    pushl two
    call add1
    addl $8,%esp
    pushl  %eax
    pushl  two
    pushl  one
    pushl $resultString
    call _printf

    addl $0x10,%esp
    ret

猜你喜欢

转载自blog.csdn.net/justin_bkdrong/article/details/77800716