Task

Take a   Natural Number   in a given base and return the sum of its digits:

   110         sums to   1
   123410   sums to   10
   fe16       sums to   29
   f0e16     sums to   29


	cpu	8086
	org	100h
section	.text
	jmp	demo
	;;;	Sum of digits of AX in base BX.
	;;;	Returns: AX = result
	;;;	CX, DX destroyed.
digsum:	xor	cx,cx		; Result
.loop:	xor	dx,dx		; Divide AX by BX
	div	bx		; Quotient in AX, modulus in DX
	add	cx,dx		; Add digit to sum
	test	ax,ax		; Is the quotient now zero?
	jnz	.loop		; If not, keep going
	mov	ax,cx		; Otherwise, return
	ret
	;;;	Print the value of AX in decimal using DOS.	
	;;;	(Note the similarity.)
pr_ax:	mov	bx,num		; Number buffer pointer
	mov	cx,10		; Divisor
.loop:	xor	dx,dx		; Get digit
	div	cx
	add	dl,'0'		; Make ASCII digit
	dec	bx		; Store in buffer
	mov	[bx],dl
	test	ax,ax		; More digits?
	jnz	.loop		; If so, keep going
	mov	dx,bx		; Begin of number in DX
	mov	ah,9		; MS-DOS syscall 9 prints $-terminated string
	int	21h
	ret
	;;;	Run the function on the given examples
demo:	mov	si,tests	; Pointer to example array
.loop:	lodsw			; Get base
	test	ax,ax		; If 0, we're done
	jz	.done
	xchg	bx,ax
	lodsw			; Get number
	call	digsum		; Calculate sum of digits
	call	pr_ax		; Print sum of digits
	jmp	.loop		; Get next pair
.done:	ret
section	.data
	db	'*****'		; Placeholder for numeric output
num:	db	13,10,'$'
tests:	dw	10, 1		; Examples
	dw	10, 1234
	dw	16, 0FEh
	dw	16, 0F0Eh
	dw	0		; End marker