[fpc-pascal]runtime 201 with swapword function

Michael Van Canneyt michael.vancanneyt at wisa.be
Fri Jul 25 16:54:41 CEST 2003


On Sat, 26 Jul 2003, James Mills wrote:

> Hi,
>
> Why does the following result in a runtime error ?
>
> program example;
>
> const
> 	THE_PORT = 9999;
>
> function swapWord(w: Word): Word;
> begin
> 	swapWord := (w SHL 8) OR (w SHR 8);
> end;
>
> var
> 	port:	Word;
> begin
> 	port := swapWord(THE_PORT)
> end.
>
> $ ./swapword
> Runtime error 201 at 0x08052AA2
>   0x08052AA2
>   0x08052AC2
>   0x080480B0

This is a range check error.
 (w SHL 8) OR (w SHR 8)
is evaluated as a longint. i.e. the result may not fit in a longint.
And I think it should be:
 ((w and $FF) SHL 8) OR (w SHR 8)

Michael.





More information about the fpc-pascal mailing list