Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

x86 Assembly Code on String to Integer Help!

1,371 views
Skip to first unread message

blakMole

unread,
Jan 22, 2004, 7:26:56 PM1/22/04
to
Help! How do I convert a string input (numbers) to its equivalent integer value. similar to function atoi in c#.

thankx is advance


Matt Taylor

unread,
Jan 22, 2004, 9:52:04 PM1/22/04
to
"blakMole" <obliv...@yahoo.com> wrote in message
news:3ff7532be9e80aaa...@localhost.talkaboutprogramming.com...

> Help! How do I convert a string input (numbers) to its equivalent integer
value. similar to function atoi in c#.
>
> thankx is advance

What do you have so far? Have you looked at the implementation of atoi() in
the C runtime library? The idea is really simple:

"1234" = 1*10^3 + 2*10^2 + 3*10^1 + 4*10^0

Here's a hint. The equation can be rearranged like so:

(((1*10+2)*10)+3*10)+4

You should be able to use that to construct a loop that reads a digit and
accumulates it.

-Matt


John H. Guillory

unread,
Jan 23, 2004, 12:14:15 AM1/23/04
to

This may not be the best, but this is working code for A386 real
mode....

jmp start

str1: db '1024',0
mult1: dw 10

eostr1 equ $

start: mov si, str1
CALL AtoI
RET

atoi: sub edx, edx ; Value = 0
sub eax, eax ; eax = 0
cld ; increment
lop: lodsb
OR AL, AL ; AL = 0?
JZ End1 ; If So, Exit Loop
PUSH AX ; Save AX
XCHG DX, AX ; Swap DX and AX
SUB DX, DX ; Clear DX
MUL WORD [mult1] ; Multiply by 10
XCHG DX, AX ; Move back to DX
POP AX
sub al, 30h ; Convert to dec.
SUB AH, AH
ADD DX, AX ; Add the next digit in...
JMP Lop ; Keep going till we hit the null....
End1: RET


Randall Hyde

unread,
Jan 23, 2004, 12:34:24 AM1/23/04
to
Check out tons of library code on Webster at
http://webster.cs.ucr.edu.
Both the UCR stdlib, HLA stdlib, and the MASM32 lib have
routines that do several different types of string<->numeric
translations.
cheers,
Randy Hyde

"blakMole" <obliv...@yahoo.com> wrote in message
news:3ff7532be9e80aaa...@localhost.talkaboutprogramming.com...

0 new messages