C++ Assembly – ADD

This section of the assembly tutorial builds on the MOV tutorial and adds one execution command to the code. This is the ADD execution command. This is still a fairly pointless piece of code that can be executed just as well in pure C++ as in assembly.

I recommend knowing basic C++, I am using Microsoft Visual Studio C++ 2010 Express for these tutorials.  Create a basic console application and include iostream I am going to be using the line using namespace std; in my examples to help keep the code clean for the beginners.  This tutorial also assumes that you have covered the previous tutorial of the MOV command.

In this example we are going to add the numbers 248 and 100 together and display them.

int Number1, Number2, Result;

__asm {

mov Number1, 248

mov Number2, 100

mov EAX, Number1

Add EAX, Number2

Mov Result, EAX

}

Let’s examine the code.

We define the integers Number1, Number2 and Result.

Mov Number1, 248

Mov Number2, 100

We initiate the two variables Number1 and Number2.

MOV EAX, Number1

This command we are moving the number 248 into the EAX register.

ADD EAX, Number2

This ASM line adds what is in EAX with Number2 and stores it in the EAX register.

Mov Result, EAX

We are moving the EAX register into the Result Variable.

If we do a cout << Result; It should show 348.

There are execution commands such as Add and more that I will explain in other documents.

#include “stdafx.h”

#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])

{

int Number1, Number2, Result;

__asm {

mov Number1, 248

mov Number2, 100

mov EAX, Number1

Add EAX, Number2

Mov Result, EAX

}

cout << “Number1 = ” << Result << endl;

return 0;

}

Recap

This document covered the basics of including basic assembly code in Microsoft Visual Studio c++. This example is rather useless as in C++ can be done in less typing but this code was just used as an example to get your feet wet with assembly.

Advertisements

C++ With Assembly – MOV

C++ with Assembly is often times a complex subject with many C++ programmers. Assembly for many modern programs are not required as the preprocessor can provide just enough speed as programming in pure ASM can provide. It is better to learn how to write the best code for the preprocessor V.S. programming in ASM.  There are a few subjects that can utilize more speed in ASM then allowing the preprocessor convert the code to ASM but these will be covered in a later topic.

I recommend knowing basic C++, I am using Microsoft Visual Studio C++ 2010 Express for these tutorials.  Create a basic console application and include iostream I am going to be using the line using namespace std; in my examples to help keep the code clean for the beginners.

To inform the compiler what sections are using assembly code you can use either of the following:

__asm {

Code

}

Or  __asm code

If you are using a different compiler then Microsoft Visual Studio then there may be different methods depending on your compiler. Please research to your specific compiler to be certain.

Our first example is going to move the number 248 into a defined variable number1.

int Number1;

int Result;

__asm {

mov Number1, 248

mov EAX, Number1

Mov Result, EAX

}

The code is pointless as it does nothing that programming in pure C++ cannot do. Let’s examine the code.

We define two integers Number1 and Result and leave them blank.

__asm {

}

Then we create the __asm block by the following code.

Mov Number1, 248

This command we are moving number 248 into number1. If we was to do a cout << Number1; It would display the number 248.

MOV EAX, Number1

This command we are moving the number 248 into the EAX register(If you do not know what the EAX register is or simply a EAX register please refer to my register section or to Intel, IBM, or AMD developer documentations.)

Mov Result, EAX

This command moves what is in register EAX into the Result variable. Now we have put the number 248 into the result variable.

If we do a cout << Result; It should show 248.

There are execution commands such as Add and more that I will explain in other documents.

#include “stdafx.h”

#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])

{

//asm(“assembly code”);

int Number1;

int Result;

__asm {

mov Number1, 248

mov EAX, Number1

Mov Result, EAX

}

cout << “Number1 = ” << Number1 << endl;

return 0;

}

Recap

This document covered the basics of including basic assembly code in Microsoft Visual Studio c++. This example is rather useless as in C++ can be done in less typing but this code was just used as an example to get your feet wet with assembly.

Programming Variable to bytes

CHAR = 1 byte

SHORT = 2 byte

LONG = 4 byte

LONGLONG = 8 byte

USN = 8 byte

DWORD = 4 byte

WORD = 2 byte

BOOLEAN = 1 byte

INT = 4 byte

NTFS MBR

UCHAR Jump[3];

UCHAR Format[8];

USHORT BytesPerSector;

UCHAR SectorsPerCluster;

USHORT BootSectors;

UCHAR Mbz1;

USHORT Mbz2;

USHORT Reserved1;

UCHAR MediaType;

USHORT Mbz3;

USHORT SectorsPerTrack;

USHORT NumberOfHeads;

ULONG PartitionOffset;

ULONG Reserved2[2];

ULONGLONG TotalSectors;

ULONGLONG MftStartLcn;

ULONGLONG Mft2StartLcn;

ULONG ClustersPerFileRecord;

ULONG ClustersPerIndexBlock;

LARGE_INTEGER VolumeSerialNumber;

UCHAR Code[0x1AE];

USHORT BootSignature;