comp.lang.ada
 help / color / mirror / Atom feed
From: "Steve" <nospam_steved94@comcast.net>
Subject: Re: Pascal to ADA
Date: Mon, 17 Nov 2003 14:42:47 GMT
Date: 2003-11-17T14:42:47+00:00	[thread overview]
Message-ID: <H75ub.172723$9E1.890804@attbi_s52> (raw)
In-Reply-To: bpa46j$o0l$1@news1.kornet.net

This is as close as I can come to a literal translation:

  subtype BYTE is Unsigned_8;
  subtype LONGINT is Integer;
  subtype WORD is Unsigned_16;

  procedure Add32(N:LONGINT;M:LONGINT;R: in out LONGINT) is
    N_As_Bytes : array(0..3) of BYTE;
    for N_As_Bytes'Address use N'Address;
    M_As_Bytes : array(0..3) of BYTE;
    for M_As_Bytes'Address use M'Address;
    R_As_Bytes : array(0..3) of BYTE;
    for R_As_Bytes'Address use R'Address;
    B : WORD;
    C : WORD;-- Carry
  begin
    R := 0 ;
    C := 0 ;
    for A in 0 .. 3 loop
      B := WORD( N_As_Bytes(A) + M_As_Bytes(A) ) + C ;
      R_As_Bytes(A) := BYTE( B and 16#FF# );
      C := Shift_Right( B, 8 );
    end loop;
  end Add32;

You'll need to have "with Interfaces; use Interfaces" included in order to
define Unsigned_8 and Unsigned_16.

I changed _N to N_As_Bytes since Ada does not permit using an underscore as
the first character in an identifier.

Steve
(The Duck)


"Tilman Gloetzner" <tilman.gloetzner@etas.co.kr> wrote in message
news:bpa46j$o0l$1@news1.kornet.net...
> Hello,
>
> 1) what is a effecient way of rewriting the following code fragement in
ADA
> ?  With the absolute keyword, Pascal  effectively casts
> N, M, and R from a 4 byte integer type to an array of 4 bytes by an
address
> access.  I guess to implement the same mechanism in
> ADA is not in line with ADA's philosophy.
>
> 2) How do I mask out bits in a (unsigned) number type, as logical
operators
> work only on booleans ?
>
> Thank you,
>
> Tilman
>
> ----------------------------------------------------------------
> PROCEDURE Add32(N:LONGINT;M:LONGINT;VAR R:LONGINT) ;
> VAR
>      _N : ARRAY[0..3] OF BYTE ABSOLUTE N ;
>      _M : ARRAY[0..3] OF BYTE ABSOLUTE M ;
>      _R : ARRAY[0..3] OF BYTE ABSOLUTE R ;
>      A  : BYTE ;
>      B  : WORD ;
>      C  : WORD ; { Carry }
> BEGIN
>      R := 0 ;
>      C := 0 ;
>      FOR A := 0 TO 3 DO BEGIN
>          B := _N[A] + _M[A] + C ;
>          _R[A] := B AND $FF ;
>          C := B SHR 8 ;
>      END ;
> END  ;
>
>





  parent reply	other threads:[~2003-11-17 14:42 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
     [not found] ` <1069065860.2826.11.camel@localhost.localdomain>
2003-11-17 13:17   ` Marius Amado Alves
2003-11-17 14:42 ` Steve [this message]
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