How to Create a Variable in x86 Assembly Language

x86 Assembly programming language


In this article, we show how to create a variable in x86 assembly language.

Variables work differently in x86 assembly language than they do in high-level languages such as C++ or Java.

The only thing we specify for a variable other than its name is the size of the variable, whether it is WORD, DWORD, or BYTE.

Unlike C++ or Java, we do not specify the type of variable it is such as int, double, float, boolean, etc.

Instead, asembly deals specifically with the size in terms of how many bits the variable can hold.

A variable of type db (standing for byte) is 8 bits in length.

A variable of type dw (standing for word) is 16 bits in length.

So if you know exactly how many bits a variable will need, you can choose the most specific type needed.

So in x86 assembly code, the declaration and initilization of variables is done within the data segment of the program.

The data segment is where created variables go.

Below is code that creates two variables and the within the code segment, we add the 2 variables together.



So in the program above, we create 2 variables.

Both variables are of type dw; this, again, holds 16 bits of data.

To create variables, the declaration and initialize must go within the .data segment of the code.

Then you must write the name of the variable, followed by the size of the variable, followed by the value of the variable. In this case, the value of the variable is specified in hexadecimal.

After doing this, we can then use the variable within the .code segment, which is the part of the program which runs the code.

If you run this program, there should be no errors, as all the variables used in the .code segment are appropriately declared and initialized in the .data segment.

After running this program, the ax register should have a value of 3000h

This is how we create (declare and initialize) variables in the x86 assembly programming language.

Related Resources



HTML Comment Box is loading comments...