本文介绍了从十六进制数到DEC数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,

我编写了一个程序,将十六进制数转换为十进制数。

一切正常但没有错误但答案不正确我不知道了解错误的位置,例如:

如果我输入了十六进制数:7B2F而右侧数字是:31535但是在我的编程中我得到了:38377

我很高兴寻求帮助。

我的编程:

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 

推荐答案



SUB AL,31h



这样'A'变为 16 (小数),你应该改为使用


This way 'A' becomes 16 (decimal), you should instead use

SUB AL,37h


这篇关于从十六进制数到DEC数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 16:03