flat assembler
Message board for the users of flat assembler.

Index > Main > How to define local string variable with default value?

Author
Thread Post new topic Reply to topic
nazha



Joined: 05 Mar 2009
Posts: 40
Location: Beijing, China
nazha 06 Mar 2009, 02:45
How to define local string variable with default value?

proc proctest
local var1:TCHAR 'abc',13,10
invoke MessageBox,HWND_DESKTOP,dword [var1],"test",MB_OK
ret
endp


when compile above code get error: illegal instruction.

Thanks
NaZha
Post 06 Mar 2009, 02:45
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20627
Location: In your JS exploiting you and your system
revolution 06 Mar 2009, 03:12
Remember that local variables are on the stack. So each time the procedure is called it would need to initialise the stack with the string.
Code:
proctest:
  sub esp,8 ;make space for string
  mov byte[esp+0],'a'
  mov byte[esp+1],'b'
  mov byte[esp+2],'c'
  mov byte[esp+3],13
  mov byte[esp+4],10
  mov byte[esp+5],0
  mov eax,esp  ;point to the beginning of the string
  invoke MessageBox,HWND_DESKTOP,eax,"test",MB_OK    
Of course it could be optimised with dword stores, but this just to show you what is required to do.
Post 06 Mar 2009, 03:12
View user's profile Send private message Visit poster's website Reply with quote
nazha



Joined: 05 Mar 2009
Posts: 40
Location: Beijing, China
nazha 06 Mar 2009, 03:17
Thanks!
Post 06 Mar 2009, 03:17
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8394
Location: Kraków, Poland
Tomasz Grysztar 06 Mar 2009, 04:06
The macros can do it automatically for you, too, but you need to use the "locals" macro:
Code:
proc proctest
locals 
 var1 TCHAR 'abc',13,10 
endl
invoke MessageBox,HWND_DESKTOP,addr var1,"test",MB_OK
ret
endp    

endp
Post 06 Mar 2009, 04:06
View user's profile Send private message Visit poster's website Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo 09 Mar 2009, 01:41
nazha wrote:

Thanks
NaZha


Hey, isn't that a girl's name? I only ask since everybody here on this forum always whines because everyone's a dude. (Not trying to pry ....)
Post 09 Mar 2009, 01:41
View user's profile Send private message Visit poster's website Reply with quote
nazha



Joined: 05 Mar 2009
Posts: 40
Location: Beijing, China
nazha 18 Mar 2009, 05:53
no, nazha(哪吒, baby-faced Nezha) is a heavenly general's name from Chinese classical novel <<Journey to the West>>.
Post 18 Mar 2009, 05:53
View user's profile Send private message Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo 19 Mar 2009, 04:16
Okay, just curious since my great-grandfather's sister was named that (but you probably pronounce it differently, tom_tobias probably knows how, not me!!). Wink
Post 19 Mar 2009, 04:16
View user's profile Send private message Visit poster's website Reply with quote
fatygant



Joined: 12 Sep 2011
Posts: 30
Location: Poznan, Poland
fatygant 04 Feb 2012, 11:53
Tomasz Grysztar wrote:
The macros can do it automatically for you, too, but you need to use the "locals" macro:
Code:
proc proctest
locals 
 var1 TCHAR 'abc',13,10 
endl
invoke MessageBox,HWND_DESKTOP,addr var1,"test",MB_OK
ret
endp    

endp

Tomasz, has something changed since the time you gave nazha the hint above? My FASM complains about the following piece of code with 'illegal instruction' error in blue line declaration (and all following too of course):
Code:
proc CreateDeviceResources

  locals
    blue D2D1_COLOR_F 0.0,0.0,1.0,1.0
    brushproperties D2D1_BRUSH_PROPERTIES 1.0,<1.0,0.0,0.0,1.0,0.0,0.0>
    RenderTargetProperties D2D1_RENDER_TARGET_PROPERTIES \
                                        D2D1_RENDER_TARGET_TYPE_DEFAULT,\
                                        <DXGI_FORMAT_UNKNOWN,D2D1_ALPHA_MODE_UNKNOWN>,\
                                        0.0,0.0,\
                                        D2D1_RENDER_TARGET_USAGE_NONE,\
                                        D2D1_FEATURE_LEVEL_DEFAULT
    HwndRenderTargetProperties D2D1_HWND_RENDER_TARGET_PROPERTIES
  endl

  ... ; proc code goes here

endp    

Even worse: it complains about uninitialized version of above too. The only version which assembles fine is:
Code:
proc CreateDeviceResources

        local blue:D2D1_COLOR_F ;0.0,0.0,1.0,1.0
        local brushproperties:D2D1_BRUSH_PROPERTIES ;1.0,<1.0,0.0,0.0,1.0,0.0,0.0>
        local RenderTargetProperties:D2D1_RENDER_TARGET_PROPERTIES; D2D1_RENDER_TARGET_TYPE_DEFAULT,\
                                                                  ;<DXGI_FORMAT_UNKNOWN,D2D1_ALPHA_MODE_UNKNOWN>,\
                                                                  ; 0.0,0.0,\
                                                                  ; D2D1_RENDER_TARGET_USAGE_NONE,\
                                                                  ; D2D1_FEATURE_LEVEL_DEFAULT
        local HwndRenderTargetProperties:D2D1_HWND_RENDER_TARGET_PROPERTIES

  ; proc code goes here

endp
    

Why? I can initialize the structures in the code - no problem. But why I can't do it within locals?
Post 04 Feb 2012, 11:53
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 04 Feb 2012, 12:11
Hm, don't you all think, the above approach to the constants is highly bloated and what is worse, the bloat is hidden from the programmer behind the macros. Initializing local variables is OK, but initializing local constants is really excessive.
Post 04 Feb 2012, 12:11
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20627
Location: In your JS exploiting you and your system
revolution 04 Feb 2012, 12:12
fatygant: Show how you have defined D2D1_COLOR_F and the other structures. Did you use struct/ends for some other method?
Post 04 Feb 2012, 12:12
View user's profile Send private message Visit poster's website Reply with quote
fatygant



Joined: 12 Sep 2011
Posts: 30
Location: Poznan, Poland
fatygant 04 Feb 2012, 16:31
The structures definition is quite ordinary IMO:
Code:
struct D3DCOLORVALUE
  r dd ?
  g dd ?
  b dd ?
  a dd ?
ends

D2D_COLOR_F  equ D3DCOLORVALUE
D2D1_COLOR_F equ D2D_COLOR_F

struct D2D1_BRUSH_PROPERTIES
  opacity dd ?
  transform D2D_MATRIX_3X2_F
ends

struct D2D_MATRIX_3X2_F
  _11 dd ?
  _12 dd ?
  _21 dd ?
  _22 dd ?
  _31 dd ?
  _32 dd ?
ends

D2D1_MATRIX_3X2_F equ D2D_MATRIX_3X2_F

struct D2D1_RENDER_TARGET_PROPERTIES
  type            dd ?
  pixelFormat D2D1_PIXEL_FORMAT
  dpiX       dd ?
  dpiY          dd ?
  usage       dd ?
  minLevel    dd ?
ends

struct D2D1_HWND_RENDER_TARGET_PROPERTIES
  hwnd                dq ?
  pixelSize     D2D_SIZE_U
  presentOptions dd ?,?
ends

struct D2D_SIZE_U
  width  dd ?
  height dd ?
ends

D2D1_SIZE_U equ D2D_SIZE_U

struct D2D1_PIXEL_FORMAT
  format    dd ?
  alphaMode dd ?
ends    
The code is 64-bit if it matters. And this is my first ever method with local structures.
Post 04 Feb 2012, 16:31
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 04 Feb 2012, 19:28
I don't know if this is the proper fix, but the following modification (besides arranging the structure declarations in such a way they don't have forward references to other structures) works:
Code:
;D2D_COLOR_F  equ D3DCOLORVALUE
D2D_COLOR_F fix D3DCOLORVALUE
;D2D1_COLOR_F equ D2D_COLOR_F
D2D1_COLOR_F fix D3DCOLORVALUE ; Notice I'm not chaining the definitions because fasm will replace only once    
Post 04 Feb 2012, 19:28
View user's profile Send private message Reply with quote
fatygant



Joined: 12 Sep 2011
Posts: 30
Location: Poznan, Poland
fatygant 04 Feb 2012, 20:47
Loco, amazing - it really works now. Thank you!

I am reading about equ and fix directives - but I only understand the inner workings of the first one... fix sounds like Chinese to me.

No offense to Chinese meant.
Post 04 Feb 2012, 20:47
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 04 Feb 2012, 21:22
This may help to understand how equ and fix works:
Code:
TEST2 equ ASSEMBLY
TEST3 fix ASSEMBLY3
TEST4 equ TEST2
TEST5 fix TEST3

; Using macro to enable backtick use
macro tests
{
  display `TEST1, 13, 10
  display `TEST2, 13, 10
  display `TEST3, 13, 10
  display `TEST4, 13, 10
  display `TEST5, 13, 10
}
tests

display '---', 13, 10

match sym, TEST1{display `sym, 13, 10}
match sym, TEST2{display `sym, 13, 10}
match sym, TEST3{display `sym, 13, 10}
match sym, TEST4{display `sym, 13, 10}
match sym, TEST5{display `sym, 13, 10}    
output wrote:
TEST1
TEST2
ASSEMBLY3
TEST4
TEST3
---
TEST1
ASSEMBLY
ASSEMBLY3
ASSEMBLY
TEST3
As you can see, fix is much more straightforward, but is uncapable of referring another fix while equ can (TEST4 and TEST5 respectively).
Post 04 Feb 2012, 21:22
View user's profile Send private message Reply with quote
fatygant



Joined: 12 Sep 2011
Posts: 30
Location: Poznan, Poland
fatygant 05 Feb 2012, 10:03
Loco, perhaps I am intellectualy disabled but I still don't get it (why it worked without struct initialization?). Anyhow, thank you very much for your efforts to make the whole thing more clear to me. And for fixing the bug of course!
Post 05 Feb 2012, 10:03
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 05 Feb 2012, 18:38
Not only the initialization fails, fasm's preprocessor doesn't even recognize D2D1_COLOR_F as an structure in your original code and hence you get the invalid instruction error (because fasm's assembler stage thinks you are using the non-existent "blue" instruction followed by D2D1_COLOR_F operand). With fix, the preprocessor replaces all the symbols that match any of the fixes declared earlier, and THEN it begins normal preprocessing of the line (which this time it will be recognized as a structure invocation because after fixing the preprocessor will see "blue D3DCOLORVALUE ..."). With equ, unless you use match directive first, the preprocessor will see "blue D2D1_COLOR_F ..." and since D2D1_COLOR_F is not an struct, the preprocessor will pass the line to the assembler stage verbatim.

PS: In case I haven't been clear enough above, what I meant is that with the following code you should also get illegal instruction error even though there are no initializations here:
Code:
  locals
    blue D2D1_COLOR_F
    brushproperties D2D1_BRUSH_PROPERTIES
    RenderTargetProperties D2D1_RENDER_TARGET_PROPERTIES
    HwndRenderTargetProperties D2D1_HWND_RENDER_TARGET_PROPERTIES
  endl    
Post 05 Feb 2012, 18:38
View user's profile Send private message Reply with quote
fatygant



Joined: 12 Sep 2011
Posts: 30
Location: Poznan, Poland
fatygant 05 Feb 2012, 19:44
Loco, this is strange... I came back to the errorneus equ version of my struct declaration and I of course still get the error: 'illegal instruction' but assembler claims it can not recognize the instruction... and now surprise: blue D3DCOLORVALUE (see enclosed picture). Seeing this error I had not suspected equ to be my problem (D2D1_COLOR_F was properly recognized as D3DCOLORVALUE). And when I was talking about the version which assembles fine - I meant the one wit local blue:D2D1_COLOR_F instead of locals-endl pair.
I hope you are not totally bored yet with all my doubts and you will take a look at this one more time... Smile


Description: Screenshot of error description for 'equ' version
Filesize: 27.58 KB
Viewed: 18312 Time(s)

Error.png


Description: The application itself.
Download
Filename: TILES.zip
Filesize: 50.78 KB
Downloaded: 705 Time(s)

Post 05 Feb 2012, 19:44
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 05 Feb 2012, 20:31
Sorry, when I've said "verbatim" it is not so literal, before passing the line to the assembler stage, all equates are resolved, so the assembler gets "blue D3DCOLORVALUE" which means nothing to it.

I didn't notice that the local version worked, but that is because local is a macro and the variable(s) are passed as parameters, and since them are processed with match, the equates are resolved.

Search for "macro local" in \INCLUDE\MACRO\PROC32.INC so you can see how match is used there and also "macro locals" where you'll see that it doesn't process your locals declarations one by one but instead it redefines basic data definition directives so it can detect what are you defining between locals and endl.
Post 05 Feb 2012, 20:31
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.