Difference between revisions of "Label"

From Free Pascal wiki
Jump to navigationJump to search
(hint to math.sign)
m (formatting)
Line 1: Line 1:
 
{{label}}
 
{{label}}
  
The <code>label</code> keyword is used for declaration of labels (markers for unconditional jumps using [[Goto|goto]] keyword) used further in the unit/program.
+
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.
  
 
A <code>label</code> section is also required for jump targets in [[Asm|<code>asm</code>-blocks]].
 
A <code>label</code> section is also required for jump targets in [[Asm|<code>asm</code>-blocks]].

Revision as of 21:49, 1 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.

program sign(input, output, stderr);

type
	signumCodomain = -1..1;

{ returns the sign of an integer }
function signum(const x: longint): signumCodomain;
{$ifdef CPUx86_64}
assembler;
{$goto on}
label
	signum_done;
{$asmMode intel}
asm
	mov @result, 1  // result := 1
	cmp x, 0        // x = 0 ?
	jg signum_done  // if x > 0 then goto signum_done
	mov @result, -1 // result := -1
	jl signum_done  // if x < 0 then goto signum_done
	mov @result, 0  // result := 0
signum_done:	
end;
{$else}
begin
	// This is what virtually math.sign does.
	// The compiled code requires _two_ cmp instructions, though. 
	if x > 0 then
	begin
		signum := 1;
	end
	else if x < 0 then
	begin
		signum := -1;
	end
	else
	begin
		signum := 0;
	end;
end;
{$endif}

var
	x: longint;
begin
	readLn(x);
	writeLn(signum(x));
end.