Pascal Programming/Miscellaneous

The last Pascal-related standards were published in 1990, ISO standard 7185 “[Standard] Pascal”, and ISO standard 10206 “Extended Pascal”. But ever since IT did not stop evolving. Several compiler manufacturers continued extending Pascal by miscellaneous extensions, some of which we are presenting here.

Inline assembly

edit

Since TP version 1.0 there exists the possibility to include assembly language inside your Pascal source code. This is called inline assembly. While normal Pascal is surrounded by a begin  end frame, assembly language can be framed by asm  end. Here is an example that can be compiled with the FPC:

program asmDemo(input, output, stdErr);
{$ifNDef CPUx86_64}
	{$fail only for x86_64}
{$endIf}
var
	foo: int64;
begin
	write('Enter an integer: ');
	readLn(foo);
	// This directive will tell FPC
	// a certain assembly language style is used
	// within the asm...end frame.
	{$asmMode intel}
	asm
		mov rax, [foo]        // rax ≔ foo^
		// ensure foo is positive
		test rax, rax         // x ≟ 0
		jns @is_positive      // if ¬SF then goto is_positive
		neg rax               // rax ≔ −rax
	@is_positive:
		// NOTE: Here we assume the popcnt instruction
		//       was supported by the processor,
		//       but this is bad style.                
		//       You ought to use the cpuid instruction
		//       (if available) in order to determine
		//       whether popcnt is available.
		popcnt rax, rax       // rax ≔ popCnt(rax)
		mov [foo], rax        // foo ≔ rax
	// An array of strings after the asm-block closing ‘end’
	// tells the compiler which registers have changed
	// (you do not want to mess with the compiler’s understanding
	// which registers mean what)
	end ['rax'];
	writeLn('Your number has a binary digital sum of ', foo, '.');
end.

Writing inline assembly code is useful if you have special knowledge about data and the compiler generates inefficient code. You can try to optimize for speed or size in order mitigate performance bottlenecks.

All, Delphi, the FPC as well as the GPC support asm frames, but each with a few subtle differences. We therefore refer to the compiler’s manuals, and not forgetting this book is about programming in Pascal.

Resource strings

edit

Run-time type information

edit

Managed types

edit

Thread variables

edit