8086 Assembler Tutorial For Beginners (Part 3)
8086 Assembler Tutorial For Beginners (Part 3)
name DB value
name DW value
ORG 100h
VAR1 DB 7
var2 DW 1234h
Copy the above code to the source editor, and press F5 key to
compile it and
load in the emulator.
You should get something like:
https://jbwyatt.com/253/emu/asm_tutorial_03.html 1/7
5/11/21 16:50 8086 assembler tutorial for beginners (part 3)
As you see this looks a lot like our example, except that variables are replaced
with actual memory locations. When compiler makes machine code, it
automatically
replaces all variable names with their offsets. By default segment
is
loaded in DS register (when COM files is loaded the value
of DS register is set
to the same value as CS register - code segment).
Compiler is not case sensitive, so "VAR1" and "var1" refer to the same variable.
You can see that there are some other instructions after the RET
instruction,
this
happens because disassembler has no idea about where the data starts,
it just
processes the values in memory and it understands them as
valid 8086
instructions (we will learn them later).
You can even write the same program using DB directive only:
ORG 100h
DB 0A0h
DB 08h
DB 01h
DB 8Bh
DB 1Eh
DB 09h
DB 01h
DB 0C3h
DB 7
https://jbwyatt.com/253/emu/asm_tutorial_03.html 2/7
5/11/21 16:50 8086 assembler tutorial for beginners (part 3)
DB 34h
DB 12h
Copy the above code to the source editor, and press F5 key to
compile and load
it in the emulator. You should get the same disassembled code,
and the same
functionality!
As you may guess, the compiler just converts the program source to
the set of
bytes, this set is called machine code, processor
understands the machine
code and executes it.
Arrays
b DB 'Hello', 0
You can access the value of any element in array using square brackets,
for
example:
MOV AL, a[3]
https://jbwyatt.com/253/emu/asm_tutorial_03.html 3/7
5/11/21 16:50 8086 assembler tutorial for beginners (part 3)
You can also use any of the memory index registers BX, SI, DI, BP,
for
example:
MOV SI, 3
If you need to declare a large array you can use DUP operator.
The syntax for DUP:
for example:
c DB 5 DUP(9)
Reminder:
In order to tell the compiler about data type,
https://jbwyatt.com/253/emu/asm_tutorial_03.html 4/7
5/11/21 16:50 8086 assembler tutorial for beginners (part 3)
For example:
BYTE PTR [BX] ; byte access.
or
in certain cases the assembler can calculate the data type automatically.
ORG 100h
RET
VAR1 DB 22h
END
ORG 100h
RET
VAR1 DB 22h
END
https://jbwyatt.com/253/emu/asm_tutorial_03.html 5/7
5/11/21 16:50 8086 assembler tutorial for beginners (part 3)
These lines:
LEA BX, VAR1
are even compiled into the same machine code: MOV BX, num
num is a 16 bit value of the variable offset.
Constants
Constants are just like variables, but they exist only until your program
is compiled (assembled). After
definition of a constant its value cannot
be changed. To define constants EQU directive is used:
For example:
k EQU 5
MOV AX, k
MOV AX, 5
You can view variables while your program executes by selecting "Variables"
from the "View" menu
of emulator.
https://jbwyatt.com/253/emu/asm_tutorial_03.html 6/7
5/11/21 16:50 8086 assembler tutorial for beginners (part 3)
To view arrays you should click on a variable and set Elements property
to array size. In assembly
language there are not strict data types, so any variable
can be presented as an array.
You can edit a variable's value when your program is running, simply double click it,
or select it and
click Edit button.
https://jbwyatt.com/253/emu/asm_tutorial_03.html 7/7