Uploaded by Salah Alfahad

2

advertisement
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:
aDB 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 AL,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
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.
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
and
Constants:
For example:
k EQU 5 MOV AX, k
w.) for WORD PTR
k EQU 5 MOV AX, k
PART 1: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
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:
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 LEA MOV MOV
RET
END
Part IV: constants representation.
1. Use constant representation to defined the same operation in following lines
2-4 Discussion:
MOV AX, 20
B. MOV BX, 35
1. Defined these two variables x = A3h, and y = FF34h, and then move x to BL,
and y to DX.
ORG 100h
Mov BL,X
Mov DX,y
RET
X DB 0A3H
Y DW 0FF34h
End
2. Defined a variable array (S = AF2Ch, AF2Eh, AF30h, AF32h, AF35h, AF37h), and
then move the fourth element to AX.
ORG 100h
Mov AX,S [4]
Mov BX,2
Mov BX,S [SI]
RET
S DW 0 AF2Ch, 0AF2Eh,0 AF30h,0 AF32h, 0AF35h,0 AF37h
end
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).
ROG 100h
Mov BL,T[3]
T DB 10 DUP (2ch)
end
5. Write a program to get the offset address of the [var1 = 25] by using MOV and
Rog 100h
Mov al,25h
Mov bx,offset var1
Mov byte ptr [bx],50h
Mov al,var1
Ret
Var1 db 25h
end
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
A=0101111h
B=0100 0010h
Z= 010
0 0010 1100 0011h
Ministry of Higher Education Scientific Research
Middle Technical University
Ele. Eng. Tech. College
Dept. Ele. Power
‫اسم التجربة‬
Variables operations
)1(‫رقم التجربة‬
‫اسم الطالب‬
‫صالح عباس طه‬
(A - ‫(الشعبة‬
2020/2/20
Download