Assembly String to Int

Programming, for all ages and all languages.
Post Reply
matiome
Posts: 6
Joined: Fri Aug 22, 2008 7:45 am

Assembly String to Int

Post by matiome »

Does anyone know how to change a string to a number in assembly?
Idealy I would like it in a function eg:

Code: Select all

.string_to_int
more code here
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Assembly String to Int

Post by Combuster »

Does anyone know how to change a string to a number in assembly?
Yes. :twisted:



What is the problem that you can't solve this puzzle yourself?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
matiome
Posts: 6
Joined: Fri Aug 22, 2008 7:45 am

Re: Assembly String to Int

Post by matiome »

1) I can't really program assembly, I have googled the problem but found nothing.
2) Can you tell me how?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Assembly String to Int

Post by Combuster »

I suggest you start with the basics:

http://www.google.com/search?q=Assembly+tutorial
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
matiome
Posts: 6
Joined: Fri Aug 22, 2008 7:45 am

Re: Assembly String to Int

Post by matiome »

I have already done that, thanks, and most if not all of the tutorials aren't in-depth.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Assembly String to Int

Post 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.
Every good solution is obvious once you've found it.
matiome
Posts: 6
Joined: Fri Aug 22, 2008 7:45 am

Re: Assembly String to Int

Post by matiome »

The reason I want the function is to develop on an operating system (written by someone else).
Thanks for the link though! :D
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Re: Assembly String to Int

Post by Dex »

We all learn by copying, see here: http://board.flatassembler.net/topic.php?t=9022
matiome
Posts: 6
Joined: Fri Aug 22, 2008 7:45 am

Re: Assembly String to Int

Post by matiome »

Could the code from that link be made to work in nasm?
User avatar
bewing
Member
Member
Posts: 1401
Joined: Wed Feb 07, 2007 1:45 pm
Location: Eugene, OR, US

Re: Assembly String to Int

Post 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:
matiome
Posts: 6
Joined: Fri Aug 22, 2008 7:45 am

Re: Assembly String to Int

Post by matiome »

In the code in the previous post how do you enter a string to it (sorry I know practicly nothing about assembly)?
User avatar
bewing
Member
Member
Posts: 1401
Joined: Wed Feb 07, 2007 1:45 pm
Location: Eugene, OR, US

Re: Assembly String to Int

Post 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.
Post Reply