flat assembler
Message board for the users of flat assembler.

 Index > Main > convert string to integer
Author
gavin

Joined: 20 Jul 2008
Posts: 23
gavin 30 Jul 2008, 18:26
Example:
string db "54333",0 needs to be -> number dw 54333
30 Jul 2008, 18:26

Joined: 25 Sep 2003
Posts: 2139
Location: Estonia
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
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)
cmp   byte[esi],0
jne   @b
;here you will have 12345 in eax to play with

string db "12345",0
```
30 Jul 2008, 22:10
mattst88

Joined: 12 May 2006
Posts: 260
Location: South Carolina
mattst88 31 Jul 2008, 02:53
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
31 Jul 2008, 02:53
kandamun

Joined: 20 Jul 2005
Posts: 25
kandamun 31 Jul 2008, 05:49
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.
31 Jul 2008, 05:49
edfed

Joined: 20 Feb 2006
Posts: 4325
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
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

```
31 Jul 2008, 15:55

Joined: 25 Sep 2003
Posts: 2139
Location: Estonia
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.
31 Jul 2008, 19:37
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]
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
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    ```
02 Aug 2008, 21:17
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum