環境
- Windows 11 Pro
- Visual Studio Community 2022
hello, world
devasm.bat
@echo off
set vcdir=C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.41.34120
path %path%;%vcdir%\bin\Hostx64\x64
set lib=%lib%;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\um\x64
prompt $e[33m$p$g$e[m
cmd
fig1.asm
; ml64 /c /Fl fig1.asm
; link /entry:start /subsystem:console fig1.obj
includelib kernel32
STD_OUTPUT_HANDLE equ -11
extern ExitProcess :proc
extern GetStdHandle :proc
extern WriteConsoleA :proc
.const
msg db 'hello, world',0dh,0ah
len equ $ - msg
.code
public start
start:
sub rsp, 56
mov ecx, STD_OUTPUT_HANDLE
call GetStdHandle
mov qword ptr [rsp+32], 0
xor r9d, r9d
mov r8d, len
lea rdx, offset msg
mov rcx, rax
call WriteConsoleA
xor ecx, ecx
call ExitProcess
end
Cコンパイラで/FAオプションを付けてアセンブリリスト(.cod)を出力すると参考になる。
fig2.c
// cl /MD /FAsc /O2 fig2.c
const char msg[7] = "hello\r\n";
int main(void)
{
void *h = GetStdHandle(-11);
WriteConsoleA(h, msg, 7, 0, 0);
ExitProcess(0);
}
MASMからCライブラリ関数を呼ぶ
fig3.asm
; ml64 /c /Fl fig3.asm
; cl /MD fig3.obj
includelib msvcrt
includelib legacy_stdio_definitions
extern printf :proc
.const
msg db 'hello, %d',0ah,0
.code
main proc
sub rsp, 40
mov edx, 42
lea rcx, offset msg
call printf
xor eax, eax
add rsp, 40
ret
main endp
end
Cからアセンブリ関数を呼ぶ
fig4.c
// cl /MD fig4.c fig4a.obj
#include <stdio.h>
int hoge(int);
int main(void)
{
printf("%d\n", hoge(42));
}
fig4a.asm
; ml64 /c fig4a.asm
.code
hoge proc
mov eax, ecx
add eax, eax
ret
hoge endp
end