Path: news.cs.au.dk!news.net.uni-c.dk!uninett.no!news.maxwell.syr.edu!newsfeed.cwix.com!206.172.150.11!news1.bellglobal.com!news.uunet.ca!not-for-mail From: Karl Waclawek Newsgroups: comp.lang.beta Subject: Re: How to do low level stuff Date: Thu, 07 Jan 1999 14:38:08 -0500 Organization: The Toronto Star Lines: 151 Message-ID: <36950D20.2FAD4D18@thestar.ca> References: <368B8A70.CF9934FF@thestar.ca> NNTP-Posting-Host: 192.206.151.130 Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Mailer: Mozilla 4.5 [en] (Win95; I) X-Accept-Language: en Xref: news.cs.au.dk comp.lang.beta:11768 Morten Grouleff wrote: > > Karl Waclawek writes: > > > I knew about BETA for a while and recently I have spent > > more time with the Personal Edition on Linux (since it is one > > of the few visual development environments available there). > > > > I find it conceptually the most elegant language I have > > ever seen, however, I have trouble to see how one would code > > low level, "byte processing" functionality. > > > > For example: Here is a CRC routine I coded in Object Pascal (Delphi 4): > > [... example ...] > > > As one can see, there is bit-shifting involved as well as accessing > > the low/high byte of a word and the low/high word of a longword. > > > > How would such a routine be coded in BETA? > > Would it be efficient? > > > > Or would I have to resort to another language (like OP or C) > > and call it from BETA? > > You could do it in C and call that from BETA. But you could also do it > in BETA: > > ORIGIN '~beta/basiclib/betaenv'; > --PROGRAM: descriptor-- > (# > BufType: (# R: [0]@Char #); > CRCTable: [256]@Integer; > > InitCRCTable: > (# > do (* Initialize CRCTable here. Not included ;) *) > #); > > GetCRC32: > (# ByteCount, CRC, BufInx, indx: @Integer; > Buffer: ^BufType; > enter Buffer[] > do (for i: Buffer.R.range repeat > (Buffer.R[i] %Bxor (CRC %srl 24)) -> indx; > (CRC %sll 8) %Bxor CRCTable[Indx+1] -> CRC; > for); > exit CRC > #); > I thought BETA did not make specific assumptions about the size and the byte ordering of the basic types. From what your code shows, however, it seems that one can assume that Char is stored in one Byte and that Integer is stored in 4 Bytes. My Pascal code really relies on the fact that a LongWord consists of 4 bytes ordered in a specific way. As far as your code goes, I think the semantics are right, but it relies on the byte ordering too (as well as bit shifting semantics). I also think that the CRC algorithm dependens on the byte ordering, but I don't know for sure. Here I have tried to rewrite a part of your code, treating the 32bit CRC value as a repetition of 4 Chars. That way, byte shifting can be avoided. Again, the assumption is that a Char is the same as a Byte! Please forgive me any syntactical errors - I am very new to coding in BETA. ORIGIN '~beta/basiclib/betaenv'; --PROGRAM: descriptor-- (# CRCType: (# R: [4]@Char #); BufType: (# R: [0]@Char #); CRCTable: [256]@CRCType; InitCRCTable: (# do (* Initialize CRCTable here. Not included ;) *) #); GetCRC32: (# CRC: ^CRCType; indx: @Char; Buffer: ^BufType; enter (Buffer[], CRC[]) do (for i: Buffer.R.range repeat (Buffer.R[i] %Bxor CRC.R[4]) -> indx; CRCTable[indx].R[1]) -> CRC.R[1]; (for j: 3 repeat (CRC.R[j] %Bxor CRCTable[indx].R[j+1]) -> CRC.R[j+1] for); for); exit CRC[] #); ... and so on ... #) Two questions: 1) About CRCType: (# R: [4]@Char #): Why can one not declare a type that is a repetition? Like this, for example: CRCType: [4]Char; 2) Char is currently compatible with integer operations and assignments. I am not sure where I read that, but it appears that that will not remain a part of the language. Is that true or am I just confused? > I think it does the same as your function, but I'm not quite sure, as I > have been guessing the semantics of some of the operations (like > "LongRec" and ".HI") Yes, your guess was right. From what I can see, with the ability to treat a Char simply as a Byte and the new bit processing operators (that can be applied to it) a lot of low level functionality is gained. The efficiency remains to be tested. If the example above works right, then I will try to use it to calculate the CRC for a large file and compare it with my Delphi version, both, for correctness and performance. Btw, my example was not a good demonstration for the need for bit-shifting, since it does really need it. I am sure, however, that there are such examples. > ============= > The following shifting primites exist, but they are not in the official > language description yet, so they may still change: > > usage E1 OP E2 > > OP: %srl shift right logical of E1 by E2 bits > %sll shift left logical of E1 by E2 bits > %sra shift right arithemetic of E1 by E2 bits > %sla shift left arithemetic of E1 by E2 bits > %ror rotate right of E1 by E2 bits > %rol rotate left of E1 by E2 bits > %Band bitwise and > %Bor bitwise or > %Bxor bitwise exclusive or > > And a unary one: > > usage OP E1 > > OP: %Bnot bitwise not Seems like a necessary addition to BETA. Karl ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Karl Waclawek KD Soft Inc. * Phone: (905) 579-3443 * E-Mail: waclawek@idirect.com