www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 701] New: Inline asm using incorrect offsets when used in inner function

reply d-bugmail puremagic.com writes:
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.


-- 
Dec 20 2006
next sibling parent Sean Kelly <sean f4.ca> writes:
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.
Dec 20 2006
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=701






 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: 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. --
Jan 23 2007
prev sibling next sibling parent d-bugmail puremagic.com writes:
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                    |





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!


-- 
Nov 13 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
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: -------
Nov 26 2010
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=701


Walter Bright <bugzilla digitalmars.com> changed:

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



01:41:00 PST ---

 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: -------
Jan 29 2012