flat assembler
Message board for the users of flat assembler.

Index > Main > convert string to integer

Author
Thread Post new topic Reply to topic
gavin



Joined: 20 Jul 2008
Posts: 23
gavin 30 Jul 2008, 18:26
Example:
string db "54333",0 needs to be -> number dw 54333
How would I go about this?
Post 30 Jul 2008, 18:26
View user's profile Send private message Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2139
Location: Estonia
Madis731 30 Jul 2008, 22:10
Take "5", subtract 30h, multiply by 10 => you get 50;
Take 50, add "4", sub 30h, mul 10 => 540;
etc.

Oh, you need the code Smile
Code:
    mov   esi,string
    xor   eax,eax
@@:
    movzx ebx,byte[esi] ;fetch a digit
    lea   eax,[eax*5] ;multiply by 5
    lea   eax,[eax*2+ebx-"0"] ;multiply by 2 (remember - 10!)
                              ;subtract 30h (or 48 in decimal)
    add   esi,1
    cmp   byte[esi],0
    jne   @b
    ;here you will have 12345 in eax to play with Smile


string db "12345",0
    
Post 30 Jul 2008, 22:10
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
mattst88



Joined: 12 May 2006
Posts: 260
Location: South Carolina
mattst88 31 Jul 2008, 02:53
Madis731 wrote:
Take "5", subtract 30h, multiply by 10 => you get 50;


A bit of clarification: 0x30 is the ASCII code for '0', so '0' (that is, 0x30) + 5 will give you the ASCII code for the character '5'.

_________________
My x86 Instruction Reference -- includes SSE, SSE2, SSE3, SSSE3, SSE4 instructions.
Assembly Programmer's Journal
Post 31 Jul 2008, 02:53
View user's profile Send private message Visit poster's website Reply with quote
kandamun



Joined: 20 Jul 2005
Posts: 25
kandamun 31 Jul 2008, 05:49
Madis731 wrote:
Take "5", subtract 30h, multiply by 10 => you get 50;

Of course as any nicely written code it has a small bug.
There will be one extra zero at the end of the result.
I guess it is
Code:
init with 0, multiply by 10, add "5", subtract 30h => you get 5.    


or any other way to remove the extra multiply by 10.
Post 31 Jul 2008, 05:49
View user's profile Send private message ICQ Number Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4353
Location: Now
edfed 31 Jul 2008, 15:55
str2num, will convert string to decimal value.
it will seek the first char of the string, then, the last char of the number, tehn, start with zero value, and make the basic n=a-'0' +n*10
after, it will point to the end of the number.

esi is used as source pointer.
it will separate multiple numbers, by soem symbols.
it can be used to convert many little strings in many numbers.

Code:
str2num:
@@:
        mov     al,[esi]
        inc     esi
        cmp     al,' '
        je      @b
@@:
        mov     al,[esi]
        inc     esi
        cmp     al,' '
        je @f
        cmp     al,'.'
        je @f
        cmp     al,','
        je @f
        cmp     al,0
        jne     @b
@@:
        dec     esi
        push    esi
        dec     esi
        xor     ebx,ebx
        lea     edx,[ebx+1]
@@:
        movzx   eax,byte[esi]
        dec     esi
        cmp     al,'-'
        je      .neg
        cmp     al,' '
        je      .end
        cmp     al,','
        je      .end
        cmp     al,'.'
        je      .end
        sub     al,'0'
        jl      @f
        cmp     al,9
        jg      @f
        imul    eax,edx
        add     ebx,eax
        imul    edx,10
        jmp     @b
@@:
        pop     esi
        clc
        ret
.neg:
        mov     al,[esi]
        cmp     al,' '
        jne     @b
        neg     ebx
.end:
        pop     esi
        stc
        ret
    


used as a snippet in fool code.
as it:
Code:
.box:
        call str2num
        jnc @f
        mov [shell.box+box.x],ebx
@@:
        call str2num
        jnc @f
        mov [shell.box+box.y],ebx
@@:
        call str2num
        jnc @f
        or ebx,ebx
        je @f
        mov [shell.box+box.xl],ebx
@@:
        call str2num
        jnc @f
        or ebx,ebx
        je @f
        mov [shell.box+box.yl],ebx
@@:
        call str2num
        jnc @f
        mov [shell.box+box.c],ebx
@@:
        ret

    
Post 31 Jul 2008, 15:55
View user's profile Send private message Visit poster's website Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2139
Location: Estonia
Madis731 31 Jul 2008, 19:37
Sorry, kandamun, the 'spoken code' was flawed, but the ASM-code itself is okay. It inits to 0, then multiplys by 10 and adds the first number.
Post 31 Jul 2008, 19:37
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
FrozenKnight



Joined: 24 Jun 2005
Posts: 128
FrozenKnight 02 Aug 2008, 21:17
this is a snippet form some code i did a few years back it coverts a floating point string to a floating point value. if you review it you will see one of my first major errors where i held the multiplier in ecx. Please note that this was my first attempt at writing code using the FPU.

note that this code doesn't do any checking if the number is valid or not as this was meant to be part of a subclassed dialog and the checking was done in a different area.

Code:
proc flttovalue, string:DWORD, value:DWORD;, decimal:DWORD
  local  temp:DWORD, cword:WORD, multiplyer:TBYTE
  push      ebx
  push   ecx
  mov    ebx, [string]
  finit
  ;fstcw [cword]
  ;mov   ax, [cword]
  ;or    ax, 00300h
  ;and   ax, 0F3FFh
  ;mov   [cword], ax
  ;fldcw [cword]
  ;mov   [temp], 0
  ;fild  [temp]
  fldz
  xor      eax, eax
  mov       eax, 1
  cmp BYTE [SS:ebx], '-'
  jnz       @f
  mov     eax, -1
  inc        ebx
 @@:
  push       eax
  ;fldz
 .ipart:
  cmp BYTE [SS:ebx], '.'
  jz   .fpart0
  cmp     BYTE [SS:ebx], 0
  jz   .fin
  ;mov   [temp], 10
  ;fild  [temp]
  fimul [const10]
  xor     eax, eax
  mov       al, BYTE [SS:ebx]
  sub  al, '0'
  mov      [temp], eax
  fild   [temp]
  faddp st1, st0
  inc     ebx
  jmp  .ipart
 .fpart0:
  inc  ebx
  lea    ecx, [multiplyer]
  fld1
  fstp   tbyte [ecx]
 .fpart1:
  cmp   BYTE [SS:ebx], '.'
  jz   .fin
  cmp        BYTE [SS:ebx], 0
  jz   .fin
  xor    eax, eax
  mov       al, BYTE [SS:ebx]
  sub  al, '0'
  mov      [temp], eax
  ;fild  [temp]
  ;mov  eax, 10                  ;<==== error here
  ;mul  ecx
  ;mov  ecx, eax
  ;mov  [temp], ecx
  ;mov   [temp], 10
  fld       TBYTE [ecx]
  fidiv [const10]
  fstp      TBYTE [ecx]
  fld    TBYTE [ecx]
  fild   [temp]
  fmulp st1, st0
  faddp st1, st0
  inc ebx
  jmp  .fpart1
 .fin:
  ;fldpi ;test
  pop  eax
  mov    [temp], eax
  fild   [temp]
  fmulp st1, st0
  ;fstcw [cword]
  ;mov   ax, [cword]
  ;mov   cx, WORD [iFloatType]
  ;shl   cx, 8
  ;or    cx, 0F0FFh
  ;and   ax, cx
  ;mov   [cword], ax
  ;fldcw [cword]
  mov       eax, [value]
  mov   edx, [iFloatType]
  cmp      edx, const80
  jnz   @f
  ;fldpi ;test
  fstp  TBYTE [eax]
 @@:
  cmp        edx, const64
  jnz   @f
  fstp    QWORD [eax]
 @@:
  cmp        edx, const32
  jnz   @f
  fstp    DWORD [eax]
 @@:
  mov        eax, esi
  pop       ecx
  pop    ebx
  ret
endp    
Post 02 Aug 2008, 21:17
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.