4

I'm trying to get address of a label - here is some sample code:

int main() {
    asm {
        mov ax,1
        mov bx,ax
    }
  _labelname:
    asm {
        mov ax, OFFSET _labelname
    }

    return 0;
}

Compilation of this code returns this error: "Undefined symbol _labelname" If I define the label in asm block, I can't even use jmp _labelname

I found this and doesn't work, for this, no way actually. This says just jumping, not addressing. And this doesn't help at all. Any suggestions?

Community
  • 1
  • 1
Mikael
  • 127
  • 2
  • 10

2 Answers2

0

There's usually a route in any language, but you need to faff about for a day or two because these things aren't always documented

Declare a global memory op space in your flavour of HLL. DIM LABELNAME1(0)

Then search for the asm syntax which puts the address into eax

mov eax, ^LABELNAME(0)
mov eax, dword [_lablename]
mov eax, ^_lablename
etc etc etc

then pop it in asm

You won't find pop [^ anywhere on google, but it's one which works in certain HLLs

push eax
pop [^LABELNAME1(0)]

Now your HLL and asm can chat to each other whenever you like

So it's well worth figuring out


Undefined symbol _labelname

Probbly needs to be declared at the very start of the program

._labelname
mov dword [_lablename], 0

and used later by asm as a label

As I say, you'll have to mess about and suss it out for your particular flavour of HLL, and globals seem to work best

You'll also need to figure out how to declare separate memory zones for storing asm dynamic variables and running the opcodes or you'll get cache overwrites which will cripple the speed advantages of asm

A small routine I wrote without separating these asm areas took 20 hours to run. With separation it took 1 hour


mov ax, OFFSET _labelname

This is 16 bit stuff, (DOS etc, with goofy memory rules) aren't you doing 32 bit stuff with your HLL???

Unless it's all happening in one segment you will need a double memory operand to find _labelname, dx:ax etc, and as mentioned previously, you're 20 years too late

jmp cs:_labelname

Works in the same segment but for a bigger program the cs part will need to be a specific segment override and a far jump/return

Additionally, if your dynamic asm variables are plonked into your asm code segment then a cardinal rule for maximising asm speed has been broken

phuclv
  • 37,963
  • 15
  • 156
  • 475
ady
  • 155
  • 1
  • 8
0

I've found a way, but you can't use a C label, it has to be an asm label:

int main(void)
{
    asm {
        mov ax,1
        mov bx,ax
    }

    asm { _labelname: }

    asm {
        mov ax, OFFSET _labelname
        jmp cs:_labelname
    }

    return 0;
}
Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
  • Why down voting? The OP wanted it, the OP got it. – Alexey Frunze Dec 04 '11 at 15:24
  • You are downvoted probably because it doesn't work with Turbo C 3.0. I just tried it. – Emir Akaydın Dec 04 '11 at 15:27
  • It did work with Turbo C++ 1.01 + TASM. The compiler switched to compilation via TASM. 3.0 should also have the -B option to compile via assembler, something like `tcc.exe -v -y -B lab.c` (-v and -y are for debugging info so one can inspect the resultant .EXE in TD.EXE easily). – Alexey Frunze Dec 04 '11 at 15:31