flat assembler
Message board for the users of flat assembler.

Index > Main > 2 questions on sections and stack frames.

Author
Thread Post new topic Reply to topic
zubi



Joined: 27 Apr 2006
Posts: 25
Location: Turkey
zubi 27 Apr 2006, 05:15
Hi,

I have recently started using FASM but I have some experience with MASM. As I am used to switch between sections, I tried the same thing in FASM; the code compiled but crashed when run. To my surprise the disassambly showed me that there are extra instructions that I didn't write, causing the problem. Here is the code I wrote and the disassambed form:

(The script is a part of a longer code, which is of MS COFF format. .text and .data already defined before and there is a macro to make Qword equ qword):

Code:
proc MAIN stdcall
local Vl_D:Qword

section '.data'
Label_1 dq 2.5

section '.text' code
FLD Qword [Label_1]
FSTP Qword [Vl_D]

section '.data'
Label_2 dq 15.0

section '.text' code
FLD Qword [Label_2]
FSUB Qword [Vl_D]
FSTP Qword [Vl_D]

ret
endp
    


The following is the result I got from a dumping utility:

Code:
00401000                                 fn_00401000:
00401000 55                             push    ebp
00401001 89E5                           mov     ebp,esp
00401003 83EC08                         sub     esp,8
00401006 CC                             int     3
00401007 CC                             int     3
00401008 DD0520304000           fld     qword ptr [403020h]
0040100E DD5DF8                         fstp    qword ptr [ebp-8]
00401011 CC                             int     3
00401012 CC                             int     3
00401013 CC                             int     3
00401014 DD0528304000           fld     qword ptr [403028h]
0040101A DC65F8                         fsub    qword ptr [ebp-8]
0040101D DD5DF8                         fstp    qword ptr [ebp-8]
00401020 C9                             leave
00401021 C3                             ret
    


So, evertyhing is what I expected to see, except those nasty int 3s. I would like to know why those interrupts appear even if I didn't write them. Can't I simply switch between code and data sections as I do in MASM?

The other question is about stack frames. I would like to have my subrutines to have stack frames even when they don't have parameters or local variables to be able to write code that does the stack trace. I know it has something to do with windows macros that ship with FASM package but I found the preprocessor code too complicated. There was that <Forceframe> directive in MASM that would save you type prologues and epilogues for such subroutines. How could I achieve the same result in FASM?

Thanks in advance.
Post 27 Apr 2006, 05:15
View user's profile Send private message MSN Messenger Reply with quote
zubi



Joined: 27 Apr 2006
Posts: 25
Location: Turkey
zubi 29 Apr 2006, 12:39
Was that an inappropriate post?
Post 29 Apr 2006, 12:39
View user's profile Send private message MSN Messenger Reply with quote
RedGhost



Joined: 18 May 2005
Posts: 443
Location: BC, Canada
RedGhost 30 Apr 2006, 06:56
why would you have a .data section inside of a procedure? sub esp, #_of_needed_bytes, reference ebp-that_many_bytes for local data
Post 30 Apr 2006, 06:56
View user's profile Send private message AIM Address MSN Messenger Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 30 Apr 2006, 07:44
When you switch between .data and .code in MASM, you go to "where you left off" in that section. In FASM, you introduce a new section every time you do it, check the output .obj file. Each section will be aligned, which is why you get those int3 instructions.

Perhaps somebody have made a macro to make "section-switching" more natural in FASM...
Post 30 Apr 2006, 07:44
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8351
Location: Kraków, Poland
Tomasz Grysztar 30 Apr 2006, 07:53
Try giving those sections "align 1" attribute - perhaps that should get rid of those int3 alignments.
Post 30 Apr 2006, 07:53
View user's profile Send private message Visit poster's website Reply with quote
zubi



Joined: 27 Apr 2006
Posts: 25
Location: Turkey
zubi 01 May 2006, 08:12
First of all, thanks for the responses.

Giving the sections align 1 attribute actually solved it, no more int3s. Moreover I discovered that as long as I use the same name for the data sections, the rusulting exe contains only one data section per name. It seems that fasm doesn't create a new section in this case, is that correct? Addresses of those floating point variables do follow each other without a gap, suggesting that this assumption is correct. (When I give different names to different data sections the result is big gap between them because of the section allignment). So, if this observation and results are correct, can we say that we can actually do switch between sections by giving same names to the sections and aligning them by 1?

Quote:

sub esp, #_of_needed_bytes, reference ebp-that_many_bytes for local data


Yes, but this is exactly what I am trying to achieve, Vl_D is already defined as local variable, question is how can I assign arbitrary floating point literals to it. If it was needed only once I could have written it like local Vl_D dq 2.5 anyways. The problem stems from the fact that there is no opcode that operates on qword floating point literals.
Post 01 May 2006, 08:12
View user's profile Send private message MSN Messenger Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 01 May 2006, 11:08
Quote:

Moreover I discovered that as long as I use the same name for the data sections, the rusulting exe contains only one data section per name. It seems that fasm doesn't create a new section in this case, is that correct?


Since you output to MS COFF format, I assume you're going through a linker. All linkers I know of will merge sections with identical names (and give warnings if multiple sections with same name but different attributes are found).

*theoretically* a linker could choose to "mix" sections from different input files, but I don't know any that do this... so it should be safe doing the align-by-1 trick.

Tomasz, is there a way (by the power of the might macro system Very Happy ) to "magically switch" between sections and only output one of each? I assume it could be done with the virtual statement or similar?
Post 01 May 2006, 11:08
View user's profile Send private message Visit poster's website Reply with quote
zubi



Joined: 27 Apr 2006
Posts: 25
Location: Turkey
zubi 01 May 2006, 11:39
Right. I got those warnings when I was experimenting on the issue. The linker I use have an option (/merge) that merges any 2 sections you name (as parameters to the option). It merges differently named sections only when you use that option. I tried that actually and, if I didn't do something wrong, I couldn't get rid of the int3s. So I assumed that fasm is writing that alignment information while creating the obj.
Post 01 May 2006, 11:39
View user's profile Send private message MSN Messenger Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 01 May 2006, 12:37
In a COFF file, each section object has an alignment value, which fasm sets, and fortunately all linkers (that I've tried) respect Smile
Post 01 May 2006, 12:37
View user's profile Send private message Visit poster's website 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.