Lab 2
Lab 2
Lab 2
Creating Variables
Basic Arithmetic Operations (addition, subtraction, multiplication, division)
Creating Arrays
Create Constants
Introduction to INC, DEC, LEA instruction
Learn how to access Memory.
Creating Variable:
Syntax for a variable declaration:
“name” DB “value”
e.g. a DB 9
name DW value
e.g. a DW 9
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.
**Variable must declare after return.
Fall 2020
Creating 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: (always
16 bit)
name EQU < any expression >
For example:
k EQU 5
MOV AX, k
Creating 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 definition examples:
You can access the value of any element in array using square brackets, for example:
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)
an alternative way of declaring is:
c DB 9, 9, 9, 9, 9
Fall 2020
d DB 5 DUP (1, 2)
an alternative way of declaring is:
d DB 1, 2, 1, 2, 1, 2, 1, 2, 1, 2
Memory Access:
To access memory we can use these four registers: BX, SI, DI, BP. Combining these registers inside [ ]
symbols, we can get different memory locations.
Displacement can be an immediate value or offset of a variable, or even both. if there are several values,
assembler evaluates all values and calculates a single immediate value.
Displacement can be inside or outside of the [] symbols, assembler generates the same machine code
for both ways.
Instructions
operand = operand + 1
Example:
MOV AL, 4
INC AL; AL = 5
RET
DEC REG Decrement.
MEM
Algorithm:
operand = operand - 1
Example:
MOV AL,86
DEC AL; AL=85
RET
LEA REG,MEM Load Effective Address.
Algorithm:
Example:
MOV AL, 5
SUB AL, 1 ; AL = 4
RET
Example:
Example:
MOV AX, 203; [AX = 00CBh]
MOV BL, 4;
DIV BL; [AL = 50 (32h), AH = 3]
RET