ettinos-programs/dec2hex.asm

88 lines
917 B
NASM

cpu 8086
org 0x3000
xor dx, dx
cmp byte [si], 0
jne atoi
mov ah, 2
mov si, overflow_msg
int 0x21
int 0x20
atoi:
lodsb
cmp al, 0
je .end
cmp al, '0'
jl .notdigit
cmp al, '9'
jg .notdigit
xor ah, ah
sub al, '0'
mov cx, ax
mov ax, dx
mul word [ten]
test dx, dx
jnz .overflow
mov dx, ax
add dx, cx
jc .overflow
jmp atoi
.notdigit:
mov byte [char_slot], al
mov si, notdigit_msg
mov ah, 2
int 0x21
int 0x20
.overflow:
mov si, overflow_msg
mov ah, 2
int 0x21
int 0x20
.end:
std
mov di, hexstring+3
xor bh, bh
mov cx, 4
itoh:
mov bl, 0xf
and bl, dl
mov al, [hexdigits + bx]
stosb
shr dx, 1
shr dx, 1
shr dx, 1
shr dx, 1
loop itoh
cld
mov si, hexstring
mov ah, 2
int 0x21
int 0x20
notdigit_msg db "Not a digit '"
char_slot db 0, "'", 0
overflow_msg db "Value must be in range 0 to 65535", 0
hexstring db "xxxx", 0
hexdigits db "0123456789abcdef"
ten dw 10