0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

MASM(x64)でdllを作る

Last updated at Posted at 2022-02-02

#MASM(x64)でdllを作る

Windows 10 Pro (64bit)
Visual Studio 2022 Community

dlltest.asm
comment *
ml64 /c /Fl dlltest.asm
link /dll /entry:DllMain dlltest
*
.code

DllMain proc
	mov	eax, 1
	ret
DllMain endp

myadd proc export
	mov	rax, rcx
	add	rax, rdx
	ret
myadd endp

mysub proc export
	mov	rax, rcx
	sub	rax, rdx
	ret
mysub endp

end
dev.bat
@echo off
path %path%;C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\bin\Hostx64\x64
path %path%;C:\Windows\Microsoft.NET\Framework64\v4.0.30319
cmd

##C#でテスト
x64版のcsc.exeにpathを通す。

dlltest.cs
// csc dlltest.cs

using System;
using System.Runtime.InteropServices;

class Program {
	[DllImport("dlltest.dll")]
	static extern int myadd(int a, int b);
	[DllImport("dlltest.dll")]
	static extern int mysub(int a, int b);

	static void Main() {
		Console.WriteLine("add = {0}", myadd(1, 2));
		Console.WriteLine("sub = {0}", mysub(1, 2));
	}
}

##実行例
m.png

#CPUIDサンプル

cpuid.asm
comment *
ml64 /c /Fl cpuid.asm
link /dll /entry:DllMain cpuid
*
.code

DllMain proc
	mov	eax, 1
	ret
DllMain endp

getcpuid proc export
	push	rbx
	mov	r8, rcx
	mov	eax, edx
	cpuid
	mov	[r8   ], eax
	mov	[r8+ 4], ebx
	mov	[r8+ 8], ecx
	mov	[r8+12], edx
	pop	rbx
	ret
getcpuid endp

end
cpuid.cs
// csc cpuid.cs

using System;
using System.Runtime.InteropServices;
using System.Text;

class Program {
	[DllImport("cpuid.dll")]
	static extern void getcpuid(byte[] p, uint eax);

	static void Main() {
		var ascii = new ASCIIEncoding();
		var b = new byte[16];
		for (uint i = 0; i < 3; i++) {
			getcpuid(b, 0x80000002 + i);
			Console.WriteLine("[{0}]", ascii.GetString(b));
		}
	}
}

出力例

C:\Projects\MASM\cpuid>cpuid
[Intel(R) Celeron]
[(R) N4000 CPU @ ]
[1.10GHz         ]

#参考
https://qiita.com/Stosstruppe/items/f0be3a9cededf3e8a62a

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?