I am sharing my works with the readers to receive comments and criticisms to correct and improve my understandings.
1. byte y1 = 0x23; byte y1 = 0x35;
is a C++ statement. A statement is a complete prescription for the computer to take an action. The result of the execution of this statement is to store the number 0x35 (as 00110101) in a free RAM location (say: 0x0120) of ATmega328P MCU. The action is conceptually presented in Fig-1.
Figure-1:
2. I know that the Compiler converts the C++ codes into Assembly Instructions and then into Machine/Binary Codes.
3. What is an Instruction?
It is a low-level command along with data/operand for the MCU to carry out a rudimentary task. For example: ldi r16, 0x35;
is a data movement instruction; where, the MCU reads 0x35 from (flash) memory and keeps it in r16 register for future use.
The ldi
(load immediate) is called operation code (opcode) and "r16, 0x35
" is called operand of which r16
is the "data destination" and 0x35 is the "data source" (in this example, the data ltself).
4. With the understanding of Step-2, I may write the following AVR Assembly Instructions for the statement: byte y1 = 0x35
.
ldi r16, 0x35; //immediate data 0x35 goes into r16 register
ldi r31, 0x01; //high byte of target address is kept into r31 reg.
ldi r30, 0x20; //low byte of target address is stored into r30 reg.
//r31:r30 forms z-pointer register. (r16) goes in RAM location whose address is in z-pointer register
st z, r16; // now (0x0120) = 0x35 means location 0x0120 holds 0x35
5. Let me use Microchip Studio Platform to get the binary codes by creating list file for Assembly Codes of Step-4.
.org $0000 ;
000000 0000 RESET: nop
000001 c03e rjmp START
.org $0040 ;application space
000040 ef0f START: ldi r16, 0xFF ; stack top at 0x08FF
000041 bf0d out spl, r16
000042 e008 ldi r16, 0x08 ;
000043 bf0e out sph, r16 ; ATmega328P
000044 e305 ldi r16, 0x35 ;data for RAM location 0x0120
000045 e0f1 ldi r31, 0x01;
000046 e2e0 ldi r30, 0x20;
000047 8300 st z, r16
.exit
6. Execution model (Fig-2) of Assembly Program of Step-5 in Flash memory.
Figure-2:
(1) At power up, the PC (Program Counter) of ATmega328P (fresh chip and NOT of UNO Board) holds 0x0000 and fetches the instruction word C03E. It is decoded to get the offset to compute the targer address 0x0040 which is then stored into PC.
Question: Opcode/Instruction fetching, decoding and reaching at the target location -- all these events just happen within 1-cycle period (for 16 Mz clock it is 62.5 ns)?
(2) At word location 0x0044 of the list file of Step-5, the instructon digits are arranged as E305 (as per instruction template) and not as E035 (8085 format: MVI A, 0x35 ===> 3E 35). Why are the digits transposed in the AVR instruction?