hex to dec
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: hex to dec

  1. #1
    Join Date
    Oct 2005
    Posts
    13

    Question hex to dec

    How can i convert the hexadecimal to decimal in TASM? For instance, 07D2h to 2002. Is it possible using DAA or AAM?
    Thank....

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: hex to dec

    You need to be more specific. 07D216 is the same as 200210 so you don't really need to convert anything.

    - petter

  3. #3
    Join Date
    Oct 2005
    Posts
    13

    Smile Re: hex to dec

    oh, sorry. I would like to display the decimal number but i have hexadecimal. So i need to convert it and display on user screen.For example,
    ax:07D2h
    convert to
    ax:2002
    in turbo debugger. Then i display the value on user screen.
    Thank for your remind. Thank you.

  4. #4
    Join Date
    Jun 2005
    Posts
    1,255

    Smile Re: hex to dec

    What I understand is that you have in the ax register a 16-bit value that you need to convert to a string of ascii characters.

    For instance, intially, you have ax=2002 (or 07D2h),
    and at the end you'll have a string of ascii characters: '2', '0', '0, '2', which are 032h, 030h, 030h, 032h.

    Then, that string of Ascii characters will be displayed by the mean of a loop and the int 21h fonction 2 (ah=2 and si = address of a character).

    But before that, in order to convert the number to a string, you'll have first to reserve some place for the string with DB ..., then divide the number by 10 in a loop to get each digit, and add 030h to each digit in order to convert them to ascii.

  5. #5
    Join Date
    Apr 2003
    Posts
    1,755

    Smile Re: hex to dec

    Here's how to do that. This function uses the stack to save the values before printing. This code is included in my POST before.
    Code:
    PRINT_WORD_Proc Proc Near p_word:WORD
       enter 0, 0
       pusha
       mov bx, sp
       mov ax, [p_word]
    PRINT_WORD_DIVIDE_AGAIN:
       xor dx, dx
       mov cx, 10
       div cx
       push dx
       or ax, ax
       jnz PRINT_WORD_DIVIDE_AGAIN
    PRINT_WORD_PRINT_AGAIN:
       mov ah, 2
       pop dx
       add dl, '0'
       int 21h
       cmp sp, bx
       jne PRINT_WORD_PRINT_AGAIN
       popa
       leave
       ret 2
    PRINT_WORD_Proc EndP
    Hope it will help you
    Last edited by rxbagain; November 5th, 2005 at 11:41 AM.

  6. #6
    Join Date
    Oct 2005
    Posts
    13

    Smile Re: hex to dec

    Yup, thank you.
    I have some question here about the coding.
    1. What is the meaning of command enter 0, 0 ?
    2. Why we need mov sp, bx?
    3. What the purpose put add dl, '0' ?
    4. What is the command for leave?

    Thanks all for given me information.
    Thanks...

  7. #7
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: hex to dec

    Quote Originally Posted by noob_Assembler
    2. Why we need mov sp, bx?
    To restore the stack state as it where before you executed any code inside the procedure. For example you are using the stack inside the function but forget to pop a value you have pushed you would run into a stack fault when it comes to "ret". Restoring sp via mov sp, bx eliminates such problems.
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  8. #8
    Join Date
    Jun 2005
    Posts
    1,255

    Smile Re: hex to dec

    Quote Originally Posted by noob_Assembler
    3. What the purpose put add dl, '0' ?
    This is the same as:
    Code:
    add dl, 30h
    This is needed because if you look at a table of Ascii characters you will see that the characters for the digits are not placed at the begining of the table, but start at the 48th (30 in hexa) position.

  9. #9
    Join Date
    Apr 2003
    Posts
    1,755

    Smile Re: hex to dec

    Quote Originally Posted by noob_Assembler
    1. What is the meaning of command enter 0, 0 ?
    4. What is the command for leave?
    ENTER is used to build the stack frame of the procedure. It's equivalent to
    Code:
    push bp
    mov bp, sp
    The first parameter is the size of yout local variables if any. It will adjust the SP (stack pointer) to accomodate the local variable. If for example, you have "enter 12, 0", it will be similar to
    Code:
    push bp
    mov bp, sp
    sub sp, 12     ;for the local variables
    LEAVE is an opcode. It is a companion for enter. It destroys the stack frame and returns SP and BP to the values before the call to enter. It's like the code
    Code:
    mov sp, bp
    pop bp
    Quote Originally Posted by noob_Assembler
    2. Why we need mov sp, bx?
    Actually, it's "cmp sp, bx". I used it to check up to how much character to print. Before I started computing the value, I saved the the SP like "mov bx, sp" to MARK THE END of the string. And when I need to print the characters (numbers) created, I use the "cmp sp, bx" to check if the end is reached.

    Hope it will help you
    Last edited by rxbagain; November 6th, 2005 at 01:52 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center