5

Hi people is there a way i can access a pointer to a structure member directly from in line assembler i tried this

procedure test(eu:PImageDosHeader);assembler;
asm
    push eu._lfanew
end;

It won't compile but if i use this

procedure test(eu:Pointer); 
var   
 xx:TImageDosHeader;
 begin    
 xx:=TImageDosHeader(eu^);  
 asm
     push xx._lfanew
 end;
 end;

It works great.Any idea how can i access a structure trough a pointer in inline asm? is a matter of optimizing the code

1

3 Answers 3

12

Yet another workaround:

procedure test(eu:PImageDosHeader);
asm
    push eu.TImageDosHeader._lfanew
end;
Sign up to request clarification or add additional context in comments.

3 Comments

This is exactly what i needed David's approach is good but this one is great.Thanks mate!
@opcode: I think that @David's approach can be used with your signature as well, so the only difference between Serg's approach and David's (and mine) is purely esthetical. For instance, in my code, you could replace mov eax, TMyStruct(ebx).A by mov eax, ebx.TMyStruct.A and mov eax, TMyStruct(ebx).B by mov eax, ebx.TMyStruct.B.
@AndreasRejbrand the difference is that i don't need to waste a register storing a address is already stored in eu. I am not talking about this piece of code in particular because i know the argument is passed via the register but in some situations a register can count.
4

The following works:

type
  PMyStruct = ^TMyStruct;
  TMyStruct = record
    A, B: cardinal;
  end;

procedure ShowCard(Card: cardinal);
begin
  ShowMessage(IntToHex(Card, 8));
end;

procedure test(Struct: PMyStruct);
asm
  push ebx                      // We must not alter ebx
  mov ebx, eax                  // eax is Struct; save in ebx
  mov eax, TMyStruct(ebx).A      
  call ShowCard
  mov eax, TMyStruct(ebx).B
  call ShowCard
  pop ebx                        // Restore ebx
end;

procedure TForm6.FormCreate(Sender: TObject);
var
  MyStruct: TMyStruct;
begin
  MyStruct.A := $22222222;
  MyStruct.B := $44444444;
  test(@MyStruct);
end;

2 Comments

I often get this problem there are many people here who downvote just becuse they don't like your nickname
Didn't downvote, but...: A: you don't need to save EAX, EDX and ECX, so you can change test to: `asm mov ecx, eax mov eax,TMyStruct(ecx).A call ShowCard mov eax,TMyStruct(ecx).B call ShowCard
2

I would write it like this:

procedure test(const eu: TImageDosHeader);
asm
    push TImageDosHeader([EAX])._lfanew
end;

The pertinent documentation is here.

1 Comment

Yeah, this link is vital for the subject. Last paragraph of symbols section contains four syntactic options for STRUCs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.