我目前正在为 8086 编写程序集。
我正在做的当前任务是将 4 个十六进制数字转换为十进制表示。我试过遵循我的导师方法,但我在必须将十六进制数字乘以临时值的部分感到困惑,特别是当我被告知将其设为 0 时,每次乘以临时值总是会是0对吗?
我到目前为止的代码如下,非常感谢任何帮助!
; ------------------------------------
; Name : Gethex
; Function: converts a word (4 hex digits) into a numerical value
; Inputs : Upto a word hex values input into the console
; Outputs : Return within DX the numerical conversion
; ------------------------------------
Gethex:
MOV BX,0H ;Temp value to 0
MOV CX,0H ;Counter set to 0
Gethexloop:
call Getch
push DX ; Putch requires DL, need to save current reg
MOV DL,AL
call Putch
pop DX ; restore DL reg
MOV BH,AH ;Use BH as temp storage as AH will be corrupted
cmp AL,30H ; ASCII 0
JL Gethexloop ; If less than 0 jump to the start as not an Alphabetical char
cmp AL,39H ; ASCII 9
JLE Nums ; If less or equal to 9 then it will be a number
cmp AL,46H
JLE Case ; Start the conversion procedure
Case:
SUB AL,10
jmp Convert
Nums:
SUB AL,30 ; Convert to normal number
Convert:
push AX ; Prepare to multiply
MOV AX,BX
mul BX
POP AX
ADD AX,BX
ADD DX,AX ; final value
ADD CX,1 ; Increase counter
cmp CX,3 ; if greater than 4 (0 being included so 3)
JG EndLoop
jmp Gethexloop
EndLoop:
ret ; return to calling statement