Your code has some problems like You can not forward reference labels in C++ asm
with one exception (jmp
instruction)...
The inbuild help and online one too:
suggest that the syntax for labels is the same as in Pascal ... that is true however usage of labels is very different !!! up to a point they are almost useless I still did not get it to work even after 15+ years they changed it as you can use labels only for jumps and maybe calls but nothing else...
Perhaps there is some directive or keyword that allows label usage but have not found any yet as all examples are in pascal syntax which does not work in C++ environment.
Another problem is that you use test
label ... You are forgetting that test
is assembly instruction so the names are in conflict!!!
You can still workaround:
reorder your code so you do not need forward referencing
This is doable as jmp
is still working as should with labels however you can forget about simple selfmodifying code...
convert your local asm variables to C++ local variables
local C++ variables are directly accessible from asm
. Beware function operands are not so if you need them copy them into local variables first.
convert addressing syntax to C++
You know this:
MOV EAX, OFFSET @test
Is not doing what you intend in C++ you have to use LEA
instruction instead of OFFSET
... just create a breakpoint and see what the OFFSET
will return (in my case its always 0xFFFFFFFF
instead of real address) while LEA
obtains correct address.
Putting all together You can try something like this instead:
void asmtest()
{
BYTE a=0x00;
asm {
lea eax,a
}
}
In case you have a really nasty code you can also convert the code into fully assembler code (no C++) and compile as asm code instead ... Then compiled/linked obj file can be linked into your C++ project. IIRC in BCB5 was an option for this however haven't use that for many years so this feature might be removed however you can still compile with any assembler (TASM,NASM...) borland was always compatible with TASM (as it was used internaly) so I recommend to use that to avoid additional problems...
00
is the opcode for a memory-destination add, but it takes a ModRM byte. So the full instruction following themov eax, imm32
will depend on whatever's in memory after the00
byte. If it's another0
of padding, it'll beadd [eax], al
. Either way, execution will then fall into whatever's next. Unless Delphi implicitly puts@label
labels in a different section and implicitly adds aret
instruction or expects execution to fall through an inline(?) asm block?@label
labels in a different section" - it does not. "and implicitly adds aret
instruction" - it does, in this case.C3 ret
before thedb 0
? If not, the machine code becomes00 C3 add bl,al
. From the doc you linked, that's what I'd expect. With no params or locals, it would be theret
that gets skipped / consumed, rather than part of amov esp,ebp
orpop ebp
, although either of those would also be fatal.ret
before thedb 0
would make a huge difference to this, although it's still weird to want the address of constant data directly following the machine code. But yeah maybe they just wanted to repro the error message, not show code that makes any sense.