comp.lang.ada
 help / color / mirror / Atom feed
* Bit operators
@ 2000-02-12  0:00 Joshua Grant
  2000-02-13  0:00 ` Robert Dewar
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Joshua Grant @ 2000-02-12  0:00 UTC (permalink / raw)


I was recently assigned a project for one of my classes that gave us the
option of implementing a simplified MIPS assempler in any preferred higher
level language.  I have choosen ADA, but I am running into a problem with
the bit operators.  I can't find them!  In case someone isn't sure of my
question.  In C++ if you perform an 'and' operation with the '&'
operator like...

base 10 
1 = 1 & 3
3 = 3 & 3
2 = 6 & 3
3 = 7 & 3

base 2 (same as above)
001 = 001 & 011
011 = 011 & 011
010 = 110 & 011
011 = 111 & 011

Can anyone tell me if ADA has an equivalent set of functions for 'and' and
'or'?  If you know please respond ASAP, I need to know if I should rewrite
my project in a language that supports those operations.


Thanks,
Josh





^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Bit operators
  2000-02-12  0:00 Bit operators Joshua Grant
  2000-02-13  0:00 ` Robert Dewar
  2000-02-13  0:00 ` Matthew Heaney
@ 2000-02-13  0:00 ` Gautier
  2 siblings, 0 replies; 8+ messages in thread
From: Gautier @ 2000-02-13  0:00 UTC (permalink / raw)
  To: Joshua Grant

Joshua Grant:

> Can anyone tell me if ADA has an equivalent set of functions for 'and' and
> 'or'?

The operators "and, "or", "xor" are available for modular types
- e.g. for... type Byte is mod 2 ** 8;
See also the Interfaces package (predef'd unsigned_*, shift operators)
HTH

-- 
Gautier

_____\\________________\_______\_________
http://members.xoom.com/gdemont/gsoft.htm




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Bit operators
  2000-02-12  0:00 Bit operators Joshua Grant
  2000-02-13  0:00 ` Robert Dewar
@ 2000-02-13  0:00 ` Matthew Heaney
  2000-02-13  0:00   ` Bit operators & asm in Ada (novice question) G
  2000-02-13  0:00 ` Bit operators Gautier
  2 siblings, 1 reply; 8+ messages in thread
From: Matthew Heaney @ 2000-02-13  0:00 UTC (permalink / raw)


In article <884o85$jjv$1@news.fsu.edu> , grant@cs.fsu.edu (Joshua Grant) 
wrote:

> I was recently assigned a project for one of my classes that gave us the
> option of implementing a simplified MIPS assempler in any preferred higher
> level language.  I have choosen ADA, but I am running into a problem with
> the bit operators.  I can't find them!  In case someone isn't sure of my
> question.  In C++ if you perform an 'and' operation with the '&'
> operator like...
>
> base 10
> 1 = 1 & 3
> 3 = 3 & 3
> 2 = 6 & 3
> 3 = 7 & 3

declare
  I : Interfaces.Unsigned_32;
begin
  I := 1 and 3;
  I := 3 and 3;
etc
end;


> base 2 (same as above)
> 001 = 001 & 011
> 011 = 011 & 011
> 010 = 110 & 011
> 011 = 111 & 011

declare
  I : Interfaces.Unsigned_32;
begin
  I := 2#110# and 2#011#;
  I := 2#111# and 2#011#;
etc
end;


> Can anyone tell me if ADA has an equivalent set of functions for 'and' and
> 'or'?  If you know please respond ASAP, I need to know if I should rewrite
> my project in a language that supports those operations.

Ada is a systems programming language, just like C/C++.  Anything you
can do in C, you can do in Ada.

Bitwise operators are predefined for modular types.  The operations are
named "and" and "or".

--
Celebrate Darwin Day on Saturday, 12 February!

<http://www.bbc.co.uk/education/darwin/index.shtml>




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Bit operators & asm in Ada (novice question)
  2000-02-13  0:00 ` Matthew Heaney
@ 2000-02-13  0:00   ` G
  2000-02-13  0:00     ` Gautier
  2000-02-13  0:00     ` Vladimir Olensky
  0 siblings, 2 replies; 8+ messages in thread
From: G @ 2000-02-13  0:00 UTC (permalink / raw)





>
> > base 10
> > 1 = 1 & 3
> > 3 = 3 & 3
> > 2 = 6 & 3
> > 3 = 7 & 3
>
> declare
>   I : Interfaces.Unsigned_32;
> begin
>   I := 1 and 3;
>   I := 3 and 3;
> etc
> end;
>
> > base 2 (same as above)
> > 001 = 001 & 011
> > 011 = 011 & 011
> > 010 = 110 & 011
> > 011 = 111 & 011
>
> declare
>   I : Interfaces.Unsigned_32;
> begin
>   I := 2#110# and 2#011#;
>   I := 2#111# and 2#011#;
> etc
> end;
>
>
>
> Ada is a systems programming language, just like C/C++.  Anything you
> can do in C, you can do in Ada.
>

  I have been reading the Art of Assembly.  I was wondering how to do exactly
these sorts of operations in Ada.  If my terminology is correct, and from what
is above, I am
thinking that I can apply bit masks to bytes by using modular integers (OR by
declaring a variable as Interface.Unsigned_32,( for 32bit ops only, I assume -
and that modular integers would allow for contraction to 16 or 8 and rotation or
shift operations ???)).  So -
in Borland C++ I may actually write asm directly into code (not that I can yet),

but - is this possible in Ada ?
  I have read many times and in many places that actual running programs spend
on average 90% of their time in 10% of the code.  And that assembly is often
employed to
rewrite that 10%.  I guess what I am wondering is, how does an assembly
programmer do this if a program has been written for Ada ?  Does the Ada
programmer have to construct their programs with a preconceived notion that it
will have to be rewritten in parts in Assembly ?  Or is it that at the machine
level these sorts of questions are totally irrelevant.  I wonder because it is
not at all transparent.

--

--
GM Wallace.

BDF
--
caveat: I can not afford a computer science education, please do not flame me.
(I received some nasty, almost threatening, messages a while back from someone
(anonymous) here so I feel I have to qualify my ignorance very cautiously).





^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Bit operators
  2000-02-12  0:00 Bit operators Joshua Grant
@ 2000-02-13  0:00 ` Robert Dewar
  2000-02-13  0:00   ` Gautier
  2000-02-13  0:00 ` Matthew Heaney
  2000-02-13  0:00 ` Bit operators Gautier
  2 siblings, 1 reply; 8+ messages in thread
From: Robert Dewar @ 2000-02-13  0:00 UTC (permalink / raw)


In article <884o85$jjv$1@news.fsu.edu>,
  grant@cs.fsu.edu (Joshua Grant) wrote:
> I was recently assigned a project for one of my classes that
gave us the
> option of implementing a simplified MIPS assempler in any
preferred higher
> level language.  I have choosen ADA, but I am running into a
problem with
> the bit operators.  I can't find them!  In case someone isn't
sure of my
> question.  In C++ if you perform an 'and' operation with the
'&'
> operator like...

All the answers you got were inappropriate (a not uncommon
occurrence around here :-) :-)

You approached the problem with a "how-do-I-write-C-in-Ada"
viewpoint, and three people answered you to tell you how :-)

But the proper answer is to use the features of Ada that allow
this to be done nicely without such nasty low level gizmos.

MIPS assembly instructions have a very regular format, there
are only a very few instruction formats.

For each of these define a record type, with nicely typed fields
and then use a record representation clause to lay out the
record in a manner that matches the MIPS layouts (you may have
to worry about endian considerations here, but the same is
true using the low level C approach anyway).

Now you assign to the fields of this record with full type
checking, e.g. if you try to put an immediate value in a
register field, you will get a compile time error.

There is no point in writing C in Ada, if you want to write
C, write C in C, it is less trouble :-)


Sent via Deja.com http://www.deja.com/
Before you buy.




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Bit operators & asm in Ada (novice question)
  2000-02-13  0:00   ` Bit operators & asm in Ada (novice question) G
  2000-02-13  0:00     ` Gautier
@ 2000-02-13  0:00     ` Vladimir Olensky
  1 sibling, 0 replies; 8+ messages in thread
From: Vladimir Olensky @ 2000-02-13  0:00 UTC (permalink / raw)



G wrote in message <38A624B3.5709A464@interact.net.au>...

>> Ada is a systems programming language, just like C/C++.  Anything you
>> can do in C, you can do in Ada.
>>
>
>  I have been reading the Art of Assembly.  I was wondering how to do
exactly
>these sorts of operations in Ada
> So -in Borland C++ I may actually write asm directly into code (not that I
can yet),
>but - is this possible in Ada ?


Of course Yes.

In Ada 95 there are two ways:

1. Machine code insertions - close to what could be done in C++ or
Borland Delphi OP but little bit more complicated .

2. Use assembler (GAS or NASM or any other)  to write what you need,
compile it and than link it to your Ada program. Of course one need to
write  Ada interface (*.ads)  to that Assembler code.

The second way is very clean and simple.

Regards,
Vladimir Olensky






^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Bit operators & asm in Ada (novice question)
  2000-02-13  0:00   ` Bit operators & asm in Ada (novice question) G
@ 2000-02-13  0:00     ` Gautier
  2000-02-13  0:00     ` Vladimir Olensky
  1 sibling, 0 replies; 8+ messages in thread
From: Gautier @ 2000-02-13  0:00 UTC (permalink / raw)


So - in Borland C++ I may actually write asm directly into code (not that I can yet),
but - is this possible in Ada ?

Yes. But you won't need it. Borland's compiler translates the operations into
unefficient, redundant machine code that actually need asm rewrite.
If you take an optimizing Ada compiler (say GNAT) and compile with "-O2 -gnatp",
the code generator will do an impressive optimisation effort, in particular with
bitfield manipulations. You can see it in action for drawing 3D polygon scanlines
(link below)...

> Does the Ada programmer have to construct their programs with a preconceived
> notion that it will have to be rewritten in parts in Assembly ?

So, fortunately not. Would it be true, they should consider changing their compiler...
In addition asm insertions break portability and security (run with range check).
Should be used only for 1-asm-instruction procedure to communicate with hardware (IMHO).

-- 
Gautier

_____\\________________\_______\_______
http://members.xoom.com/gdemont/e3d.htm




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Bit operators
  2000-02-13  0:00 ` Robert Dewar
@ 2000-02-13  0:00   ` Gautier
  0 siblings, 0 replies; 8+ messages in thread
From: Gautier @ 2000-02-13  0:00 UTC (permalink / raw)


Robert Dewar wrote:

> All the answers you got were inappropriate (a not uncommon
> occurrence around here :-) :-)

Thank you! We are looking forward for the Words of Wisdom!

> You approached the problem with a "how-do-I-write-C-in-Ada"
> viewpoint, and three people answered you to tell you how :-)

Sorry, I only have a Basic, Pascal & Fortran background. Difficult
to tell how to write C (or Cobol or...) in these conditions.
You lend me (or us three) such criminal intentions ?!

> But the proper answer is to use the features of Ada that allow
> this to be done nicely without such nasty low level gizmos.

What indicates you that Mr Grant doesn't know enumarated types
and nice records and doesn't want to use them ? If he thought to
try it in Ada, it was surely to make use of Ada's "+"es...

It just seems to me that he has encountered the simple problem
of performing the "AND" instruction that surely exists among the
MIPS assembly instructions. That's all! How can you do easily
without modular types ? Arrays of boolean is an alternative,
perhaps cleaner. Anyway I guess that the goal of his project is
not getting compile time errors! 

> There is no point in writing C in Ada, if you want to write
> C, write C in C, it is less trouble :-)

You dismiss the people who write elegant, portable C - maybe the
huge amount of 0.05 % of the C sources that are on the Web!

-- 
Gautier

_____\\________________\_______\
http://members.xoom.com/gdemont/




^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2000-02-13  0:00 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-02-12  0:00 Bit operators Joshua Grant
2000-02-13  0:00 ` Robert Dewar
2000-02-13  0:00   ` Gautier
2000-02-13  0:00 ` Matthew Heaney
2000-02-13  0:00   ` Bit operators & asm in Ada (novice question) G
2000-02-13  0:00     ` Gautier
2000-02-13  0:00     ` Vladimir Olensky
2000-02-13  0:00 ` Bit operators Gautier

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