アセンブラ10/16進表示サンプル

# x64 16進表示

```disp16.s
# Assembly (as, x64, Linux)
.intel_syntax noprefix

.data
msg:	.ascii	"xxxxxxxx\n"
len = . - msg

.text
hex:	.ascii	"0123456789ABCDEF"

.global _start
_start:
	mov	edx, 0x0123cdef
	mov	rdi, offset msg
	mov	ebx, offset hex
	mov	ecx, 8
1:
	mov	al, dl
	and	al, 0x0f
	xlat
	ror	edx, 4
	dec	ecx
	mov	[rdi + rcx], al
	jnz	1b

	mov	edx, len
	mov	rsi, offset msg
	mov	edi, 1		# stdout
	mov	eax, 1		# write
	syscall
	xor	edi, edi
	mov	eax, 60		# exit
	syscall
```

https://tio.run/##bY/NTsMwEITP3qdYtRcQadRACaUSh/J34g0oqjaxU4xcB9kubHn54DQW7QEf/DP6ZnZM3qttZfYT8l03xmV64hn5DLmcZfii7Y7PIdc2KLP2exuI0bafTjWaAXJJgWDrNwuRk6@1FiNOa2VHYJTFO8xxghGJdFAc4F3xkZ4Wl1ez6/Jmfru8f3h8eh5FamPaigyufSAXYDgWILbtl1CSM5xy76qlagbRSZ1h2zRehcOcgaz4T4wTk1hHcQ5FSiOToTQgyMrDPQbHSDYUQLjWDdNmIKSqe@tgeo3j8AJdzW8ZUnR/2B9RVHBSMH47NfP/NevrFkKM0QfZ7kJSiZP67XRQIPze12RiPh@aRE/cTthy2sOKdTiyXfcL

# x64 10進表示

```disp10.s
# Assembly (as, x64, Linux)
.intel_syntax noprefix

.data
msg:	.ascii	"xxxxxxxxxx\n"
len = . - msg

.text
.global _start
_start:
	mov	eax, -1
	mov	rdi, offset msg
	mov	ebx, 10
	mov	ecx, 10
1:
	xor	edx, edx
	div	ebx
	add	dl, '0'
	dec	ecx
	mov	[rdi + rcx], dl
	jnz	1b

	mov	edx, len
	mov	rsi, offset msg
	mov	edi, 1		# stdout
	mov	eax, 1		# write
	syscall
	xor	edi, edi
	mov	eax, 60		# exit
	syscall
```

https://tio.run/##bY/LTsMwEEXXM18xahcF4USJhLqoxII9fwCocmK3MnJslJmCw88H5wF0gRe27ujc0bFmtl3jh0LzOG7pcY10o1lR2t8renLhkm6xdEGsP/IQRCcK8b23J5cQS6NFY8fnA5SaW@dgk37PS9igt4EeqKSCMpR5sUmwPPvYaE9HFt0LLs8BoYsfYHVSVNRL6I1TFE8ntjL3F6LJRF2toV1Cnesp9mBNzvlCMG5GEbQxYLyiXbXLU9tOnaX8nPfTHfVtelVkPMJb@IK6wXX1tCr7ryr8n8rkVwNsicXEi1x9YZ5@9k4sAg/cau9/DN1k6K7YfTXBNjn5Y8fxGw

# x86 16進表示

```disp16.asm
# as --32 -o disp16.o disp16.s
# ld -m elf_i386 -o disp16 disp16.o

.intel_syntax noprefix

.data
msg:	.ascii	"xxxxxxxx\n"
len = . - msg

.text
.global _start
_start:
	mov	edx, 0x0123cdef
	mov	edi, offset msg
	call	tohex

	mov	edx, len
	mov	ecx, offset msg
	mov	ebx, 1		# stdout
	mov	eax, 4		# write
	int	0x80
	xor	ebx, ebx
	mov	eax, 1		# exit
	int	0x80

tohex:
	mov	ebx, 8
1:
	mov	al, dl
	and	al, 0x0f
	cmp	al, 10
	sbb	al, 0x69
	das
	ror	edx, 4
	dec	ebx
	mov	[edi+ebx], al
	jnz	1b
	ret
```

# V30 16進表示

```disp16.asm
; ml /c disp16.asm
; link16 /t disp16.obj;
.186
.model tiny
.code
	org	0100h
start:
	mov	dx, 0f90ah
	mov	di, offset msg
	call	tohex
	mov	dx, offset msg
	mov	ah, 09h
	int	21h
	ret

tohex:
	mov	bx, 4
@@:
	mov	al, dl
	and	al, 0fh
	cmp	al, 10
	sbb	al, 69h
	das
	ror	dx, 4
	dec	bx
	mov	[di+bx], al
	jnz	@b
	ret

msg	db	'xxxx',0dh,0ah,'$'
end start
```

# Z80 16進表示

```disp16.asm
;10 bload "program.bin",r

CHPUT	equ	$00a2

	org	$d000
start:
	ld	de, $f90a
	ld	hl, msg + 3
	call	tohex4

	ld	hl, msg
L1:
	ld	a, (hl)
	or	a
	ret	z
	call	CHPUT
	inc	hl
	jr	L1

tohex4:
	call	tohex2
	ld	e, d
tohex2:
	ld	a, e
	call	tohex1
	ld	a, e
	rept 4
	rra
	endm
tohex1:
	and	$0f
	cp	10
	sbc	a, $69
	daa
	ld	(hl), a
	dec	hl
	ret

msg	db	'xxxx',$0d,$0a,0
end start
```

https://msxpen.com/codes/-O5Vt_hR1zgnZDc0f2qC

<details><summary>disp16b.asm</summary>

```disp16b.asm
;10 bload "program.bin",r

CHPUT	equ	$00a2

	org	$d000
start:
	ld	hl, $f90a
	ld	de, msg
	call	tohex

	ld	hl, msg
L1:
	ld	a, (hl)
	or	a
	ret	z
	call	CHPUT
	inc	hl
	jr	L1

tohex:
	ld	b, 4
tohex1:
	xor	a
	rept 4
	add	hl, hl
	rla
	endm
	cp	10
	sbc	a, $69
	daa
	ld	(de), a
	inc	de
	djnz	tohex1
	ret

msg	db	'xxxx',$0d,$0a,0
end start
```

</details>

# Z80 10進表示

```disp10.asm
;10 bload "program.bin",r

CHPUT	equ	$00a2

	org	$d000
start:
	ld	hl, -1
	ld	de, msg
	call	todec

	ld	hl, msg
L1:
	ld	a, (hl)
	or	a
	ret	z
	call	CHPUT
	inc	hl
	jr	L1

todec:
	ld	bc, 10000
	call	todec1
	ld	bc, 1000
	call	todec1
	ld	bc, 100
	call	todec1
	ld	bc, 10
	call	todec1
	ld	a, l
	add	a, '0'
	ld	(de), a
	ret
todec1:
	ld	a, '0' - 1
	or	a
todec2:
	sbc	hl, bc
	inc	a
	jr	nc, todec2
	add	hl, bc
	ld	(de), a
	inc	de
	ret

msg	db	'xxxxx',$0d,$0a,0
end start
```

https://msxpen.com/codes/-O5VtowgU_N6MT_O7iQI