Incorrect x86 variable offsets?

Coordinator
Feb 15, 2009 at 7:06 PM

The compiler does not generate the correct x86 binary for the following method:

 public unsafe static void WriteChar(byte c, byte color, byte* address)
{
    byte* address2 = address + 1;
    *address = c;
    *address2 = color;    // this is the problem statement
}

The IL generated for this method is:

.method public hidebysig static void  WriteChar(uint8 c, uint8 color, uint8* address) cil managed
{
  // Code size       13 (0xd)
  .maxstack  2
  .locals init ([0] uint8* address2)
  IL_0000:  nop
  IL_0001:  ldarg.2
  IL_0002:  ldc.i4.1
  IL_0003:  conv.i
  IL_0004:  add

  IL_0005:  stloc.0
  IL_0006:  ldarg.2
  IL_0007:  ldarg.0
  IL_0008:  stind.i1

  IL_0009:  ldloc.0
  IL_000a:  ldarg.1
  IL_000b:  stind.i1

  IL_000c:  ret
} // end of method Boot::WriteChar

And finally here's the disassemble code :

.text:00402EC1                 mov     [ebp+var_14], 1
.text:00402ECB                 mov     eax, [ebp+arg_8]
.text:00402ED1                 add     eax, [ebp+var_14]
.text:00402ED7                 mov     [ebp+var_10], eax

.text:00402EDD                 mov     eax, [ebp+arg_8]
.text:00402EE3                 mov     dl, [ebp+arg_0]
.text:00402EE9                 mov     [eax+0], dl

.text:00402EEF                 mov     eax, [ebp+var_C]   ; should be mov eax, [ebp+var_10] ???
.text:00402EF5                 mov     dl, [ebp+arg_4]
.text:00402EFB                 mov     [eax+0], dl

The offset should be var_10 (and not var_C) in my opinion. 

Any suggestions on how to fix this?
 

Coordinator
Feb 15, 2009 at 7:52 PM
I agree, it should have been var_10 and not var_C. I'll add a test for this so we can fix it.
Coordinator
Feb 15, 2009 at 8:29 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.