In most programming languages you declare a variable by entering a name, and setting its value equal to a static piece of data. You don't need to worry about where the value inside the variable is located. Most people view variables like a box, which can be created and destroyed. While the box is still in box form, data is placed in it. When the box no longer has a purpose and is removed from the program, it ceases to exist, right?
Actually, a variable is more like a drawer in a large shelf made up of identical drawers. When you declare a variable and place a name on it, you put a label on that drawer. Any values that are assigned to the variable are put in the drawer. When the variable is removed from the program, the contents and label are removed from the drawer, but it continues to exist.
It is important to understand this distinction when creating variables in assembly language. While most other languages appear to have the “box” style of variables, assembly language forces you to choose what "drawer" you want your variable to be.
When setting up a program in assembly, it is divided into two main parts, the code segment and the data segment. In the code segment, the program’s instructions are written. In the data segment, the variables are declared.
To declare variables in assembly, the program counter of the data segment must be set. Depending on how much code you plan on writing, you can decide where you would like to begin the program counter. As long as it does not interfere with the code segment, any value should work.
For our first application of assembly, we will create a program that takes two variables containing integers, adds them, and stores the sum in a third variable. Simple, right? Not exactly.
The first step is to create the data segment.
; Data segment: ORG 200o ; Set Program Counter to address 200 VAL1: DB 5o ; Data Byte at address 200 = 5 VAL2: DB 10o ; Data Byte at address 201 = 8 (10 octal) SUM: DB 0o ; Data Byte at address 202 = 0
In this code, ORG sets the program counter, and the number immediately after declares it to start at address 200. Using the labels, the following lines establish the variables. Each variable assumes the address of the program count