如何为 win32 汇编简单的独立函数,哪个汇编器?

发布于 2024-12-13 14:09:54 字数 1497 浏览 3 评论 0原文

非常tnx寻求帮助

我想组装一些简单的函数,例如

    struct float3
    {
     float x;
     float y;
     float z;
    };

    inline float dot(float3* a, float3* b)
    {
      return (*a).x * (*b).x + (*a).y * (*b).y + (*a).z * (*b).z;
    }

或更简单的函数,一开始

    int add(int a, int b) 
    { 
      return a + b; 
    }

我需要在x86程序集中编写它并生成.obj文件来链接 它并从c代码调用

1) 汇编程序应该是免费的并且可以免费使用 2)它应该为win32组装x86 3)应该能够生成较旧的 omf obj 二进制文件(这也许我可以跳过,因为 也许我可以使用 agnerfog 的一些工具 objnonv 将 coff 转换为 omf,也许) 4)应该支持新的指令集 - 至少是 sse 但更好的 avx

我还需要一个例子,如何编写这样的函数,在它下面,以及如何 将其组装成 obj

much tnx, fir

[编辑]

我找到了部分解决方案 - 在 nasm 中,我可以

    segment _TEXT public align = 1 class = CODE use32

    global  asm_main

    asm_main:
    enter   0,0
    pusha
    ;---------------------------
    ;
    ;
    ;----------------------------
    popa
    mov     eax, 2324            ; return back to C
    leave
    ret

在 ci 中进行组装,可以使用它,

    extern "C" int __cdecl asm_main(void);

    int ret_status = asm_main();  //gets 2324 as i want

但是当我尝试在其余部分之前添加数据部分时,

    segment _DATA public align = 4 class = DATA use32

    txt1 db    "xxxxxxxxxxxxx", 0
    txt2 db    "yyyyyyyyyyyyy", 0

我遇到了下一个错误,我的旧 borland c++ 5.5.1 编译器有

    Fatal: 'myasm.obj': Additional segments need to be defined in a .def file

错误知道我能做什么吗?

much tnx for help

I want to assembly some simple function like

    struct float3
    {
     float x;
     float y;
     float z;
    };

    inline float dot(float3* a, float3* b)
    {
      return (*a).x * (*b).x + (*a).y * (*b).y + (*a).z * (*b).z;
    }

or yet simpler for the beginning

    int add(int a, int b) 
    { 
      return a + b; 
    }

I need to write it in x86 assembly and produce .obj file to link
it and call from c code

1) assembler should be free of charge and free to use
2) it should assemble x86 for win32
3) should be able to produce older omf obj binary (this maybe i can skip because
possibly i could convert coff to omf with some tool objnonv by agner fog, maybe)
4) should support new instruction sets - at least sse but better avx also

also i need an example how tu write such function, under it, and how to
assemble it to obj

much tnx,
fir

[edit]

i have found partial solution - in nasm i can assemble

    segment _TEXT public align = 1 class = CODE use32

    global  asm_main

    asm_main:
    enter   0,0
    pusha
    ;---------------------------
    ;
    ;
    ;----------------------------
    popa
    mov     eax, 2324            ; return back to C
    leave
    ret

in c i can use it

    extern "C" int __cdecl asm_main(void);

    int ret_status = asm_main();  //gets 2324 as i want

but i have next error when i am trying to add data section before the rest

    segment _DATA public align = 4 class = DATA use32

    txt1 db    "xxxxxxxxxxxxx", 0
    txt2 db    "yyyyyyyyyyyyy", 0

i have error from my old borland c++ 5.5.1 compiler

    Fatal: 'myasm.obj': Additional segments need to be defined in a .def file

doeas anyone know what i can do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月依秋水 2024-12-20 14:09:54

MASM 是迄今为止 Windows 上最好的汇编器。 NASM 更面向 Linux。

结构以以下形式编写:

FLOAT3 STRUCT
    x DWORD ?
    y DWORD ?
    z DWORD ?
FLOAT3 ENDS

因为我对 nasm 更熟悉,所以 add 函数将如下所示:

编辑:

假设我们有另一个 rutine,想要在屏幕上打印 hello world (这是一个 linux rutine!

segment .data

text dw "hello",10,0 ; dw stans for define word
text_len EQU $ - text ; get the length of text

segment .text
extern _add
extern print

print:    ; not exportable to C in this format
    mov eax,4 ; 4 indices we what to write
    mov ebx,1 ; 1 indicated standard output
    mov ecx,text ; address of text is now in registe ecx
    mov edx,text_len ; value of text len is now in register edx
    int 80H ; now we call the kernel

    mov eax,1 ; last 3 lines enable us to exit the program normally. 
    xor ebx,ebx
    int 80H




_add:
   enter 0,0
   mov eax,[ebp+8]  ; first argument
   mov ebx,[ebp+12]  ; second argument
   add eax,ebx
   leave
   ret

)可以使用 nasm -f win32 first.asm 组装它,

然后在 c extern 中声明原型

MASM is by far the best assembler for Windows. NASM is much more Linux oriented.

A structure is written in form:

FLOAT3 STRUCT
    x DWORD ?
    y DWORD ?
    z DWORD ?
FLOAT3 ENDS

Because I am much more familiar with nasm, the add function would look like this:

EDITED:

assume we have another rutine, that want to print hello world on the screen ( this is a linux rutine!)

segment .data

text dw "hello",10,0 ; dw stans for define word
text_len EQU $ - text ; get the length of text

segment .text
extern _add
extern print

print:    ; not exportable to C in this format
    mov eax,4 ; 4 indices we what to write
    mov ebx,1 ; 1 indicated standard output
    mov ecx,text ; address of text is now in registe ecx
    mov edx,text_len ; value of text len is now in register edx
    int 80H ; now we call the kernel

    mov eax,1 ; last 3 lines enable us to exit the program normally. 
    xor ebx,ebx
    int 80H




_add:
   enter 0,0
   mov eax,[ebp+8]  ; first argument
   mov ebx,[ebp+12]  ; second argument
   add eax,ebx
   leave
   ret

you can assemble it using nasm -f win32 first.asm

then declare the prototype in c extern

捶死心动 2024-12-20 14:09:54

NASM 是迄今为止我用过的最好的。请在此处查找 OMF 支持。
关于将汇编代码链接到 C++,您可以在这里查看 链接

NASM is the best i've used so far. Look here for OMF support.
About linking Assembly code to C++ you can look here link

狂之美人 2024-12-20 14:09:54

使用 MASM,它是免费下载的或 MS Visual Studio 的一部分:

http://www.masm32.com/masmdl .htm

有一个命令行选项可以导出将您的函数链接到其他应用程序所需的目标文件:

http://msdn.microsoft.com/en-us/ library/s0ksfwcf(v=vs.80).aspx

或者您可以创建一个 DLL 并像往常一样链接它。

Use MASM which is a free download or part of MS Visual Studio:

http://www.masm32.com/masmdl.htm

There is a command line option to export the object file needed to link your function to other applications:

http://msdn.microsoft.com/en-us/library/s0ksfwcf(v=vs.80).aspx

Or you can just create a DLL and link it as usual.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文