Fibonacci Sequence Program

This program will read user input to give the number of fibonacci sequence that want to be showed. by user Then, the program will show the Fibonacci Sequence. This program is consists of 4 files. If you don’t have known about compilation using makefile, see my other posting about Make Makefile File and C Compilation using GCC.
Make inputn.c
#include "fibo.h"
#include <stdio.h>
int main()
    int nf;
    printf("Fibonacci Sequence Program\n");
    printf("Number of Sequence (>=1) = "); scanf("%d",&nf);
    printf("Sequence = ");
    return 0;

Make fibo.c
#include "fibo.h"
#include <stdio.h>
int fibonacci(int m)
    int t=0;
    if          (m == 1) {return 1;}
    else if     (m == 2) {return 1;}
    else if (m >= 3) {t = fibonacci(m-1)+fibonacci(m-2); return t;};

void showfibonacci(int n)
    int i;
    for (i=1;i<=n;i++)
          if (i==1)
          {printf(", %d",fibonacci(i));};

File fibo.h
#ifndef FIBO_H
#define FIBO_H
int fibonacci(int m);
void showfibonacci(int n);

File makefile to make fiboprog.exe
fiboprog : inputn.o fibo.o
    gcc inputn.o fibo.o -o fiboprog

inputn.o : inputn.c
    gcc -c inputn.c

fibo.o : fibo.c
    gcc -c fibo.c

You can see the result below.

How to Make Makefile File for Compilation

Makefile is a file where we can type some instructions on it  to compile a program automatically. We just use “make” instruction in command prompt.
The basic makefile is composed of:

target: dependencies
[tab] system command

Target is the output that we want. Dependencies is the related file to make the output that we want. System command is the rule to produce the output. Don’t  forget to press tab on keyboard before you type the system command.
Let’s we learn from example. The steps are:
1.       Make a work directory for all of our code.
2.       Make the first source code and save as main_text.c in your work directory.
#include "text.h"
void main()
3.       Make the second source code and save as text.c in your work directory.
#include <stdio.h>
#include "text.h"
void test()
      printf("Arsitektur Komputer I");
4.       Make the third source and save as text.h in your work directory.
#ifndef TES_H
#define TES_H
void test();
5.       Make the fourth source and save as makefile in your work directory.
all: coba

coba: main2.o coba.o
     gcc main_text.o text.o -o coba

main2.o: main_text.c
     gcc -c main_text.c

coba.o: text.c
     gcc -c text.c

-          main2.o will execute gcc c main_text.c and the result is main_text.o
-          coba.o will execute gcc c text.c and the result is text.o
-          coba will execute gcc main_text.o text.o o coba and result is coba.exe
-          in this case, we learn that the name of target and dependencies is up to us. But we still should use the name of the result of the target in the system command, not the name of the target. We still type:
gcc main_text.o text.o o coba  
NOT gcc main2.o coba.o o coba

6.       Open command prompt, Enter your work directory and type "make", Enter, Then type "coba", Enter. The result is :

Now, you can use makefile file to compile and bulid a program just by type "make" in command prompt.

Optimization of Compilation

There are some optimization options of compilation that we can use.
1.       –O1
2.       –O2
3.       –O3, optimization in speed and being hoped to have small size too.
4.       –Os, optimization in size.
Ok, let we see by an example to see the difference among them.
1.       Make a code2.c
int main()
int a=2;
int b=3;
int c=4;
int sum (int x, int y, int z)
int t = x+y+z;
return t;
return 0;
2.       Using command prompt to compile and produce an object file. Add -Ox before -c. For example, $ gcc -O2 -c code2.c. (See my posting about how to compile program using GCC in command prompt)
3.       Disassemble the object file in command prompt. (See my posting about how to disassemble program)
4.       See the result on picture below.
compilation using O1 optimization option

compilation using O2 optimization option

compilation using O3 optimization option

compilation using Os optimization option
Now you can see the differance of compilation result with different optimization option. The optimization will affect the speed and size of the program.

Disassembler Instruction

You can disassemble object or executable file using command prompt. The instruction is (for example using file code.o  that I had made before) :
$ objdump –d code.o

You can also disassemble executable file. The instruction is :
$ objdump –d prog

As usual, disassembly process of executable file will result a very long code. It is caused by the library that have been linked by the program.
You can see that disassembler process result an assembly program and show the register that is used.

C Program Compilation using GCC

To use GCC  in Windows command prompt, you must make a path in environment variables to directory where gcc.exe is located. In this case, I use gcc.exe from Dev-Cpp. The gcc.exe file is located in Dev-Cpp\bin. There are many gcc.exe that you can use, for example from codeblocks, MinGW, and many other compilers.
The steps to make a path:
Control Panel -> System -> Advanced System Setting -> Environment Variables -> double click Path -> Fill the location. In this case, I use gcc from E:\Dev-Cpp\bin

 Contents of bin directory in Dev-Cpp:

Now, How to Compile a Program.
1.        You should make a source code using text editor. You can use this sample code, code.c.
#define START_VAL 0
int accum = STRAT_VAL;
int sum(int x, int y)
         int t = x+y;
         accum += t;
         return t;

Save code.c in a directory.
2.       Open command prompt.exe.
3.       In Comand Prompt, move to the directory where code.c is located.
4.       There are a few products that you can create from a compilation process.
-          Preprocess, it doesn’t create any files, it just show you a preprocess in command prompt.
$ gcc –E code.c

-          Compile for assembly file, it creates only code.s
$ gcc –S code.c
Note: -O2 is a optimization type, it is just an option

-          Compile for object file, it creates code.o
$ gcc –c code.c
Note: Object file is a binary file (not a text file) so you can’t see the code in text editor. Object file is a set of instruction of a program that have no link to library so computer still can’t to execute this program.

5.       To make executable file
-          Compile program for object file, like the last step before this.
-          Make a source code, for example main.c, and save it in the same directory where code.c is located. To make executable file the program should has main function.
int main()
         return 0;
-          Compile all code, for example to create prog.exe as executable file:
$ gcc –o prog code.c main.c

6.       If you want to make a program that use main.c as a main program and the program can use accum variable and sum() function from code.c, You must make some additional file and instruction.
-          Make code.h file :
#ifndef CODE_H
#define CODE_H

int accum;
int sum(int x, int y);


-          Include code.h in code.c :
#include code.h
#define START_VAL 0
int accum = STRAT_VAL;
int sum(int x, int y)
            int t = x+y;
            accum += t;
            return t;

-          For example, you make a simple program to show result of sum(4,5). The main.c program is:
#include "code.h"
#include <stdio.h>
int main()
printf("%d",sum (4,5));
return 0;
Compile all file to prog2.exe using: $ gcc –o prog2 code.c main.c 
after you run prog2 it results 9.
Ok, that's about C compilation steps, instructions, and results.

How to Use WinAVR and AVRStudio

 1.       Install WinAVR, check all option that can be installed

2.       Install AVRStudio

3.       After instalation, The first tool that you can use is Programmer’s Notepad. This program is in the directory WinAVR\pn. The executable filename is pn.exe.  You can use this program to create a .c file which will be compiled to hex, coff, etc .  The appearance likes the picture below.

-          Choose C/C++ type
-          On Text Clip, change it to WinAVR, double click application if you want to make program tamplate.
-          Then you can type your program. For example :

#include <avr/io.h>

void main(void) {
  DDRA = 0xff;  //set PORT A as output
  PORTA = 0b00000000;
  PORTA = 0b00000011;
  PORTA = 0b00001111;
  PORTA = 0b00111111;

-          After that, save your .c file.
Note: all WinAVR header files are in directory WinAVR\avr\include. There are many of header files that you can use.

4.       The second tools is MFile. You can open this program in Start -> All Programs -> WinAVR -> MFile [WinAVR]. This program is used to make some files such as file.hex, file.coff, and some others that we want from the file.c that we had made.  The appearance likes the picture below.

 The steps to use:
-          Choose Makefile -> Main file name. Type the name of your file without the extension. For example, if your file is exercise1.c  just  type exercise1 on main file name.
-          Choose Makefile -> MCU type. Choose type of your AVR microcontroller.
-          Ouput format = ihex
-          Choose debug format that suitable with your AVRStudio series.
-          Choose programmer that you use.
-          After you complete all changes, save the makefile file in the same directory where yourfile.c is  saved.
Note: Don’t forget to change the F_CPU by type it manually on the makefile. Scroll the makefile file and find it. Choose makefile -> enable editing, so you can edit the source code of makefile file.

5.       Open again the programmer’s notepad. In Tools tab, there are some instructions:
-          “make all” = yourfile.c  will be compiled to some type of file.
-          “make clean” = delete all product files of compiled .c file.
-          “make program” = used to download  hex file to chip, but it need set some configurations.
You can add some other output files not only .hex, .elf, .coff, etc.
For example, you want make .extcoff file after yourfile.c being compiled. The step:
-          Choose Tool -> Option -> Tools -> Add
-          Fill it
name            : [WinAVR] make extcoff
command    : make.exe
folder            : %d
parameter  : extcoff

After all the process, now, choose Tool  -> Make all

6.       Open AVR Studio -> Choose Open -> Find yourfile.coff or yourfile.elf. -> Save -> Choose AVR Simulator -> Choose your AVR type.

Then you will see :

After debug was started, you can choose PORTA and see the change in microcontroller step by step by press F11.

Note: You see that the change of bit in PORTA occurs in the second step of looping. AVRstudio can give us the real step and step by step. We can learn more about process in microcontroller because of this.

Ok, That is all about basic of using WinAVR and AVRStudio.

by  Luki,