I’m alive and I have an excuse for no updates! I’ve spent a holiday in Egypt (amazing country), but now I’m back and going to keep updating here.
Well, I love learning. Especially learning new things in computing, programming and game development, so recently I’ve been learning some assembly language. After a good week of learning I’ve managed to make some cool x86 apps.
I’ve been doing x86 from the comfort of FASM (a great assembler) and the msvcrt.dll linked library which I’m using for simple calls to printf, scanf and puts 🙂
The first was a recursive Fibonacci converter; basically the way I did this was to create a base label/function of fib(x), which inside will keep returning (eax) the value of fib(x-1)+fib(x-2) until x is smaller than 3 (in which case it will return 1). Alright, it sounds simple from a high level perspective. However this was a great exercise for someone like me (new to asm) as I had to deal with the stack, all the standard registers, and some arithmetic aswell.
Last time, we learnt how to make a project and print some text onto the screen. In this tutorial, we are going to learn about variables.
Like in maths, a variable is a value that can change. We use these all the time in programming to store numbers and data in our programs.
For example, say we were making a game, and we wanted to store how many points the player has.
We would make a variable, lets call it ‘x‘.
At the start of the game, the player would have no points. So we would set x to 0
x = 0
When a player shoots an enemy, we would want him to gain a point. For this we would have to increase x by one.
x = x + 1
Hopefully this explains the importance and use of variables to you.
In C, we have to declare the variable by name to tell the computer it’s there. We declare a variable like so;
Where type is the type of the variable stored. Common types are int (integer number), float (floating point, can store up to 7 significant figures), char (a character) and double (a float with twice the precision).
The name is what we want to call the variable. In maths, we commonly name a variable ‘x’, so let’s use that as an example.
This is how you would declare a integer called x
Now we have a space in memory, named x, which can hold an integer number value.
Now, to set x to a value, we use the ‘=’ assignment operator. If we wanted x to contain 5, we would do this;
x = 5;
To write an integer onto the screen, we use printf like so;
Where %i says we’re printing an integer, and x is the name of the variable we’re printing.
Consider the following program;
alpha = 3;
beta = 2;
gamma = alpha + beta;
Can you guess what is printed onto the screen? The answer is 5. Let’s go over this code in more detail.
At the beginning of our main function, we declare 3 variables
Their names are alpha, beta, and gamma, and they are all integers. This declaration allows them to be used to hold integer numbers in our program.
Next, we assign numbers to alpha and beta.
alpha = 3;
beta = 2;
This puts the number 3 into alpha, and the number 2 into beta. Now alpha holds the number 3 in memory, and beta holds the number 2.
Our next line does something more interesting. We now use the + operator to assign a value to gamma.
gamma = alpha + beta;
This line tells gamma to hold the value held by alpha, ADDED TO the value held by beta.
In our program, alpha holds 2 and beta holds 3. 2 + 3 = 5, so now gamma holds the number 5.
This prints the value held by gamma onto the screen. Gamma is holding 5, therefore, our program prints the number 5 onto the screen. You can experiment with this to try subtracting, multiplying, dividing or combinations of all 4 using the +, -, * and / operators.
Once you are confident with the concepts in this tutorial, the next tutorial will go into more depth about variables.
Hello, and welcome to my first tutorial. In this tutorial, I will help you set up a programming environment, and guide you through to writing and running your first C program.
The program we will be using to write and compile our programs in this tutorial is called Code::Blocks. It’s a very lightweight, easy to use IDE ( Integrated Development Environment ) and will be perfect to set you on your way into C programming.
Step 1: Installing & Setting Up the Program
First, you will need to download the Code::Blocks IDE. Follow this link to download the installer;
When you have downloaded the installer, start it up and follow the steps. I recommend you install it to C:/Codeblocks, rather than in Program Files, because this makes life easier when you become better at programming. However it is totally your choice.
Follow the install through until you’ve succesfully installed the program. Now that it is installed, open it up and you should be presented with a screen that looks something like this;
Click “GNU GCC Compiler” (usually right at the top of the list) , and click Set as Default. Be warned that you do not click any of the other GCC compilers (such as GNU GCC Compiler for MSP430).
Now that you’ve set a default compiler, let me tell you what a compiler does.
When we write programming code, all we’re doing is writing into a text file. As it is, the code cannot be run by our computers. This is why we use a Compiler, which takes our code, and turns it into an executable (.exe) file, which our computer can run.
Step 2: Creating a New Project
Now that you should be able to run Codeblocks, you are ready to start a new project.
When we work with C, we use ‘projects’ to manage our files. Each project makes a program from the code in the project.
Because we want to make a new program today, we’re going to click Create New Project.
Click on New Project in the Codeblocks start screen.
Now we are presented with a choice of project templates to help us get started.
However we only want to make a basic starting program. So we choose Empty Project, and click Go!
After clicking Go, we are presented with a setup guide. Click Next and you will be asked to enter a project name and directory.
For the directory, choose somewhere on your computer where you want to save your programming projects. I’ve use My Documents / Projects as a folder to store all my projects.
Next we enter a name for the project. For mine, I’ve called it ‘First’. You can choose whatever name you want.
(You do not have to change Resulting Filename and Project Filename fields, so leave those as they are.)
Now click next and make sure your screen looks like this;
Now click Finish and you will have created your first Codeblocks Project!
Now we’re going to add a source file for our project. This will contain our code. To do this, we first click File -> New -> Empty File.
Then Codeblocks will ask us if we want to add the file to our project. Click YES.
Now save the file as “main.c” . It is important you give it this name, so that the compiler knows this is the main code.
Make sure we are adding the file to the right compile targets and click OK.
Now we finally get to write some C!
The first thing we are going to write in our new file is
This first line of code is basically saying “Allow us to use the Standard Input/Output methods.”
This allows us to write to the screen, and also allow the user to type values into our program.
Our next line of code will be
This signals the start of our program (our ‘main’ procedure). The code after this will be what the computer runs when we start our program.
The int stands for integer. You do not need to know much about this yet, but it mainly allows us to give error numbers if something goes wrong.
In this program, we will write the words “Hello, World!” onto the screen. To do this we use “printf”. This is our next line of code
‘printf’ is what we call a ‘function’ which prints a string onto the screen. A string is basically text – a string of seperate characters (letters) next to each other. What printf does is basically put a string (some text) onto our screen.
After writing onto the screen, we want to end our program. We now write the line
This gives the error code 0. 0 means there have been no errors during our program. This basically tells the computer that the program has finished without any problems.
Our final line will contain
This closes our “main” function. This tells the computer to stop running our code here.
Now your code is complete! Your final code should look like this;
When you’re done, press F9 to compile and run your program!
You should be presented with the following screen;
Congratulations! You’ve created your first C program.
I hope this leads you on a long, fun, and interesting journey in C programming.
Hello, my name is Ryan Pridgeon. I am 17 years old as of writing, and a keen game designer, programmer, musician and skateboarder.
I’ve been developing games since I was 8 years old, using tools such as Flash and eventually working up to using C and C++, where I normally use the SDL library with OpenGL. I’m also competent in VB.net, PHP, C#, and I’m currently learning x86-64 assembly aswell as some Java.
This is a new blog I’ve made to hopefully share some knowledge and interesting things with you. I plan on having some useful references, tutorials, programming exercises and maybe showcase some of my own work.
The programming will be mostly C/C++ and Actionscript 3 as these are my favourite languages. Hopefully you will find this blog interesting.