简体   繁体   中英

Compiling GNU assembler to windows

I need to compile assembly file that is written in GNU assembler in windows. The file is compiled correctly in Linux using as assembler. The assembly file contains a global deceleration of function that receives 2 params, which should be called from C/C++ visual studio 2013.

How can I do that ?

Here are part of .s file:

.globl func
func:
pushq %rbp
pushq %r12
pushq %r15
pushq %r13
pushq %r14
subq $40,%rsp

movq %rdi,%rbp //the first param
movq %rsi,%r12 //the second param

//real code starts here...


//restore stack and return

addq $40,%rsp
popq %r14
popq %r13
popq %r15
popq %r12
popq %rbp
ret

Thanks all !

Microsoft calling convention differs from the sysv abi that pretty much the rest of the world uses. If the code that you omitted from the question is self-contained, you basically only need to worry about argument passing and preserving registers.

Luckily, to access the arguments you just have to replace the two registers rdi and rsi with rcx and rdx , respectively.

As for saving registers, the code is presumably not modifying rbx as that needs to be preserved and there is no matching push . Additionally, both conventions preserve r12 through r15 so we don't need to worry about those. Microsoft convention however also mandates rdi and rsi be preserved, which are used for arguments under the sysv abi and are otherwise considered volatile. Thus, you need to add a push / pop pair for those.

You didn't mention whether the function body uses any SIMD or FPU things, so I am not going to cover those. Also, I assume your function is returning void .

As such, the modified assembly could look like:

.globl func
func:
pushq %rbp
pushq %r12
pushq %r15
pushq %r13
pushq %r14
pushq %rdi
pushq %rsi
subq $40,%rsp

movq %rcx,%rbp //the first param
movq %rdx,%r12 //the second param

//real code starts here...


//restore stack and return

addq $40,%rsp
popq %rsi
popq %rdi
popq %r14
popq %r13
popq %r15
popq %r12
popq %rbp
ret

On the C side, you just need to declare it as extern void func(int a, int b) or equivalent. When calling from c++, you should use extern "C" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM