News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Convert ASCII numbers in Hex

Started by shekel, August 24, 2015, 05:29:34 AM

Previous topic - Next topic

shekel

Sorry by my English, is not my native language..

I have in EAX this value 00003833, with a table ascii i can see that the value 38h represent the number 8 and the value 33h represent the number 3..

So, if i concatenate this numbers i have 83 and in hexadecimal represent the value 53h..

My question is how can i go from 00003833 to 53h

I can do this without use API, only with maths??

jj2007

Perhaps you should explain once more what exactly you want to achieve...

include \masm32\include\masm32rt.inc

.code
start:
  mov eax, 00003833h
  push eax
  print hex$(eax), 13, 10 ; 00003833
  print esp , 13, 10 ; 38 - the string composed of 33="3" and 38="8"
  pop eax
  print str$(eax), 13, 10 ; 14387 decimal = 3833h
  exit

end start

dedndave

we would normally perform this conversion on a string, rather than a register value
that way, it can handle longer numbers   :t

we would use a function to convert the string into a dword binary value
then another function to convert the dword binary value into a hexadecimal string

but, if the numbers are ALWAYS 2 decimal ASCII digits,
    mov     eax,3833h
    and     ax,0F0Fh         ;convert 2 ASCII digits to binary, EAX = 803h
    aad                      ;ASCII Adjust before Division instruction, EAX = 53h
    print   uhex$(eax),13,10 ;console mode


dedndave

if you want "53h"
    print   right$(uhex$(eax),2),104,13,10

104 = 'h'
13 = carriage return
10 = line feed

jj2007

Or print right$(uhex$(eax),2),"h",13,10

shekel

Quote from: dedndave on August 24, 2015, 06:30:17 AM
we would normally perform this conversion on a string, rather than a register value
that way, it can handle longer numbers   :t

we would use a function to convert the string into a dword binary value
then another function to convert the dword binary value into a hexadecimal string

but, if the numbers are ALWAYS 2 decimal ASCII digits,
    mov     eax,3833h
    and     ax,0F0Fh         ;convert 2 ASCII digits to binary, EAX = 803h
    aad                      ;ASCII Adjust before Division instruction, EAX = 53h
    print   uhex$(eax),13,10 ;console mode


This code works.. i dont know that exist 'aad' ;) Thank you

dedndave

AAA, AAD, AAM, and DAA are really old instructions - lol
i think DAA goes all the way back to the intel 4004   :biggrin:
the others were added when the 8080 came out, as i recall

they aren't used much anymore - at least, not as intended
because we don't do much ASCII or BCD string math
in the early days, they were used primarily for accounting programs

they were kept in the 8086/8088, mostly for backward compatibility

Gunther

Quote from: dedndave on August 24, 2015, 10:54:04 PM
AAA, AAD, AAM, and DAA are really old instructions - lol

These instructions are not valid in 64-bit mode.

Gunther
You have to know the facts before you can distort them.

TouEnMasm

The atodw is perfect and not an api (Why exclude them ?)

atodw proc String:DWORD

  ; ----------------------------------------
  ; Convert decimal string into dword value
  ; return value in eax
  ; ----------------------------------------

    push esi
    push edi

    xor eax, eax
    mov esi, [String]
    xor ecx, ecx
    xor edx, edx
    mov al, [esi]
    inc esi
    cmp al, 2D
    jne proceed
    mov al, byte ptr [esi]
    not edx
    inc esi
    jmp proceed

  @@:
    sub al, 30h
    lea ecx, dword ptr [ecx+4*ecx]
    lea ecx, dword ptr [eax+2*ecx]
    mov al, byte ptr [esi]
    inc esi

  proceed:
    or al, al
    jne @B
    lea eax, dword ptr [edx+ecx]
    xor eax, edx

    pop edi
    pop esi

    ret

atodw endp


.data
string db 20 dup(0)
.code
       mov [string],eax      ;take care order AH,al
      ..
     invoke atodw,addr string




Fa is a musical note to play with CL

dedndave

i didn't exclude it intentionally
i just figured a little theory is good
i know, for myself, i don't learn anything by using a canned routine - lol