flat assembler
Message board for the users of flat assembler.

Index > Windows > My fasm hello world; how to do sectionless variables?

Author
Thread Post new topic Reply to topic
mindcooler



Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler
This is my FASM hello world, copies an entire volume to an image file.

Code:
include 'win32wx.inc' ; you can simply switch between win32ax, win32wx, win64ax and win64wx here

.data
        pbuffer dd 0
        readcount dd 0
        writecount dd 0
.code

_buffer = 65536

  start:
        invoke  CreateFileW,'\\.\G:',GENERIC_READ,FILE_SHARE_READ or FILE_SHARE_WRITE,0,\
                OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0
        mov esi,eax

        invoke  CreateFileW,'\\.\C:\dump.img',GENERIC_WRITE,FILE_SHARE_READ or FILE_SHARE_WRITE,0,\
                CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0
        mov edi,eax

        invoke  VirtualAlloc,0,_buffer,$3000,4
        mov [pbuffer],eax
  @:
        invoke ReadFile,esi,[pbuffer],_buffer,readcount,0
        invoke WriteFile,edi,[pbuffer],[readcount],writecount,0

        cmp [readcount],$10000
        je @

        invoke  ExitProcess,0
.end start    


How do I get rid of the .data and .idata sections from my .exe? I would like to have a variables section for address calculation, but I do not want it taking up space in my exe when I just as well can allocate it myself.

Can this be done with zero-initialized variables? How about initialized ones? And code-initialized ones?

_________________
This is a block of text that can be added to posts you make.
Post 06 Feb 2010, 23:36
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 619
cod3b453
Change
Code:
        pbuffer dd 0 
        readcount dd 0 
        writecount dd 0    

to
Code:
        pbuffer rd 1 
        readcount rd 1 
        writecount rd 1    

these will then be allocated at run time (zeroed)


Last edited by cod3b453 on 07 Feb 2010, 00:29; edited 1 time in total
Post 07 Feb 2010, 00:10
View user's profile Send private message Reply with quote
mindcooler



Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler
Excellent, thank you.

If I need initialized variables, how can I avoid allocating an entire section for them? Is initalization by code the way to go?
Post 07 Feb 2010, 00:16
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
cod3b453, actually you should use "1" instead of "0".

I recommend to also change pbuffer to "buffer rb _buffer" (no longer a pointer) and not use VirtualAlloc anymore.
Post 07 Feb 2010, 00:26
View user's profile Send private message Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 619
cod3b453
Sorry yes I forgot to change the values, thanks LocoDelAssembly Cool
Post 07 Feb 2010, 00:28
View user's profile Send private message Reply with quote
windwakr



Joined: 30 Jun 2004
Posts: 827
Location: Michigan, USA
windwakr
mindcooler wrote:
Excellent, thank you.

If I need initialized variables, how can I avoid allocating an entire section for them? Is initalization by code the way to go?


I think FASM includes an example that has data and code in the same section, I think it's the "Beer" one.

_________________
----> * <---- My star, won HERE
Post 07 Feb 2010, 00:33
View user's profile Send private message Reply with quote
mindcooler



Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler
LocoDelAssembly wrote:
cod3b453, actually you should use "1" instead of "0".

I recommend to also change pbuffer to "buffer rb _buffer" (no longer a pointer) and not use VirtualAlloc anymore.


Ah, letting the loader do the job for me!

_________________
This is a block of text that can be added to posts you make.
Post 07 Feb 2010, 00:54
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
mindcooler



Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler
windwakr wrote:

I think FASM includes an example that has data and code in the same section, I think it's the "Beer" one.


Yes, it keeps constant data at the end of the code segment, but it fails for variable data.

Thinking allocating zeroed space and copying constants to it might be a good idea.

_________________
This is a block of text that can be added to posts you make.
Post 07 Feb 2010, 00:56
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number 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. Also on YouTube, Twitter.

Website powered by rwasa.