SBN

C Code in Assembly

Introduction

Reverse engineering analysts have a good grasp of C code language and how it’s converted into assembly listings. C code was designed to function as a short form of assembly language, which, despite being time-consuming to code, had inherent efficiencies. C code was able to capitalize on some of these efficiencies by employing code constructs. 

This article is an overview of C code in assembly, including variables and “if” statements, “for” and “while” loops, switch statements, arrays, structs, linked lists, stacks and heaps.

Variables

Variables are used in code to hold values. Based on where the variable is declared, variables are of two types — local variables and global variables. Values stored in a local variable are accessible within a function, whereas values stored in global variables can be accessed from anywhere in the program. 

The following C program shows how local and global variables are used in a C program.

#include <stdio.h>

int a = 10; /**global variable**/

void main()

{

int b = 20; /**local variable**/

a = a+b; 

printf(“The new value of a is %dn”, a);

}

The following is the assembly equivalent of the preceding code. This is generated by OllyDbg when the executable is loaded.

“If” statements

“If” statements are commonly used in C programming. They are used to change the control flow based on certain conditions. The following code example shows an if condition being used in a C program.

#include <stdio.h>

void main()

{

int a = 30;

int b = 20;

if (a > b){

printf(“a is greater than bn”);

}

else{

printf(“b is greater than an”);

}

}

The following is the assembly equivalent of the preceding code. This is generated by OllyDbg when the executable is loaded.

Loops

Loops are used in programming for executing (Read more...)

*** This is a Security Bloggers Network syndicated blog from Infosec Resources authored by Srinivas. Read the original post at: http://feedproxy.google.com/~r/infosecResources/~3/dHGpcmCG0Hc/

Secure Guardrails