Bug 701 – Inline naked asm uses incorrect offsets

Status
RESOLVED
Resolution
WONTFIX
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2006-12-20T20:11:00Z
Last change time
2014-02-15T13:20:22Z
Keywords
iasm, wrong-code
Assigned to
bugzilla
Creator
sean

Comments

Comment #0 by sean — 2006-12-20T20:11:47Z
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.
Comment #1 by sean — 2006-12-21T00:55:17Z
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.
Comment #2 by thomas-dloop — 2007-01-23T06:05:08Z
> 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.
Comment #3 by clugdbug — 2008-11-13T12:50:49Z
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!
Comment #4 by bugzilla — 2012-01-29T01:41:00Z
(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.