简单的nasm和cl互操作

来源:互联网 发布:短期网络理财产品 编辑:程序博客网 时间:2024/06/04 04:38

一共三个文件:

m.asm 汇编模块

test.c c调用模块

bd.cmd 批处理编译模块

编译的时候要设置好nasm和cl相关的环境变量。


m.asm内容:

segment .bssglobal _bufglobal _buf1_buf    resb 16_buf1   rest 1segment .dataglobal _v1global _pv2global _v3global _v4my_size equ 7%define my_size 7 + 1_v1 dd -1_pv2 dd _v1_v3 db "1Hello World1",0_v4 times 4 dd 3msg db "in func 'Return1'",0ah,0segment .textglobal _Return1_Return1:push    ebpmov     ebp,esppush    ecxextern _printfpush    msgcall    _printfadd     esp,4mov     eax,[ebp + my_size]add     eax,[ebp + 12]mov     [ebp - 4],eaxmov     dword [_v4],1mov     dword [_v4 + 4],2mov     dword [_v4 + 8],3mov     dword [_v4 + 12],4inc     dword [_v4 + 12]dec     dword [_v4 + 12]mov     byte [_buf1],11mov     byte [_buf1 + 1],12mov     byte [_buf1 + 2],13mov     byte [_buf1 + 3],14mov     byte [_buf1 + 4],15mov     byte [_buf1 + 5],16mov     byte [_buf1 + 6],17mov     byte [_buf1 + 7],18mov     esp,ebppop     ebpret

test.c内容:

#include <stdio.h>#include <assert.h>int Return1(int a, int b);extern int v1;extern int* pv2;extern char v3[];extern int v4[];extern char buf[]; // buf's size = 16extern char buf1[]; // buf1's size = 8int main(int argc, char* argv[]){    {        sprintf(buf, "Hello World\n");        printf(buf);    }    printf("%d\n", Return1(3, 7));     printf("%d\n", v1);    printf("%d\n", *pv2);    printf("%d, %d\n", &v1, pv2);    printf("%s\n", v3);    {        int i;        for (i = 0; i < 4; ++i)        {            printf("%d, ", v4[i]);        }        printf("\n");        for (i = 0; i < 8; ++i)        {            printf("%d, ", buf1[i]);        }     }    return 0;}

bd.cmd文件内容:

@echo offecho + batch script for windows sdk(c version).echo + usage      : type "bd" or "bd NDEBUG" or "bd clean".echo + "bd"       : build debug version.echo + "bd NDEBUG": build release version.echo + "bd clean" : delete result files.if "%1" equ "clean" goto cleanif "%1" neq "" (if "%1" equ "NDEBUG" (echo + & echo + build release version. & echo + & goto release) else (echo + & echo + invalidate parameter. & echo + & goto end)) else (echo + & echo + build debug version. & echo + & goto debug):cleanecho + & echo + delete result files. & echo + & del *.obj & del *.exe & del *.res & del *.exp & del *.lib & del *.dllgoto end:releasenasm -f win32 m.asmcl /MD /D%1 test.c m.objgoto end:debugnasm -f win32 m.asmcl /MDd test.c m.objgoto end:endif exist test.exe.manifest mt -manifest test.exe.manifest -outputresource:test.exedel *.manifest


编译和执行过程:

D:\bd\inst\ms-ex\test>dir 驱动器 D 中的卷是 program 卷的序列号是 0433-59C8 D:\bd\inst\ms-ex\test 的目录2011-11-24  15:07    <DIR>          .2011-11-24  15:07    <DIR>          ..2011-11-10  10:22               901 bd.cmd2011-11-24  09:44               945 m.asm2011-11-23  09:29               753 test.c               3 个文件          2,599 字节               2 个目录 60,737,560,576 可用字节D:\bd\inst\ms-ex\test>bd NDEBUG+ batch script for windows sdk(c version).+ usage      : type "bd" or "bd NDEBUG" or "bd clean".+ "bd"       : build debug version.+ "bd NDEBUG": build release version.+ "bd clean" : delete result files.++ build release version.+Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86Copyright (C) Microsoft Corporation.  All rights reserved.test.cMicrosoft (R) Incremental Linker Version 8.00.50727.762Copyright (C) Microsoft Corporation.  All rights reserved./out:test.exetest.objm.objMicrosoft (R) Manifest Tool version 5.2.3790.2075Copyright (c) Microsoft Corporation 2005.All rights reserved.D:\bd\inst\ms-ex\test>testHello Worldin func 'Return1'10-1-14206652, 42066521Hello World11, 2, 3, 4,11, 12, 13, 14, 15, 16, 17, 18,D:\bd\inst\ms-ex\test>


nasm虽然没有masm那样高级的宏,但是从另一方面来讲,这也是它的优点吧。

参考资料来自:http://www.drpaulcarter.com/pcasm/pcasm-book-simplified-chinese.zip,还要谢谢这位来自中国的译者,如果没有他,我就只有看英文了。


原创粉丝点击