Thread overview
[Issue 701] New: Inline asm using incorrect offsets when used in inner function
Dec 21, 2006
d-bugmail
Dec 21, 2006
Sean Kelly
Jan 23, 2007
d-bugmail
[Issue 701] Inline naked asm uses incorrect offsets
Nov 13, 2008
d-bugmail
Jan 29, 2012
Walter Bright
December 21, 2006
http://d.puremagic.com/issues/show_bug.cgi?id=701

           Summary: Inline asm using incorrect offsets when used in inner
                    function
           Product: D
           Version: 0.177
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: sean@f4.ca


I expect the following code:

    void main()
    {
        int i = 0;

        void fn()
        {
            asm
            {
                naked;
                lea EAX, i;
                mov [EAX], 42;
                ret;
            }
        }
        fn();
        printf( "i = %d\n", i );
    }

to print "42" but instead it prints "0".  This is because the assembler uses the offset of 'i' that would be used within main() rather than adjusting for the inner function.  Changing the code to this:

    void main()
    {
        int i = 0;

        void fn()
        {
            asm
            {
                naked;
                lea EAX, i;
                add EAX, 4;
                mov [EAX], 42;
                ret;
            }
        }
        fn();
        printf( "i = %d\n", i );
    }

Prints "42" as desired, but a manual adjustment of offsets should not be necessary.  This is particulrly problematic in situations where "naked" is not used, so the amount to adjust the offset by is not fixed.


-- 

December 21, 2006
Upon reflection, I'm not entirely sure what the correct behavior should be here.  However, I think it's misleading that the code currently complies and silently produces the incorrect result.  If nothing else, it would be nice if this worked with 'naked' not present.
January 23, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=701





------- Comment #2 from thomas-dloop@kuehne.cn  2007-01-23 06:05 -------
> mov [EAX], 42;

This should be

> mov int ptr [EAX], 42;

I don't think there is a way to use a single "lea" to solve your problem, however lea seems to be broken:

# asm{
#    lea EAX, [EBP-24] + 1;
#    lea EBX, 1 - [EBP-24];
# }

results in

> 8d 45 e9 lea eax, [ebp-23]
> 8d 5d 19 lea ebx, [ebp+25]

I'm not a master of all x86 addressing modes but it seems odd.


-- 

November 13, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=701


clugdbug@yahoo.com.au changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|Inline asm using incorrect  |Inline naked asm uses
                   |offsets when used in inner  |incorrect offsets
                   |function                    |




------- Comment #3 from clugdbug@yahoo.com.au  2008-11-13 12:50 -------
I'm changing the name of this issue, since it actually has nothing to do with inner functions. It applies to _any_ use of 'naked'. Basically naked calculates offsets assuming that a stack frame is present -- even though the main use of naked is to avoid having a stack frame!


-- 

November 26, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=701


Andrei Alexandrescu <andrei@metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
                 CC|                            |andrei@metalanguage.com
         AssignedTo|nobody@puremagic.com        |bugzilla@digitalmars.com


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 29, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=701


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |WONTFIX


--- Comment #4 from Walter Bright <bugzilla@digitalmars.com> 2012-01-29 01:41:00 PST ---
(In reply to comment #3)
> I'm changing the name of this issue, since it actually has nothing to do with inner functions. It applies to _any_ use of 'naked'. Basically naked calculates offsets assuming that a stack frame is present -- even though the main use of naked is to avoid having a stack frame!

Naked assumes you set up your own stack frame, not that you don't have one. I don't think there's any magic answer to this.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------