0

How do I call an Exception with a resource string message using inline assembler?

I tried various things, but nothing seems to work. I get things like inline assembler syntax error or operant sizes does not match.

resourcestring
  sMessage = 'Test 123';

procedure foo;
asm
  mov eax, @sMessage
  push eax
  call Exception.CreateRes
end;
Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
Daniel Marschall
  • 3,739
  • 2
  • 28
  • 67
  • Are you building a 64-bit executable? You can't push a 32 bit register in 64-bit mode. Use `lea rax, [sMessage]` / `push rax` to make position-independent code, or `push @sMessage` if the absolute address fits in a 32-bit immediate operand for `push` (which will be sign-extended to 64-bit). You might need to look up the right syntax, I don't know Delphi syntax, but I do know how x86 machine code works. – Peter Cordes Jun 29 '18 at 20:48
  • Delphi uses its own *register* calling convention. To see how this works, just raise an exception using CreateRes and see what the compiler produces. Set a breakpoint n the line and open the CPU window when it is reached. That will show you how to raise an exception. What you are doing is not nearly what is required. I generally don't do this in assembler at all. I just write a simple Pascal function that raises the exception and call it from my assembly code. Much easier, and the few extra cycles it takes to call a function is irrelevant. Exceptions take time anyway. – Rudy Velthuis Jun 29 '18 at 21:12
  • Sorry, I didn't mention that. I am trying to write a x86 application – Daniel Marschall Jun 29 '18 at 21:14
  • FWIW, a resourcestring can be treated like a normal string, so you simple do, **in Pascal**: `raise Exception.Create(sMessage);`. Then look what code is produced in the CPU view. – Rudy Velthuis Jun 29 '18 at 21:15
  • @Rudy yes, I already did that. The asm code that the compiler created contained the absolute address in the memory. So the compiler has resolved the address of the resource string before the compilation. – Daniel Marschall Jun 29 '18 at 21:16
  • In assembler, you can use the name, i.e. `sMessage`, no need for an absolute address. Delphi's built-in assembler (BASM) knows how to handle this. The debugger's CPU view will display the address, but your code can use the name.But I'd still raise the exception in a Pascal procedure and call the function from my BASM code. There is no need to do this in assembler. – Rudy Velthuis Jun 29 '18 at 21:18
  • both, "push sMessage" and "mov eax, sMessage" cause the error "inline assembler syntax error". – Daniel Marschall Jun 29 '18 at 21:21
  • I tried all kinds of syntax, like `offset sMessage`, etc. but couldn't get rid of the error. You'll have to do this in Pascal, as I recommended already. – Rudy Velthuis Jun 29 '18 at 22:32

0 Answers0