ettinos-programs/hex2dec.asm

102 lines
1.1 KiB
NASM

cpu 8086
org 0x3000
xor dx, dx
cmp byte [si], 0
jne htoi
mov ah, 2
mov si, overflow_msg
int 0x21
int 0x20
htoi:
lodsb
cmp al, 0
je .end
.digit09:
cmp al, '0'
jl .notdigit
cmp al, '9'
jg .digitcapital
sub al, '0'
jmp .digit
.digitcapital:
cmp al, 'A'
jl .notdigit
cmp al, 'F'
jg .digitminuscule
sub al, 'A' - 10
jmp .digit
.digitminuscule:
cmp al, 'a'
jl .notdigit
cmp al, 'f'
jg .notdigit
sub al, 'a' - 10
jmp .digit
.digit:
xor ah, ah
test dx, 0xf000
jnz .overflow
shl dx, 1
shl dx, 1
shl dx, 1
shl dx, 1
or dx, ax
jmp htoi
.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:
mov si, decimalstring + 5
mov ax, dx
itoa:
dec si
xor dx, dx
div word [ten]
add dl, '0'
mov byte [si], dl
test ax, ax
jnz itoa
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 ffff", 0
decimalstring db "xxxxx", 0
ten dw 10