Understanding The assembly language part 2

0
I shortly explained some basic things and some instruction in the part 1. In this post i will discuss more about assembly language such as Variable, Array, Conditional, Loop etc. Remember, These post only for basic understanding of asm , not to be coder.

Variable:

Variable is data storage location. If you did the other programming language(You should before asm) then you know how variables work. Variable store some data to be used later when needed. I want to give you 3 example of these programming language.

1.Python example code:
 #!/usr/bin/env python

variable1 = 1337
 variable2 =”you are”  

print “This variable testing.
print “Now you  know what is variable because”, var1,var2

So here “variable1” and “variable2” is storing some data (value:1337 and “you are”). At last statement (print) called these two variables then it printed  “Now you know what is variable because you are 1337” . This is how variables work.



2. PHP example code:

<?php
$var1=”You are 1337″          //Note : PHP variables start with “$”
echo “Great !!! “;
echo $var;
?>



Here $var1 is the variable and storing the data “You  are 1337”.

OK, This how variables work for high level language but for assembly language it is very different. Suppose we want to add and subtract some numeric. here is the example:

.data
var1 dword 1000      ;Note: If it is string(A) then it should be like “A”(Quote)
var2 dword 337
var3 dword 1337
.code
mov eax,var1
add eax,var2
sub eax,var3




We know that all uninitialized in .data section. we need define the variable in the .data section.  So here i declared 3 variable var1,var2 and var3 and the value are var1=1000, var2=337, var3=1337.
So first we move the var1 value(1000) into eax register, then we add extra var2(337) so the eax register  value now “1337” , Again we subtract with var3(1337) value and now eax register became “0”. 

This is how variable works for assembly language. 






Array:

We know that array is a variable which hold multiple data/element in same data type. Suppose we have variable 



a=1
b=2
c=3
d=4


Instead declaring individual variable we can also do by a array like(Imagine), a=1,2,3,4.

We use array in C programming language within second bracket:

int something[5] = {1,2,3,4};



When we access to the data of an array then we code like this  “something[0]”…no?

Assembly is different . In assembly language does not accept {} and we need to declare an array in .data section. Such as ,

.data

array

……


Array is like variable . It just hold multiple value. So we need keep it on .data section…:

.data
array1 dword 1234,5678,9101h
.code
mov esi,array1+4  ;esi=5678
mov array1+0,esi  ;store 5678 in first element of array.
lea esi,offset[array1+8]  ; (Load address)remember it is 32 bit asm.

.
.
.


———-

Conditional:

Programming language allow us to take decision for True and False. Often we want to make conditional decision.

Imagine, >>If it you are true hacker>>>Then you are researcher>>If you are not true hacker>>>Then you are just a skid>>>So Skip here>>>No way and Close.



We can also make conditional decision for asm code (Example 1):

cmp eax,10 ;Comparing with 10
je target      ; If eax=10 then jump to target label

target:
       code
          .
          .
         mov esi,1337
        cmp esi,1337
                je, tart2
Example 2:

mov eax, 100
cmp eax,60
jg target3    ; jg=Jump if Greater





Some Conditional Jump instructions:

jnz = Jump if not zero (Zero Flag=0)
jns = Jump if not signed (Signed Flag=0)
jc = Jump if Carry (Carry flag=1)
jo= Jump if Overflow(Overflow=1)
jno=jump if not overflow (Overlow=0)

etc…






Looping

 

We know how a loop is work . In python we use like , “for loop in somewhere: start looping”. But In assembly there are only two specific instruction(Short details Below).

LOOPNZ Instruction(Loop if not zero): It will loop while the value ecx is greater than 0. 

example:

loopnz label1

LOOPNE Instruction(Loop If not equal): It is equipment to LOOPNZ and it share same opcode.



Now let’s discus about some extra things which we need to know… 





Data Related Operator:


I understand about 4 type of data operator below:

PTR operator : Suppose we want to move 16 bits of dword variable into a 16 bit register. Actually it is not allowed simply as it will not match because operand size does not match. So perhaps only possible way is “PTR” Operator.


.data
dwd dword 33333333h

.code
mov si, WORD PTR dwd  ;This is the way




Wrong:
.data 
dwd dword 33333333h



.code
mov si,dwd ; It will not work because Double word can’t store into 16 bit reg.




LENGHTOF OPERATOR:  LENGTHOF operator counts that how many elements in an array. Example:

.data
thisisthearray dword 10,11,12,13,14,15


.code

mov eax,LENGHTOF thisisthearray ;   So 6 elements in the “thisisthearray”  array




 
TYPE operator: TYPE operator simple check how much the size of a variable, array. Example:

.data
myvar dword “ethical”

.code
mov esi,TYPE myvar  ; The size is 8







SIZEOF operator: 

.data

arry WORD 16 DUP(0)
.code
mov eax,SIZEOF arry


ah How is working? “arry” has TYPE 2 and LENGHTOF 32. So it will be double , Mean eax=64. Because SIZEOF operator do multiple lenghtof by TYPE.




OK guys …. Now time to practices. Just do some google search, and read more. I just tried to explained(Sorry for bad English, i tried). I think that you are now interest to learn more about asm. So at last i recommended you to visit :



Some Instruction: Go here

Some code. Most of code for 16 bits but i believe you will understand the code clearly because you already know that different only “E” for example ax=eax. So  Go here


Just I try to read ASM code over and over and if i don’t understand any instruction or anything then i first start to search on Google. Perhaps this is how i am improving my assembly Language. 



Welcome any Feedback (sec00rit3y@gmail.com)