Assembly Programming
-By Debaloy Chatterjee
Table of Contents
Clear top 4 bits of AL register using BL register 3
Clear BL register if AL > BL 3
Sum of numbers in an Array 4
Find highest value in an array of 4 elements 5
Increment value at a memory location 6
Find sum of 5 numbers at consecutive memory locations 7
Find average of AX and BX registers 8
Search an element in an array and display if found or not 8
2
Clear top 4 bits of AL register using BL register
Given that if AL = 0111 0101
Then after performing some operation with BL, the value of AL should be: 0000 0101
Performing a bitwise AND operation is suitable for this question.
Put 0000 1111 in BL reg and perform AND.
MOV AL, 01110101B
MOV BL, 00001111B
AND AL, BL
Clear BL register if AL > BL
If value in AL register is more than the value in BL register then clear BL register.
Use CMP mnemonic to compare AL and BL values. The only case when BL needs to
be cleared is when both Zero Flag and Carry Flag are Zero. Otherwise, AL is cleared.
If AL and BL are equal then nothing is done.
MOV AL, 11001010B
MOV BL, 11001000B
CMP AL, BL
JZ EXIT
JC CLEAR_AL
MOV BL, 0H
JMP EXIT
CLEAR_AL:
MOV AL, 0H
EXIT:
MOV AH, 4CH
INT 21H
3
Sum of numbers in an Array
Given an array of five integers, find the sum and store it in sum variable.
.MODEL SMALL
.STACK 100H
.DATA
ARR DB 2,3,8,7,9
SUM DB ?
.CODE
MOV AX, @DATA
MOV DS, AX
MOV SI, OFFSET ARR
MOV CX, 5
L1:
MOV AL, [SI]
ADD SUM, AL
INC SI
LOOP L1
MOV AH, 4CH
INT 21H
4
Find highest value in an array of 4 elements
Given an array of 4 numbers (>0), find the highest value.
.MODEL SMALL
.STACK 100H
.DATA
ARR DB 2,3,8,7
MAX DB 0
.CODE
MOV AX, @DATA
MOV DS, AX
MOV SI, OFFSET ARR
MOV CX, 4
L1:
MOV BH, MAX
MOV BL, [SI]
CMP BL, BH
INC SI
JZ L2
JC L2
MOV MAX, BL
L2:
LOOP L1
MOV AH, 4CH
INT 21H
5
Increment value at a memory location
Given a memory location, get the value from the location, increment it by 2 and
then store the updated value at the memory location.
Let’s say the value is at location 0720 0040h. Store 0720h in SI and 0040h in DI.
Now you can move the data of [DL] in a register to get the value at that location.
Click on Emulate.
1. Click on aux
2. Select memory
3. Type the memory location value
4. Click on the first number
5. Update the signed value to whatever you want
.MODEL SMALL
.STACK 100H
.DATA
.CODE
MOV AX, @DATA
MOV DS, AX
MOV SI, 0720h
MOV DI, 0040h
MOV AX, [SI]
6
MOV BX, [DI]
ADD BX, 2
MOV [DI], BX
MOV AH, 4CH
INT 21H
Find sum of 5 numbers at consecutive memory
locations
Let’s assume the numbers are stored from 07200040h to 07200045h.
The sum is stored in the variable sum. Follows the same logic as array sum.
.MODEL SMALL
.STACK 100H
.DATA
SUM DB ?
.CODE
MOV AX, @DATA
MOV DS, AX
MOV SI, 0720h
MOV DI, 0040h
MOV CX, 5
L1:
MOV AL, [DI]
ADD SUM, AL
INC DI
LOOP L1
MOV AH, 4CH
INT 21H
7
Find average of AX and BX registers
Given 4 values at AL, AH, BL, BH. Find the average value of AX and BX. Store the
result in DX.
MOV AH, 0
MOV AL, 3
MOV BH, 0
MOV BL, 2
ADD AX, BX
MOV DL, 2
DIV DL
MOV DX, AX
Search an element in an array and display if found or
not
Linear Search logic.
.MODEL SMALL
.STACK 100H
.DATA
ARR DB 2,1,4,5,8
KEY DB 44
MSG1 DB "ELEMENT FOUND$"
MSG2 DB "ELEMENT NOT FOUND$"
.CODE
MOV AX, @DATA
MOV DS, AX
MOV SI, OFFSET ARR
MOV CX, 5
MOV BL, KEY
L1:
MOV BH, [SI]
8
CMP BL, BH
JZ FOUND
INC SI
LOOP L1
LEA DX, MSG2
MOV AH, 9
INT 21H
JMP EXIT
FOUND:
LEA DX, MSG1
MOV AH, 9
INT 21H
EXIT:
MOV AH, 4CH
INT 21H