如何将x86和x64 asm文件包含到单个Visual Studio项目中?

时间:2018-07-06 21:04:39

标签: visual-studio assembly masm

我正在使用Visual Studio 2017社区构建测试控制台C ++应用程序。我需要在该项目中包含一个汇编函数:

extern "C" void* __fastcall getBaseFS(void);

要包含一个asm文件,我右键单击该项目,然后转到“构建依赖项”->“构建定制”,然后在列表中选中“ masm”。

然后,我可以通过右键单击我的项目->添加新项->然后添加“ asm_x64.asm”文件来添加一个asm文件,在其中写入我的x86-64 asm代码:

.code

getBaseFS PROC

mov ecx, 0C0000100H   ; IA32_FS_BASE
rdmsr

shl rdx, 32
or rax, rdx

ret

getBaseFS ENDP

END

这适用于64位项目。

问题是当我将解决方案平台从x64切换到x86时:

enter image description here

我的asm文件需要更改。因此,从某种意义上讲,我需要在编译中包含一个不同的“ asm_x86.asm”文件,该文件仅用于x86构建与x64构建。

自动执行此开关的最佳方法是什么?

2 个答案:

答案 0 :(得分:2)

好的,感谢Michael Petch,我已经解决了。必须将x64x86代码都放在一个.asm文件中。

(还有另一个建议的选项来处理构建配置,但是我更喜欢在此显示的方法。当解决方案从计算机转移到计算机时,这些构建配置消失了,我真倒霉。)

因此,我不确定using IFDEF RAX worksMicrosoft's own proposed ifndef X64为什么不这样做。但是,哦。如果有人知道,请发表评论。

asm_code.asm文件:

IFDEF RAX
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; x64 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; WinAPI to call
extrn Beep : proc


.data

align   8
beep_freq:
    dq  700 ; hz
beep_dur:
    dq  200 ; ms
str_from:
    db  "Hail from x64 asm", 0



.code

useless_sh_t_function__get_GS_a_string_and_beep PROC
    ; parameter = CHAR** for a string pointer
    ; return = value of GS register selector

    mov     rax, str_from
    mov     [rcx], rax

    mov     rdx, qword ptr [beep_dur]
    mov     rcx, qword ptr [beep_freq]
    call    Beep

    mov     rax, gs
    ret
useless_sh_t_function__get_GS_a_string_and_beep ENDP




ELSE
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; x86 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.686p  
.XMM  
.model flat, C  


.data

align   4
beep_freq   dd  700 ; hz
beep_dur    dd  200 ; ms
str_from    db  "Hail from x86 asm", 0


.code

; WinAPI to call
extrn stdcall Beep@8 : proc


useless_sh_t_function__get_GS_a_string_and_beep PROC
    ; parameter = CHAR** for a string pointer
    ; return = value of GS register selector

    mov     eax, [esp + 4]
    mov     [eax], OFFSET str_from

    push    dword ptr [beep_dur]
    push    dword ptr [beep_freq]
    call    Beep@8

    mov     eax, gs
    ret
useless_sh_t_function__get_GS_a_string_and_beep ENDP


ENDIF

END

main.cpp文件:

#include "stdafx.h"
#include <Windows.h>

extern "C" {
    size_t useless_sh_t_function__get_GS_a_string_and_beep(const CHAR** ppString);
};

int main()
{
    const char* pString = NULL;
    size_t nGS = useless_sh_t_function__get_GS_a_string_and_beep(&pString);
    printf("gs=0x%Ix, %s\n", nGS, pString);

    return 0;
}

答案 1 :(得分:2)

很好,您找到了一种处理用例的方法。

但是,如果您有更多的asm文件,或者只需要某些构建类型的文件,则还可以更改每个单独文件的设置,使其与项目默认设置不同。

只需右键单击文件名即可获得其各个属性。

要仅在x64版本中包含一个asm文件,可以使用以下设置:

enter image description here

然后将其从32位版本中排除:

enter image description here

这些设置适用于所有文件类型,不仅适用于.asm文件。