### Memory alignment requirement for variables

• Transfering data between CPU and the memory

• Recall that:

• The CPU and memory are connected by a system of wires called the system bus:

The system bus consists of

 Address bus that transfers the address request from CPU to the memory Data bus that transfers the data bits (value) between CPU and the memory Control bus that transfers the command request from CPU to the memory

• Data bus:

• Modern computer has a data bus that have 32 wires - each wire can transfer 1 bit of data:

 Such a computer can transfer 4 bytes (32 = 4 × 8 bits) on the data bus in one transfer

• More recently (2015), computers have 64 bit databus:

 A 64 bit computer can transfer 8 bytes (64 = 8 × 8 bits) on the data bus in one transfer

• Detailed connection between memory and data bus

• I will use a 32 bit data bus to illustrate the idead

• The databus of a 32 bit computer consist of 32 wires:

A 32 bit databus can tranfer 4 bytes simultaneously !

• The memory bytes are arranged as follows:

• The first 4 bytes are transfered as follows:

• The second group of 4 bytes are transfered as follows:

And so on !!!

• Important consequence:

• Due to the wiring of the memory to the databus:

 Some groups of 4 bytes requires 2 memory transfers !!!

Example:

• To transfer this group of 4 bytes:

we must perform 2 transfers:

• Transfer 1:

• Transfer 2:

• Memory alignment requirement to make transfer efficient

• To improve data transfer efficiency, computers impose a data alignment requirement of how you must store the variables in memory.

• Alignment requirement for 32 bit computers:

 A byte typed variable can be stored anywhere in memory A short (= 2 bytes) typed variable must be stored starting at an address that is divisible by 2 A int (= 4 bytes) typed variable must be stored starting at an address that is divisible by 4 A long (= 8 bytes) typed variable must be stored starting at an address that is divisible by 4 A float (= 4 bytes) typed variable must be stored starting at an address that is divisible by 4 A double (= 8 bytes) typed variable must be stored starting at an address that is divisible by 4

(When you comply with the alignment requirements, the computer can use 1 transfer to obtain byte, short and int variables !!!)

(A 32 bit computer must use 2 transfers to obtain a long or double variable from memory)

• Alignment requirement for 64 bit computers:

 A byte typed variable can be stored anywhere in memory A short (= 2 bytes) typed variable must be stored starting at an address that is divisible by 2 A int (= 4 bytes) typed variable must be stored starting at an address that is divisible by 4 A long (= 8 bytes) typed variable must be stored starting at an address that is divisible by 8 A float (= 4 bytes) typed variable must be stored starting at an address that is divisible by 4 A double (= 8 bytes) typed variable must be stored starting at an address that is divisible by 8

(When you comply with the alignment requirements, a 64 bit computer can use 1 transfer to obtain any type of variable !!!)

• Memory alignment of M68000

• M68000 is a 16 bit computer and has the following memory alignment requirement:

 A byte typed variable can be stored anywhere in memory A short (= 2 bytes) typed variable must be stored starting at an address that is divisible by 2 A int (= 4 bytes) typed variable must be stored starting at an address that is divisible by 2 We don't use long typed variables in this course We don't use float typed variables in this course We don't use double typed variables in this course

• Even: assembler directive used to enforce the alignment requirement

• Syntax:

 ``` even ```

Effect:

• If the current unreserved memory cell has an even memory address, the even directive is ignored (= does nothing)

• If the current unreserved memory cell has an odd memory address, the even directive will:

 reserve the next memory cell

so that the the current unreserved memory cell will have an even memory address,

• How to use the even directive to define short and int typed variables:

• To define a short typed variable (named A), use:

 ``` even A: ds.b 2 // Reserve 2 bytes that // start at an even address ! ```

• To define an int typed variable (named A), use:

 ``` even A: ds.b 4 // Reserve 4 bytes that // start at an even address ! ```