-1
\$\begingroup\$

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?

\$\endgroup\$
9
  • 2
    \$\begingroup\$ Just read the code for the AVR-GCC, I suppose. That will tell you exactly the steps involved in that compiler system. Why do you need to know? \$\endgroup\$ Jun 8 at 1:51
  • 1
    \$\begingroup\$ @Creator A good textbook on processor architecture and design will go over the steps involved in memory access. Or are you talking about compiler design? ...or do you really just want to understand AVR assembly and don't know the words to ask for that? \$\endgroup\$
    – Hearth
    Jun 8 at 2:26
  • 1
    \$\begingroup\$ @Creator The shortest cycle would be to directly generate machine code in the compiler without an intermediate machine model. In this case, [File I/O] --(characters)--> [Lex/tokenizer] --(tokens)--> [parser] --(basic blocks/DAGs)--> [various optimizations, register assignments, etc] --(improved basic blocks/DAGs)--> [Code Generator] --(binary)--> [File I/O]. The optimizations step could readily be skipped. Nicklaus Wirth's book Algorithms + Data Structures = Programs provides a complete example for PL/0, start to finish, in just a couple of chapters. If this isn't homework then a job appl.? \$\endgroup\$ Jun 8 at 2:29
  • 4
    \$\begingroup\$ Is it the compiler, the machine code or the processor? Which one is the question about? \$\endgroup\$
    – user253751
    Jun 8 at 2:35
  • 1
    \$\begingroup\$ @Creator please edit your question: what you ask about in the question (compiler) contradicts what you ask about in the title. Note that what you ask in the question is "what is a compiler" (not: complier) and both off-topic here and easily researchable. \$\endgroup\$ Jun 8 at 8:39

1 Answer 1

3
\$\begingroup\$

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.

\$\endgroup\$
7
  • \$\begingroup\$ The examiner told that above explanation for processor is for char type variable not int type. Would you please help? \$\endgroup\$
    – Creator
    Jun 9 at 5:41
  • \$\begingroup\$ @Creator 8-bit processors only have char type variables. That's why they are called 8-bit processors. The rest are hallucinated by the compiler. \$\endgroup\$
    – user253751
    Jun 9 at 7:41
  • \$\begingroup\$ Compiler only provides size information to the processor not type information of a variable. Therefore 8 bit processor cannot treat char or int differently. Is this the correct logic? I will appreciate a comment. \$\endgroup\$
    – Creator
    Jun 9 at 20:09
  • \$\begingroup\$ @Creator You write, "He is after this question for nearly a month, first the question was related to compiler so i said all compiler may be different now the question changed to the present one." This suggests what you placed in your question is all you have to work from. So then: "What are the steps an 8-bit processor must perform to read/write an integer variable in memory?" The question sounds more like a hardware question, than for a C compiler. But if C, then the specific mechanism would depend upon where that variable is located: static lifetime, stack, heap, register, etc. \$\endgroup\$ Jun 10 at 6:29
  • \$\begingroup\$ @Creator Since you have been at this for a month already, just get Fraser and Hanson's "A Retargetable C Compiler: Design and Implementation". The authors did a remarkable job. Just skim through it. You'll find answers readily available there. And yes, what they write can be readily applied to 8-bit processors. \$\endgroup\$ Jun 10 at 6:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.