Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2003-07

Re: runtime 201 with swapword function

12 messages · 7 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Jonas Maebe
to James Mills: Why does the following result in a runtime error ? program example; const THE_PORT = 9999;
James Mills wrote 11 lines
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;

In Pascal, all calculations are performed using the base type (which is longint in FPC), so if w > 255, then w shl 8 > high(word) and will result in a range check error when assigning to swapWord. Even if we calculated everything using words, you'd still get an overflow error, this time when doing the shl.

Jonas

James Mills

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

cheers
James

- -Zero Defect Software Engineers Group - ZDSEG - -You need only two tools. WD-40 and duct tape. -If it doesn't move and it should, use WD-40. -If it moves and shouldn't, use the tape.
Jonas Maebe
to James Mills: How can I correct this ? This bit of code was taken directly from server.pp in one of the…
James Mills wrote 8 lines
> In Pascal, all calculations are performed using the base type (which > is > longint in FPC), so if w > 255, then w shl 8 > high(word) and will > result in a range check error when assigning to swapWord. Even if we > calculated everything using words, you'd still get an overflow error, > this time when doing the shl. How can I correct this ?

Use a typecast:

function swapWord(w: Word): Word;
begin
	swapWord := Word((w SHL 8) OR (w SHR 8));
end;

Jonas

Florian Klaempfl
to James Mills: How can I correct this ? This bit of code was taken directly from server.pp in one of the…
James Mills wrote 27 lines
On Fri, Jul 25, 2003 at 04:21:36PM +0200, Jonas Maebe wrote: >On vrijdag, jul 25, 2003, at 16:22 Europe/Brussels, James Mills wrote: > > >>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; > >In Pascal, all calculations are performed using the base type (which is >longint in FPC), so if w > 255, then w shl 8 > high(word) and will >result in a range check error when assigning to swapWord. Even if we >calculated everything using words, you'd still get an overflow error, >this time when doing the shl. How can I correct this ? This bit of code was taken directly from server.pp in one of the old 2002 mailing threads...
Turn range checking off for this piece of code or do
function swapWord(w: Word): Word;
   begin
      swapWord := ((w SHL 8) OR (w SHR 8)) and $ffff;
   end;
Nikolay Nikolov
to James Mills: How can I correct this ? This bit of code was taken directly from server.pp in one of the…
Show quoted text 16 lines
>>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; >> How can I correct this ? This bit of code was taken directly from server.pp in one of the old 2002 mailing threads...
swapWord := ((w AND $FF) SHL 8) OR (w SHR 8);
James Mills
to Jonas Maebe: In Pascal, all calculations are performed using the base type (which is longint in FPC),…
Jonas Maebe wrote 20 lines
On vrijdag, jul 25, 2003, at 16:22 Europe/Brussels, James Mills wrote: >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; In Pascal, all calculations are performed using the base type (which is longint in FPC), so if w > 255, then w shl 8 > high(word) and will result in a range check error when assigning to swapWord. Even if we calculated everything using words, you'd still get an overflow error, this time when doing the shl.

How can I correct this ?
This bit of code was taken directly from server.pp in one of the old 2002 mailing threads...

cheers
James

Show quoted text 8 lines
Jonas _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
- -Zero Defect Software Engineers Group - ZDSEG - -You need only two tools. WD-40 and duct tape. -If it doesn't move and it should, use WD-40. -If it moves and shouldn't, use the tape.
Marco van de Voort
to James Mills
Show quoted text 21 lines
On Fri, Jul 25, 2003 at 04:21:36PM +0200, Jonas Maebe wrote: > > On vrijdag, jul 25, 2003, at 16:22 Europe/Brussels, James Mills wrote: > > >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; > > In Pascal, all calculations are performed using the base type (which is > longint in FPC), so if w > 255, then w shl 8 > high(word) and will > result in a range check error when assigning to swapWord. Even if we > calculated everything using words, you'd still get an overflow error, > this time when doing the shl.
change to
  swapword word (w shl 8) OR word (w shr 8);

or temporarily disable {$R+} there.

How can I correct this ? This bit of code was taken directly from server.pp in one of the old 2002 mailing threads...

But was it under {$R+} there?

James Mills
to Jonas Maebe: Use a typecast: function swapWord(w: Word): Word; begin swapWord := Word((w SHL 8) OR (w…
Jonas Maebe wrote 18 lines
On vrijdag, jul 25, 2003, at 16:32 Europe/Brussels, James Mills wrote: >>In Pascal, all calculations are performed using the base type (which >>is >>longint in FPC), so if w > 255, then w shl 8 > high(word) and will >>result in a range check error when assigning to swapWord. Even if we >>calculated everything using words, you'd still get an overflow error, >>this time when doing the shl. > >How can I correct this ? Use a typecast: function swapWord(w: Word): Word; begin swapWord := Word((w SHL 8) OR (w SHR 8)); end;

Excellent thank you :)

James

Show quoted text 8 lines
Jonas _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
- -Zero Defect Software Engineers Group - ZDSEG - -You need only two tools. WD-40 and duct tape. -If it doesn't move and it should, use WD-40. -If it moves and shouldn't, use the tape.
James Mills
to Florian Klaempfl: Turn range checking off for this piece of code or do function swapWord(w: Word): Word;…
Florian Klaempfl wrote 35 lines
James Mills wrote: >On Fri, Jul 25, 2003 at 04:21:36PM +0200, Jonas Maebe wrote: > >>On vrijdag, jul 25, 2003, at 16:22 Europe/Brussels, James Mills wrote: >> >> >>>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; >> >>In Pascal, all calculations are performed using the base type (which is >>longint in FPC), so if w > 255, then w shl 8 > high(word) and will >>result in a range check error when assigning to swapWord. Even if we >>calculated everything using words, you'd still get an overflow error, >>this time when doing the shl. > > >How can I correct this ? >This bit of code was taken directly from server.pp in one of the old >2002 mailing threads... Turn range checking off for this piece of code or do function swapWord(w: Word): Word; begin swapWord := ((w SHL 8) OR (w SHR 8)) and $ffff; end;

Least now I know the reason :)

cheers
James

Show quoted text 5 lines
_______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
- -Zero Defect Software Engineers Group - ZDSEG - -You need only two tools. WD-40 and duct tape. -If it doesn't move and it should, use WD-40. -If it moves and shouldn't, use the tape.
Michael Van Canneyt
to James Mills: Why does the following result in a runtime error ? program example; const THE_PORT = 9999;
James Mills wrote 25 lines
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.

Matt Emson
to James Mills: Why does the following result in a runtime error ? program example; const THE_PORT = 9999;

Doesn't system contain a routine called 'swap' that does this exact thing??

This program would seem to work in both Delphi and FPC1.1:

program test;
{$APPTYPE CONSOLE}
uses
  sysutils;
var
  i, j: word;
begin
  i := $9988;
  writeln( IntToHex(i, 4) ); //output '9988'
  j := swap( i );
  writeln( IntToHex(j, 4)); //output '8899'
  readln;
end.

No need to go to the trouble of writing your won routine!! ;-)

Matt

Show quoted text 42 lines
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 cheers James -- - -Zero Defect Software Engineers Group - ZDSEG - -You need only two tools. WD-40 and duct tape. -If it doesn't move and it should, use WD-40. -If it moves and shouldn't, use the tape. _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
James Mills
to Matt Emson
Matt Emson wrote 23 lines
Doesn't system contain a routine called 'swap' that does this exact thing?? This program would seem to work in both Delphi and FPC1.1: program test; {$APPTYPE CONSOLE} uses sysutils; var i, j: word; begin i := $9988; writeln( IntToHex(i, 4) ); //output '9988' j := swap( i ); writeln( IntToHex(j, 4)); //output '8899' readln; end. No need to go to the trouble of writing your won routine!! ;-)

I didn't write it, one of the FPC developers did last year in the mailing list :)

cheers
James

Show quoted text 52 lines
Matt > 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 > > > cheers > James > > -- > - > -Zero Defect Software Engineers Group - ZDSEG > - > -You need only two tools. WD-40 and duct tape. > -If it doesn't move and it should, use WD-40. > -If it moves and shouldn't, use the tape. > > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal > _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
- -Zero Defect Software Engineers Group - ZDSEG - -You need only two tools. WD-40 and duct tape. -If it doesn't move and it should, use WD-40. -If it moves and shouldn't, use the tape.