spigot.asm
comment *
	MASM32 SDK
ml /c /AT /Fl spigot.asm
link16 /t spigot;
*
	.8086
	.model	tiny
 
BASE	equ	10000		; 基底
 
	.code
	org	0100h
 
;-------+-------+-------+-------+-------+
start	proc
				; for (n = 350; ; )
	mov	n, 350
@L1:
				; temp = 0;
	mov	temp, 0
	mov	temp+2, 0
				; for (i = n - 1; ; )
	mov	ax, n
	dec	ax
	mov	idx, ax
@L2:
				; denom = 2 * i - 1;
	mov	ax, idx
	add	ax, ax
	dec	ax
	mov	denom, ax
				; temp = temp * i;
	mov	ax, temp
	mov	dx, temp+2
	mov	cx, idx
	call	mul32
	mov	temp, ax
	mov	temp+2, dx
				; numer[i];
	mov	bx, idx
	add	bx, bx
	add	bx, offset numer
				; numer[i] * BASE;
	mov	ax, [bx]
	mov	dx, BASE
	mul	dx
				; temp += dx:ax;
	add	temp, ax
	adc	temp+2, dx
				; temp / denom;
	mov	ax, temp
	mov	dx, temp+2
	mov	cx, denom
	call	div32
				; numer[i] = cx;
	mov	[bx], cx
				; temp = dx:ax;
	mov	temp, ax
	mov	temp+2, dx
				; for ( ; i > 0; i--)
	dec	idx
	jnz	@L2
				; temp / BASE;
;	mov	ax, temp
;	mov	dx, temp+2
	mov	cx, BASE
	call	div32
				; outval + ax; 出力
				; outval = cx;
	add	ax, outval
	mov	outval, cx
	call	print
				; for ( ; n > 0; n -= 14)
	sub	n, 14
	jnz	@L1
				; exit(0);
	mov	ax, 4c00h
	int	21h
start	endp
 
n	dw	?, 0		; 計算項数
idx	dw	?, 0		; ループ変数
temp	dw	2 dup (?)	; 一時変数/繰り上がり
outval	dw	0, 0		; 出力値
denom	dw	?, 0		; 分母
numer	dw	350 dup (2000)	; 分子
 
;-------+-------+-------+-------+-------+
print	proc
	mov	di, offset buf+4
	mov	bx, 10
	mov	cx, 4
@@:
	xor	dx, dx
	div	bx
	add	dl, '0'
	dec	di
	mov	[di], dl
	loop	@b
 
	mov	ah, 09h
	mov	dx, offset buf
	int	21h
	ret
 
buf	db	4 dup (?), '$'
print	endp
 
;-------+-------+-------+-------+-------+
; dx:ax * cx = dx:ax
mul32	proc
	local	xl:WORD
	local	zh:WORD
 
	mov	xl, ax
	mov	ax, dx
	mul	cx
	mov	zh, ax
	mov	ax, xl
	mul	cx
	add	dx, zh
	ret
mul32	endp
 
;-------+-------+-------+-------+-------+
; dx:ax / cx = dx:ax ... cx
div32	proc
	local	xl:WORD
	local	zh:WORD
 
	mov	xl, ax
	mov	ax, dx
	xor	dx, dx
	div	cx
	mov	zh, ax
	mov	ax, xl
	div	cx
	mov	cx, dx
	mov	dx, zh
	ret
div32	endp
 
	end	start
 

最終更新:2019年08月31日 12:01
添付ファイル