Difference between revisions of "Label"

From Free Pascal wiki
Jump to navigationJump to search
(changed example with Asm)
(more colorful syntax highlighting)
Line 1: Line 1:
 
{{label}}
 
{{label}}
  
The <code>label</code> keyword is used for declaration of labels (markers for unconditional jumps using [[Goto|<code>goto</code>]] keyword) used further in the unit/program.
+
The <syntaxhighlight lang="pascal" enclose="none">label</syntaxhighlight> keyword is used for declaration of labels (markers for unconditional jumps using [[Goto|<syntaxhighlight lang="pascal" enclose="none">goto</syntaxhighlight>]] keyword) used further in the [[Unit|<syntaxhighlight lang="pascal" enclose="none">unit</syntaxhighlight>]]/[[Program|<syntaxhighlight lang="pascal" enclose="none">program</syntaxhighlight>]].
  
A <code>label</code> section is also required for jump targets in [[Asm|<code>asm</code>-blocks]].
+
A <syntaxhighlight lang="pascal" enclose="none">label</syntaxhighlight> section is also required for jump targets in [[Asm|<syntaxhighlight lang="pascal" enclose="none">asm</syntaxhighlight>-blocks]].
  
<syntaxhighlight>program sumExample(input, output, stderr);
+
<syntaxhighlight lang="pascal" start="0" line highlight="8-12,18">program sumExample(input, output, stderr);
  
 
{ iteratively calculates the sum over first n integers }
 
{ iteratively calculates the sum over first n integers }
Line 53: Line 53:
 
writeLn(iterativeSumFirstNIntegers(n));
 
writeLn(iterativeSumFirstNIntegers(n));
 
end.</syntaxhighlight>
 
end.</syntaxhighlight>
Of course in a production program, you would use an algorithm applying the formula <code>sum := (n * (n + 1)) div 2</code> (“Gaussian sum formula”).
+
Of course in a production program, you would use an algorithm applying the formula <syntaxhighlight lang="pascal" enclose="none">sum := (n * (n + 1)) div 2</syntaxhighlight> (“Gaussian sum formula”).
 
 
  
  
 
[[category:Pascal]]
 
[[category:Pascal]]
 
[[Category:Control Structures]]
 
[[Category:Control Structures]]

Revision as of 17:35, 11 February 2018

Deutsch (de) English (en) français (fr) русский (ru)

The label keyword is used for declaration of labels (markers for unconditional jumps using goto keyword) used further in the unit/program.

A label section is also required for jump targets in asm-blocks.

 0program sumExample(input, output, stderr);
 1
 2{ iteratively calculates the sum over first n integers }
 3function iterativeSumFirstNIntegers(const n: longword): qword;
 4{$ifdef CPUX86_64} // ============= optimized implementation
 5// assembler modifier appended to routine declaration
 6assembler;
 7// you have to familiarize the compiler with symbols
 8// which are meant to be jump targets
 9{$goto on}
10label
11	isfni_iterate;
12{$asmMode intel}
13asm
14	xor rax, rax // rax := 0
15	// ecx is used as counter by loop instruction
16	mov ecx, n   // ecx := n
17isfni_iterate:
18	add rax, qword(ecx) // rax := rax + ecx
19	loop isfni_iterate  // dec(ecx)
20	// if ecx <> 0 then goto isfni_iterate
21	
22	// the @result macro represents the functions return value
23	mov @result, rax // result := rax
24// note, a list of modified registers (here ['rax', 'ecx'])
25//       is ignored for pure assembler routines
26end;
27{$else} // ========================== default implementation
28var
29	i: longword;
30	x: qword;
31begin
32	x := 0; // mov rax, 0
33	for i := n downto 1 do // mov ecx, n
34	begin
35		x := x + i; // add rax, ecx
36	end; // loop isfni_iterate
37	iterativeSumFirstNIntegers := x; // mov @result, rax
38end;
39{$endif}
40
41// M A I N =================================================
42var
43	n: longword;
44begin
45	readLn(n);
46	writeLn(iterativeSumFirstNIntegers(n));
47end.

Of course in a production program, you would use an algorithm applying the formula sum := (n * (n + 1)) div 2 (“Gaussian sum formula”).