Home > Blockchain >  Translate Delphi asm to C Builder asm
Translate Delphi asm to C Builder asm

Time:06-15

I am trying to convert part of a Delphi project to a C Builder project.

I reduced the problem to the following code:

procedure Test;
asm
    MOV     EAX, OFFSET @test
@test:
    db 00H
end;

If I try to compile this, and several variations, I get an error:

[BCC32 Fehler] Unit1.cpp(21): E2451 Undefiniertes Symbol '@test'

Can anybody help?

CodePudding user response:

Why are you trying to translate assembler to assembler at all? The code you showed is defining a single byte in memory and then returning its memory address to the caller. That can and should be written in native C instead, let the compiler handle creating the necessary assembler for you, ie:

const char* Test() {
    static const char ch = 0;
    return &ch;
}

CodePudding user response:

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:

  1. 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...

  2. 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.

  3. 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...

  • Related