The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: fusspot on August 14, 2007, 01:18:38 PM

Title: getting index of entered character
Post by: fusspot on August 14, 2007, 01:18:38 PM
Hi! I'm so new to assembly language programming and I'm just lost.  :dazzled:
I have an array of characters and my program asks the user to enter a character. How do I get the index (in the array) of the entered character?
For example, my array is "abcde" and the user enters 'c' (which is index 2). How do I obtain the index of 'c' in "abcde"?
Thanks.

Title: Re: getting index of entered character
Post by: ramguru on August 14, 2007, 05:24:48 PM
You should first learn concepts of programming before entering dangerous asm zone  :lol
;So the string is just array of bytes, first you get a pointer to string's starting location
mov esi, OFFSET str
;Then declare register that will be your index
xor ecx, ecx
;Then you enter the loop
@funky_loop:
;Then you get a character from the array
mov al, BYTE PTR [esi]
;Then you compare it with your character
cmp al, 'c'
;If it corresponds... you jump @outta_funky_loop
jz  @outta_funky_loop
;Then you increase the index register
inc ecx
;Then you increase pointer
inc esi
;Then you check if it's the end of string
cmp al, 0
;If it's not the case you again jump to the beginning of the loop
jnz @funky_loop

@outta_funky_loop:
Title: Re: getting index of entered character
Post by: raymond on August 15, 2007, 02:53:37 AM
Sounded like HOMEWORK didn't it???? :naughty: :naughty: :naughty: