You are on page 1of 28

Micro Processor 8086

Assembly Language

Lecturer: Dr. Adnan Ismail

Answers Of Lecture’s homeworks


And Extra Examples

Prepared By: Mohammed JM


Rojan Salim
Lecture 2 – Examples :
*Write a program to exchange the content of memory locations 0200h and 0201h with
each other: ( slide 7 )
MOV AX , [200] ;AL = [200] = X , AH = [201] = Y
MOV [200] , AH ;[200] = Y
MOV [201] , AL ;[201] = X

*Two 16 numbers are stored consecutively in memory locations 250h - 253h. Write a
sequence of instructions to exchange their contents with each other: ( slide 18 )
MOV SI , 250 ;SI = 0250
MOV AX , [SI] ;AX = X2X1
XCHG AX , [SI+2] ;AX = Y2Y1 , [SI+2] = X2X1
MOV [SI] , AX ;[SI] = Y2Y1

*Suppose that a 7-segment LED display lookup table is stored in memory at address
TABLE. The XLAT instruction then uses the lookup table to translate the BCD number in
AL to a 7-segment code in AL. The sequence of instructions shown below converts from a
BCD number to a 7-segment code: ( slide 20 )
TABLE: 3FH, 06H, 5BH, 4FH, 66H, 6DH, 7DH, 27H, 7FH, 6FH
MOV AL , 05 ;We chose the code of number ( 5 ) you can chose what you want
MOV BX , TABLE ;Table is the starting address of the TABLE of 7 segments
XLAT ;AL = 6DH

Lecture 2 – Extra Examples:


Q1/A/ Write a program to move the content of memory locations ( 200H – 203H ) to (
204H - 207H ):
MOV SI , 200 ;SI = 200
MOV AX , [SI] ;AX = X2X1
MOV [SI+4] , AX ;[SI+4] = X2X1
MOV AX , [SI+2] ;AX = Y2Y1
MOV [SI+6] , AX ;[SI+6] = Y2Y1
B/ Write an equivalent program the previous example using push and pop:
MOV SP , 200 ;SP = 200
POP AX ;AX = X2X1
POP BX ;BX = Y2Y1
MOV SP , 208 ;SP = 208
PUSH BX ;[SP+6] = Y2Y1
PUSH AX ;[SP+4] = X2X1

Q2/A/ Write a Program to move the content of memory locations ( 200H - 2003H ) To (
204H – 207H ) By reverse:
MOV SI , 200 ;SI = 200 MOV SI , 200 ;SI = 200
MOV AX , [SI] ;AX = X2X1 MOV AX , [SI] ;AX = X2X1
XCHG AH,AL ;AX = X1X2 MOV [SI+7] , AL ;[SI+7] = AL
MOV [SI+6] , AX ;[SI+6] = X1X2 OR MOV [SI+6] , AH ;[SI+6] = AH
MOV AX , [SI+2] ;AX = Y2Y1 MOV AX , [SI+2] ;[SI+2] = AX
XCHG AH , AL ;AX = Y1Y2 MOV [SI+5] , AL ;[SI+5] = AL
MOV [SI+4] , AX ;[SI+4] = Y1Y2 MOV [SI+4] , AH ;[SI+4] = AH

B/ Write an equivalent program the previous example using push and pop:
MOV SP , 200 ;SP = 200
POP AX ;AX = X2X1
POP BX ;BX = Y2Y1
XCHG AH , AL ;AX = X1X2
XCHG BH , BL ;BX = Y1Y2
MOV SP , 208 ;SP = 208
PUSH AX ;[SP+6] = X1X2
PUSH BX ;[SP+4] = Y1Y2

Q3/A/ Write a program to exchange the content of memory location ( 200H – 203H ) and
( 204H – 207H ):
TABLE : DS:0200 X1 , X2 , X3 , X4 , Y1 , Y2 , Y3 , Y4
MOV SI , 0200 ;SI = 200
MOV AX , [SI] ;AX = X2X1
XCHG AX , [SI+4] ;AX = Y2Y1 , [SI+4] = X2X1
MOV [SI] , AX ;[SI] = Y2Y1
MOV AX , [SI+2] ;AX = X4X3
XCHG AX , [SI+6] ;AX = Y4Y3 , [SI+6] = X4X3
MOV [SI+2] , AX ;[SI+2] = Y4Y3
B/ Write an equivalent program the previous example using push and pop:
MOV SP , 200 ;SP = 200
POP AX ;AX = X2X1
POP BX ;BX = X4X3
POP CX ;CX = Y2Y1
POP DX ;DX = Y4Y3
PUSH BX ;[SP+6] = X4X3
PUSH AX ;[SP+4] = X2X1
PUSH DX ;[SP+2] = Y4Y3
PUSH CX ;[SP] = Y2Y1

Q4/A/ Write a program to find the square root of number ( 3 ) by XLAT:


TABLE = DS:0200 00 , 01 , 04 , 09 , 10 ….. ( in HEXA )
MOV AL , 03 ;AL = 03 ( to find square root of number 3 in HEXA )
MOV BX , 0200 ;BX = 200 ( starting address )
XLAT ;AL = 09 ( the square root of number 3 in HEXA )

B/ Write an equivalent program the the previous example using push and pop:
TABLE = SS:0200 00 , 01 , 04 , 09 , 10 ….. ( in HEXA )
MOV SP,0203 ;SP = 203 ( to find square root of number 3 )
POP AX ;AL = 09 ( the square root of number 3 in HEXA )
Lecture 3 – Examples
*Write a program to add two numbers in memory locations 0050h and 0051h. Store
the result in memory locations starting at address 0052h: ( slide 6 )
MOV SI , 50 ;SI = 50
MOV AL , [SI] ;AL = X
MOV AH , 00 ;AH = 0 , AX =0X
ADD AL , [SI+1] ;AL = X + [SI+1]
ADC AH , 00 ;AH = AH + 0 + CARRY , RESULT = AX
MOV [SI+2] , AX ;[SI+2] = AX ( Result )

*Write a program to compute 8 bit with 16 bits. The numbers are stored in memory
locations 0500h - 0502h respectively. Store the result in memory locations starting at
address 0503h: ( slide 7 )
MOV SI , 500 ;SI = 500
MOV AX , [SI+1] ;AX = Y2Y1
MOV DX , 0000 ;DX = 0000
ADD AL , [SI] ;AL = Y1 + X1
ADC AH , 00 ;AH = Y2 + 00 + CARRY
ADC DX , 0000 ;DX = DX + 0000 + CARRY , RESULT = DXAX
MOV [SI+3] , AX ;[SI+3] = AX
MOV [SI+5] , DX ;[SI+5] = DX

*Write a program to subtract two numbers stored in memory locations 03F0h and
03F1h. Store the result in memory locations starting at address 03F2h: ( slide 9 )
MOV SI , 03F0 ;SI = 03F0
MOV AL , [SI] ;AL = X
MOV AH , 00 ;AH = 0 , AX =0X
SUB AL , [SI+1] ;AL = X – Y
SBB AH , 00 ;AH = AH - 0 - CARRY , RESULT = AX
MOV [SI+2] , AX ;[SI+2] = AX ( Result )

*Write a program to multiply two bytes stored in memory locations 0200h - 0201h.
Store the result in memory locations starting at memory locations 0300h - 0301h:
( slide 12 )
MOV SI , 200 ;SI = 200
MOV AL , [SI] ;AL = X
MUL BYTE PTR [SI+1] ;AX = X * Y
MOV [SI+100] , AX ;[SI+100] = AX = X * Y
*Write a program to multiply 8 bit number with 24 bit number. They stored in memory
locations 400h - 403h receptively. Store the result in memory locations starting at
address 0450h: ( slide 13 )
MOV SI , 400 ;SI = 400
MOV AL , [SI] ;AL=X1
MOV AH , 00 ;AH = 00
MUL WORD PTR [SI+1] ;DXAX = Y2Y1 * X1
MOV BX , AX ;BX=AX
MOV AL , [SI] ;AL = X1
MUL BYTE PTR [SI+3] ;AX = Y3 * X1
ADD DX , AX ;DX = DX + AX , RESULT = DXBX
MOV [SI+50] , BX ;[SI+50] = BX
MOV [SI+52] , DX ;[SI+52] = DX

*Write a program to compute the result of following arithmetic expression:


Y = X2 + 3X + 5
Store the result in memory locations starting at address 201h. Assume X is stored in
memory location 200h: ( slide 14 )
MOV SI , 200
MOV AL , [SI] ;AL = X
MOV BL , AL ;BL = AL = X
MUL AL ;AX = X2
MOV CX , AX ;CX = X2
MOV AL , 03 ;AL = 03
MUL BL ;AX = 3X
MOV DX , 0000
ADD AX , CX
ADC DX , 0000 ;DXAX = X2 + 3X
ADD AX , 0005 ;DXAX = X2 + 3X + 5
MOV [SI+1] , AX
MOV [SI+3] , DX
16
*Write a program to compute : ( slide 16 )
−3
MOV AX , 0010 ;AX = 10 ( 16 in decimal )
MOV BL , 03 ;BL = 03
NEG BL ;BL = -3
16
IDIV BL ;AX = , ( AL = Quotient , AH = Remainder )
−3
0F
*Write a program to compute : ( slide 18 )
05

MOV AX , 000F ;AX = 0F


MOV BL , 05 ;BL = 05
0F
DIV BL ;AX = , ( AL = Quotient , AH = Remainder )
05

3X2 +X
*Write a program to compute the result of . Store the result in memory
X+2
locations starting at address 201h. Assume X is stored in memory location 200h: (slide 19)
MOV SI , 200
MOV AL , [SI] ;AL = X
MOV BL , AL ;BL = AL = X
MUL AL ;AX = X2
MOV CX , 0003
MUL CX ;DXAX = 3X2
MOV DX , 0000
MOV BH , 00
ADD AX , BX
ADC DX,0000 ;DXAX = 3X2 + X
ADD BX , 0002 ;BX = X + 2
3X2 +X
DIV BX ;DXAX = , ( AX = Quotient , DX = Remainder )
X+2

*Write a program to compute the average of three bytes stored in memory starting at
address 051Fh: ( slide 20 )
MOV SI , 051F
MOV AL , [SI] ;AL = X1
MOV AH , 00
ADD AL , [SI+1]
ADC AH , 00 ;AX = X1 + X2
ADD AL , [SI+2]
ADC AH , 00 ;AX = X1 + X2 + X3
MOV BL , 03
X1+X2+X3
DIV BL ;AX = , ( AL = Quotient , AH = Remainder )
3
*Write a set of instructions to do the following: ( slide 23 )
1. Clear bits 0 and 1 of register AL 2. Set bits 6 and 7 of register BH
1111 1111 0000 0000
1111 1100 = FC 1100 0000 = C0
Answer// AND AL , FC Answer// OR BH , C0

3. Invert bit 5 of register CL


0000 0000
0010 0000 = 20
Answer// XOR CL , 20

*Write a program to compute the result of following expression:


Y = ̅̅̅̅̅̅̅̅̅̅̅̅̅̅
A+B ⊕C
where A, B, and C are stored in memory locations 1A00h - 1A02h: ( slide 24 )
MOV SI , 1A00
MOV AL , [SI+1] ;AL = B
XOR AL , [SI+2] ;AL = B ⊕ C
OR AL , [SI] ;AL = A + B ⊕ C
NOT AL ;Y = ̅̅̅̅̅̅̅̅̅̅̅̅̅̅
A+B ⊕C

*Multiply AX by 10 using shift instructions: ( slide 28 )


SHL AX , 1 ;AX 2 TIMES
MOV BX , AX ;BX 2 TIMES 3 shift 2 shift 1 shift
SHL AX , 1 ;AX 4 TIMES 8 times 4 times 2 times
SHL AX , 1 ;AX 8 TIMES 23 22 21
ADD AX , BX ;AX = 8 + 2 = 10 TIMES

*Multiply AX by 18 using shift instructions: ( slide 28 )


SHL AX , 1 ;AX 2 TIMES
MOV BX , AX ;BX 2 TIMES
MOV CL , 03
SHL AX , CL ;AX 16 TIMES
ADD AX , BX ;AX = 16 + 2 = 18 TIMES

*Multiply AX by 14 using shift instructions:


SHL AX , 1 ;AX 2 TIMES
MOV BX , AX ;BX 2 TIMES
MOV CL , 03
SHL AX , CL ;AX 16 TIMES
SUB AX , BX ;AX = 16 - 2 = 14 TIMES
Lecture 3 – Extra Examples:
Q1/Write a program to compute the result of following arithmetic expression:
Y = X3 - X2 - X
The value of x is stored in memory location 2000:1020
MOV AX , 2000
MOV DS , AX
MOV SI , 1020 ;2000:1000 = DS:SI
MOV AL , [SI] ;AL = X
MOV BL , AL ;BL = X
MUL AL ;AX = X2
MOV CX , AX ;CX = X2
XOR BH , BH ;BX = 0X
MUL BX ;DXAX = X3
SUB AX , CX
SBB DX , 0000 ;DXAX = X3 - X2
SUB AX , BX
SBB DX , 0000 ;DXAX = X3 - X2 – X

Q2/Write a program to compute the result of following arithmetic expression:


Y = X4 – X3 – X2
The value of x is stored in memory location 0500H
Y = X 4 – X 3 – X 2 = X2 ( X 2 – X – 1 )
MOV SI , 500
MOV AL , [SI] ;AL = X
MOV BL , AL ;BL = X
MUL AL ;AX = X2
MOV CX , AX ;CX = X2
XOR BH , BH ;BX = 0X
SUB AX , BX ;AX = X2 – X
SUB AX , 0001 ;AX = X2 – X – 1
MUL CX ;DXAX = X2 ( X2 – X – 1 )
Q3/Write a program to compute the result of following arithmetic expression:
Y = X4 + X3 + X2
The value of x is stored in memory location 0500H
Y = X 4 + X 3 + X 2 = X2 ( X 2 + X + 1 )
MOV SI , 500
MOV AL , [SI] ;AL = X
MOV BL , AL ;BL = X
MUL AL ;AX = X2
XOR BH , BH ;BX = 0X
ADD BX , AX ;BX = X2 + X
ADD BX , 0001 ;BX = X2 + X + 1
MUL BX ;DXAX = X2 ( X2 + X + 1 )

Q4/Write a program to compute the result of following arithmetic expression:


Y = X4 - X3 + X2
The value of x is stored in memory location 0500H
Y = X4 - X3 + X2 = X2 ( X 2 - X + 1 )
MOV SI , 500
MOV AL , [SI] ;AL = X
MOV BL , AL ;BL = X
MUL AL ;AX = X2
MOV CX , AX ;CX = X2
XOR BH , BH ;BX = 0X
SUB AX , BX ;AX = X2 – X
ADD AX , 0001 ;AX = X2 – X + 1
MUL CX ;DXAX = X2 ( X2 – X + 1 )
Q5/The three lengths of parallelogram ( X , Y , and Z ) stored in memory at starting
address DS:200. Write a sequence of instructions to compute the surface area of the
shape and store the result in the top pf the stack.
Surface area = 2*x*y + 2*x*z + 2*y*z
2*x*y + 2*x*z + 2*y*z = 2 ( x*y + x*z + y*z )
MOV SI , 200
MOV AL , [SI] ;AL = X
MUL BYTE PTR [SI+1] ;AX = X*Y
MOV DX , 0002
MUL DX ;DXAX = 2*X*Y
MOV CX , DX
MOV BX , AX ;CXBX = 2*X*Y
MOV AL , [SI] ;AL = X
MUL BYTE PTR [SI+2] ;AX = X*Z
MOV DX , 0002
MUL DX ;DXAX = 2*X*Z
ADD BX , AX
ADC CX , DX ;CXBX = 2*X*Y + 2*X*Z
MOV AL , [SI+1] ;AL = Y
MUL BYTE PTR [SI+2] ;AX = Y*Z
MOV DX , 0002
MUL DX ;DXAX = 2*Y*Z
ADD AX , BX
ADC DX , CX ;DXAX = 2*X*Y + 2*X*Z + 2*Y*Z

Q6/ N1 is 24 bit and N2 is 8 bit signed numbers stored in memory location starting
address DS:300 . Write a program to compute N1/N2
MOV SI , 300
MOV AX , [SI] ;AX = X2X1
MOV DL , [SI+2]
CBW ;DX = X3
MOV BL , [SI+3]
CBW ;BX = Y
IDIV BX ;DXAX = N1/N2 = X3X2X1/Y
Q7/ Write a program to compute the result of following arithmetic expression:
X
F = X2 − X +
2
The value of x is stored in memory location 2000:1020
MOV AX , 2000
MOV DS , AX
MOV SI , 1020
MOV AL , [SI] ;AL = X
MOV BL , AL ;BL = X
MUL AL ;AX = X2
XOR BH , BH ;BX = 0X
SUB AX , BX ;AX = X2 – X
MOV CX , AX ;CX = X2 – X
MOV AL , [SI] ;AL = X
XOR AH , AH
MOV BL , 02
X
DIV BL ;AX =
2
XOR AH , AH
X
ADD AX , CX ;AX = X2 – X +
2

Q8/Write a program to compute the result of following arithmetic expression:


X4 −2X−5
Y= , DS:200 = X
X2
MOV SI , 200
MOV AL , [SI]
MOV BL , 02
MUL BL ;AX = 2X
MOV BX , AX ;BX = 2X
MOV AL , [SI] ;AL = X
MUL AL ;AX = X2
MOV CX , AX ;CX = X2
MUL AX ;DXAX = X4
SUB AX , BX
SBB DX , 0000 ;DXAX = X4 – 2X
SUB AX , 0005 ;DXAX = X4 – 2X – 5
X4 −2X−5
DIV CX ;DXAX = Y =
X2
Q9/Write a program to compute the result of following expression:
Y = ̅̅̅̅̅̅̅̅̅̅̅
A+B ̅ . C ⊕ A. C
where A, B, and C are stored in memory locations 200h – 202h
MOV SI , 200
MOV AL , [SI+1] ;AL = B
NOT AL ̅
;AL = B
AND AL , [SI+2] ̅ .C
;AL = B
OR AL , [SI] ;AL = A + B ̅ .C
NOT AL ̅̅̅̅̅̅̅̅̅̅̅
;AL = A +B ̅ .C
MOV BL , [SI] ;BL = A
AND BL , [SI+2] ;BL = A.C
XOR AL , BL ̅̅̅̅̅̅̅̅̅̅̅
;AL = A +B ̅ . C ⊕ A. C

Q10/Write a program to compute the result of following expression:


Y = ̅̅̅̅̅̅̅̅̅̅̅̅
A ⊕ ̅̅̅̅̅
B. C
where A, B, and C are stored in memory locations 200h – 202h
MOV SI , 200
MOV AL , [SI+1] ;AL = B
AND AL , [SI+2] ;AL = B.C
NOT AL ;AL = ̅̅̅̅̅
B. C
XOR AL , [SI] ;AL = A ⊕ ̅̅̅̅̅
B. C
NOT AL ;AL = ̅̅̅̅̅̅̅̅̅̅̅̅
A ⊕ B. ̅̅̅̅̅
C
Lecture 4 – Examples
*Write a program to compute the sum of numbers in memory range 0500h - 0520h.
Store the result in memory locations starting at the address 600h: ( slide 7 )
MOV SI , 500
MOV CX , 0021 ;520 – 500 = 20 ---- > 20 +1 = 21
XOR AX , AX
NEXT: ADD AL , [SI]
ADC AH , 00
LOOP NEXT ;LOOP NEXT = DEC CX
MOV [SI+100] , AX JNZ NEXT ; ZF=0

* Write a program to count positive numbers in memory range 0500h - 0520h: ( slide 9 )
XOR AH , AH ;To count positive number
MOV SI , 500
MOV CX , 0021 ;520 – 500 = 20 ---- > 20 +1 = 21
NEXT : MOV AL , [SI]
TEST AL , AL ;In order to know if the number is + or –
JS NEXT1 ;If number is negative ( SF=1 ) jump to NEXT1
INC AH ;If number is positive ( SF=0 ) INC AH to count number
NEXT1: INC SI ;To go to next memory location and then repeat the same step
LOOP NEXT

*Write a program to find the largest element in block of data. The length of the block
stored in memory location A001h and the block starting at memory address A002h.
Store the result in memory location A000h: ( slide 11 )
MOV SI , A000
MOV AL , [SI+1] ; AL = X
CMP AL , [SI+2] ; [SI+2] = Y , CMP AL , [SI+2] = X – Y
JNC NEXT ;If X>Y (CF = 0) jump to NEXT
AL , [SI+2] ;AL = Y , in case if X<Y (CF = 1)
NEXT : [SI] , AL ;[SI] = X If X>Y OR [SI] = Y If X<Y
*Convert the following segment of program written in C to Assembly language.
Assume x and y are stored in conseqiuve memory locations and index by SI register:
if (x < y) { ( slide 12)
y=y-1;
x=x+1;
}
...
MOV AL , [SI] ;AL = X
MOV BL , [SI+1] ;BL = Y
CMP AL , BL
JNC NEXT
DEC BL ;BL = Y – 1
XOR AH , AH
INC AX ;AX = X + 1
NEXT : . . . . . . ;we don’t have ELSE question so, it’s not containing instructions

*Repeat previous example if the source code is modified as follows:


if (x < y) { ( slide 13)
y=y-1;
x=x-1;
}
else {
y=y-2;
x=x-2;
}
XOR AX , AX
XOR BX , BX
MOV AL , [SI] ;AL = X
MOV BL , [SI+1] ;BL = Y
CMP AL , BL
JNC NEXT
DEC BL ;BL = Y – 1
DEC AL ;AL = X – 1
NEXT: DEC BL
DEC BL ;BX = Y – 2
DEC AL
DEC AL ;AX = X – 2
NOTE//This two questions are special for lec5,, you have to store result in variables.
In lec5 examples we will solve it.
Examples ( slide 14 )
2. Write a program to compute the sum of numbers greater than 31h in a table of 30
Bytes:
TB//The number is greater if flag registers: CF = 0 , ZF = 0
MOV CX , 1E ;30 --- > 0000 1110 = 1Eh
NEXT : MOV BL , [SI]
CMP BL , 31
JC NEXT1 ;If number less than 31
JZ NEXT1 ;If number equal to 31
ADD AL , BL ;If number greater than 31
ADC AH , 00 ;AX = the sum of the number greater than 31
NEXT1: INC SI
LOOP NEXT

3. Write a program to find the maximum number in an array of 10 bytes:


XOR AL,AL
MOV CX,000Ah
NEXT : MOV AH,[SI]
CMP AH,AL
JC NEXT1 ;If number is less than ( CF=1 ) jump NEXT1
MOV AL,AH
NEXT1: INC SI
LOOP NEXT

4. Write a program to compute the sum of numbers in the range 00 – FF:


XOR AX , AX
MOV CX , 00FF
NEXT: ADD AX , CX
LOOP NEXT

5. Write a program to compute the sum of odd numbers in the range 00 – FF:
XOR AX , AX
MOV CX , 00FF
NEXT : TEST CX , 0001
JZ NEXT1 ;for even number ( ZF=1 ) but, for odd number ( ZF=0 )
ADD AX , BX
NEXT1: LOOP NEXT
6. Write a program to count negative numbers in memory range 1000h – 1070h:
XOR AH , AH ;To count negative number
MOV SI , 1000
MOV CX , 0071 ;1070 – 1000 = 70 --- > 70 + 1 = 71
NEXT : MOV AL , [SI]
TEST AL , AL
JNS NEXT1 ;If number is positive ( SF=0 ) jump to NEXT1
INC AH ;If number is negative ( SF=1 ) INC AH to count number
NEXT1: INC SI
LOOP NEXT

*A table of 10 bytes stored in memory locations starting at address 300h. Write a


program using a subroutine to construct another table (B) according to following
expression: Y = X2 – 5X + 3Y
Store table B at memory address 400h. ( slide 17 )
MOV SI , 03 TABLE:
MOV DI , 400 MOV AL , [SI] ;AL = X
MOV CX , 000A MOV BL , AL ;BL = X
NEXT: CALL TABLE MUL AL ;AX = X2
MOV [DI] , DX MOV DX , AX ;DX = X2
ADD DI , 02 MOV AL , 05 ;AL = 5
INC SI MUL BL ;AX = 5X
LOOP NEXT SUB DX , AX ;DX = X2 – 5X
ADD DX , 0003 ;DX = X2 – 5X + 3
RET
*Write an 8086 assembly language program that will perform:
5X + 6Y + Y/8
where X is an unsigned 8-bit number stored at offset 0100H and Y is a 16-bit signed
number stored at offsets 0200H and 0201H. Neglect the remainder of Y/8. Store the
result in registers BX and BP. BX holds the low 16-bit of the 32-bit result and BP holds the
high 16-bit of the 32-bit result: ( slide 19 )
MOV SI , 200
MOV AL , [SI] ;AL = X
MOV CL , 05
MUL CL ;AX = 5X
MOV BX , AX ;BX = 5X
MOV AL , [SI+1] ;AL = Y
MOV CL , 06
MUL CL ;AX = 6Y
ADD BX , AX ;BX = 5X + 6Y
MOV AL , [SI+1] ;AL = Y
XOR AH , AH ;AX = 0Y
MOV CL , 08
DIV CL ;AX = Y/8 ( AL = Quotient , AH = Remainder )
XOR AH , AH ;To remove the remainder from the result in AX
ADD AX , BX ;AX = 5X + 6Y + Y/8

*Write a program to count number that divisible by 8 in the range 00 – FF: ( slide 20 )
MOV CX, 00FF
MOV BX, 0008
NEXT : MOV AX, CX
DIV BL
TEST AH, AH
JNZ NEXT1 ;when we have a remainder so the result ≠ 0 ( ZF = 0 )
INC BH ;If we don’t have remainder that means this number is divisible of 8
NEXT1: LOOP NEXT
Lecture 4 – Extra Examples:
Q1/Write a program to find average of positive numbers in 10 bytes from address 1000h:
MOV SI , 1000
MOV CX , 000A
XOR AX , AX
XOR BH , BH
NEXT : MOV BL , [SI]
TEST BL , BL ;To test if the number is + or –
JS NEXT1 ;If number is negative ( SF=1 ) jump to NEXT1
ADD AL , BL
ADC AH , 00 ;AX = sum of positive numbers for average
INC BH ;Count positive number
NEXT1: INC SI
LOOP NEXT
sum AX
DIV BH ; = = average
count BH

NOTE//
1- To find average of negative numbers exchange:
- ( JS NEXT1 ) by ( JNS NEXT1 )
2- To find average of even numbers exchange:
- ( TEST BL , BL ) by ( TEST BL , 01 )
- ( JS NEXT1 ) by ( JNZ NEXT1 )
3- To find average of odd numbers exchange:
- ( TEST BL , BL ) by ( TEST BL , 01 )
- ( JS NEXT1 ) by ( JZ NEXT1 )

Q2/ Write a program to find sum of positive numbers in 10 bytes from address 1000h:
MOV SI , 1000
MOV CX , 000A
XOR AX , AX
NEXT : MOV BL , [SI]
TEST BL , BL ;To test if the number is + or –
JS NEXT1 ;If number is negative ( SF=1 ) jump to NEXT1
ADD AL , BL
ADC AH , 00 ;AX = sum of positive numbers
NEXT1: INC SI
LOOP NEXT
NOTE//
1- To find sum of negative numbers exchange:
- ( JS NEXT1 ) by ( JNS NEXT1 )
2- To find sum of even numbers exchange:
- ( TEST BL , BL ) by ( TEST BL , 01 )
- ( JS NEXT1 ) by ( JNZ NEXT1 )
3- To find sum of odd numbers exchange:
- ( TEST BL , BL ) by ( TEST BL , 01 )
- ( JS NEXT1 ) by ( JZ NEXT1 )

Q3/ Write a program to count positive numbers in 10 bytes from address 1000h:
MOV SI , 1000
MOV CX , 000A
XOR AH , AH ;To count positive number
NEXT : MOV AL , [SI]
TEST AL , AL ;In order to know if the number is + or –
JS NEXT1 ;If number is negative ( SF=1 ) jump to NEXT1
INC AH ;If number is positive ( SF=0 ) INC AH to count number
NEXT1: INC SI ;To go to next memory location and then repeat the same step
LOOP NEXT
NOTE//
1- To find count of negative numbers exchange:
- ( JS NEXT1 ) by ( JNS NEXT1 )
2- To find count of even numbers exchange:
- ( TEST AL , AL ) by ( TEST AL , 01 )
- ( JS NEXT1 ) by ( JNZ NEXT1 )
3- To find count of odd numbers exchange:
- ( TEST AL , AL ) by ( TEST AL , 01 )
- ( JS NEXT1 ) by ( JZ NEXT1 )
Q4/Write a program to average number that divisible by 5 in 10 bytes from address
1000h:
MOV SI , 1000H
MOV CX, 000AH
XOR DX , DX
MOV BX, 0005H
NEXT : XOR AH , AH
MOV AL , [SI]
DIV BL
TEST AH, AH
JNZ NEXT1 ;when we have a remainder so the result ≠ 0 ( ZF = 0 )
ADD DL , [SI]
ADC DH , 00H ;sum = DX
INC BH ;count
NEXT1: INC SI
LOOP NEXT
MOV AX , DX
DIV BH

Q5/Write a program to sum number that divisible by 5 in 10 bytes from address 1000h:
MOV SI , 1000
MOV CX, 000A
XOR DX , DX
MOV BX, 0005
NEXT : XOR AH , AH
MOV AL , [SI]
DIV BL
TEST AH, AH
JNZ NEXT1 ;when we have a remainder so the result ≠ 0 ( ZF = 0 )
ADD DL , [SI]
ADC DH , 00 ;sum = DX
NEXT1: INC SI
LOOP NEXT
Q6/Write a program to count number that divisible by 5 in 10 bytes from address 1000h:
MOV SI , 1000
MOV CX, 000A
MOV BX, 0005
NEXT : XOR AH , AH
MOV AL , [SI]
DIV BL
TEST AH, AH
JNZ NEXT1 ;when we have a remainder so the result ≠ 0 ( ZF = 0 )
INC BH
NEXT1: INC SI
LOOP NEXT

Q7/Write a program to count number that divisible 7 by 3 in 10 bytes from address


1000h:
MOV SI , 1000
MOV CX, 000A
MOV BX, 0015 ;7*3 = 21 In decimal ---- > 15H
NEXT : XOR AH , AH
MOV AL , [SI]
DIV BL
TEST AH, AH
JNZ NEXT1 ;when we have a remainder so the result ≠ 0 ( ZF = 0 )
INC BH
NEXT1: INC SI
LOOP NEXT
Lecture 5 – Examples
*Homework ( slide 32 )
1. Two arrays A and B, each has 10 elements (8 bit signed number). Write a program to
construct another array (C) defined as follows: C(n) = A2(n) − B(n)
.MODEL small
.STACK 100H
.DATA
A DB 1FH,0EFH,12H,05H,2FH,0CEH,34H,07H,0FH,0AH ;Initialize 10 bytes what you want
B DB 01H,02H,03H,04H,05H,06H,07H,08H,09H,0AH
C DW 10 DUP (?) ;save 10 words for variable C
.CODE
START: MOV AX , @DATA
MOV DS , AX
MOV SI , OFFSET A
MOV DI , OFFSET B
MOV BX , OFFSET C
MOV CX , 000A
NEXT : MOV AL , [SI] ;AL = A
MUL AL ;AX = A2
SUB AL , [DI]
SBB AH , 00 ;AX = A2 – B
MOV [BX] , AX ;C = A2 – B
INC SI
INC DI
ADD BX , 0002
LOOP NEXT
MOV AH , 04CH
INT 21H
END START
2.Write a program to find the average of negative numbers in array of 50 bytes:
.MODEL small
.STACK 100H
.DATA
A DB 0FH,0FEH,0FDH,0CH,0BH,0AH, …. ;Initialize 50 bytes
AVERAGE DW ?
.CODE
START: MOV AX , @DATA
MOV DS , AX
MOV SI , OFFSET A
XOR AX , AX
XOR BH , BH
MOV CX , 0032 ;50 = 32H
NEXT : MOV BL , [SI]
TEST BL , BL
JNS NEXT1
ADD AL , BL
ADC AH , 00
INC BH
NEXT1: INC SI
LOOP NEXT
DIV BH
MOV AVERAGE , AX
MOV AH , 04CH
INT 21H
END START
3. Write a program to compute the average of positive and negative numbers in the array
of 100 bytes.
.MODEL small
.STACK 100H
.DATA
A DB 0FH,0FH,0FFH,0FFH,….
P DW ? ;Average of positive numbers
N DW ? ;Average of negative numbers
.CODE
START: MOV AX , @DATA
MOV DS , AX
MOV SI , OFFSET A
XOR DX , DX
XOR BX , BX
XOR SP , SP
XOR BP , BP
MOV CX , 0064H
NEXT : XOR DH , DH
MOV DL , [SI]
TEST DL , DL
JNS NEXT2
ADD SP , DX ;Sum of negative numbers
INC BL ;Count negative numbers
JMP NEXT3
NEXT2: XOR DH , DH
ADD BP , DX ;Sum of positive numbers
INC BH ;Count positive numbers
NEXT3: INC SI
LOOP NEXT
MOV AX , BP
DIV BH ;Average positive numbers
MOV P , AX
MOV AX , SP
DIV BL ;Average negative numbers
MOV N , AX
MOV AH , 04CH
INT 21H
END START
4.An array contains 100 bytes. Write a program to construct another array according to
the following relation: y = x2 − 2x + 1
.MODEL small
.STACK 100H
.DATA
A DB 055H,066EH,077H, ….
B DW 100 (?)
.CODE
START: MOV AX , @DATA
MOV DS , AX
MOV SI , OFFSET A
MOV DI , OFFSET B
MOV CX , 0064H
NEXT : MOV AL , [SI]
MOV BL , AL
MUL AL
ADD BL , BL
SUB AX , BX
INC AX
MOV [DI] , AX
INC SI
ADD DI , 0002
LOOP NEXT
MOV AH , 04CH
INT 21H
END START
5.Write an assembly language program to computes the sum of numbers that divisible
by 05 in memory range 2000:1A00 to 2000:1AF0:
.MODEL small
.STACK 100H
.DATA
A DB 055H,066EH,077H, ….
B DW 100 (?)
.CODE
START: MOV AX , 2000H
MOV DS , AX
MOV SI , 1A00H
XOR DX , DX
MOV BX, 0005
MOV CX, 00F1H ;Last – first + 1
NEXT : XOR AH , AH
MOV AL , [SI]
DIV BL
TEST AH, AH
JNZ NEXT1
ADD DL , [SI]
ADC DH , 00
NEXT1: INC SI
LOOP NEXT
MOV AH , 04CH
INT 21H
END START
Lecture 6 – Examples
*Homework ( slide 14 )
1.Write a program to move the first 100 bytes from segment 1000h to segment 2000h
after squaring each element. Use string instructions:
.MODEL tiny
.STACK 100H
.CODE
ORG 100H
START: MOV AX , 1000H
MOV DS , AX
MOV SI , 0
MOV AX , 2000H
MOV ES , AX
MOV DI , 0
MOV CX , 0064H ;CX = 100
CLD
NEXT : LODSB ;SI -----> AL
MUL AL ;AX = X2
STOSW ;AX ----> DI
LOOP NEXT
MOV AH , 4CH
INT 21H
END START

2.Write a program to count the number of memory locations that contain 0Fh in segment
1000 using string instructions:
.MODEL tiny
.STACK 100H
.CODE
ORG 100H
START: MOV AX , 1000H
MOV ES , AX
MOV DI , 0000
MOV CX , 0000
MOV AL , 0FH
XOR DX , DX
CLD NEXT2 : LOOP NEXT
NEXT : SCASB MOV AH , 4CH
JNZ NEXT2 INT 21H
INC DX END START

You might also like