Skip to content

Latest commit

 

History

History
64 lines (36 loc) · 4.63 KB

part-14-hello-world.md

File metadata and controls

64 lines (36 loc) · 4.63 KB

Part 14 - Hello World

For a complete table of contents of all the lessons please click below as it will give you a brief of each lesson in addition to the topics it will cover. https://github.com/mytechnotalent/Reverse-Engineering-Tutorial

Today we begin our journey into the world of C++ and gaining a better understanding of how C++ interacts with our ARM processor.

The prior lessons in this series focus on the basics of the ARM processor and touch upon its architecture and how everything ultimately translates down to Assembly Language and then ultimately opcodes into machine language.

We start with our first program in C++ which is our “Hello World” program. Let’s dive in and break each line down step-by-step and see how this language works. We will call this example1.cpp and save it to our device.

#include <iostream>
 
int main(void) {
    std::cout << “Hello World” std::endl;
 
    return 0;
}

To compile this we simply type:

g++ example1.cpp -o example1

We simply then type:

./example1

SUCCESS! We see “Hello World” printed to the standard output or terminal!

Lets break it down line by line:

__#include <iostream> __is referred to as a preprocessor statement. These preprocessor statements happen just before the compilation of the rest of the code. The #include keyword will find a file called iostream and take all of the contents of that file and paste it into the existing code we just created. These files are also called header files. 

We call iostream because we need a declaration for a function called cout and endl. The cout function allows us to print text to the standard output or terminal and the endl function creates a new line after the text has been displayed.

The main section which is of type integer is the entry point into the main application or binary. You will notice a void inside the () which indicates that it does not have any parameters which will be passed into the function.

The __std __indicates a namespace which is quite simply a mechanism to organize code into logical groups in order to prevent name collisions when you are dealing with multiple libraries.

You will see many examples where they declare a using namespace std; however I will NEVER utilize this approach as it can cause naming collisions in more complex applications.

The << operator is referred to as an overloaded operator. They are essentially a function very similar to printf in the C language. We are simply moving the __“Hello World” __string into the cout function through the use of the << overloaded operator. We then push the endl which creates a new line to the console.

The final line is the return 0. Since our main function is of type int, we have to return something. In C++ 11 there is no need for this in the main function however is required for every other function. I will stick to tradition and simply include it.

The next stage is that we compile the file. The first thing that occurs is the entire contents of the iostream header goes into the source file as we discussed. The compile process is where the C++ code gets translated into machine code. The next stage of compilation occurs when the rest of the lines of our existing code are parsed through. Essentially we have all of the contents of iostream into a new file and then all of the contents of our existing file added to a single file.

Compiling takes our text file the cpp file and converts it into an intermediate format called an obj file. An abstract syntax tree is created which is a conversion of constant data, variables and instructions.

Once the tree is created the code is generated. This means we now have machine code that our ARM CPU will execute. Every cpp file (translation units) which will have its own respective obj file associated with it. 

Linking takes our obj files, our compiled files, in addition to the C++ Standard Library and finds where each symbol and function is and link them all together into one executable.   

The concepts above may appear a bit confusing if you are new to programming however as you code and compile and later debug and hack in Assembly Language it will all become very clear and you will learn to master the processor.

Next week we will dive into Debugging Hello World.