LoginSignup
0
0

Visual Studio アセンブラでwsprintfを使ってみた

Last updated at Posted at 2022-12-05

環境

Windows 11 Pro (64bit)
Visual Studio Community 2022

x86

バッチファイル

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

アセンブリ ソース

wsprintf.asm
comment *
ml /c /Fl /Sa wsprintf.asm
link /subsystem:console wsprintf
*
includelib kernel32
includelib user32

.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
wsprintfA proto c :dword,:dword,:vararg

.const
fmt	db	'[%x]',0dh,0ah,0

.data?
hConOut	dd	?
buf	db	1024 dup (?)
len	dd	?

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

実行例

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

 Assembling: wsprintf.asm

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


D:\Projects\msvc>wsprintf
[cafebabe]

D:\Projects\msvc>

x64

バッチファイル

dev64.bat
@echo off
path %path%;C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.37.32822\bin\Hostx64\x64
set lib=%lib%;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22000.0\um\x64
prompt $e[33m$p$g$e[m
cmd

アセンブリ ソース

printf64.asm
comment *
ml64 /c /Fl printf64.asm
link /subsystem:console /entry:start printf64
*
includelib kernel32
includelib user32

NULL equ 0
STD_OUTPUT_HANDLE equ -11

extern ExitProcess :proc
extern GetStdHandle :proc
extern WriteConsoleA :proc
extern wsprintfA :proc

.const
fmt	db	'hoge %#x %d',0dh,0ah,0

.data?
hOut	dq	?
buf	db	1024 dup(?)

.code
start proc
	sub	rsp, 28h
	mov	ecx, STD_OUTPUT_HANDLE
	call	GetStdHandle
	mov	hOut, rax

	mov	r9d, 59634
	mov	r8d, 0f3c8h
	mov	rdx, offset fmt
	mov	rcx, offset buf
	call	wsprintfA

	mov	qword ptr [rsp+20h], NULL
	mov	r9d, NULL
	mov	r8d, eax
	mov	rdx, offset buf
	mov	rcx, hOut
	call	WriteConsoleA

	mov	ecx, 0
	call	ExitProcess
start endp
end

実行例

Microsoft Windows [Version 10.0.22621.2134]
(c) Microsoft Corporation. All rights reserved.

C:\Projects\msvc>ml64 /c /Fl printf64.asm
Microsoft (R) Macro Assembler (x64) Version 14.37.32822.0
Copyright (C) Microsoft Corporation.  All rights reserved.

 Assembling: printf64.asm

C:\Projects\msvc>link /subsystem:console /entry:start printf64
Microsoft (R) Incremental Linker Version 14.37.32822.0
Copyright (C) Microsoft Corporation.  All rights reserved.


C:\Projects\msvc>printf64
hoge 0xf3c8 59634
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