flat assembler
Message board for the users of flat assembler.

Index > Main > size operator

Author
Thread Post new topic Reply to topic
flat_user



Joined: 01 Jun 2010
Posts: 13
flat_user 15 Jun 2010, 22:13
sorry, if this question is noobish or has been asked a thousand times, I couldn't find any particular answers...

How could I write something like this (or is there such an operator?):

Code:
mov eax,size var ;=4
mov eax,length var2 ;=40
mov eax,size var2 * length var2 ;=80

var rd 1
var2 rw 40    

thank you
Post 15 Jun 2010, 22:13
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 15 Jun 2010, 23:05
flat_user,

Overload directives with struc-macro:
Code:
irps s, b w d f p q t {
  struc r#s num* \{
    . r#s 1
    .size = $-.
      r#s num-1
    .length = num
  \}
}
mov eax,var.size ;=4
mov eax,var2.length ;=40
mov eax,var2.size * var2.length ;=80

var rd 1
var2 rw 40    
Note that var3: rq 2 won't define any of those numeric constants (because of colon, struc-macro won't be invoked).

Similar technique could be used for db-like directives.
Post 15 Jun 2010, 23:05
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20303
Location: In your JS exploiting you and your system
revolution 15 Jun 2010, 23:05
Code:
struc rd val {
  . rd val
  sizeof.#. = 4
  lengthof.#. = val
}
struc rw val {
  . rw val
  sizeof.#. = 2
  lengthof.#. = val
}

mov eax,sizeof.var ;=4
mov eax,lengthof.var2 ;=40
mov eax,sizeof.var2 * lengthof.var2 ;=80

var rd 1
var2 rw 40    
Post 15 Jun 2010, 23:05
View user's profile Send private message Visit poster's website Reply with quote
flat_user



Joined: 01 Jun 2010
Posts: 13
flat_user 15 Jun 2010, 23:25
Thanks,
but I would need to include those macros in every project...
I also could insert them into FASM's package's includes so they would be easier to use, but then such source code would need to be given out with those include-mods as well means it'd be more extensive to compile...
Nevertheless, I'd say such operators are very useful, so
I've got the question if there will be native support?
Thank you!
Post 15 Jun 2010, 23:25
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20303
Location: In your JS exploiting you and your system
revolution 15 Jun 2010, 23:55
flat_user wrote:
I've got the question if there will be native support?
I doubt it. The usefulness is not all that great. It would seem just convenient for your particular situation. Not many people need to have the size of a dword defined as a variable, mostly those things are defined as constants rather than magic numbers.
Code:
BUFFER_LENGTH = 40

;...

buff rd BUFER_LENGTH

;...

mov ecx,BUFFER_LENGTH    
Post 15 Jun 2010, 23:55
View user's profile Send private message Visit poster's website Reply with quote
flat_user



Joined: 01 Jun 2010
Posts: 13
flat_user 16 Jun 2010, 11:56
Code:
_64BIT equ 1

call ReadProcessMemory,[proc],[addr],var,size var,0

if _64BIT eq 1
  var rq 1
else
  var rd 1
endif    

I guess this would be the most straightforwarded and most dynamical way
without using any extra symbols or additional macros which you would need to distribute...
(if there was native support)
Post 16 Jun 2010, 11:56
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 16 Jun 2010, 12:15
flat_user,

What if var is a structure? FASM knows nothing about structures.

No need for feature that can be implemented almost hassle-free using current tools (macros from MACRO\STRUCT.INC for structures, as an example).
Post 16 Jun 2010, 12:15
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 16 Jun 2010, 18:57
baldr,

Your code fails when the size is zero. I've fixed it:
Code:
irps s, b w d f p q t {
local elemSize

  virtual at 0
    r#s 1
    elemSize = $
  end virtual

  struc r#s num* \{
    . r#s num
    .size = elemSize
    .length = num
  \}
}    
Double dots were used so this code chunk can be placed anywhere safely.

[edit]Removed the double dots. See the reason below.[/edit]


Last edited by LocoDelAssembly on 16 Jun 2010, 19:33; edited 1 time in total
Post 16 Jun 2010, 18:57
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 16 Jun 2010, 19:24
LocoDelAssembly,

I knew about this. My first variant was similar to yours, only virtual block was inside struc-macro (just in case rx being overloaded again Wink), then I've simplified it for readability.

Numeric constant definition doesn't set new label prefix, you don't need ..elemSize.
Post 16 Jun 2010, 19:24
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 16 Jun 2010, 19:30
Quote:

Numeric constant definition doesn't set new label prefix, you don't need ..elemSize.
Damn, I always forget that!
Post 16 Jun 2010, 19: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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.