8086 assembler tutorial for beginners (part 2)

Memory Access

To access memory we can use these four registers: BX, SI, DI, BP.

Combining these registers inside [ ] symbols, we can get different memory locations.

These combinations are supported (addressing modes):

[BX + SI]
[BX + DI]
[BP + SI]
[BP + DI]
[SI]
[DI]
d16 (variable offset only)
[BX]
[BX + SI + d8]
[BX + DI + d8]
[BP + SI + d8]
[BP + DI + d8]
[SI + d8]
[DI + d8]
[BP + d8]
[BX + d8]
[BX + SI + d16]
[BX + DI + d16]
[BP + SI + d16]
[BP + DI + d16]
[SI + d16]
[DI + d16]
[BP + d16]
[BX + d16]

d8 - stays for 8 bit signed immediate displacement (for example: 22, 55h, -1)

d16 - stays for 16 bit signed immediate displacement (for example: 300, 5517h, -259).

Displacement can be a immediate value or offset of a variable, or even both. If there are several values, assembler evaluates all values and calculates a single immediate value.

Displacement can be inside or outside of the [ ] symbols, assembler generates the same machine code for both ways.

Displacement is a signed value, so it can be both positive or negative.

Generally the compiler takes care about difference between d8 and d16, and generates the required machine code.


For example, let's assume that DS = 100, BX = 30, SI = 70.
The following addressing mode: [BX + SI] + 25
Is calculated by processor to this physical address: 100 * 16 + 30 + 70 + 25 = 1725.

By default DS segment register is used for all modes except those with BP register, for these SS segment register is used.

There is an easy way to remember all those possible combinations using this chart:



All valid combinations can be formed by taking only one item from each column or skipping the column by not taking anything from it. BX and BP never go together. Neither SI and DI do.

Examples of valid addressing modes:

[BX+5]

[BX+SI]

[DI+BX-4]





The value in segment register (CS, DS, SS, ES) is called a segment, and the value in general purpose register (BX, SI, DI, BP) is called an offset.

When DS contains value 1234h and SI contains the value 7890h it can be also recorded as 1234:7890. The physical address will be 1234h * 10h + 7890h = 19BD0h.

If zero is added to a decimal number it is multiplied by 10, however 10h = 16, so If zero is added to a hexadecimal value, it is multiplied by 16, for example:

7h = 7
70h = 112





In order to say the compiler about data type, these prefixes should be used:

byte ptr - for byte.
word ptr - for word (two bytes).

for example:
byte ptr [BX]     ; byte access.
    or
word ptr [BX]     ; word access.
Emu Assembler supports shorter prefixes as well:

b. - for byte ptr
w. - for word ptr

in certain cases the assembler can calculate the data type automatically.




MOV instruction

these types of operands are supported:

MOV REG, memory
MOV memory, REG
MOV REG, REG
MOV memory, immediate
MOV REG, immediate
REG: AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP.

memory: [BX], [BX+SI+7], variable

immediate: 5, -24, 3Fh, 10001101b


for segment registers only these types of MOV are supported:

MOV SREG, memory
MOV memory, SREG
MOV REG, SREG
MOV SREG, REG
SREG: DS, ES, SS, and only as second operand: CS.

REG: AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP.

memory: [BX], [BX+SI+7], variable


The MOV instruction cannot be used to set the value of the CS and IP registers.

A short program that demonstrates the use of MOV instruction:

ORG 100h           ; this directive required for a simple 1 segment .com program. 
MOV AX, 0B800h     ; set AX to hexadecimal value of B800h. 
MOV DS, AX         ; copy value of AX to DS. 
MOV CL, 'A'        ; set CL to ASCII code of 'A', it is 41h. 
MOV CH, 1101_1111b ; set CH to binary value. 
MOV BX, 15Eh       ; set BX to 15Eh. 
MOV [BX], CX       ; copy contents of CX to memory at B800:015E 
RET                ; returns to operating system. 



Once the above program is typed into the code editor, and [Compile and Emulate] button is pressed (F5 key)

The emulator window should open with this program loaded, clicking [Single Step] button should change the register values.


How to do copy & paste:
  1. Select the above text using mouse, click before the text and drag it down until everything is selected.

  2. Press Ctrl + C combination to copy.

  3. Click inside the source code editor and press Ctrl + V combination to paste.


";" is used for comments, anything after ";" symbol is ignored.


The result of a working program:



Actually the above program writes directly to video memory, now we can see that MOV is a very powerful instruction.



<<< previous part <<<      >>> Next Part >>>