comp.lang.ada
 help / color / mirror / Atom feed
From: Ole-Hjalmar Kristensen <ole-hjalmar.kristensen@substitute_employer_here.com>
Subject: Re: Pascal to ADA
Date: 17 Nov 2003 12:41:56 +0100
Date: 2003-11-17T11:41:56+00:00	[thread overview]
Message-ID: <wvbrllqfi5mj.fsf@sun.com> (raw)
In-Reply-To: bpa46j$o0l$1@news1.kornet.net


1. Why do you need this routine at all? 

2. You can accomplish the same in Ada in different ways. two obvious
   solutions are unchecked_conversion or an overlay with an address
   clause between a packed array of bytes and your 32 bit integer.

Overlay version:

type Long_Int is range -2**31 .. 2**31 - 1;
type Byte is mod 2**8;
...

Procedure Add(N: Long_Int; M: Long_Int; R : out Long_Int) is
  Cheat_N : array(0..3) of Byte;
  pragma pack(Cheat_N);
  for Cheat_N'address use N'address;
...
begin
...
end Add;

3. Logical operators also work on modular integers. Ada also has
   arithmetic shift, so you can conveniently access the carry bit.

>>>>> "TG" == Tilman Gloetzner <tilman.gloetzner@etas.co.kr> writes:

    TG> Hello,
    TG> 1) what is a effecient way of rewriting the following code fragement in ADA
    TG> ?  With the absolute keyword, Pascal  effectively casts
    TG> N, M, and R from a 4 byte integer type to an array of 4 bytes by an address
    TG> access.  I guess to implement the same mechanism in
    TG> ADA is not in line with ADA's philosophy.

    TG> 2) How do I mask out bits in a (unsigned) number type, as logical operators
    TG> work only on booleans ?

    TG> Thank you,

    TG> Tilman

    TG> ----------------------------------------------------------------
    TG> PROCEDURE Add32(N:LONGINT;M:LONGINT;VAR R:LONGINT) ;
    TG> VAR
    TG>      _N : ARRAY[0..3] OF BYTE ABSOLUTE N ;
    TG>      _M : ARRAY[0..3] OF BYTE ABSOLUTE M ;
    TG>      _R : ARRAY[0..3] OF BYTE ABSOLUTE R ;
    TG>      A  : BYTE ;
    TG>      B  : WORD ;
    TG>      C  : WORD ; { Carry }
    TG> BEGIN
    TG>      R := 0 ;
    TG>      C := 0 ;
    TG>      FOR A := 0 TO 3 DO BEGIN
    TG>          B := _N[A] + _M[A] + C ;
    TG>          _R[A] := B AND $FF ;
    TG>          C := B SHR 8 ;
    TG>      END ;
    TG> END  ;



-- 
Strange attractors stole my wife



  parent reply	other threads:[~2003-11-17 11:41 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-11-17 10:12 Pascal to ADA Tilman Gloetzner
2003-11-17 10:24 ` Vinzent 'Gadget' Hoefler
2003-11-17 10:44 ` Marius Amado Alves
2003-11-17 11:41 ` Ole-Hjalmar Kristensen [this message]
     [not found] ` <1069065860.2826.11.camel@localhost.localdomain>
2003-11-17 13:17   ` Marius Amado Alves
2003-11-17 14:42 ` Steve
2003-11-17 23:14   ` tmoran
2003-11-20 12:24 ` Lutz Donnerhacke
  -- strict thread matches above, loose matches on Subject: below --
2001-02-14 19:29 pascal to Ada Munir Albeelbisi
2001-02-14 21:30 ` Pascal Obry
2001-02-15 12:22   ` Marc A. Criley
2001-02-15 17:31     ` Pascal Obry
2001-02-15 10:43 ` David C. Hoos, Sr.
2001-02-15 17:21 ` Randy Brukardt
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox