Re: C language on 8-bit microcontrollers



On Jul 31, 3:32 am, "Mark" <mark_cruzNOTFORS...@xxxxxxxxxxx> wrote:
It's clear that for 8-bit MCUs the native word length is 8 bits. But we
can't represent 'int' data in 8 bits, as it contradicts the standard (the
int type must contain at least 16 bits to hold the range of values). Does it
mean, compilers used on 8-bit processors are not standard compliant, or they
provide some tricks to overcome this and make illusion of 16-bit ints?

Thanks in advance.

--
Mark

It's no trick. The compiler must generate code that does the
arithmetic in accordance with the C standard. The case of 16-bit (or
more) arithmetic just requires more than one instruction per
operattion. For example most processors have an "add with carry"
instruction. This accounts for the carry of the previous operation.
So adding 16-bit quantities A and B will be something like

MOV lo8(A) to R1
ADD lo8(B) to R1
MOV hi8(A) to R2
ADC hi8(B) to R2

Now the register pair R2|R1 contains the 16-bit result. There are
other solutions that don't even need the ADC instruction.

Of course all the other arithmetic ops can be implemented with
appropriate sequences like this. When the sequence is long (as in the
case of floating point ops), the code is often written as a subroutine
in the runtime system, and the compiler generates a call to the
routine, which is comparatively short.

.



Relevant Pages

  • Re: pointers?
    ... int foo ... If the compiler takes the effort to actually recognize that this is ... addresses do not work like in a flat address space. ... "contiguous region" in the standard document. ...
    (comp.lang.forth)
  • Re: Size of an arraylist in bytes
    ... It will be undefined behaviour, ... I think one shall not try to understand the C standard, ... They allow the compiler to generate the ... volatile int a; ...
    (comp.lang.c)
  • Re: Unknown function
    ... int main ... message) "the compiler actually dos not "know" the signature of malloc()", I understand it in the way that malloc is not known to the compiler at that specific point of program. ... What the standard mandates that the compiler must do and what the compiler can do in addition to what the standard mandates. ...
    (comp.lang.c)
  • Using restricted pointers with newly allocated arrays/structures
    ... return (int) x; ... The use of "restrict" here is intended to inform the compiler that x ... However, according to 6.7.3.1p4 of the standard, the code seems to ... based on restricted pointer x, is used to access the object x(and ...
    (comp.std.c)
  • Re: Memory allocation for an initialized character pointer;
    ... But this would be the kind of extension that a compiler wouldn't be ... explicitly mentioned in the standard, a compiler can now accept it in ... Since no strictly conforming program can use ... normally the int value returned from main, ...
    (comp.lang.c)