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

Recommended Answers

All 4 Replies

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.

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

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

could you explain this lines please
sub al,48 ;;;;;why why subtracts by 48
sub dl,48 ;;; what is different between this and the above
mov ebx,10 ;;;and for what we multiply by 10

thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.