はじめに
先ずCのアセンブリ出力を参考にしてx64版を作り、x86版を作る。
環境
- Windows 11 Pro (64bit)
- Visual Studio Community 2022
VC++
fig1.c
// cl /MD fig1.c
// cl /c /FAscu fig1.c
#include <windows.h>
const char msg[] = "hello, world\r\n";
#define len 14
static HANDLE hOut;
int main(void)
{
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
WriteConsoleA(hOut, msg, len, NULL, NULL);
ExitProcess(0);
}
MASM(x64)
dev2.bat
@echo off
set vcdir=C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.42.34433
path %path%;%vcdir%\bin\Hostx64\x64
set lib=C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\um\x64
prompt $e[33m$p$g$e[m
cmd
fig2.asm
comment *
ml64 /c /Fl fig2.asm
link /subsystem:console /entry:_start fig2
*
includelib kernel32
extern ExitProcess :proc
extern GetStdHandle :proc
extern WriteConsoleA :proc
.const
_msg db 'hello, world',0dh,0ah
_len equ $ - _msg
.data?
_hOut dq ?
.code
public _start
_start:
sub rsp, 40
mov ecx, -11 ; STD_OUTPUT_HANDLE
call GetStdHandle
mov _hOut, rax
mov qword ptr [rsp+32], 0
xor r9d, r9d
mov r8d, _len
lea rdx, offset _msg
mov rcx, _hOut
call WriteConsoleA
xor ecx, ecx
call ExitProcess
end
MASM(x86)
dev3.bat
@echo off
set vcdir=C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.42.34433
path %path%;%vcdir%\bin\Hostx64\x86
set lib=C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\um\x86
prompt $e[33m$p$g$e[m
cmd
fig3.asm
comment *
ml /c /Fl /Sa fig3.asm
link /subsystem:console fig3
*
includelib kernel32
.model flat
ExitProcess proto stdcall, :dword
GetStdHandle proto stdcall, :dword
WriteConsoleA proto stdcall, :ptr, :ptr, :dword, :ptr, :ptr
.const
_msg db 'hello, world',0dh,0ah
_len equ $ - _msg
.data?
_hOut dd ?
.code
public _start
_start:
invoke GetStdHandle, -11 ; STD_OUTPUT_HANDLE
mov _hOut, eax
invoke WriteConsoleA, _hOut, addr _msg, _len, 0, 0
invoke ExitProcess, 0
end _start
mlに/coffを付け、.386を指定すればMASM32でも作れる。