Assembly

       
This is a quick assembly tutorial teaches you the basic instructions and writing simple programs using assembly. All the programs in this tutorial is writting in Visual C++ 2008 Express Edition, using Irvine32.lib. You can get the Irvine32.lib in kipirvine's website click here. All the installation instruction can be found on kipirvine's website.

 

   Lets learn a few basic things first before we start programming.

   use semi-colon when you want to write a single comment.

   ; This is a single line comment

   To write multiple lines comment, we will have to do this

    comment *

                 This is a multiple lines comment section

    *

   comment /

                 This is a multiple lines comment section

   /

 

     Note: we can replace the aterisk with anything. As long as we end with the same thing of what we start with.

  In assembly, we will work with Registers. The general purpose of registers are primiarily used for arithmetic and data movement. Each register can be addressed as either a single 32-bit value or a 16-bit value. Here is a list of the most common 32-bit and 16-bit registers.

32-bit 16-bit
EAX AX
EBX BX
ECX CX
EDX DX
ESI SI
EDI DI
ESP SP

EAX is used by multiplication and division instructions. It is often called the extended accumulator register. Also, when we read input from the user, the data is stored into EAX.

ECX is used for loop counter. Whenever we do loops we will use the ECX register.

ESP addresses data on the statck. Mainly used for stack pointer.

ESI and EDI are used for memory transfer instructions. It's used for array indexing.

EDX is OFFSET register. Often used whenever we want to write something to the console.

Here is a list of integer storage sizes

   byte    8

   word    16

   dword   32

   qword    64

It is probably very confusing right now, but just keep in mind of all the registers and the storage sizes. Now lets see the most famous example in programming the "Hello World Program."

Click Here To Download Example

In this example, we move the offset of variable into edx, the values are store into edx. Then we call writstring and writestring will write anything that is store into the edx into the cmd.

Lets see another example

Click Here To Download Example

 

Now, what if you want to get input from users. Well! lets see a simple example on how to get an input from the user then output the user's input to the cmd.

 

Click Here To Download Example


Lets work on a more interesting examples. Get two integers from the user, store an integer into a dword which is 32-bits. Then output the sum of the two integers into the console.


Click Here To Download Example

I'm sure you have already notice that whenever we want to write something to the screen we use " writestring, writedec, and so on." Whenever we want to get input from the user we use " readstring, readdec and so on." Hence, keep in mind that whenever we want to print something we use "write" and whenever we want to get something we use "read."

Notice, the add instruction, we add the value of the right into the value of the left, then store the value into the left. Thus, in the above example, we add val to eax, then store the value back into the eax register.


Now we will take a look at loops and condition statements. Here is a table some of the most common condition.

Mnemonic Description
je Jump if equal (leftOp = rightOp)
jne Jump if not equal
jg Jump if greater (leftOP > rightOp)
jnle Jump if not less than or equal (same as jg)
jnl Jump if not less (same as jge)
jnge Jump if not greater than or equal(same as JL)
jle Jump if less than or equal(if leftOp <= rightOp)
jng Jump if not greater(same as jle)
cmp Compare the right to the left

Okay, here is a simple example of a while loop. It asks the user for three integers, then it will sum up the integers and output the sum of the integers.

Click Here To Download Example

So in the above example, we have two registers. The eax and the ecx register. Recall, whenever we dealing with loops we use the ecx register. The ecx register act as the counter for the loop. In the example, we set the ecx to 3, thus the while loop will loop three times to ask the user for an integer.

Inside the while loop, we use the compare instruction " cmp ". The instruction will compare the ecx register to 0, if the ecx register is 0, we will jump to end0. You can look at end0 as a method, that you only jump to whenever the condition is met.

Notes: while0, it's just a name that we created for the while loop. We can call it whatever names we want. You'll see what I meant as you read on to the examples.

The next example uses a different loop similar to the for loop in java or C++.


Click Here To Download Example

In the above example, we created a loop called "Loop0". The only difference between this loop and the while loop is, the while loop we have to decrement the counter (ecx ) by ourself. Whereas this loop, the counter ( ecx ) decrements by itself.

Now lets look at an example on creating an empty array, store elements in the array and accessing the elements in the array.

 

Click Here To Download Example

The main ideas about array is the "ESI" pointer. Array starts at index 0, then increases by 4. Thus, the first element of the array is at 0, then the second one is at 4, third one is at 8, fourth one is at 12, and so on. Thus, when storing elements in the array we have to increment the "ESI" register by 4. Hence, that goes the same when accessing elements from the array.

 

  <Home>