?
Solved

Convert 16 Bit Register (HEX) to Decimal

Posted on 2003-03-05
14
Medium Priority
?
503 Views
Last Modified: 2012-05-04
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
0
Comment
Question by:BrianGEFF719
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
14 Comments
 
LVL 19

Author Comment

by:BrianGEFF719
ID: 8070246
when i say not change as much as possible I mean leave pretty much every register the same, thanks
0
 
LVL 9

Expert Comment

by:BeyondWu
ID: 8076052
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
0
 
LVL 19

Author Comment

by:BrianGEFF719
ID: 8076703
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
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 19

Author Comment

by:BrianGEFF719
ID: 8076711
dont you need to do

mov ax,@data
mov es,ax

???
0
 
LVL 19

Author Comment

by:BrianGEFF719
ID: 8076787
dont you need to do

mov ax,@data
mov es,ax

???
0
 
LVL 9

Expert Comment

by:BeyondWu
ID: 8076830
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.
0
 
LVL 9

Accepted Solution

by:
BeyondWu earned 400 total points
ID: 8076872
;Here is a little sample;)
.model small

data segment
result       db      5 dup (' ') ;<--should be at least 5 chars length. my mistake!LOL
termi        db      0ah, 0dh, '$'
radix        dw      10 ;decimal
data ends

sseg segment stack
             dw      256 dup(?)
sseg ends

code segment
        assume cs:_TEXT, ds:data, ss:sseg
Loc_start:
        mov   ax, data
        mov   ds, ax

          call  ChangeResult2String

        ;show result
        mov  dx, offset result
        mov  ax, 0900h
        int  21h
exit:
        mov  ax, 4c00h                ;terminate
        int  21h

;convert the ax to string, and save it to es:di(result)
ChangeResult2String      proc
        push es          ;change the result to ascii.
        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

code ends
        end Loc_start

0
 
LVL 7

Expert Comment

by:billious
ID: 8076914
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
0
 
LVL 19

Author Comment

by:BrianGEFF719
ID: 8077492
BeyondWu:

div   [radix] is causing my program to crash, It looks like its crashing because of a Division By Zero...help please :/


-brian
0
 
LVL 9

Expert Comment

by:BeyondWu
ID: 8077652
Are you using my sample?
0
 
LVL 9

Expert Comment

by:BeyondWu
ID: 8077684
If you use it within your own code, please check if the value [radix] when debug, or post your code.
0
 
LVL 9

Expert Comment

by:BeyondWu
ID: 8077687
please check if the value [radix] == 0 when debug.
0
 
LVL 9

Expert Comment

by:BeyondWu
ID: 8077696
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".
0
 
LVL 19

Author Comment

by:BrianGEFF719
ID: 8085707
awesome! I can take it from here, thanks for all your help
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
Hey fellow admins! This time, I have a little fairy tale for you. As many tales do, it starts boring and then gets pretty gory. I hope you like it. TL;DR: It is about an important security matter, you should read it if you run or administer Windows …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question