0

Hi!
I'm trying to write a program that converts an inputted decimal number into its hex equivalent. Here is the source code along with the .h files:

.286
.model small
datas SEGMENT byte PUBLIC 'data'
	newl	DB 10,13,'$'
	msg_hex	DB "Hexa: $"
	msg_zec	DB "Dec  : $"
	tabela	DB '0123456789ABCDEF'
	bufin	DB 6, 7 DUP (?)
	zece	Dw 10
datas ENDS

stacks SEGMENT PARA STACK 'stack'
	db 256 dup(?)
stacks ENDS

codes SEGMENT BYTE PUBLIC 'code'
	assume cs:codes,ss:stacks,ds:datas
	mov ax,datas
	mov ds,ax
	
	jmp start

include cio.h
include hexa.h
include citcuv.h
include asciitab.h

start:

citzec:
	outstr msg_zec
	call citcuv
	outstr newl	
	mov cx, 5	
	xor ax, ax
	mov si, offset bufin + 2
	
trans:
	mul zece
	mov dx,ax
	xor ax,ax
	lods bufin
	cmp al, '0'
	jl citzec
	cmp al, '9'
	jg citzec
	sub al, '0'
	add dx,ax
	mov ax,dx	
	loop trans
		
afisare:
	outstr msg_hex
	mov dx,ax

	xor ax,ax
	mov al, dh
	call ascii
	co ah
	co al

	xor ax,ax
	mov al, dl
	call ascii
	co ah
	co al

	outstr newl

final:
	mov ax,4c00h
	int 21h
	
codes ENDS
end

cio.h:

;reading an inputted character
ci 	macro
	mov ah,1
	int 21h
	endm

;reading an inputted string in a buffer (bufin) that starts with the even offset
in_str	macro	even
	pusha
	mov ah,0ah
	lea dx,even
	int 21h
	popa
	endm

;outputting a character
co	macro   even
	pusha
	mov ah,2
	mov dl,even
	int 21h
	popa
	endm

;outputting a string that starts with the even offset
outstr	macro even 
	pusha
	mov ah,9
	lea dx,even
	int 21h
	popa
	endm

hexa.h:

;the routine converts an ASCII character ('0'...'9' 'A'...'F' 'a'...'f') in a Hexa digit ('0'...'F')
;in:	AL = the ASCII character to be converted
;out:	CF = 1: invalid character
;	CF = 0: CX(CL) = the coresponding Hexa digit

tabconvinv db 'fedcbaFEDCBA9876543210'

hexa PROC
   cld
   mov cx,22
   push cs
   pop ES
   lea DI,tabconvinv
   REPNE scasb
   JZ cont
   STC
   RET
cont:
   CMP CX,0FH
   JBE OK
   SUB CX,6
ok:
   CLC
   RET
hexa ENDP

citcuv.h:

;the routine transforms a ASCII string  in a 16b word
;the routine needs declaration of a bufin strin in the data segment:
;BUFIN DB 6, 7 DUP (?)

;out: 	CF = 1: invalid character
;	CF = 0: DX = word

citcuv	 proc	
	in_str bufin
	mov cl,bufin+1
	xor ch,ch
	xor dx,dx
	cld
	lea si,bufin+2
altcar:
	push cx
	lodsb
	call hexa 	;CX = 000h
	jnc oka
	ret
oka:
	shl dx,4	;DX = xxx0
	or dx,cx	;DX = xxxh
	pop cx
	loop altcar
	clc
	ret
citcuv	endp

asciitab.h:

;the routine converts a Hexa digit ('0'...'F') in the coresponding ASCII character ('0'...'9' = 30h...39h; 'A'...'F' = 41h...46h)
;the routine uses a conversion tabel like the one declared in the data segment
;tabela  db '0123456789ABCDEF'

;in:	AL = x:h
;out:	AL = car. ASCII

ascii 	proc
	and al,0fh
	lea bx,tabela
	xlat
	ret
ascii   endp

The decimal numbers are inputted as 5 digits: 10 = 00010; 142 = 00142; 67528 = 67528

The problems I've encountered are:
1. The conversion seems to be going well, but only the second and forth hex characters are outputted correctly
2. How do I treat numbers greater that 65536? This code outputs 4 hex digits, whereas for numbers greater that 65536, 5 hex digits must be outputted

I appreciate any type of help.

1
Contributor
1
Reply
3
Views
6 Years
Discussion Span
Last Post by rawtea
0

Sorry, I've noticed a few mistakes I made while writing the post.

First of all, in the source code, I forgot to include bascii.h. Also, this is the correct code for 'afisare':

afisare:
    outstr msg_hex
    mov dx,ax

    xor ax,ax
    mov al, dh
    call bascii
    co ah
    co al

    xor ax,ax
    mov al, dl
    call bascii
    co ah
    co al

    outstr newl

bascii.h:

    ;the routine converts a byte (two hex digits) in two coresponding ASCII characters

    ;in: AL = h:l
    ;out:AX = AH:AL  - 2 ASCII characters

    bascii proc
        mov ah,al
        shr al,4
        call ascii
        xchg ah,al
        call ascii
        ret
    bascii endp

Sorry again. Please lend me a hand.

Edited by Dani: Formatting fixed

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.