Re: C programming on ARM




"David Brown" <david.brown@xxxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:47a23426$0$14988$8404b019@xxxxxxxxxxxxxxxxxx
Wilco Dijkstra wrote:
"David Brown" <david@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:47a1a46d$0$14996$8404b019@xxxxxxxxxxxxxxxxxx

there are many architectures with different pointer types and different memory spaces. These don't fit well with C
(which expects a single memory space), but they are the reality we live with. On an AVR Mega256, a general flash
pointer will 24 bits, while a ram pointer will be 16 bits. On some architectures you get "far" and "near" pointers.
There are some that have a section of bit-addressable memory, with a different pointer type. You can't rely on
these things having a consistent pointer size - you should not even rely on the C requirement of a "void *"
supporting them all (embedded compilers don't always follow the standards if they conflict with generating good
object code for real-world source code).

But as you say C doesn't map onto these architectures, so we're not
talking about standard C. Most programs won't compile for these
architectures (assuming the code would fit), and if they did still wouldn't
run correctly - precisely because these architectures do not support the
defacto assumptions that most C programs make.


C doesn't map directly onto these architectures - but that does not stop people using C for them on a regular basis!
Look at the AVR, for example - it has different address spaces for flash and ram data, so the pointers are
incompatible. On the larger AVRs, a full flash pointer must be 24 bits, while ram pointers are only 16 bits. Yet
somehow there are vast numbers of these devices in use, almost all of them programmed in C. You certainly can't take
a random C program off the internet and expect it to compile and work on an AVR, but there is no problem writing
portable code that works on the 8-bit AVR, 16-bit msp430, 32-bit big-endian ColdFire, 32-bit little-endian ARM, and
even some of these horrible DSP architectures with their 16-bit chars. You just have to stick to assumptions that are
appropriate for embedded programming, rather than thinking "big" C.

There are indeed architectures which can't fully support standard C. You
can indeed write standard conforming programs on these if you avoid
any of the default assumptions (casting pointers being one of them) and
use macros to map the differences (assuming you know all of them).

However this illustrates the difference between "it just works" and putting
a lot of effort into making it work. Most software isn't specifically written to
be portable, and yet porting is surprisingly easy. You can download random
source code from the internet and expect it to compile and work on ARM,
MIPS, PPC, ColdFire, AVR32 or any other modern embedded architecture
with minimal effort.

The reverse is true as well, programs written for these C dialects do not
work on standard C compilers. One case that comes up often is:
unsigned char c = 255; if (~c == 0) ... This is always false in standard C,
but there are compilers which think it is true...


If code like that comes up "often", you have bigger problems than just bad assumptions - that code snippet is
horrible. But it is certainly true that code written for small embedded C systems often has compiler/target specific
features that don't work on larger systems - code for a compiler with an extra "flash" keyword for flash-based data
will clearly not compile on anything else without some changes.

It was an example of what goes wrong when people try to port applications
when moving to a different architecture, and find that their code doesn't
work any more. Some 8-bit compilers apparently don't apply the standard
integer promotion rules, which makes porting of all but the most trivial
programs rather tricky.

Wilco


.



Relevant Pages

  • Re: C programming on ARM
    ... different memory spaces. ... But as you say C doesn't map onto these architectures, ... dialects do not work on standard C compilers. ...
    (comp.arch.embedded)
  • Re: If you were inventing CoBOL...
    ... >>COBOL74) precisely because what the RDIV operator did wasn't exactly the ... >>same as what the standard required. ... The pre-1974 COBOL compilers for the three major Burroughs architectures ...
    (comp.lang.cobol)
  • Re: Cant understand this! (Help required please)
    ... Yes and some online courses NETG does not seem to understand what ... > New to programming: ... I have heard of comeau don't they make compilers too? ... An argument I have used many times myself, I want to learn standard C++, ...
    (alt.comp.lang.learn.c-cpp)
  • Re: GCC
    ... The header names changed explicitly to ... The Standard says that the standard ... that did not put iostream into the std namespace. ... and compilers change along with them. ...
    (Debian-User)
  • Re: subroutine stack and C machine model
    ... Herb has found out that a+bis ... Peter: But the STANDARD says... ... Most compilers have left to right evaluation as it happens. ... evidence appear to be more than one piece of evidence. ...
    (comp.lang.c)

Loading