Difference between revisions of "Label"
From Lazarus wiki
Jump to navigationJump to search (label section for Asm-blocks containing jump targets required, too) |
|||
Line 1: | Line 1: | ||
{{label}} | {{label}} | ||
− | The | + | 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. |
− | < | + | |
− | < | + | A <code>label</code> section is also required jump targets in [[Asm|<code>asm</code>-blocks]]. |
+ | <syntaxhighlight>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 | ||
+ | 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.</syntaxhighlight> | ||
[[category:Pascal]] | [[category:Pascal]] | ||
[[Category:Control Structures]] | [[Category:Control Structures]] |
Revision as of 22:38, 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 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
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.