Click here to Skip to main content
15,906,766 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey Guys,
I wrote a program that converts a hexadecimal number to decimal number.
Everything works correctly without errors but the answer is not correct and I do not understand where the mistake, for example:
if i enterd hex num : 7B2F and the right dec num is : 31535 but in my prog i got : 38377
Id be happy for some help.
My prog:
; HW1.asm
;
    .MODEL SMALL
    .STACK 100h
    .DATA
START_TEXT	DB 'This program converts numbers from Hex to Decimal'
            DB    'Enter number up to FFFF:' ,13,10,'$'
RESULT_TEXT	DB  ' H = XXXXX D ','$'
decNum	 DW  ?
Ten 	 DW  10

;Program start here:
	.CODE
	MOV AX,@DATA   ; DS can be written to only through a register
    MOV DS,AX      ; Set DS to point to data segment
    MOV AH,9       ; Set print option for int 21h
    MOV DX,OFFSET START_TEXT  ;  Set  DS:DX to point to START_TEXT
    INT 21h
;
;   Get HEX num - First digit
	MOV AH,1
	INT 21H
	MOV AH,0
	CMP AL,41h		
	JAE SUB_31		;jump if its in the range A-F
	SUB AL,30h
	JMP DecNumReady		;dec num is ready
SUB_31:
	SUB AL,31h
DecNumReady:
	MOV DX,0
	MOV BX,4096		;16^3
	MUL BX
	MOV decNum,AX
;   HEX num - Sec digit
	MOV AH,1
	INT 21H
	MOV AH,0
	CMP AL,41h	
	JAE SUB_SEC_31		;jump if its in the range A-F
	SUB AL,30h
	JMP DecNumReady_SEC		;dec num is ready
SUB_SEC_31:
	SUB AL,31h
DecNumReady_SEC:
	MOV DX,0
	MOV BX,256		;16^2
	MUL BX
	ADD decNum,AX
;   HEX num - Third digit
	MOV AH,1
	INT 21H
	MOV AH,0
	CMP AL,41h		
	JAE SUB_THIRD_31		;jump if its in the range A-F
	SUB AL,30h
	JMP DecNumReady_SEC		;dec num is ready
SUB_THIRD_31:
	SUB AL,31h
DecNumReady_THIRD:
	MOV DX,0
	MOV BX,16		;16
	MUL BX
	ADD decNum,AX
;   HEX num - Fourth digit
	MOV AH,1
	INT 21H
	MOV AH,0
	CMP AL,41h
	JAE SUB_FOURTH_31
	SUB AX,30h
	JMP DecNumReady_FOURTH
SUB_FOURTH_31:
	SUB AX,31h
DecNumReady_FOURTH:
	ADD decNum,AX
;
;	Print
	MOV DX,0
	MOV AX,decNum
	MOV BX,10
	DIV BX
	ADD DL,'0'
	MOV RESULT_TEXT[9],DL
;
	MOV DX,0
	MOV AX,decNum
	MOV BX,10
	DIV BX
	ADD DL,'0'
	MOV RESULT_TEXT[8],DL
;
	MOV DX,0
	;MOV decNum,AX
	MOV BX,10
	DIV BX
	ADD DL,'0'
	MOV RESULT_TEXT[7],DL
;
	MOV DX,0
	;MOV decNum,AX
	MOV BX,10
	DIV BX
	ADD DL,'0'
	MOV RESULT_TEXT[6],DL
;
	MOV DX,0
	;MOV decNum,AX
	MOV BX,10
	DIV BX
	ADD DL,'0'
	MOV RESULT_TEXT[5],DL
;
	MOV AH,9       ; Set print option for int 21h
    MOV DX,OFFSET RESULT_TEXT		;  Set  DS:DX to point to RESULT_TEXT
    INT 21h
;Program end's here:

     MOV AH,4Ch       ; Set terminate option for int 21h
     INT 21h       ; Return to DOS (terminate program)
     END 
Posted
Updated 22-Aug-13 1:20am
v2
Comments
OriginalGriff 22-Aug-13 7:12am    
That's not a very helpful error description: "the answer is not correct"
Why not give us an example of your input, your expected output, and the actual output?
Tell us what it does that you didn't expect, or did that you didn't expect!
Use the "Improve question" widget to edit your question and provide better information.
idobry 22-Aug-13 7:22am    
Thanks for the enlightenment I improved my question

1 solution

You are using
SUB AL,31h

This way 'A' becomes 16 (decimal), you should instead use
SUB AL,37h
 
Share this answer
 
Comments
idobry 22-Aug-13 7:43am    
Can you please add an explanation?
i worte "SUB AL,31h" becuse after that i have the dec form for A to F in HEX
CPallini 22-Aug-13 7:49am    
You shouldn't have the 'dec form' of the hex digit. You should have its actual (binary) value (note you are subtracting 30h from the 'already decimal').
idobry 22-Aug-13 7:54am    
i changed as you suggested but still i get the worng answer
CPallini 22-Aug-13 8:04am    
What are you getting now?
idobry 22-Aug-13 8:08am    
i enterd 7FF8 and i got 27600 insted of 32760

maybe i have mistake in the print code? cuz the number is suspiciously similar

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900