Unsigned integer math in BlackBox

From: [at]} <Wojtek>
Date: Sun, 16 Nov 2003 12:04:32 -0500 (EST)

----boundary-LibPST-iamunique-1359096229_-_-
Content-type: text/plain

Hello:

 I just reached a conclusion, that lack of unsigned integer math
in BlackBox is a royal pain in the bottom. If not for my respect
to our OuS friends, I would have used more explicit language
to describe their language design decision.

Here is the story. For last three days I has chasing missing bits
in the memory pointer which I read from an field-programmable
gate array. As usual in such circumstances, I was suspecting
bad FPGA timing, or bugs in the embedded microcontroller C-code,
or perhaps bad solder joints on my PCB. It did not occur to me
that our rock-solid BlackBox can introduce subtle bugs. Right?
Wrong. It can and it did. Here is how.

The FPGA memory pointer is read in two 8-bit chunks, an MSB and LSB.
The LSB contains lower 8 bits, while the MSB contains the remaining
bits. The MSB and LSB arrive in a byte buffer, and then they are
combined in the PC into one word. Written in the (vastly inferior
and known for its obscurity) C-language this would have been simple:

  unsigned char lsb, msb;
  unsigned int memPtr;
  msb = buffer [2];
  lsb = buffer [3];
  memPtr = (msb << 8) + lsb;

In the (vastly superior and known for its clarity) Component Pascal
I have written code, which I thought was doing the same thing.

   VAR buffer: ARRAY 256 of BYTE;
       memPtr: INTEGER;
   memPtr := ASH (buffer [2], 8) + buffer [3];

This is nice and looks innocent enough. Then the hell broke loose.
Sometimes the memory pointer was correct. But sometimes it was not.
Sometimes, but not always, bits were missing in the upper part of
the pointer. After three days of debugging it turned out that
the correct code should have been:

  VAR lsb, msb: INTEGER;
       buffer: ARRAY 256 of BYTE;
       memPtr: INTEGER;
  msb := ORD (BITS (ASH (buffer [2], 8)) * {8..15});
  lsb := ORD (BITS (buffer [3]) * {0..7});
  memPtr := msb + lsb;

I leave it to you to figure out why this obfuscated code is the
correct CP version of "(msb << 8) + lsb". It is also important
to note that the bug was showing only for some bit patterns,
but not for the others, what made it exceptionally hard to debug
the original CP code.

I want to point out that the CP language is not complete. The unsigned
math should have been there. The users have been arguing with OuS for
years that the unsigned math is essential, all in vain. I hope that
I have made it more clear why the unsigned should have been in the
language.

Thank you,

Wojtek Skulski skulski{([at]})nowhere.xy

--------------------------------------------

To unsubscribe from this mailing list, send a message containing the word "unsubscribe" to:
   blackbox-request{([at]})nowhere.xy

To get a list of valid e-mail commands and instructions on their usage, send a message containing the word "help" to the above address.

Send any problem reports or questions related to this email list to the list owner at
   owner-blackbox{([at]})nowhere.xy

Current posting policy:

a) To post you should use the same address by which you are subscribed to the mailing list. That way, the list server will recognize you as subscriber and forward your posting immediately, without creating any overhead.

b) If, for some reason, you cannot post from the address, by which you are subscribed, your message will be moderated to avoid spam. Please understand that moderation will often cause some delay, in particular over weekends or holydays.



----boundary-LibPST-iamunique-1359096229_-_-
Content-type: application/rtf
Content-transfer-encoding: base64
Content-Disposition: attachment; filename="rtf-body.rtf"

e1xydGYxXGFuc2lcYW5zaWNwZzEyNTJcZnJvbXRleHQgXGRlZmYwe1xmb250dGJsDQp7XGYwXGZz
d2lzcyBBcmlhbDt9DQp7XGYxXGZtb2Rlcm4gQ291cmllciBOZXc7fQ0Ke1xmMlxmbmlsXGZjaGFy
c2V0MiBTeW1ib2w7fQ0Ke1xmM1xmbW9kZXJuXGZjaGFyc2V0MCBDb3VyaWVyIE5ldzt9fQ0Ke1xj
b2xvcnRibFxyZWQwXGdyZWVuMFxibHVlMDtccmVkMFxncmVlbjBcYmx1ZTI1NTt9DQpcdWMxXHBh
cmRccGxhaW5cZGVmdGFiMzYwIFxmMFxmczIwIEhlbGxvOlxwYXINClxwYXINCiBJIGp1c3QgcmVh
Y2hlZCBhIGNvbmNsdXNpb24sIHRoYXQgbGFjayBvZiB1bnNpZ25lZCBpbnRlZ2VyIG1hdGhccGFy
DQppbiBCbGFja0JveCBpcyBhIHJveWFsIHBhaW4gaW4gdGhlIGJvdHRvbS4gSWYgbm90IGZvciBt
eSByZXNwZWN0XHBhcg0KdG8gb3VyIE91UyBmcmllbmRzLCBJIHdvdWxkIGhhdmUgdXNlZCBtb3Jl
IGV4cGxpY2l0IGxhbmd1YWdlXHBhcg0Kd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UgbWFpbGlu
ZyBsaXN0LiBUaGF0IHdheSwgdGhlIGxpc3Qgc2VydmVyIHdpbGwgcmVjb2duaXplIHlvdSBhcyBz
dWJzY3JpYmVyIGFuZCBmb3J3YXJkIHlvdXIgcG9zdGluZyBpbW1lZGlhdGVseSwgd2l0aG91dCBj
cmVhdGluZyBhbnkgb3ZlcmhlYWQuXHBhcg0KXHBhcg0KYikgSWYsIGZvciBzb21lIHJlYXNvbiwg
eW91IGNhbm5vdCBwb3N0IGZyb20gdGhlIGFkZHJlc3MsIGJ5IHdoaWNoIHlvdSBhcmUgc3Vic2Ny
aWJlZCwgeW91ciBtZXNzYWdlIHdpbGwgYmUgbW9kZXJhdGVkIHRvIGF2b2lkIHNwYW0uIFBsZWFz
ZSB1bmRlcnN0YW5kIHRoYXQgbW9kZXJhdGlvbiB3aWxsIG9mdGVuIGNhdXNlIHNvbWUgZGVsYXks
IGluIHBhcnRpY3VsYXIgb3ZlciB3ZWVrZW5kcyBvciBob2x5ZGF5cy5ccGFyDQpccGFyDQp9


----boundary-LibPST-iamunique-1359096229_-_---
Received on Sun Nov 16 2003 - 18:04:32 UTC

This archive was generated by hypermail 2.3.0 : Thu Sep 26 2013 - 06:29:03 UTC