Il 17/08/2018 15:46, Marco Borsari via fpc-pascal ha scritto:

I found that someone made that for me at link
http://www.mindfruit.co.uk/2012/01/switch-blocks-jump-tables.html
and I have tried to rewrite the program in at&t syntax

program branch2;
{$ASMMODE att}
label next,stop,a,b,c;
var idx:byte;
begin
write('Index? ');
readln(idx);
asm
movb idx,%al
jmp *next(,%eax,4)
next:
jmp a;
jmp b;
jmp c;
end['EAX'];
a:
writeln('0');
goto stop;
b:
writeln('1');
goto stop;
c:
writeln('2');
stop:
writeln('stop');
end.

but this fails compilation at line 10 with messages
Unsupported symbol type for operand
Unknown identifier 'NEXT'
Well, I think I can live without it, it is only for
the sake of curiosity,
Marco

I modified your code, to add a jump table (as it is in the example you mention) and here it works just fine. The code which works is:
program branch2;
{$ASMMODE att}
label stop,a,b,c;
var idx:byte;
    Jtab: array [0..2] of pointer;
begin
// Fill up the Jump table with the addresses of jump targets
Jtab[0] := @a;
Jtab[1] := @b;
Jtab[2] := @c;

write('Index? ');
readln(idx);
asm
mov $0x0,%eax // clear upper bits of eax
movb idx,%al  // so that it contains just idx
jmpq *Jtab(,%eax,8) // 8 is for x86_64; replace with 4 for i386
end['EAX'];
a:
writeln('0');
goto stop;
b:
writeln('1');
goto stop;
c:
writeln('2');
stop:
writeln('stop');
end. 
Enjoy programming!

Giuliano
-- 
Do not do to others as you would have them do to you.They might have different tastes.
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to