Assembler p3
Assembler p3
Assembler p3
http://ce.kashanu.ac.ir/sabaghian/assembly/8086 tutorial/...
8086 Assembler
Variables
Variable is a memory location. For a programmer it is much
easier to have some value be kept in a variable named "var1"
then at the address 5A73:235B, especially when you have 10 or
more variables.
Label supports two types of variables: BYTE and WORD.
Syntax for a variable declaration:
name DB value
name DW value
DB - stays for Dene Byte.
DW - stays for Dene Word.
name - can be any letter or digit combination, though it
should start with a letter. It's possible to declare unnamed
variables by not specifying the name (this variable will have
an address but no name).
value - can be any numeric value in any supported
numbering system (hexadecimal, binary, or decimal), or "?"
symbol for variables that are not initialized.
VAR1 DB 7
var2 DW 1234h
1 of 7
11/13/2015 08:17 AM
http://ce.kashanu.ac.ir/sabaghian/assembly/8086 tutorial/...
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 osets. By default segment is loaded
in DS register (when COM les is loaded the value of DS
register is set to the same value as CS register - code segment).
In memory list rst row is an oset, second row is a
hexadecimal value, third row is decimal value, and last row
is an ASCII character value.
Compiler is not case sensitive, so "VAR1" and "var1" refer to
the same variable.
The oset of VAR1 is 0108h, and full address is 0B56:0108.
The oset of var2 is 0109h, and full address is 0B56:0109,
this variable is a WORD so it occupies 2 BYTES. It is assumed
that low byte is stored at lower address, so 34h is located
before 12h.
You can see that there are some other instructions after the
RET instruction, this happens because the debugger 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:
DB 0A0h
DB 08h
DB 01h
DB
DB
DB
DB
8Bh
1Eh
09h
01h
DB 0C3h
DB 7
DB 34h
2 of 7
11/13/2015 08:17 AM
http://ce.kashanu.ac.ir/sabaghian/assembly/8086 tutorial/...
DB 12h
Arrays
Arrays can be seen as chains of variables. A text string is an
example of a byte array, each character is presented as an
ASCII code value (0..255).
Here are some array denition examples:
a DB 48h, 65h, 6Ch, 6Ch, 6Fh, 00h
b DB 'Hello', 0
b is an exact copy of the a array, when assembler sees a string
inside quotes it automatically converts it to set of bytes. This
chart shows a part of the memory where these arrays are
declared:
You can access the value of any element in array using square
brackets, for example:
MOV AL, a[3]
You can also use any of the memory index registers BX, SI, DI,
BP, for example:
MOV SI, 3
MOV AL, a[SI]
If you need to declare a large array you can use DUP operator.
The syntax for DUP:
number DUP ( value(s) )
3 of 7
11/13/2015 08:17 AM
http://ce.kashanu.ac.ir/sabaghian/assembly/8086 tutorial/...
Reminder:
In order to tell the compiler about data type,
these prexes 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.
4 of 7
11/13/2015 08:17 AM
http://ce.kashanu.ac.ir/sabaghian/assembly/8086 tutorial/...
MOV
LEA
AL, VAR1
BX, VAR1
MOV
MOV
AL, VAR1
RET
VAR1 DB 22h
MOV
AL, VAR1
MOV
MOV
MOV
AL, VAR1
RET
VAR1 DB 22h
END
11/13/2015 08:17 AM
http://ce.kashanu.ac.ir/sabaghian/assembly/8086 tutorial/...
Constants
Constants are just like variables, but they exist only until your
program is compiled (assembled). After denition of a constant
its value cannot be changed. To dene constants EQU directive
is used:
name EQU < any expression >
For example:
k EQU 5
MOV AX, k
11/13/2015 08:17 AM
http://ce.kashanu.ac.ir/sabaghian/assembly/8086 tutorial/...
7 of 7
11/13/2015 08:17 AM