flat assembler
Message board for the users of flat assembler.

Index > Main > Array

Author
Thread Post new topic Reply to topic
Alessio



Joined: 26 Sep 2003
Posts: 35
Location: Viterbo, Italy
Alessio 17 Jan 2004, 00:09
Hi,
sorry to bother you, but I'm new to asm,
and I've found a bit complicated array stuff...
Supposed I've an array (it is ?) declared in this way...

;number as ID - zero string as description

_simple_array db 1,'cat',0,\
2,'dog',0,\
3,'dolhpin',0,\
5,'monkey',0,\
6,'donkey',0,\
9,'pig',0


How can I access to string element?
I've coded a small proc, it is very poor in syntax,
but (seems to) works, there's a better way?

proc GetStringFromArray,animalID

enter
push ecx edx edi
mov eax,[animalID]
xor ecx,ecx
xor edx,edx
mov edi,_simple_array

.mainloop:
cmp byte[edi+edx],al ;animalID is equal to byte at address... ?
je .found
inc ecx
cmp cl,al ;array ID is above of animalID ?
ja .notfound
cmp ecx,6 ;number of elements in array
ja .notfound

.secondloop: ;this loop to address Array ID
inc edx ;next byte after zero terminated string
cmp byte[edi+edx],0
jnz .secondloop
inc edx
jmp .mainloop

.found:
;do something
jmp .finish

.notfound:
;do something

.finish:
pop edi edx ecx

return


Can someone help me?
I know that it is a stupid example. Sorry.

Thanks.
Post 17 Jan 2004, 00:09
View user's profile Send private message MSN Messenger Reply with quote
roticv



Joined: 19 Jun 2003
Posts: 374
Location: Singapore
roticv 17 Jan 2004, 02:27
You need something to mark the end of the array... something like a double null or something like that.
Post 17 Jan 2004, 02:27
View user's profile Send private message Visit poster's website MSN Messenger Reply with quote
VitalOne



Joined: 29 Jul 2003
Posts: 54
Location: USA
VitalOne 17 Jan 2004, 06:18
Code:
proc GetString,ID
enter
push edx ebx ecx
mov dl,byte[ID]
lea esi,[_simple_array]
mov ecx,0
.loop:
lodsb
cmp al,dl
je .done
inc ecx
jmp .loop
.done:
inc ecx
lea eax,[_simple_array+ecx]
pop edx ebx ecx
return
    


This works for your situation but if your array ID is a character then it won't work (like 'A' and 65) So you should use a different system unless your arrays are small.
Post 17 Jan 2004, 06:18
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote
Alessio



Joined: 26 Sep 2003
Posts: 35
Location: Viterbo, Italy
Alessio 18 Jan 2004, 18:50
Thank you all.
Post 18 Jan 2004, 18:50
View user's profile Send private message MSN Messenger Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2139
Location: Estonia
Madis731 18 Jan 2004, 19:52
It seems to me that your code switches the places of ecx and edx
...
push edx ebx ecx
...
pop edx ebx ecx
...

OR does it really compile to
push edx
push ebx
push ecx
pop ecx
pop ebx
pop edx
???
Post 18 Jan 2004, 19:52
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
VitalOne



Joined: 29 Jul 2003
Posts: 54
Location: USA
VitalOne 19 Jan 2004, 03:02
No, that was an error , thanks.
Post 19 Jan 2004, 03:02
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote
HarryTuttle



Joined: 26 Sep 2003
Posts: 211
Location: Poland
HarryTuttle 20 Jan 2004, 10:11
Code:
include '%fasminc%/win32ax.inc'
.data
_simple_array:
cat     db 'cat',0
dog     db 'dog',0
dolphin db 'dolhpin',0
monkey  db 'monkey',0
donkey  db 'donkey',0
pig     db 'pig',0
end_array db 0,0
_simple_pointer2array dd cat,dog,dolphin,monkey,donkey,pig  ;0->cat; 5->pig
.code
start:
mov edi,2  ;  <---which element
shl edi,2
invoke MessageBox,0,[_simple_pointer2array+edi],"array",0; show me-> _simple_array[2]
invoke ExitProcess,0
.end start 
    

_________________
Microsoft: brings power of yesterday to computers of today.
Post 20 Jan 2004, 10:11
View user's profile Send private message Reply with quote
HarryTuttle



Joined: 26 Sep 2003
Posts: 211
Location: Poland
HarryTuttle 20 Jan 2004, 15:40
Thanx to Privalov:

Code:
include '%fasminc%/win32ax.inc'
macro strtbl name,[string]
     {
      common
        label name dword
      forward
        local label
        dd label
      forward
        label db string,0
     }

.data
strtbl array_animal,'dog','cat','dolphin','monkey','donkey','pig'
.code
start:
mov eax,5 ;<--- give me the pig  , 0->dog ; 1->cat ;dolphin->2
mov esi,[array_animal+eax*4]
invoke MessageBox,0,esi,"array",0
invoke ExitProcess,0
.end start 
    

_________________
Microsoft: brings power of yesterday to computers of today.
Post 20 Jan 2004, 15:40
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.