Question

My program receives a string representing a hex value, say "324" and I want to convert it to an actual value in decimal (aka get the integer 804)

So far, I've read char by char, and for each one, I would convert it to value (move it to AL, and sub 48), then multiply by 16, and only at the end divide one time by 16. The problem: If I input a hex string more than "99" in hex, my registers reset out to 0, but I need to be able to support much larger values.

My sketch:

initialize ecx register with 0 that will hold our overall decimal integer value
for each step:
    1. mov al, byte [ebx] (ebx holds our string)
    2. sub, al 48
    3. add eax, ecx
    4. multiply eax by 16, then move content of eax to ecx

Is there another way?

Était-ce utile?

La solution

If I understand correctly you are trying to parse hex decimal string to value in register. It would be easier to see the problem if you posted actual code.

I would do something like this:

  ; accepts null terminated string with characters '0'-'9', 'A'-'F'
  ; return value in eax
    xor  eax, eax
.loop:
    mov  cl, byte [ebx]
    test cl, cl
    jz   .end
    cmp  cl, 65
    jl   .numeric
    sub  cl, 7 ; 'A' code is 65, need to subtract 7 more so 'A' = 10
.numeric:
    sub  cl, 48
    shl  eax, 4
    or   al, cl
    inc  ebx
    jmp  .loop
.end:
    ret
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top