Lab Task 6.1 Arrays & Logical Instructuions
Lab Task 6.1 Arrays & Logical Instructuions
Lab: 5
Instructions:
Name: ___________________
1. Attempt all questions.
Roll No: _________________ 2. Time management is the key to so success.
Date: ___________________ 3. No extra time will be given.
Assembly - Arrays
We have already discussed that the data definition directives to the assembler are used
for allocating storage for variables. The variable could also be initialized with some
specific value. The initialized value could be specified in hexadecimal, decimal or binary
form.
For example, we can define a word variable 'months' in either of the following way −
MONTHS DW 12
MONTHS DW 0CH
MONTHS DW 0110B
The data definition directives can also be used for defining a one-dimensional array. Let
us define a one-dimensional array of numbers.
NUMBERS DW 34, 45, 56, 67, 75, 89
The above definition declares an array of six words each initialized with the numbers 34,
45, 56, 67, 75, 89. This allocates 2x6 = 12 bytes of consecutive memory space. The
symbolic address of the first number will be NUMBERS and that of the second number
will be NUMBERS + 2 and so on.-
Let us take up another example. You can define an array named inventory of size 8,
and initialize all the values with zero, as −
INVENTORY DW 0
DW 0
DW 0
DW 0
DW 0
DW 0
DW 0
DW 0
Which can be abbreviated as −
INVENTORY DW 0, 0 , 0 , 0 , 0 , 0 , 0 , 0
The TIMES directive can also be used for multiple initializations to the same value.
Using TIMES, the INVENTORY array can be defined as:
INVENTORY 8 DW 0
Example
org 100h
.MODEL SMALL
.DATA
ARR DB 1,2,3,4,5
.CODE
MAIN PROC
MOV CX,5
MOV SI,0
MOV AH,2
OUTPUT:
MOV DL, ARR[SI]
ADD DL, 30H
INT 21H
INC SI
LOOP OUTPUT
MAIN ENDP
END
ret
The processor instruction set provides the instructions AND, OR, XOR, TEST, and NOT
Boolean logic, which tests, sets, and clears the bits according to the need of the
program.
The format for these instructions −
2 OR OR operand1, operand2
The first operand in all the cases could be either in register or in memory. The second
operand could be either in register/memory or an immediate (constant) value. However,
memory-to-memory operations are not possible. These instructions compare or match
bits of the operands and set the CF, OF, PF, SF and ZF flags.
The AND Instruction
The AND instruction is used for supporting logical expressions by performing bitwise
AND operation. The bitwise AND operation returns 1, if the matching bits from both the
operands are 1, otherwise it returns 0. For example −
Operand1: 0101
Operand2: 0011
----------------------------
After AND -> Operand1: 0001
The AND operation can be used for clearing one or more bits. For example, say the BL
register contains 0011 1010. If you need to clear the high-order bits to zero, you AND it
with 0FH.
AND BL, 0FH ; This sets BL to 0000 1010
Let's take up another example. If you want to check whether a given number is odd or
even, a simple test would be to check the least significant bit of the number. If this is 1,
the number is odd, else the number is even.
Assuming the number is in AL register, we can write −
AND AL, 01H ; ANDing with 0000 0001
JZ EVEN_NUMBER
The following program illustrates this −
When the above code is compiled and executed, it produces the following result −
Even Number!
Change the value in the ax register with an odd digit, like −
mov ax, 9h ; getting 9 in the ax
The program would display:
Odd Number!
Similarly to clear the entire register you can AND it with 00H.
The OR Instruction
The OR instruction is used for supporting logical expression by performing bitwise OR
operation. The bitwise OR operator returns 1, if the matching bits from either or both
operands are one. It returns 0, if both the bits are zero.
For example,
Operand1: 0101
Operand2: 0011
----------------------------
After OR -> Operand1: 0111
The OR operation can be used for setting one or more bits. For example, let us assume
the AL register contains 0011 1010, you need to set the four low-order bits, you can OR
it with a value 0000 1111, i.e., FH.
OR BL, 0FH ; This sets BL to 0011 1111
Example
The following example demonstrates the OR instruction. Let us store the value 5 and 3
in the AL and the BL registers, respectively, then the instruction,
OR AL, BL
Task
The following example demonstrates the above concepts by defining a 3-element array
x, which stores three values: 2, 3 and 4.
1. The following example demonstrates the above concepts by defining a 3-element array x,
which stores three values: 2, 3 and 4.(Not yet)
2. Write a code in assembly language where you Use AND, OR & XOR operator and check two
values ( 7B and 4D ) results. And define line by line commenting also run output on screen. Int
21h, proc, endp, and mov command is core part of this code.
References:
https://www.tutorialspoint.com/assembly_programming/assembly_arrays.htm
https://home.adelphi.edu/~siegfried/cs174/174l3.pdf
https://www.tutorialspoint.com/assembly_programming/assembly_arithmetic_instructions.htm