C program to print number from 1 to 10

Thread Starter

vead

Joined Nov 24, 2011
629
Hello everyone
I wrote this C program to understand basic concept. I have tested program. It print number 1 to 10
Code:
//Header file//
#include<stdio.h>

//main function where the program execution begins//
int main()
{
// local variable  //
    int number = 1;

  //for loop execution//
   for (number = 1; number <= 10; number++)
   {
      printf(" %d  \n", number);
    }
   return (0);
}
I am starting with very basic. Data memory or Ram is used to store data . The Data memory made of registers. It is made of address register and data register. Address register store address of registers and data registers store variables

Example: Name of register Is R1 and address of register is 000000001
Name of register Is R2 and address of register is 000000010
---------- ---------- ---------
Name of register Is R10 and address of register is 1000000000
Code:
Name of register           address of register
Address register R1:              0000000001
Address register R2:              0000000010
Address register R3:              0000000100
Address register R4:              0000001000
Address register R5:              0000010000
Address register R6:              0000100000
Address register R7:              0001000000
Address register R8:              0010000000
Address register R9:              0100000000
Address register R10:             1000000000
Here I am trying to explain that the compiler store data at memory address
Code:
Address register            decimal value                         Binary value
Address register R1:                1                            0000000001
Address register R2:                2                            0000000010
Address register R3:                3                            0000000100
Address register R4:                4                            0000001000
Address register R5:                5                            0000010000
Address register R6:                6                            0000100000
Address register R7:                7                            0001000000
Address register R8:                8                            0010000000
Address register R9:                9                            0100000000
Address register R10:              10                            1000000000

Does compiler automatically store value to memory registers? I think yes

Compiler automatically store value decimal number 1 to register R1
Compiler automatically store value decimal number 2 to register R2
Compiler automatically store value decimal number 3 to register R3
Compiler automatically store value decimal number 4 to register R4
Compiler automatically store value decimal number 5 to register R5
Compiler automatically store value decimal number 6 to register R6
Compiler automatically store value decimal number 7 to register R7
Compiler automatically store value decimal number 8 to register R8
Compiler automatically store value decimal number 9 to register R9
Compiler automatically store value decimal number 10 to register R10

//for loop execution//
for (number = 1; number <= 10; number++)
Here loop run 10 times , first it set to 1 than increase by 1 and stop at 10

Does compiler automatically store value to memory registers?
Which internal part of PC or Microcontrller execute for Loop ?
 
Last edited:

JohnInTX

Joined Jun 26, 2012
4,787
Does compiler automatically store value to memory registers? I think yes
Yes and No. In your example you declare a variable that you named 'number' . The compiler allocates space for that SINGLE variable in RAM - because it is a variable and can hold different values.

Your code, the 'for' loop, initializes the variable called 'number' to 1, prints it, then increments the same location in memory and prints the new value of that same location. It does that 10 times. It is important to understand that the compiler does not use 10 different locations in RAM for the 10 different values - it increments the value stored in the ONE location ('number') and prints that value over and over.

Which internal part of PC or Microcontrller execute for Loop ?
No particular internal part. The compiler parses the source code and generates assembler code that does that function. When the compilation of the source to assembly language is complete, it launches the assembler program that converts the compiler-generated assembly language to 'object' code which is the raw binary instructions that the processor itself understands - for small processors the object code is an image of the raw bits in the program memory. Object code is usually converted to something that the chip's programmer understands i.e. Intel HEX so that the chip can be programmed.

For a PIC the process is:
Compiler: Parses the C code and generates assembly language. Assembly language is the instruction set of the PIC that is described in the datasheet.
Assembler: Convert the assembly language to object code, the raw binary instructions that the PIC hardware understands. The raw binary is what is described in each instruction description in the datasheet. The output of the assembler is a bit for bit image of what will be programmed into the PIC memory for execution.
Since a bit for bit image is hard to manage when communicating with a programmer like the PICkit2/3 or PM3, it is further translated to a text-based format like Intel HEX. This format provides address information, checksums etc. and can be transmitted over a variety of links to a variety of programmers using standard serial comms links.

All of these things (and more) happen automatically when you click 'Build' in MPLAB.
 
Last edited:

MrChips

Joined Oct 2, 2009
30,806
Hello everyone
I wrote this C program to understand basic concept. I have tested program. It print number 1 to 10
Code:
//Header file//
#include<stdio.h>

//main function where the program execution begins//
int main()
{
// local variable  //
    int number = 1;

  //for loop execution//
   for (number = 1; number <= 10; number++)
   {
      printf(" %d  \n", number);
    }
   return (0);
}
I am starting with very basic. Data memory or Ram is used to store data . The Data memory made of registers. It is made of address register and data register. Address register store address of registers and data registers store variables
Example: Name of register Is R1 and address of register is 000000001
Name of register Is R2 and address of register is 000000010
---------- ---------- ---------
Name of register Is R10 and address of register is 1000000000
No.
The CPU (Central Processing Unit) is the heart and brain of the MCU (microcontroller unit). CPU contains registers. The number of registers in the CPU depends on the model and architecture of the CPU. The address of the register is usually unknown to the programmer and not required to be known by the programmer. This information is usually reserved for the microcoding of the CPU and is buried within the design of the CPU.

Code:
Name of register           address of register
Address register R1:              0000000001
Address register R2:              0000000010
Address register R3:              0000000100
Address register R4:              0000001000
Address register R5:              0000010000
Address register R6:              0000100000
Address register R7:              0001000000
Address register R8:              0010000000
Address register R9:              0100000000
Address register R10:             1000000000
Here I am trying to explain that the compiler store data at memory address
Code:
Address register            decimal value                         Binary value
Address register R1:                1                            0000000001
Address register R2:                2                            0000000010
Address register R3:                3                            0000000100
Address register R4:                4                            0000001000
Address register R5:                5                            0000010000
Address register R6:                6                            0000100000
Address register R7:                7                            0001000000
Address register R8:                8                            0010000000
Address register R9:                9                            0100000000
Address register R10:              10                            1000000000
No.
Assuming you are using binary representation,
0000000000 is the binary representation for zero
0000000001 is the binary representation for one
0000000010 is the binary representation for two
0000000100 is the binary representation for four
0000001000 is the binary representation for eight, etc.

Does compiler automatically store value to memory registers? I think yes
No.

Compiler automatically store value decimal number 1 to register R1
Compiler automatically store value decimal number 2 to register R2
Compiler automatically store value decimal number 3 to register R3
Compiler automatically store value decimal number 4 to register R4
Compiler automatically store value decimal number 5 to register R5
Compiler automatically store value decimal number 6 to register R6
Compiler automatically store value decimal number 7 to register R7
Compiler automatically store value decimal number 8 to register R8
Compiler automatically store value decimal number 9 to register R9
Compiler automatically store value decimal number 10 to register R10
No. Registers are very precious resources within the CPU. There are a limited number of registers in the CPU. Some CPUs have only 16 registers. Other CPUs have only 1 register. Values are stored in registers if it is convenient and efficient to do so. Normally values are stored in RAM.

//for loop execution//
for (number = 1; number <= 10; number++)
Here loop run 10 times , first it set to 1 than increase by 1 and stop at 10

Does compiler automatically store value to memory registers?
Which internal part of PC or Microcontrller execute for Loop ?
It depends on the model and architecture of the CPU. Some advanced CPUs have special mechanism for executing loops efficiently.
Most CPUs rely on a simple algorithm for loop execution such as:

Step 1. Initialize loop counter
Step 2. execute body of loop
Step 3. decrement loop counter
Step 4. test if loop counter is zero
Step 5. if loop counter is zero, go to Step 2, otherwise move to Step 6.
Step 6. continue with next code
 

Thread Starter

vead

Joined Nov 24, 2011
629
No. Registers are very precious resources within the CPU. There are a limited number of registers in the CPU. Some CPUs have only 16 registers. Other CPUs have only 1 register. Values are stored in registers if it is convenient and efficient to do so. Normally values are stored in RAM.
It depends on the model and architecture of the CPU. Some advanced CPUs have special mechanism for executing loops efficiently.
Most CPUs rely on a simple algorithm for loop execution such as:
Its quit difficult to understand I want to understand exactly how the various parts of the MCU work. so I am taking another small example that will print only 1234
(only 4 bit data)
Code:
#include<stdio.h>
int main()
{

    int number = 1;
   for (number = 1; number <= 4; number++)
   {
      printf(" %d  \n", number);
    }
   return (0);
I am explaining If I am wrong somewhere please correct me. I think mainly three part of MCU are working for above code. These are program memory , program counter and RAM or Data Memory

· Program memory address bus - 4 bits (PA0~3);
· Program memory data bus - 4 bits (PD0~3);
· Program memory read enable;

ROM to store program
Code:
PA0  PA1    PA2    PA3    PD0    PD1    PD2    PD3
0    0    0    0    x    x    x    X
0    0    0    1    X    x    x    x
0    0    1    0    x    x    x    x
0    0    1    1    X    x    x    x
0    1    0    0    x    x    x    x
0    1    0    1             
0    1    1    0             
0    1    1    1             
1    0    0    0             
1    0    0    1             
1    0    1    0             
1    0    1    1             
1    1    0    0             
1    1    0    1             
1    1    1    0             
1    1    1    1
Instruction size is 4 bit wide
Processor will read instruction one line at one time
At address 0000 first Instruction (select Ram to store variable)
At address 0001 second Instruction (store variable of content 1= 0001)
At address 0002 third Instruction (increment value by 1 so 2=0010)
At address 0003 fourth Instruction (increment value by 1 so 3=0100)
At address 0004 fifth Instruction (increment value by 1 so 4=1000)

Program counter
Code:
Decimal    PC0    PC1    PC2    PC3
0             
1             
2             
3             
4             
5             
6             
7             
8             
9             
10             
11             
12             
13             
14             
15

RAM address bus – 4 bits (RA0,3);
RAM Data In - 4 bits (RD0~3);
RAM Data Out
RAM read enable;
RAM write enable
RAM to store content of variable
Code:
Write enable    Read Enable    RA0    RA1    RA2    RA3    RD0    RD1    RD2    RD3
when variable store to ram and loop execute which and how does various parts of the MCU work?
 
Last edited:

Thread Starter

vead

Joined Nov 24, 2011
629
Step back and try to explain something more basic such as:
this is program to print reverse number from 10 to 1
Code:
#include <stdio.h>

int main()
{
    int n =10;              / deceleration of variable

    while(n>=1)
    {
        printf("%d \n", n);
        n--;
    }

    return 0;
}
here variable name is n and assign value with variable is 10
it will show the reverse value of numbers from 0 to 10 if the condition (n>=1) is true
print reverse 10 number if the condition (10>=1)
print reverse 9 number if the condition (9>=1)
print reverse 8 number if the condition (8>=1)
print reverse 7 number if the condition (7>=1)
print reverse 6 number if the condition (6>=1)
print reverse 5 number if the condition (5>=1)
print reverse 4 number if the condition (4>=1)
print reverse 3 number if the condition (3>=1)
print reverse 2 number if the condition (2>=1)
print reverse 9 number if the condition (1>=1)
don't print anything if the condition (0>=1)
I will try to understand first and If then there is problem. I will ask you?
 

Brian Griffin

Joined May 17, 2013
64
Some digital electronics textbook has a short section about computers, especially CPU and data bus in a rudimentary level.

Programming cannot be easily mastered by just visiting the forums. It takes years of experimenting, and a bit of frivolousness helps. What I mean on the "frivolousness" is, play around with the values, make a silly program, push the limits and such.

I used to study and practise on and off C++ as a teenager and when I reached college I sharpened them as much as possible.
 
Top