Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (1 vote)
143 views7 pages

Assembler p3

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 7

8086 Assembler Tutorial for Beginners (Part 3)

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.

As you probably know from part 2 of this tutorial, MOV


instruction is used to copy values from source to destination.
Let's see another example with MOV instruction:

MOV AL, var1


MOV BX, var2

VAR1 DB 7
var2 DW 1234h

1 of 7

11/13/2015 08:17 AM

8086 Assembler Tutorial for Beginners (Part 3)

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

8086 Assembler Tutorial for Beginners (Part 3)

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

8086 Assembler Tutorial for Beginners (Part 3)

http://ce.kashanu.ac.ir/sabaghian/assembly/8086 tutorial/...

number - number of duplicate to make (any constant value).


value - expression that DUP will duplicate.
for example:
c DB 5 DUP(9)
is an alternative way of declaring:
c DB 9, 9, 9, 9, 9
one more example:
d DB 5 DUP(1, 2)
is an alternative way of declaring:
d DB 1, 2, 1, 2, 1, 2, 1, 2, 1, 2
Of course, you can use DW instead of DB if it's required to keep
values larger then 255, or smaller then -128. DW cannot be
used to declare strings!
The expansion of DUP operand should not be over 1020
characters! (the expansion of last example is 13 chars), if you
need to declare huge array divide declaration it in two lines
(you will get a single huge array in the memory).

Getting the Address of a Variable


There is LEA (Load Eective Address) instruction and
alternative OFFSET operator. Both OFFSET and LEA can be
used to get the oset address of the variable.
LEA is more powerful because it also allows you to get the
address of an indexed variables. Getting the address of the
variable can be very useful in some situations, for example
when you need to pass parameters to a procedure.

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

8086 Assembler Tutorial for Beginners (Part 3)

http://ce.kashanu.ac.ir/sabaghian/assembly/8086 tutorial/...

Here is rst example:

MOV
LEA

AL, VAR1
BX, VAR1

; check value of VAR1 by moving it to AL.


; get address of VAR1 in BX.

MOV

BYTE PTR [BX], 44h

MOV

AL, VAR1

; modify the contents of VAR1.

; check value of VAR1 by moving it to AL.

RET
VAR1 DB 22h

Here is another example, that uses OFFSET instead of LEA:

MOV

AL, VAR1

; check value of VAR1 by moving it to AL.

MOV

BX, OFFSET VAR1

; get address of VAR1 in BX.

MOV

BYTE PTR [BX], 44h

; modify the contents of VAR1.

MOV

AL, VAR1

; check value of VAR1 by moving it to AL.

RET
VAR1 DB 22h
END

Both examples have the same functionality.


These lines:
LEA BX, VAR1
MOV BX, OFFSET VAR1
are even compiled into the same machine code: MOV BX, num
5 of 7

11/13/2015 08:17 AM

8086 Assembler Tutorial for Beginners (Part 3)

http://ce.kashanu.ac.ir/sabaghian/assembly/8086 tutorial/...

num is a 16 bit value of the variable oset.


Please note that only these registers can be used inside square
brackets (as memory pointers): BX, SI, DI, BP!

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

The above example is functionally identical to code:


MOV AX, 5

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.
Variable can be viewed in any numbering system:
HEX - hexadecimal (base 16).
BIN - binary (base 2).
OCT - octal (base 8).
6 of 7

11/13/2015 08:17 AM

8086 Assembler Tutorial for Beginners (Part 3)

http://ce.kashanu.ac.ir/sabaghian/assembly/8086 tutorial/...

SIGNED - signed decimal (base 10).


UNSIGNED - unsigned decimal (base 10).
CHAR - ASCII char code (there are 256 symbols, some
symbols are invisible).
You can edit a variable's value when your program is running,
simply double click it, or select it and click Edit button.
It is possible to enter numbers in any system, hexadecimal
numbers should have "h" sux, binary "b" sux, octal "o"
sux, decimal numbers require no sux. String can be entered
this way:
'hello world', 0
(this string is zero terminated).
Arrays may be entered this way:
1, 2, 3, 4, 5
(the array can be array of bytes or words, it depends whether
BYTE or WORD is selected for edited variable).
Expressions are automatically converted, for example:
when this expression is entered:
5+2
it will be converted to 7 etc...

<<< Previous Part <<<

7 of 7

>>> Next Part >>>

11/13/2015 08:17 AM

You might also like