C++ Learning Community Forum
August 01, 2010, 02:48:02 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Hello. Smiley
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: fasm struct  (Read 2242 times)
krondos
rotartsinimdA
N00b!!1
*
Posts: 15



View Profile
« on: December 13, 2007, 09:02:03 AM »

Code:
struct point
  x db ?
  y db ?
ends


im confused about how to make a structure array, could someone show me an example with the data above Grin
« Last Edit: December 14, 2007, 05:23:00 AM by krondos » Logged


for(p=0;;*(p++)=0);
FrozenKnight
ASM Freak
Global Moderator
Dr. of C++ology
*****
Posts: 546


Do it yourself it's the only way to learn.


View Profile
« Reply #1 on: December 13, 2007, 02:49:26 PM »

Code
GeSHi (asm):
;first you need to name your struct
struct point
 x db ?
 y db ?
ends
;then you need to reserve space for it
pt     point ;you don't need the '?' because it's already inside the struct
      rb sizeof.point * (array_size -1) ;unfortunately this is currently required because the 'dup' instruction can't handle structs yet
                                       ;but hopefully it will be soon.
 
Created by GeSHI 1.0.7.18
Logged


Imagine the impossible, then make it happen.
krondos
rotartsinimdA
N00b!!1
*
Posts: 15



View Profile
« Reply #2 on: December 14, 2007, 06:13:31 AM »

ty,
         could you provide me with a small example going thew the array an assigning data? Cheesy

array size being 2
Code:
mov ebx,pt
mov [ebx + point.y * 2],5

 Huh Huh
« Last Edit: December 14, 2007, 10:56:15 AM by krondos » Logged


for(p=0;;*(p++)=0);
FrozenKnight
ASM Freak
Global Moderator
Dr. of C++ology
*****
Posts: 546


Do it yourself it's the only way to learn.


View Profile
« Reply #3 on: December 14, 2007, 08:06:20 PM »

Code:
mov ebx,pt
mov [ebx + (sizeof.point*(2-1)) + point.y],5
you need the "sizeof.point*(2-1)" to be the offset of the current array member you want. the "-1" part is required because the array count starts at 0 just like in C
Logged


Imagine the impossible, then make it happen.
krondos
rotartsinimdA
N00b!!1
*
Posts: 15



View Profile
« Reply #4 on: December 15, 2007, 06:52:16 AM »

ok, all makes sense now, thankyou  Cheesy Cheesy
Logged


for(p=0;;*(p++)=0);
krondos
rotartsinimdA
N00b!!1
*
Posts: 15



View Profile
« Reply #5 on: December 15, 2007, 03:04:36 PM »

This is kinda really confusing me, The first way wont asseble, but 2nd way does work, when the struct doesnt contain a dw. Sad

error message:
Code
GeSHi (asm):
mov [pt + (sizeof.COORD*(edi)) + COORD.X],eax
error:invalid address
 
Created by GeSHI 1.0.7.18

1st way
Code
GeSHi (asm):
 
format PE console 4.0
entry start
 
include 'c:\fasm\INCLUDE\WIN32A.INC'
 
section '.data' readable writeable
 
       struct COORD
               X dd ?
               Y dw ?
       ends
 
       pt COORD
       rd (sizeof.COORD * (2-1))
 
section '.code' readable executable
       start:
               mov eax,1
               mov bx,2
               mov edi,0
 
               mov [pt + (sizeof.COORD*(edi)) + COORD.X],eax
               mov [pt + (sizeof.COORD *(edi)) + COORD.Y],bx
 
       exit:
               push 0
               call [ExitProcess]
 
section '.idata' import readable executable
       library kernel32,'KERNEL32.DLL'
 
       import kernel32,\
               ExitProcess,'ExitProcess'
 
Created by GeSHI 1.0.7.18


2nd way
Code
GeSHi (asm):
 
format PE console 4.0
entry start
 
include 'c:\fasm\INCLUDE\WIN32A.INC'
 
section '.data' readable writeable
 
       struct COORD
               X dw ?
               Y dw ?
       ends
 
       pt COORD
       rd (sizeof.COORD * (2-1))
 
section '.code' readable executable
       start:
               mov ax,1
               mov bx,2
               mov edi,0
 
               mov [pt + (sizeof.COORD *(edi)) + COORD.X],ax
               mov [pt + (sizeof.COORD *(edi)) + COORD.Y],bx
 
       exit:
               push 0
               call [ExitProcess]
 
section '.idata' import readable executable
       library kernel32,'KERNEL32.DLL'
 
       import kernel32,\
               ExitProcess,'ExitProcess'
 
Created by GeSHI 1.0.7.18
« Last Edit: December 15, 2007, 03:08:40 PM by krondos » Logged


for(p=0;;*(p++)=0);
FrozenKnight
ASM Freak
Global Moderator
Dr. of C++ology
*****
Posts: 546


Do it yourself it's the only way to learn.


View Profile
« Reply #6 on: December 21, 2007, 10:07:07 AM »

This is an example of FASM's size checking ( a from of type checking) you cant store a 32 bit variable into a 16 bit memory space. dd is 32 bits so it will work with eax, ebx, ecx, edx, etc... as long as it's 32 bits. however if you try to store a 32 bit var into a 16 bit location then you have trouble, you will need to use 16 bit registers ax, bx, cx, dx, etc...
Logged


Imagine the impossible, then make it happen.
FrozenKnight
ASM Freak
Global Moderator
Dr. of C++ology
*****
Posts: 546


Do it yourself it's the only way to learn.


View Profile
« Reply #7 on: December 22, 2007, 09:58:29 AM »

sorry i examined this a little further and found the problem. in address calculations you can only multiply the size by 2, 4 or 8.
So "(sizeof.COORD *(esi))" only works when "sizeof.COORD" evaluates to 2, 4 or 8. otherwise i would recommend doing this multiply outside this address calculation.

sorry bout my earlier post but didn't really have time to fully examine this issue.

Edit: I thought i would elaborate this a little more so that you could fully understand. for address calculations on intell based processors. the syntax is.
Code:
const + reg + reg * const(must be power of 2 no higher than 8)
of course not all instructions support this elongated form some limit you to some combination there of such as
Code:
const + reg
or
Code:
const + reg * const(must be power of 2 no higher than 8)

to give an example your
Code:
mov [pt + (sizeof.COORD*(edi)) + COORD.X],eax
evaluates to
Code:
mov [const + (const*(reg)) + const], reg
if you combine all constants you get
Code:
mov [const + const*reg], reg
btw, this is a restriction intell placed into the language to help processors it's not meant to help humans. so it's not a flaw with the language.

Just remember these restrictions when calculating address or debugging addressing problems. hope all of this helps.
« Last Edit: December 22, 2007, 11:21:28 AM by FrozenKnight » Logged


Imagine the impossible, then make it happen.
krondos
rotartsinimdA
N00b!!1
*
Posts: 15



View Profile
« Reply #8 on: December 23, 2007, 03:41:46 AM »

Thank you,
                   but what im confused about is, what happens when you want 3 double words in the structure.
Logged


for(p=0;;*(p++)=0);
FrozenKnight
ASM Freak
Global Moderator
Dr. of C++ology
*****
Posts: 546


Do it yourself it's the only way to learn.


View Profile
« Reply #9 on: December 24, 2007, 09:00:46 AM »

you will need to calculate the array position in another instruction instruction.

Note, because of this limitation you will get the best speed performance from your strcts if you align them to a size that is a power of 2.
as you can use the 'shl' instruction to do a very fast multiply. ex: shl edi, 4; is the same as multiply edi by 2^4 (16)
Logged


Imagine the impossible, then make it happen.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!