You are on page 1of 6

Middle Technical University Digital Controller Lab.

Electrical Engineering Technical College Third Stage


Electrical Power Technical Engineering Dept. Mohammed D. Altamemi

Experiment-2
Variables Operations
2-1 Object:
To know what is mean in variables and be familiar with its types, and how you can
defined a variable in EMU8086.
2-2 Theory:
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.
Our compiler supports two types of variables: BYTE and WORD.

Syntax for a variable declaration:


name DB value
name DW value

DB - stays for Define Byte.


DW - stays for Define 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 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.
The ORG value is a compiler directive (it tells compiler how to handle the source
code). This directive is very important when you work with variables. As example ORG
100h, it tells compiler that the executable file will be loaded at the offset of 100h (256
bytes), so compiler should calculate the correct address for all variables when it replaces
the variable names with their offsets. Directives are never converted to any real machine
code.

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) see appendix 1. Here are some
array definition examples:
Middle Technical University Digital Controller Lab.
Electrical Engineering Technical College Third Stage
Electrical Power Technical Engineering Dept. Mohammed D. Altamemi

a DB 48h, 65h, 6Ch, 6Ch, 6Fh, 00h

b DB 'Hello', 0

b is an exact copy of the a array, when compiler sees a string inside quotes it
automatically converts it to set of bytes. Figure (a) shows a part of the memory where these
arrays are declared:

Figure (a) part of memory storage array of H-E-L-L-O

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) )

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)

it is an alternative way of declaring


Middle Technical University Digital Controller Lab.
Electrical Engineering Technical College Third Stage
Electrical Power Technical Engineering Dept. Mohammed D. Altamemi

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.

Getting the Address of a Variable:

There is LEA (Load Effective Address) instruction and


alternative OFFSET operator. Both OFFSET and LEA can be used to get the offset
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.

Actually the below expressions are even compiled into the same machine code: MOV
BX, num, where num is a 16 bit value of the variable offset.

LEA BX, VAR1


MOV BX, OFFSET VAR1

In order to tell 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.
WORD PTR [BX] ; word access.

assembler supports shorter prefixes as well, (b.) for BYTE PTR (w.) for WORD PTR,
and in certain cases, the assembler can calculate the data type automatically.

Also, note that only these registers can be used inside square brackets (as memory
pointers): BX, SI, DI, and BP.
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.

name EQU < any expression >


For example:
k EQU 5
MOV AX, k
Middle Technical University Digital Controller Lab.
Electrical Engineering Technical College Third Stage
Electrical Power Technical Engineering Dept. Mohammed D. Altamemi

2-3 Procedures:
Part I: variable declaration
1. Copy the below code to the source editor, and press emulate key to compile it and
load in the emulator.
ORG 100h

MOV AL, var1


MOV BX, var2

RET ; stops the program.

VAR1 DB 7
var2 DW 1234h
2. Run the program and then record the value of AL, and BX registers
AL
BX

Part II: Array Variable


1. Write an array variable [w = 5, 15, 25, 35, 45, 55].
W DB 5h, 15h, 25h, 35h, 45h, 55h
2. Move the third element to AL
MOV AL, w[3]
3. Move the fifth element to BL using one of memory index register (DI).
MOV DI, 5
MOV BL, w[DI]
4. Use DUP to represent array variable (g) consist of 20 elements of AF3.
g DW 20 DUP (0AF3h)
Part III: get the offset address of the variable by LEA instruction.
1. Copy the below code to the source editor, and press emulate key to compile it and
load in the emulator.
ORG 100h
VAR1 DB 22h

MOV AL, VAR1 ; check value of VAR1 by moving it to AL.


LEA BX, 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
END
2. Run the program and record the values of AL, BX, and VAR1.
Middle Technical University Digital Controller Lab.
Electrical Engineering Technical College Third Stage
Electrical Power Technical Engineering Dept. Mohammed D. Altamemi

Table (2)
AL
BX
VAR1

Part IV: constants representation.


1. Use constant representation to defined the same operation in following lines
A. MOV AX, 20
B. MOV BX, 35

2-4 Discussion:
1. Defined these two variables x = A3h, and y = FF34h, and then move x to BL, and
y to DX.
2. Defined a variable array (S = AF2Ch, AF2Eh, AF30h, AF32h, AF35h, AF37h),
and then move the fourth element to AX.
3. Defined a variable array T, has a 10 element of (2Ch), and then move the third
element to BL by using index source register (DI).
4. Write your first name using array variable (use table in Appendix 1).
5. Write a program to get the offset address of the [var1 = 25] by using MOV and
offset instructions, and then change the value of var1 to (50), and check the value
of var1 after changing.
6. Represent the following constants using Assembly language and EMU8086.
A. X = 5Fh
B. Y = 42h
C. Z = 42C3h
Middle Technical University Digital Controller Lab.
Electrical Engineering Technical College Third Stage
Electrical Power Technical Engineering Dept. Mohammed D. Altamemi

Appendix 1: Character set of ASCII

ASCII (1977/1986)

_0 _1 _2 _3 _4 _5 _6 _7 _8 _9 _A _B _C _D _E _F

NUL SOH STX ETX EOT ENQ ACK BEL BS HT LF VT FF CR SO SI


0_ 0000 0001 0002 0003 0004 0005 0006 0007 0008 0009 000A 000B 000C 000D 000E 000F
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS RS US
1_ 0010 0011 0012 0013 0014 0015 0016 0017 0018 0019 001A 001B 001C 001D 001E 001F
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

2_
SP
0020
! " # $ % & ' ( ) * + , - . /
0021 0022 0023 0024 0025 0026 0027 0028 0029 002A 002B 002C 002D 002E 002F
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

3_
0 1 2 3 4 5 6 7 8 9 : ; < = > ?
0030 0031 0032 0033 0034 0035 0036 0037 0038 0039 003A 003B 003C 003D 003E 003F
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

4_
@ A B C D E F G H I J K L M N O
0040 0041 0042 0043 0044 0045 0046 0047 0048 0049 004A 004B 004C 004D 004E 004F
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

5_
P Q R S T U V W X Y Z [ \ ] ^ _
0050 0051 0052 0053 0054 0055 0056 0057 0058 0059 005A 005B 005C 005D 005E 005F
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

6_
` a b c d e f g h i j k l m n o
0060 0061 0062 0063 0064 0065 0066 0067 0068 0069 006A 006B 006C 006D 006E 006F
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

7_
p q r s t u v w x y z { | } ~ DEL
007F
0070 0071 0072 0073 0074 0075 0076 0077 0078 0079 007A 007B 007C 007D 007E
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

Letter Number Punctuation Symbol Other undefined Chan


ged from 1963 version

You might also like