各種OSアセンブリでプロセス終了

# CP/M

https://z88dk.org/site/

https://www.vector.co.jp/soft/dl/win95/util/se378130.html

```dev.bat
@echo off
path %path%;C:\etc\z88dk\bin
path %path%;C:\etc\cpm32_04
prompt $e[33m$p$g$e[m
cmd
```

```hello.asm
;z80asm -l -b -ohello.com hello.asm
;cpm hello

	org	$0100

	ld	de, msg
	ld	c, $09
	call	5
	jp	0

msg:	db	"hello, world",$0d,$0a,"$"
```

* 出力例

```
C:\Projects\CP_M>z80asm -l -b -ohello.com hello.asm

C:\Projects\CP_M>cpm hello
hello, world
```

# MS-DOS

https://www.masm32.com/

http://takeda-toshiya.my.coocan.jp/msdos/

```dev.bat
@echo off
path %path%;C:\masm32\bin
path %path%;C:\etc\msdos\binary\v30_x64
prompt $e[33m$p$g$e[m
cmd
```

```ext16.asm
comment *
ml /c /Fl ext16.asm
link16 /t ext16;
msdos ext16
echo %errorlevel%
*
.model tiny
.code
	org	0100h
start:
	mov	al, 42
	mov	ah, 4ch
	int	21h

end start
```

* 出力例

```
C:\Projects\MASM32>ml /c /Fl ext16.asm
Microsoft (R) Macro Assembler Version 6.14.8444
Copyright (C) Microsoft Corp 1981-1997.  All rights reserved.

 Assembling: ext16.asm

C:\Projects\MASM32>link16 /t ext16;

Microsoft (R) Segmented Executable Linker  Version 5.60.339 Dec  5 1994
Copyright (C) Microsoft Corp 1984-1993.  All rights reserved.

LINK : warning L4045: name of output file is 'ext16.com'

C:\Projects\MASM32>msdos ext16

C:\Projects\MASM32>echo %errorlevel%
42
```

## サンプル

<details><summary>hello</summary>

```hello.asm
comment *
ml /c /Fl hello.asm
link16 /t hello;
*
.model tiny
.code
	org	0100h
start:
	mov	dx, offset msg
	mov	ah, 09h
	int	21h
	mov	ax, 4c00h
	int	21h

msg	db	'hello, world',0dh, 0ah,'$'
end start
```

</details>

# Windows (x86)
Visual Studio 2022

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

```ext32.asm
comment *
ml /c /Fl /Sa ext32.asm
link /subsystem:console ext32
echo %errorlevel%
*
includelib kernel32

.model flat

ExitProcess proto stdcall :dword

.code
start proc c
	invoke	ExitProcess, 42
start endp

end start
```

* 出力例

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

 Assembling: ext32.asm

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


C:\Projects\msvc>ext32

C:\Projects\msvc>echo %errorlevel%
42
```

## サンプル

<details><summary>hello32</summary>

```hello32.asm
comment *
ml /c /Fl /Sa hello32.asm
link /subsystem:console hello32
*
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
msg	db	'hello, world',0dh,0ah
len	equ	$ - msg

.data?
hOut	dd	?

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

end start
```

</details>

# Windows (x64)
Visual Studio 2022

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

```ext64.asm
comment *
ml64 /c /Fl ext64.asm
link /subsystem:console /entry:start ext64
echo %errorlevel%
*
includelib kernel32

extern ExitProcess :proc

.code
start proc
	sub	rsp, 28h
	mov	ecx, 42
	call	ExitProcess
start endp

end
```

* 出力例

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

 Assembling: ext64.asm

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


C:\Projects\msvc>ext64

C:\Projects\msvc>echo %errorlevel%
42
```

## サンプル

<details><summary>hello64</summary>

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

NULL equ 0
STD_OUTPUT_HANDLE equ -11

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

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

.data?
hOut	dq	?

.code
start proc
	sub	rsp, 28h
	mov	ecx, STD_OUTPUT_HANDLE
	call	GetStdHandle
	mov	hOut, rax
	mov	qword ptr [rsp+20h], NULL
	mov	r9d, NULL
	mov	r8d, len
	mov	rdx, offset msg
	mov	rcx, hOut
	call	WriteConsoleA
	mov	ecx, 0
	call	ExitProcess
start endp

end
```

</details>

# Linux (x86)

https://www.sakura.ad.jp/services/cloudshell/

```ext32.s
/*
Assembly(gcc,x64,Linux)
-m32 -nostdlib

as --32 -o ext32.o ext32.s
ld -m elf_i386 -s -o ext32 ext32.o
./ext32
echo $?
*/
.intel_syntax noprefix
.text
.global _start
_start:
	mov	ebx, 42
	mov	eax, 1
	int	0x80
```

* 出力例

```
sakura@cloud-shell% vi ext32.s
sakura@cloud-shell% as --32 -o ext32.o ext32.s
sakura@cloud-shell% ld -m elf_i386 -s -o ext32 ext32.o
sakura@cloud-shell% ls
README-cloud-shell.txt  ext32*  ext32.o  ext32.s
sakura@cloud-shell% ./ext32
sakura@cloud-shell% echo $?
42
```

https://tio.run/##PY9BDoIwEEXXnVPMwoUaWhSMIW6Me@9AWizYpLSGqaZc3oqKrv78l5dJviTSvbIj75ompXwNpxksJ5DF/S47G3ePK@B9WSB3nsLFGgUgCTl/I486hrIQvySwF@Q9atvWpqz2yOkv/VQQ@ecC3Vw9Lo6wzkEYF7StaXRBRnT@NujWRBBhMkF01itpsaYghwDfOADr/YNpFTPcFXORU9kCm56xTaw2kNKzaa3sKL0XpP@CFw

<details><summary>NASM</summary>

```ext32.s
; Assembler 32bit (nasm 2.14)
section .const
msg	db	'hello, world',0ah
len	equ	$ - msg

section .text
global _start
_start:
	mov	edx, len
	mov	ecx, msg
	mov	ebx, 1
	mov	eax, 04h
	int	80h

	mov	ebx, 0
	mov	eax, 01h
	int	80h
```

https://ideone.com/wLNGtV

</details>

## サンプル

<details><summary>hello32</summary>

```hello32.s
/*
as --32 -o hello32.o hello32.s
ld -m elf_i386 -s -o hello32 hello32.o
*/
.intel_syntax noprefix

.data
msg:	.ascii	"hello, world\n"
len = . - msg

.text
.global _start
_start:
	mov	edx, len
	lea	ecx, msg
	mov	ebx, 1
	mov	eax, 4
	int	0x80
	mov	ebx, 0
	mov	eax, 1
	int	0x80
```

</details>

# Linux (x64)

https://www.mztn.org/lxasm64/x86_x64_table.html

https://www.sakura.ad.jp/services/cloudshell/

```ext64.s
/*
Assembly(as,x64,Linux)

as -o ext64.o ext64.s
ld -o ext64 ext64.o
./ext64
echo $?
*/
.intel_syntax noprefix
.text
.global _start
_start:
	mov	edi, 42
	mov	eax, 60
	syscall
```

* 出力例

```
sakura@cloud-shell% vi ext64.s
sakura@cloud-shell% as -o ext64.o ext64.s
sakura@cloud-shell% ld -o ext64 ext64.o
sakura@cloud-shell% ls
README-cloud-shell.txt  ext64*  ext64.o  ext64.s
sakura@cloud-shell% ./ext64
sakura@cloud-shell% echo $?
42
```

https://tio.run/##PYrBDoIwEETP7FfswYOSUowhHLwY7/4DWaBqk6U1bDXt11ej4OnNmxkSMVPPqSLJuS7hvPiWRMW2URfrnnEHJFh5NDG0jV4pwOO/XTfQ9TeBGe4eNycoa9DWBcOdJBcoovOP2VxtBB0@T9A39j0xdhJoDvDDEYrJvwozWoXNYRGKCts9FJJkIGbI@Q0

## サンプル

<details><summary>hello64</summary>

```hello64.s
/*
Assembly(as,x64,Linux)

as -o hello64.o hello64.s
ld -o hello64 hello64.o
*/
.intel_syntax noprefix

.data
msg:	.ascii	"hello, world\n"
len = . - msg

.text
.global _start
_start:
	mov	edx, len
	lea	rsi, msg
	mov	edi, 1
	mov	eax, 1
	syscall
	mov	edi, 0
	mov	eax, 60
	syscall
```

</details>