LoginSignup
0
1

More than 1 year has passed since last update.

VC++インラインアセンブリとMASMとのリンク

Posted at

はじめに

を実際に動かしてみた。

環境

Windows 11 Pro (64bit)
Visual Studio Community 2022

インラインアセンブリ

ソースなど

dev.bat
@echo off
path %path%;D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.34.31933\bin\Hostx64\x86
set include=%include%;D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.34.31933\include
set include=%include%;C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\ucrt
set lib=%lib%;D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.34.31933\lib\x86
set lib=%lib%;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.19041.0\ucrt\x86
set lib=%lib%;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.19041.0\um\x86
cmd
power2.cpp
/*
cl /Wall /FAscu power2.cpp
*/
#include <stdio.h>

int power2(int num, int power)
{
	__asm {
	mov	eax, num
	mov	ecx, power
	shl	eax, cl
	}
}

int main(void)
{
	printf("3 * 2^5 = %d\n", power2(3, 5));
}

実行例

D:\Projects\msvc>cl /Wall /FAscu power2.cpp
Microsoft(R) C/C++ Optimizing Compiler Version 19.34.31935 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

power2.cpp
Microsoft (R) Incremental Linker Version 14.34.31935.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:power2.exe
power2.obj

D:\Projects\msvc>power2
3 * 2^5 = 96

D:\Projects\msvc>

MASMとのリンク

ソースなど

powera.asm
comment *
ml /c /Fl /Sa powera.asm
*
.model flat
.code
num$ = 8
power$ = 12
power2 proc c
	push	ebp
	mov	ebp, esp
	mov	eax, num$[ebp]
	mov	ecx, power$[ebp]
	shl	eax, cl
	pop	ebp
	ret
power2 endp
end
powerc.cpp
/*
cl /Wall /FAscu powerc.cpp powera.obj
*/
#include <stdio.h>

extern "C" int power2(int, int);

int main(void)
{
	printf("3 * 2^4 = %d\n", power2(3, 4));
}

実行例

D:\Projects\msvc>ml /c /Fl /Sa powera.asm
Microsoft (R) Macro Assembler Version 14.34.31935.0
Copyright (C) Microsoft Corporation.  All rights reserved.

 Assembling: powera.asm

D:\Projects\msvc>cl /Wall /FAscu powerc.cpp powera.obj
Microsoft(R) C/C++ Optimizing Compiler Version 19.34.31935 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

powerc.cpp
Microsoft (R) Incremental Linker Version 14.34.31935.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:powerc.exe
powerc.obj
powera.obj

D:\Projects\msvc>powerc
3 * 2^4 = 48

D:\Projects\msvc>
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1