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.

Visual Studio コンソールアプリをアセンブルしてみた

Posted at

環境

Windows 11 Pro (64bit)
Visual Studio Community 2022

Cをアセンブリ出力

Cをアセンブリ出力して参考にする。

バッチファイル

dev32crt.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\shared
set include=%include%;C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\ucrt
set include=%include%;C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\um
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

Cソース

hello_c.c
/*
cl /MD /FAscu hello_c.c
*/
#include <windows.h>

int main()
{
	static char buf[] = "hello, world\r\n";
	static HANDLE hConOut;

	hConOut = GetStdHandle(STD_OUTPUT_HANDLE);
	WriteConsole(hConOut, buf, sizeof buf, NULL, NULL);
	ExitProcess(0);
}

実行例

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

hello_c.c
Microsoft (R) Incremental Linker Version 14.34.31935.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:hello_c.exe
hello_c.obj

D:\Projects\msvc>hello_c
hello, world

D:\Projects\msvc>

アセンブリ出力

抜粋

hello_c.cod
; 11   : 	hConOut = GetStdHandle(STD_OUTPUT_HANDLE);

  00003	6a f5		 push	 -11			; fffffff5H
  00005	ff 15 00 00 00
	00		 call	 DWORD PTR __imp__GetStdHandle@4
  0000b	a3 00 00 00 00	 mov	 DWORD PTR ?hConOut@?1??main@@9@9, eax

アセンブラ

バッチファイル

dev32.bat
@echo off
path %path%;D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.34.31933\bin\Hostx64\x86
set lib=%lib%;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.19041.0\um\x86
cmd

アセンブリ ソース

hello.asm
comment *
ml /c hello.asm
link /subsystem:console hello
*
includelib kernel32

.model flat

NULL equ 0
STD_OUTPUT_HANDLE equ -11

ExitProcess proto stdcall :dword
GetStdHandle proto stdcall :dword
WriteConsoleA proto stdcall :dword,:dword,:dword,:dword,:dword

.const
buf	db	'hello, world',0dh,0ah,0
len	equ	$ - buf

.data?
hConOut	dd	?

.code
start proc c
	invoke	GetStdHandle, STD_OUTPUT_HANDLE
	mov	hConOut, eax
	invoke	WriteConsoleA, hConOut, addr buf, len, NULL, NULL
	invoke	ExitProcess, 0
start endp

end start

実行例

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

 Assembling: hello.asm

D:\Projects\msvc>link /subsystem:console hello
Microsoft (R) Incremental Linker Version 14.34.31935.0
Copyright (C) Microsoft Corporation.  All rights reserved.


D:\Projects\msvc>hello
hello, world

D:\Projects\msvc>
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?