Professional Documents
Culture Documents
Assembly Language
*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
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
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
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
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
* 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
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
*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
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