Page 1 of 1
Assembly String to Int
Posted: Fri Aug 22, 2008 7:55 am
by matiome
Does anyone know how to change a string to a number in assembly?
Idealy I would like it in a function eg:
Re: Assembly String to Int
Posted: Fri Aug 22, 2008 8:38 am
by Combuster
Does anyone know how to change a string to a number in assembly?
Yes.
What is the problem that you can't solve this puzzle yourself?
Re: Assembly String to Int
Posted: Fri Aug 22, 2008 8:40 am
by matiome
1) I can't really program assembly, I have googled the problem but found nothing.
2) Can you tell me how?
Re: Assembly String to Int
Posted: Fri Aug 22, 2008 8:43 am
by Combuster
Re: Assembly String to Int
Posted: Fri Aug 22, 2008 8:45 am
by matiome
I have already done that, thanks, and most if not all of the tutorials aren't in-depth.
Re: Assembly String to Int
Posted: Fri Aug 22, 2008 8:56 am
by Solar
http://webster.cs.ucr.edu/AoA/DOS/index.html
Writing the function for you won't get you anywhere, since I doubt "string-to-int" is the only function for which you have a compelling reason for having to do it in assembly.
I.e., we write string-to-int for you, you return asking for int-to-double or somesuch.
Hence, all you will get here (hopefully) are pointers to "how-to-learn-Assembly".
The link above is one of the most in-depth books I have seen on the matter.
Re: Assembly String to Int
Posted: Fri Aug 22, 2008 9:09 am
by matiome
The reason I want the function is to develop on an operating system (written by someone else).
Thanks for the link though!
Re: Assembly String to Int
Posted: Fri Aug 22, 2008 11:36 am
by Dex
Re: Assembly String to Int
Posted: Sat Aug 23, 2008 5:34 am
by matiome
Could the code from that link be made to work in nasm?
Re: Assembly String to Int
Posted: Sat Aug 23, 2008 6:23 am
by bewing
Some of them are already in nasm format (or, at least, intel format).
The rest are in AT&T format. It's more complicated to change those. To "fix" those, you have to swap the order of all the operands, and change the names of the labels from @whatever to a legal name.
In its simplest form, I prefer the loop that goes:
Code: Select all
xor ebx, ebx ; clear the loop variables
xor eax, eax
.declp:
lodsb
test al, al ; hit end of string?
je short .done
lea ebx, [ebx + ebx*4] ; first multiply by 5
lea ebx, [eax + ebx*2 - 0x30] ; then multiply by 2, add in al, subtract ascii 0
jmp short .declp
.done:
Re: Assembly String to Int
Posted: Sat Aug 23, 2008 7:56 am
by matiome
In the code in the previous post how do you enter a string to it (sorry I know practicly nothing about assembly)?
Re: Assembly String to Int
Posted: Sat Aug 23, 2008 9:00 pm
by bewing
ESI points to the string in memory (assuming that you are in Protected Mode). LODSB reads one byte from ESI, puts it in AL, and increments ESI. The LEA functions allow you to do multiple additions all at one time, effectively. EBX contains the cumulative result, as each new digit is inserted into the sum. The TEST function checks if register AL is set to 0.