flat assembler
Message board for the users of flat assembler.

Index > Windows > On simple array

Author
Thread Post new topic Reply to topic
TmX



Joined: 02 Mar 2006
Posts: 821
Location: Jakarta, Indonesia
TmX
Let's say I have an array like this: [10,20,30,40]

If I'm going to access the 3rd element, which is 30, in C, the code would be like this:
Code:
#include <stdio.h>

int main(){
    int array[] = {10,20,30,40};
      printf("%d",array[2]);
    return 0;
}
    


And this my FASM code:
Code:
format PE console 4.0

include 'win32a.inc'

entry start

section '.data' data readable
array db 10,20,30,40
fmt db "%d",0

section '.code' readable executable
start:
 mov eax,[array+2*4]
 cinvoke printf,fmt,eax
 invoke ExitProcess,0

section '.idata' data import readable writeable
library kernel32,'kernel32.dll',msvcrt,'msvcrt.dll'
import kernel32,ExitProcess,'ExitProcess'
import msvcrt,printf,'printf'       


FASM says "Error: operand size doesn't match" on "mov eax,[array+2*4]"?
Any explanations? Confused
Post 01 Jun 2009, 06:41
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1287
Location: behind the arc
Picnic
Change db to dd like this.
Code:
array dd 10,20,30,40
    


But If you need to declare bytes instead of dwords use like this

Code:
array db 10,20,30,40
;
;
movzx eax,[array+3]
    
Post 01 Jun 2009, 06:56
View user's profile Send private message Reply with quote
TmX



Joined: 02 Mar 2006
Posts: 821
Location: Jakarta, Indonesia
TmX
Ah OK, that works. I should take a note on that.

BTW, how can I expand the array, e.g becoming a 2D array like this:
[[10,20],[30,40],[50,60]]

So accessing array[1][1] would give you 40?
Post 01 Jun 2009, 07:18
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1287
Location: behind the arc
Picnic
Checkout here for multi-dimensional arrays..

http://board.flatassembler.net/topic.php?t=9078
Post 01 Jun 2009, 07:30
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-2020, Tomasz Grysztar.

Powered by rwasa.