Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2008-11

Intel x86 inline assembler problem

3 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

leledumbo
procedure DumpRegs;
var
  Reg: array [0..7] of LongWord;
  SReg: array [0..5] of Word;
  EIP: LongWord;
  EFLAGS : LongWord;
begin
  asm
    // save registers while they are not modified by another procedure call.
note that depending
    // on your compiler settings, ebp may already be trashed (stack frame)
    mov dword ptr Reg[4*0],eax
    mov dword ptr Reg[4*1],ecx
    mov dword ptr Reg[4*2],edx
    mov dword ptr Reg[4*3],ebx
    mov dword ptr Reg[4*4],esp // esp is already incorrect since it was
decreased by the amount of stack space the local variables require
    mov eax, 16*4+6*2+4+4
    add dword ptr Reg[4*4],eax // correct esp
    mov dword ptr Reg[4*5],ebp
    mov dword ptr Reg[4*6],esi
    mov dword ptr Reg[4*7],edi
    // save segment registers
    mov word ptr SReg[2*0],ds
    mov word ptr SReg[2*1],es
    mov word ptr SReg[2*2],cs
    mov word ptr SReg[2*3],ss
    mov word ptr SReg[2*4],fs
    mov word ptr SReg[2*5],gs
    // save EFLAGS
    pushfd
    pop dword ptr EFLAGS
    // now get eip.
    //call $+3          -- $ is considered Hex instead of current line
address
    //pop dword ptr EIP -- invalid operand (EIP)
  end;
  WriteStrLn('EAX    = '+HexStr(Reg[0],8));
  WriteStrLn('ECX    = '+HexStr(Reg[1],8));
  WriteStrLn('EDX    = '+HexStr(Reg[2],8));
  WriteStrLn('EBX    = '+HexStr(Reg[3],8));
  WriteStrLn('ESP    = '+HexStr(Reg[4],8));
  WriteStrLn('EBP    = '+HexStr(Reg[5],8));
  WriteStrLn('ESI    = '+HexStr(Reg[6],8));
  WriteStrLn('EDI    = '+HexStr(Reg[7],8));
  WriteStrLn('DS     = '+HexStr(SReg[0],8));
  WriteStrLn('ES     = '+HexStr(SReg[1],8));
  WriteStrLn('CS     = '+HexStr(SReg[2],8));
  WriteStrLn('SS     = '+HexStr(SReg[3],8));
  WriteStrLn('FS     = '+HexStr(SReg[4],8));
  WriteStrLn('GS     = '+HexStr(SReg[5],8));
  WriteStrLn('EFLAGS = '+HexStr(EFLAGS,8));
  WriteStrLn('EIP    = '+HexStr(EIP,8));
end;

Questions:

What's Free Pascal's substitution for $ (used as Hex in FPC, current line address in normal assembler)?
EIP is recognized by the assembler? How come? AFAIK, it's not visible to the programmer. Thus, the above EIP should refer to EIP in the local variables section.

View this message in context: http://www.nabble.com/Intel-x86-inline-assembler-problem-tp20572969p20572969.html Sent from the Free Pascal - General mailing list .
Jonas Maebe
to leledumbo
leledumbo wrote
What's Free Pascal's substitution for $ (used as Hex in FPC, current line address in normal assembler)?

I don't think there is any. Use a label instead. Or do you want to jump into the middle of an instruction?

Show quoted text 5 lines
EIP is recognized by the assembler? How come? AFAIK, it's not visible to the programmer. Thus, the above EIP should refer to EIP in the local variables section.

While it's not user-visible, it's still the name of a cpu register. And given that its 64 bit cousin (RIP) is exposed, it might also be exposed in 32 bit mode in the future (although this is unlikely).

Jonas

leledumbo
to Jonas Maebe
Jonas Maebe-2 wrote
I don't think there is any. Use a label instead. Or do you want to jump into the middle of an instruction?

OK, changed to:

procedure DumpRegs;
label

  GetEIP;

...
call GetEIP
GetEIP:
pop dword ptr EIP
...

It runs OK, but I'm not sure whether this is the correct value or not. I'm trying to get the current EIP value. Do you have any idea?

Jonas Maebe-2 wrote 5 lines
While it's not user-visible, it's still the name of a cpu register. And given that its 64 bit cousin (RIP) is exposed, it might also be exposed in 32 bit mode in the future (although this is unlikely).

OK, so from the point of view FPC inline assembler, EIP is visible and has higher precedence than local variable with the same name. I'll use a different name then.

View this message in context: http://www.nabble.com/Intel-x86-inline-assembler-problem-tp20572969p20594054.html Sent from the Free Pascal - General mailing list .