跳轉到內容

Pascal 程式設計/雜項

來自華夏公益教科書,開放書籍,開放世界

最後一個與 Pascal 相關的標準釋出於 1990 年,ISO 標準 7185“[標準] Pascal”,以及 ISO 標準 10206“擴充套件 Pascal”。但是自從IT 沒有停止發展。幾家編譯器製造商繼續透過各種擴充套件來擴充套件 Pascal,我們在這裡介紹其中的一些。

內聯彙編

[編輯 | 編輯原始碼]

自從TP 1.0 版本以來,就可以在 Pascal 原始碼中包含組合語言。這被稱為內聯彙編。雖然正常的 Pascal 程式碼用begin  end 框架包圍,組合語言可以用asm  end 框架包圍。以下是一個可以使用 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.

如果你對資料有特殊的瞭解,而編譯器生成的程式碼效率低下,編寫內聯彙編程式碼很有用。你可以嘗試最佳化速度或大小,以減輕效能瓶頸。

所有,Delphi,FPC 以及 GPC 都支援asm 框架,但它們之間存在一些細微的差異。因此,我們參考編譯器的文件,並且不要忘記這本書是關於Pascal程式設計的。

資源字串

[編輯 | 編輯原始碼]

執行時型別資訊

[編輯 | 編輯原始碼]

託管型別

[編輯 | 編輯原始碼]

執行緒變數

[編輯 | 編輯原始碼]
華夏公益教科書