Does an 8-bit processor have a problem?

If I have an int32 integer in 8-bit processor memory, say 8051, how can I determine the endianess of this integer? Is it compiler specific? I think this is important when sending multibyte data over serial lines, etc.

+4
source share
7 answers

With an 8-bit microcontroller that does not have built-in support for wider integers, the finiteness of the integers stored in memory really depends on the author of the compiler.

The SDCC compiler, widely used on 8051, stores integers in little-endian format (the user manual for this compiler claims that it is more efficient for this architecture due to the presence of a command to increase the data pointer but not for decrement).

+7
source

If the processor has any operations that act on multibyte values ​​or have multibyte registers, it has the ability to have an endpoint.

http://69.41.174.64/forum/printable.phtml?id=14233&thread=14207 suggests that 8051 mixes different consistency in different places.

+5
source

Concreteness relates to CPU architecture. Since the compiler must target a specific processor, the compiler will also be aware of this. Therefore, if you need to send data over a serial connection, network, etc., you can use the built-in functions to place data in a network byte order, especially if your code must support several architectures.

For more information see http://www.gnu.org/s/libc/manual/html_node/Byte-Order.html

+3
source

This is not only up to the compiler - '51 has some own 16-bit registers (DPTR, PC in standard, ADC_IN, DAC_OUT and such in variants) of a given consistency, which the compiler must obey - but outside this, the compiler can use any orientation that it prefers or select in the project configuration ...

+3
source

An integer has no idea in it. You cannot determine if it is just looking at bytes, big or small. You just need to know: for example, if your 8-bit processor is a little oriented, and you get a message that is known to be a big endian (because, for example, the fieldbus system defines a large end), you need to convert values ​​over 8 bits . You will need either hard code or the definition of some definition in the system by which the bytes will be replaced.

Note that replacing bytes is easy. You may also need to swap bits in bit fields, since the order of the bits in the bit fields is compiler specific. Again, you basically should know this at build time.

+2
source
unsigned long int x = 1; unsigned char *px = (unsigned char *) &x; *px == 0 ? "big endian" : "little endian" 

If x is assigned a value of 1, then a value of 1 will have the least significant byte. If we then point to x as a pointer to bytes, the pointer will point to the smallest memory location x. If this memory location is 0, it is a large endiant, otherwise it is of little importance.

+1
source
 #include <stdio.h> union foo { int as_int; char as_bytes[sizeof(int)]; }; int main() { union foo data; int i; for (i = 0; i < sizeof(int); ++i) { data.as_bytes[i] = 1 + i; } printf ("%0x\n", data.as_int); return 0; } 

The interpretation of the conclusion is up to you.

0
source

Source: https://habr.com/ru/post/1304567/


All Articles