Friday, 29 November 2013

ARM Assembly Part 1 - An Introduction

0 comments Posted by srini0x00 on 12:52
We have seen a very basic C Program in the previous article to get a brief understanding of how a processor processes and executes the code written in a High Level language like C. If you have missed it, you can refer it from here.  Now let’s see a brief introduction to Assembly Language and ARM Processors.

Assembly Language

Assembly is a low level programming language with which we can directly communicate with the Processor.
Our Processor can understand only 0s and 1s (binary) where, it is very difficult for a human being to understand binary.

Assembly Language acts as a bridge between the human and Processor. Though, there are many advantages with Assembly,  it is machine dependent. As it is very specific to Hardware Processor family, Programs written for one computer might not run in another computer with different hardware configuration.
It means, A Program written for a machine working on Intel processor might not work with a machine running on ARM processor. Hence, Different processors need different assembly language.

Example Processor families:
Intel
ARM
MIPS

This series focuses only on ARM Assembly language.

What is ARM?

ARM is one of the leading companies which provide processors for embedded devices such as mobile Phones. These ARM processors are based on RISC Architecture.

Most of the devices around us today including mobile Phones, tablets, Multimedia players, and routers are running with ARM Processors. i-devices  such as iPhone, iPad and most of the smart phones running on android platform use ARM processors because of their enhanced power saving design.

Checking for Processor information on an Android Device

We can see the details of the processor being used in our android mobile Phone by following the steps below.

In this example, I am using an emulator instance to show how we can see the CPU information on a device. Just in case if you are using a real device, steps remain the same.

Step 1: Connect your Phone to the machine using adb or over SSH to open a shell on the device.
Please refer to this article if you want to know more about adb

Step 2: type the following command
cat /proc/cpuinfo

It looks as shown in the figure 1.1

figure 1.1
The above figure says that the processor being used is ARMv7.
Note: When launching a new emulator, we can explicitly specify which processor we want to use with the emulator instance as shown in figure 1.2
figure 1.2

Summary:

We have seen what assembly language is and how it is specific to each processor family. We have also seen the details about ARM family of processors. In the next article, we will see the lab setup to start programming ARM Assembly.

Hope this information helps you. Please post your comments for any queries or suggestions.

A deeper insight into “Hello World!” in C

0 comments Posted by srini0x00 on 10:52

Motivation:

In the world of Computer Science, probably the very first output that every programmer would see on his computer screen is “Hello World!”.

So, Lets recall the Hello World! program one more time before actually getting into the nitty gritty details of ARM Assembly Language.

Let the game begin

When we compile and run a very simple C Program, all we see is a binary giving us an output.

 Source Code  -> Compiler -> Binary


But, in the process of generating the binary, there will be some intermediate files generated during different phases which we often don't care. 

Sourcecode -> pre processing -> Compiling -> assembling -> linking -> Binary


Lets look at all these Phases with a sample program. I am using an Ubuntu machine with Intel Processor. 
Let’s first write a simple C Program as shown below.
/* Hello World Program in C */
#include<stdio.h>
main()
{
printf("Hello World!\n");
return 0;
}

Compile the above code in a way that it saves all the temporary files in the current directory where we have written our code.

gcc –save-temps helloworld.c –o hello

The above command generates the following files as shown in Figure 1.1
helloworld.i      -preprocessed file
helloworld.s     -assembly file
helloworld.o    -object file
hello                -binary

Figure 1.1

Pre Processing: 

Pre-processing is setting up everything ready for a compiler to do its job (Compiling). 

WikiPedia: A pre-processor is a program that processes its input data to produce output that is used as input to another program. The output is said to be a pre-processed form of the input data, which is often used by some subsequent programs like compilers. 

In our case, the output after preprocessing is helloworld.i. We can see this file by opening it with any text editor or by using the following command.
cat helloworld.i

Compiling: 

Compiling is basically a process where it takes the high level language’s source code as an input and generates an output usually to a lower level language like assembly.

In our case, the output after compiling is helloworld.s. We can again see the code as explained in preprocessing phase.

Assembling: 

The previous step has generated an assembly language code. Here in this step the code generated by the compiler will be assembled and a new output will be generated for a linker to link it to a final executable. 

In our case, the output after compiling is helloworld.o. 

Linking: 

As explained in the previous phase, this is where the linker will link the object files to a final executable.

In our case, the output after compiling is hello which is the final executable. Even if we try to see the code of this file using any text editors, we end up understanding nothing since it is an executable file.

Now, to see the output, we can execute it as shown in figure 1.2

 figure 1.2

Summary

The Programmer’s ultimate goal is, processor should process and execute what the programmer is asking it to do(Printing "Hello World!" onto the screen in our example). Since, our processor cannot understand the programs we are writing in a high-level language like C, we are converting them into a form which our processor can understand.

Hope this information helps you. Please post your comments for any queries or suggestions.

Tuesday, 26 November 2013

Android Debug Bridge - An introduction

0 comments Posted by srini0x00 on 09:32

What is ADB?

Android Debug Bridge (adb) is a command line tool which acts as a bridge between you and your Android device. It allows the users to control the mobile device right from your computer/laptop.Using ADB, We can do many operations on the Connected Device or Emulator such as, 

  • Checking what are the devices/emulators connected to the computer.
  • Installing an application.
  • Copying files from to/from a phone onto your computer.
  • Executing shell commands.
  • Exploring the directories on a device/emulator.
  • Analyzing the installed app directories for vulnerabilities.

What do we need to work with adb?


  • A Laptop/Computer with adb installed
  • An Android mobile phone/ Emulator(USB Debugging should be enabled on mobile)
  • A USB Cable(If you are using a mobile phone)

How does adb work?

ADB has three important components.
  1. A Client
  2. A Server 
  3. A Daemon
Client: 
Client resides on the computer we are working on. We can explicitly execute it by navigating to android installation path and then moving to platform tools. The adb client looks as shown in figure 1.1
Syntax: <Your installation path>\platform-tools

In my case, I have it in the following path.
C:\Users\DELL\AppData\Local\Android\android-sdk\platform-tools

figure 1.1

Server:
It is a background process running on the computer we are working on, it is used to manage the communication between the client running on the computer and daemon running on the device.

Daemon: 
It is a background process by default running on each android device or emulator.

When we start adb client on the computer, it checks whether the server is running or not, if it is not running, it will start it on port 5037 as shown in figure 1.2

Figure 1.2 adb command looking for attached devices.

Since, my emulator is still switching on, the above figure shows the emulator's status as offline. Once, if it is started it shows the status device as shown in the figure 1.2.1.

figure 1.2.1

Checking the adb connections with netstat

Lets recheck whether this 5037 port is listening for any connections or not using netstat command line utility.

netstat  -a | findstr 5037

-a is to display all connections and ports
| is to pipe the results
findstr is to find the specific string rather than displaying all the output on the screen.
The output is as shown in figure 1.3
figure 1.3

As we can clearly see, a process is listening for a connection on port number 5037. Once if we connect to a device/emulator the status becomes “ESTABLISHED”
Lets run the command “adb shell” as shown in figure 1.4, it gives us a shell on the emulator/device where we can execute the commands.
figure 1.4

If we now check the same nestat command, status says “ESTABLISHED”.

Running Commands

We can now start using adb for different purposes. For instance, let us do a quick “ls” to see all the files and folders in the current directory as shown in the figure 1.5
figure 1.5

We keep using adb throughout this series wherever it is needed. So, its always a good idea to get your hands dirty with adb. 

Hope this information helps you. Please post your comments for any queries or suggestions.


 

Recent posts

Recent Comments

Bookmark & Share