We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,901 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Converting String to integer. x86

I am doing a project that must convert each string in X to an integer and save it in the corresponding location in Y. A dollar sign is used to mark the end of a string. I have 90% of the code, but it only works for the first string value. How can I step through this so that it does all the strings in the array.

include irvine32.inc

.data 
X   byte      "12345$", "-12345$", "99999$", "-99999$" ;Strings to be converted.
Y   sdword    4 dup (?)

.code
start:

mov esi, offset X
cmp byte ptr [esi],'-'

je negative		;if X ='s a - then jump to negative.
mov ch, 0		;ch=0, a flag for positive
jmp convert		; if positive then jump to convert.

negative:
mov ch,1		;ch =1, a flag for negative
inc esi			; esi -> the first digit char
convert:
mov al,[esi]	;al = first digit char
sub al,48		; subtracts al by 48 first digit
movzx eax,al	;al=>eax, unsigned
mov ebx,10		;ebx=>10, the multiplier

next:
inc esi					;what's next?
cmp byte ptr [esi],'$'	;end of string
je fin					; if finished store result in Y

mul ebx					;else, eax*ebx==>edx eax
mov dl,[esi]			;dl = next byte
sub dl,48				;dl=next digit
movzx edx,dl			;dl => edx, unsigned
add eax,edx
jmp next

fin:
cmp ch, 1				; a negative number?
je changeToNeg
jmp storeResult
changeToNeg:
neg eax
storeResult:
mov y,eax

invoke exitProcess,0
end start

Thank you Very much for any help

3
Contributors
3
Replies
4 Days
Discussion Span
1 Year Ago
Last Updated
4
Views
Question
Answered
kww228
Newbie Poster
15 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

I would first put something at the end of the array to indicate there is no more data -- that way the program knows when it is done. The last byte in the array could be either another $ or binary 0, but anything except numeric digits could be used too. After that, just modify the code you have to keep looping through the array's memory until end-of-array byte is found. You might want to put the code in another function then repeatedly call that function for each item in the array.

Ancient Dragon
Achieved Level 70
Team Colleague
32,124 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,575
Skill Endorsements: 69

Thank you. I was able to get it to work just by adding a new character for ptr to look for.

kww228
Newbie Poster
15 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Hi,

You may find this code useful.

cvt_str2int_array:
	lea	esi, [cvt_input]
	lea	edi, [cvt_output]
.big_loop:
	xor	eax, eax		; accu
	xor	ebx, ebx		; number
	xor	ecx, ecx		; minus flag: 0 = positive, -1 = minus
.next_chr:
	lodsb
	cmp	al, 0
	je	.exit
	cmp	al, '$'
	je	.dollar
	cmp	al, '-'
	je	.minus
	sub	al, '0'			; normalize ASCII to number
	jc	.exit
	cmp	al, 9
	jg	.exit
	lea	ebx, [ebx+ebx*4]	; ebx = ebx * 5
	lea	ebx, [eax+ebx*2]	; ebx = (ebx * 2) + digit
	jmp	.next_chr
.minus:
	not	ecx			; toggle 'minus' bit in ecx
	jecxz	.exit			; jump if we have more than 1 '-' sign
	jmp	.next_chr
.dollar:	
	mov eax, ebx			; move number to eax
	xor eax, ecx			; if minus then ecx = -1, so effectively eax = not eax
	neg ecx				; if positive then ecx = 0, so nothing happens to eax
	add eax, ecx			; Effectively, we do a conditional NEG on eax, depending on ecx's value
	stosd				; store value and increment pointer
	jmp	.big_loop
.exit:
	ret


cvt_input db '12345$-12345$99999$-99999$',0
align 4
cvt_output	dd 0,0,0,0
pgcoder
Newbie Poster
6 posts since Feb 2012
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0
Question Answered as of 1 Year Ago by Ancient Dragon and pgcoder

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.2162 seconds using 2.68MB