Tuesday, 21 July 2015

Understanding the Assembly Variables!

What is variable?
A variable is a data storage unit used in your program.Variables are used in programs to store pieces of information that may change and may be used to dynamically influence the program.

In high-level languages like C/C++, Java, variables must be declared before they can be used.Unlike HLL(high-level languages), we do not specify a variable type in the declaration in assembly language. Instead we declare the name and size of the variable, i.e. the number of bytes the variable will occupy. We may also specify an initial value.

How declare variables in Assembly?

Declaring variables in Assembly is completely different from declaring variables in High-level languages. In C/C++, The basic syntax is:
data_type var, var, …;
where data_type is one of the four basic types, an integer, character, float, or double type.
For Example:
int a, b, c;
char t, arr[10];


In assembly, The declaration syntax is as follows:
variable_name   db   value
Where variable_name is your variable name and value is the corresponding default value of variable_name. Variable  declarations should be preceded by the .DATA section or directive.
A directive (i.e. a command to the assembler) is used to define variables. In 8086 assembly language, the directive db defines a byte sized variable; dw defines a word sized variable (16 bits) and dd defines a double word (long word, 32 bits) variable. The full list is:
DB, DW, DD, DQ, DT, DDQ, and DO.
But here .we are only concerned on DB, DW, and DD.

The basic forms of the these three define directive:
Directive        Purpose                 Storage Space                Limit
DB            Define Byte                allocates 1 byte            0-255
DW           Define Word             allocates 2 bytes              0-65535
DD          Define Doubleword      allocates 4 bytes        0-4294967295

Example:
    reply db ‘y’
    prompt db ‘Enter your favourite colour: ’, 0
    colour db 80 dup(?)
    i db 20
    k db ?
    num dw 4000
    large dd 50000



                         



reply is defined as a character variable, which is initialized to ‘y’.
prompt is defined as a string, terminated by the Null character.

The definition of the variable colour demonstrates how to declare an array of characters of size 80, which contains undefined values. The purpose of dup is to tell the assembler to duplicate or repeat the data definition directive a specific number of times, in this case 80 dup specifies that 80 bytes of storage are to be set aside since dup is used with the db directive.

The (?) with the dup means that storage allocated by the directive is uninitialized or undefined.

i and k are byte sized variables, where i is initialized to 20 and k is left undefined. num is a 16-bit variable, initialized to 4000 and the variable large is a 32-bit variable, initialized to 15000.

Note:
1. If you are not certain about the default value of a variable, or you just lazy to specify one, you can give a question mark ("?") instead. For example:
another_var dw ?
2.String VariablesGiven that we have defined a string variable message as
message db ‘Hello’,0,
an important feature is that the characters are stored in consecutive memory locations. If the ‘H’ is in location 1024, then ‘e’ will be in location 1025, ‘l’ will be in location 1026 and so on.
A technique known as indirect addressing may be used to access the elements of the array.
Indirect addressing allows us store the address of a location in a register and use this register to access the value stored at that location. This means that we can store the address of the string in a register and access the first character of the string via the register. If we increment the register contents by 1, we can access the next character of the string. By continuing to increment the register, we can access each character of the string, in turn, processing it as we see fit.

3.Multi-valued Variables: there is no restriction on how many values we can define for each variable names.For Example:
multivar db 12h, 34h, 56h, 78h, 00h, 11h, 22h, 00h
Well, you can consider that multi-valued variable as an array. In fact, high level programming language will translate array definitions to this.

Using dup: Another way to declare a multi-valued variables are using dup command. See the example below:
my_array db 5 dup (00h)
That example above is similar to:
my_array db 00h, 00h, 00h, 00h, 00h

Reference:http://programmingethicalhackerway.blogspot.in/2015/07/what-is-variable-variables-for-hackers.html
If you like this post or have any question, please feel free to comment!
Facebook Twitter Google+

 
Back To Top