BrianGEFF719
asked on
Convert 16 Bit Register (HEX) to Decimal
Hi, I need a chunk of code that will convert a hex value in AX (16 BIT!) and store it somewhere in the data segment I dont care where you store it in the code I will change that later....if possible can you make the code NOT change as much as possible thanks
thanks
-brian
thanks
-brian
Here is my lame code;)
data segment
;...
result db 3 dup (' ')
termi db 0ah, 0dh, '$'
num1 dw 0
radix dw 10 ;decimal
data ends
;convert the ax to string, and save it to es:di(result)
ChangeResult2String proc
push es ; es = ds; only available for 16bit DOS
push ds
pop es
mov di, offset termi-1
std
loooop:
xor dx, dx
div [radix]
add dx, '0' ;convert the remainder to ascii
push ax
mov al,dl
stosb ;store al to ES:DI
pop ax
cmp ax, 0 ;check if the quotient = 0
je finish
jmp loooop
finish:
pop es
ret
ChangeResult2String endp
data segment
;...
result db 3 dup (' ')
termi db 0ah, 0dh, '$'
num1 dw 0
radix dw 10 ;decimal
data ends
;convert the ax to string, and save it to es:di(result)
ChangeResult2String proc
push es ; es = ds; only available for 16bit DOS
push ds
pop es
mov di, offset termi-1
std
loooop:
xor dx, dx
div [radix]
add dx, '0' ;convert the remainder to ascii
push ax
mov al,dl
stosb ;store al to ES:DI
pop ax
cmp ax, 0 ;check if the quotient = 0
je finish
jmp loooop
finish:
pop es
ret
ChangeResult2String endp
ASKER
I am really not understanding your code...here is what I am doing...I have the HEX VALUE "0B40", in AX, I have played with your code a little and I dont see what it is doing...mov di, offset termi-1? it looks like you are overwriting the CRLF? that is in termi? Could you give me a little more help with this, because it doesnt look like anything is being stored in any of the memory locations of those variables...
-brian
-brian
ASKER
dont you need to do
mov ax,@data
mov es,ax
???
mov ax,@data
mov es,ax
???
ASKER
dont you need to do
mov ax,@data
mov es,ax
???
mov ax,@data
mov es,ax
???
Hi Brian,
This is not a complete program, only a code snippet, used to convert ax to a string that stores in "result", it will not overwrite the CRLF, because before doing this, I called "std".
Yes, you can also use "mov ax,@data then mov es, ax",
but for most situations, the ds already point to the correct segment.
This is not a complete program, only a code snippet, used to convert ax to a string that stores in "result", it will not overwrite the CRLF, because before doing this, I called "std".
Yes, you can also use "mov ax,@data then mov es, ax",
but for most situations, the ds already point to the correct segment.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The following works, and preserves all registers (except flags)
PUSH AX
PUSH DX
PUSH CX
PUSH SI
PUSH DI
MOV DI,OFFSET CONVS+4
MOV SI,10 ;constant divisor
MOV CX,5 ;#loops
DIVLP:
XOR DX,DX ;DX := 0
DIV SI
ADD DL,'0' ;to decimal
MOV [DI],DL ;put
DEC DI ;next char position
LOOP DIVLP
POP DI
POP SI
POP CX
POP DX
POP AX
CONVS:
DB 5 DUP (?)
HTH
...Bill
PUSH AX
PUSH DX
PUSH CX
PUSH SI
PUSH DI
MOV DI,OFFSET CONVS+4
MOV SI,10 ;constant divisor
MOV CX,5 ;#loops
DIVLP:
XOR DX,DX ;DX := 0
DIV SI
ADD DL,'0' ;to decimal
MOV [DI],DL ;put
DEC DI ;next char position
LOOP DIVLP
POP DI
POP SI
POP CX
POP DX
POP AX
CONVS:
DB 5 DUP (?)
HTH
...Bill
ASKER
BeyondWu:
div [radix] is causing my program to crash, It looks like its crashing because of a Division By Zero...help please :/
-brian
div [radix] is causing my program to crash, It looks like its crashing because of a Division By Zero...help please :/
-brian
Are you using my sample?
If you use it within your own code, please check if the value [radix] when debug, or post your code.
please check if the value [radix] == 0 when debug.
if so the [radix]==0, it always caused by the wrong value of DS, please ensure you have set in correctly with " mov ax, data; mov ds, ax".
ASKER
awesome! I can take it from here, thanks for all your help
ASKER