[fpc-pascal] Branch table

Giuliano Colla giuliano.colla at fastwebnet.it
Thu Aug 16 17:40:32 CEST 2018


Il 14/08/2018 15:21, Marco Borsari via fpc-pascal ha scritto:

> Why the code below does exit gracefully without prints anything?
> Sure, it is for my poor knowledge of the assembler, but in some 
> details, please...
>
> program branch;
> {$ASMMODE intel}
> label next,stop,a,b,c;
> var idx:byte;
> begin
> write('Index? ');
> readln(idx);
> asm
> mov ax,idx;
> shl ax,2;
> mov bx,next;
> add bx,ax;
> jmp (*short*) [ebx+4];
> next:
> jmp a;
> jmp b;
> jmp c;
> end['EAX','EBX'];
> a:
> writeln('0');
> goto stop;
> b:
> writeln('1');
> goto stop;
> c:
> writeln('2');
> stop:
> writeln('stop');
> end.
>
> Just another question: why the short modifier is unrecognized?
> Thanks for any help in this holydays time,
> Marco

I tested your code in my environment (Linux - x86_64), compiled with fpc 
-g -a branch.pas. (-g is to add debug information, -a to get the 
assembler listing).
Then launched it with gdb debugger:
 >gdb branch
(gdb)run

The output is the following:

(gdb) run
Starting program: /home/colla/Applicazioni/Lazarus/Branch/branch
Index? 1

Program received signal SIGSEGV, Segmentation fault.
main () at branch.pas:13
13      jmp (*short*) [ebx+4];

The first thing I notice is that you load BX, which is a 16 bit register 
(the lower 16 bits of EBX), with the value of "next", and then Jump to 
the content of EBX which is a 32 bit register, whose lower 16 bits have 
been loaded but whose upper 16 bits are undefined. A SIGSEV is therefore 
to be expected. If you are in a 32 Bit environment you should use EBX, 
if you're in a 64 Bit environment you should use RBX.

What I would do, if I were in your place, would be to rewrite your 
program in pure Pascal, compile it with the -a switch to get the 
assembler listing, and use the compiler generated assembler code as a 
guideline to your optimized assembler.

Giuliano

-- 
Do not do to others as you would have them do to you.They might have different tastes.




More information about the fpc-pascal mailing list