Skip to content

Data Declaration

To define a portion of our program as holding data, we use an assembly-directive that is define byte or db.

db somevalue

Similarly, another assembly-directive is define word or dw which stores 2 bytes or a word.

dw somevalue

In assembly, we can assign symbols to these data elements by using labels.
They are placeholders and are replaced by the address of those data elements we defined using db or dw.
This is very similar to variables in higher level programming languages.

Direct Addressing

; a program to add three numbers using memory variables  
[org 0x0100]  
              mov  ax, [num1]         ; load first number in ax  
              mov  bx, [num2]         ; load second number in bx  
              add  ax, bx             ; accumulate sum in ax  
              mov  bx, [num3]         ; load third number in bx  
              add  ax, bx             ; accumulate sum in ax  
              mov  [num4], ax         ; store sum in num4  
              mov  ax, 0x4c00         ; terminate program  
              int  0x21 

num1:         dw   5  
num2:         dw   10  
num3:         dw   15  
num4:         dw   0

The listing file generated by the assembler is as follows:

[org 0x0100]
00000000 A1[1700]    mov  ax, [num1]        
00000003 8B1E[1900]  mov  bx, [num2]        
00000007 01D8        add  ax, bx             
00000009 8B1E[1B00]  mov  bx, [num3]        
0000000D 01D8        add  ax, bx            
0000000F A3[1D00]    mov  [num4], ax        
00000012 B8004C      mov  ax, 0x4c00        
00000015 CD21        int  0x21
00000017 0500        num1:         dw   5
00000019 0A00        num2:         dw   10
0000001B 0F00        num3:         dw   15
0000001D 0000        num4:         dw   0

The opcode for moving constants into the AX register was B8.
The opcode for moving data into the AX register is A1.

The instruction is followed by 1700 which is 00 17 which is the offset of the num1.

; a program to add three numbers accessed using a single label
[org 0x0100]
     mov  ax, [num1]         ; load first number in ax
     mov  bx, [num1+2]       ; load second number in bx
     add  ax, bx             ; accumulate sum in ax
     mov  bx, [num1+4]       ; load third number in bx
     add  ax, bx             ; accumulate sum in ax
     mov  [num1+6], ax       ; store sum at num1+6
     mov  ax, 0x4c00         ; terminate program
     int  0x21

num1: dw   5
      dw   10
      dw   15
      dw   0

Here, the [num1+2] refers to 2 bytes offset with respect to num1: dw 5, that is:

dw 10

The label section can also be written as:

num1: dw 5, 10, 15, 0

This method of accessing memory is called direct-addressing.
Regarding the movement of data, only following are allowed: 1. register to register 2. register to memory 3. memory to register 4. constant to memory 5. constant to register

The disallowed ones are: 1. Register to constant 2. Memory to constant 3. Memory to memory