What are the steps an 8-bit processor must perform to read/write an integer variable in memory?
Consider I have written C code for ATmega128.
What exactly are the steps involved for the compiler to get the machine code?
Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. It only takes a minute to sign up.
Sign up to join this communityWhat are the steps an 8-bit processor must perform to read/write an integer variable in memory?
Consider I have written C code for ATmega128.
What exactly are the steps involved for the compiler to get the machine code?
The compiler writes a single instruction of assembly code or machine code, to store (some value) at (some address). Here's a sample of the instructions it can use in AVR (ATMega and ATTiny) and their explanations:
// Load 1 byte from data memory address stored in register X.
// Put answer in register r1.
LD r1, X
// Load 1 byte from data memory address 1234 (hexadecimal)
// in the current data segment. Put answer in register r1.
LDS r1, $1234
// Load 1 byte from program memory address stored in register Z.
// Put answer in register r1.
LPM r1, Z
// Store 1 byte to data memory address stored in register Y.
// The byte stored is the byte in register r6.
ST Y, r6
// Store 1 byte to data memory address $1234 in the current data segment.
// The byte stored is the byte in register r7.
STS $4321, r7
The instructions available on the ATMega are probably simpler ones. Most processors don't actually let you use addresses like (register)+(number) - x86 is the exception. And of course an 8 bit processor only does 1 byte at a time.
Note that we prefer to explain things in assembly code because it's a lot easier to read, but this is just a different way of writing machine code.
When the processor runs this instruction, it finds what the address is (e.g. in a register or the second half of the instruction), puts that on the address bus, sends the read memory signal, waits a clock cycle, gets the data from the data bus and stores that in a register. That's a read. For a write, it puts the address and data on the address and data busses, sends the write memory signal, and waits a clock cycle. Some processors in some computers it is made to wait more than one clock cycle, depending on the type of processor and memory.
8-bit processors do not have 16-bit variables - even the 16-bit memory addresses are processed in 2 8-bit halves. A 16-bit variable is actually two 8-bit variables and a 32-bit variable is actually four 8-bit variables. They are loaded and stored separately. The compiler has to make them work together - e.g. add 1 to the bottom 8 bits, then check if it wrapped around, and if it wrapped around, add 1 to the top 8 bits. At least they do provide instructions like "add with carry" to make this code shorter.