1303 views|7 replies

970

Posts

0

Resources
The OP
 

I would like to ask, regarding the byte alignment issue, should the variable with more bytes be defined first and the variable with less bytes be defined later? [Copy link]

邀请:@maychang   @damiaa   @jimtien   @vincentc543   参与回复

I would like to ask, regarding the byte alignment issue, should the variable with more bytes be defined first and the variable with less bytes be defined later?

This post is from stm32/stm8

Latest reply

Put it together artificially and put it together if you can.   Details Published on 2022-11-28 09:26
 

2w

Posts

0

Resources
2
 

"The byte alignment problem is that variables with more bytes are defined first, and variables with fewer bytes are defined later?"

Byte alignment, isn't that what it means? There is no such thing as "variables with more bytes are defined first, and variables with fewer bytes are defined later".

This post is from stm32/stm8
 
 

2w

Posts

0

Resources
3
 

The program may define several one-byte variables, several two-byte variables, several four-byte variables... The compiler simply assigns the first address to these variables of different lengths in the order of definition, and does not distinguish which is at the low address and which is at the high address.

This post is from stm32/stm8
 
 

6788

Posts

2

Resources
4
 

It should not be designed like this. It depends on how many bytes your system is aligned to. If it is 4-byte aligned, then you should try to put some variables smaller than four bytes together to form 4 of them.

For example, the number of bytes in a variable is 1 1 2 2 4 4

So don't design it like 1 4 1 4 2 2

You can understand it yourself first.

This post is from stm32/stm8

Comments

Try to keep small byte variables together. 1+1+2 equals 4, so these variables are just 4-byte aligned. Or you can force 1, 2, 4, etc. byte alignment. You can search for this and study it.  Details Published on 2022-11-25 10:56
 
 
 

6788

Posts

2

Resources
5
 
wangerxian posted on 2022-11-25 10:54 It should not be designed like this. It depends on how many bytes your system is aligned. If it is 4-byte aligned, then you should try to make some variables smaller than four bytes...

Try to keep small-byte variables together. 1+1+2 equals 4, so these variables are just 4-byte aligned.

Or you can force byte alignment of 1, 2, 4, etc. You can search and study it~

This post is from stm32/stm8
 
 
 

6069

Posts

4

Resources
6
 
This post was last edited by damiaa on 2022-11-25 11:04

Two things: byte alignment and endianness

Byte alignment: Bytes are arranged in space according to certain rules. Benefits of byte alignment: Improve efficiency and save space.

For example, a 32-bit processor bus accesses memory. Each cycle starts from an even address to access 32-bit memory data, and the memory data is stored in bytes. If a 32-bit data is not stored at a memory address that is divisible by 4 bytes, the processor needs 2 bus cycles to access it, which reduces efficiency.

In order for the CPU to access data quickly, the starting address of the data should have an "aligned" feature. For example, the starting address of 4-byte data should be located on a 4-byte boundary, that is, the starting address can be divided by 4.

Byte alignment can save storage space.

Endian mode definition.

Little-Endian means that the low-order bytes are arranged at the low-address end of the memory, and the high-order bytes are arranged at the high-address end of the memory.

Big-Endian means that the high-order bytes are arranged at the low-address end of the memory, and the low-order bytes are arranged at the high-address end of the memory.

STM32: Little-endian mode
STM8: Big-endian
KEIL C51: Big-endian
x86: Little-endian
ARM can work in both big-endian mode and little-endian mode

This post is from stm32/stm8

Comments

I thought all microcontrollers counted from right to left (big endian)  Details Published on 2022-11-25 17:36
 
 
 

224

Posts

0

Resources
7
 
damiaa posted on 2022-11-25 11:01 Two things: byte alignment and big and small endianness Byte alignment problem: bytes are arranged in space according to certain rules, which is byte alignment. Word...

I thought all microcontrollers counted from right to left (big endian)

This post is from stm32/stm8
 
 
 

7462

Posts

2

Resources
8
 

Put it together artificially and put it together if you can.

This post is from stm32/stm8
 
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list