Sheet 3 Solution
Sheet 3 Solution
Faculty of Engineering
Division of Communications & Electronics
e) PUSHF
SP ← SP-2
[SP] ← The flag register
f) IN AL,0EEH
AL ← byte from the input device connected at address 0EE h
1
The first: The input device connected at address 10 h ← Word in AX (Direct port
addressing mode)
The second: The input device connected at the address stored in DX ← Word in AX
(Indirect port addressing mode)
3) Write a program that fills memory locations from 5000H to 5FFFH with value 77H.
4) Write a program that uses REP and MOVS to copy an array. Assume that the length
of the array is stored at memory location 0400H, the original array starts at 0340H
and the destination area starts at 360H. Run your program using:
[0400H] = 03H, [0340H] = 4FH, [0341H] = 55H, [0342H] = 54H.
5) Write a program for the 8086 microprocessor that moves a block of data located in
memory from address B0200 to address B02F9 to a new location starting at address
B0600. The contents of the Data segment register DS is to be B000.
MOV CX, 0FAh ; 2F9h – 200h + 1 = FAh
MOV AX, 0B000h
MOV DS, AX
MOV ES, AX
MOV SI, 0200h
MOV DI, 0600h
CLD
REP MOVSB
6) a) Write a program that forms a lookup table originated at 380H of the squares of
the decimal digits from zero to 8d.
CLD
MOV AX, 0
MOV ES, AX
MOV DI, 0380h
MOV AL, 0
AGAIN:
2
PUSH AX
MUL AL
STOSB
POP AX
CMP AL, 8
JE EXIT
INC AL
JMP AGAIN
EXIT: HLT
b) Use the formed table in (a) to square the digit in 400H and place the square in
401H. Run your program using:
i) [400H] = 04H ii) [400H] = 07H
MOV AX, 0
MOV DS, AX
MOV BX, 0380h
MOV AL, [0400h]
XLAT
MOV [0401h], AL
c) Write a program that uses the table in (a) to add the squares of 400H and 401H
and put the sum in 402H. Run the program using: [400H] = 03H and [401H] =
06H.
MOV AX, 0
MOV DS, AX
MOV BX, 0380h
MOV AL, [0400h]
XLAT
MOV CL, AL
MOV AL, [0401h]
XLAT
ADD AL, CL
MOV [0402h], AL
AX = 16 and BL = -3
3
16/-3 = - (16/3) = - (15/3 + 1/3) = -(5 + 1/3) = -5 +1/-3
AL ← -5 = FBh
AH ← 1 = 01h
OR BP, 1122h
OR AH, WHEN
MOV WHEN, AH
e) XOR the data addressed by BX and DX and save the result in memory.
MOV [BX], DX
f) XOR the data stored 30 words after the location addressed by BP with DX and
save the result in DX.
10) Develop a short sequence of instructions that perform the following operations:
a) Add the contents of AL, BL, CL, DL and AH and save the result in DH.
MOV DH, AL
ADD DH, BL
ADD DH, CL
ADD DH, DL
ADD DH, AH
b) Subtract the numbers in DI, SI and BP from the AX register and store the
difference in BX.
MOV BX, AX
SUB BX, DI
4
SUB BX, SI
SUB BX, BP
c) Cube the 8-bit number found in CL, assuming that CL contains a 5 initially.
MOV AL, CL
MUL CL
MUL CL
MOV CL, AL
d) Divide the number in BL by the number in CL and then multiply the result by 2.
MOV AL, BL
CBW
IDIV CL
SAL CL, 1
CBW
MOV BX, AX
CBW
MOV CX, AX
SAL CX, 3 ; CX ← 8 AL
SUB CX, AX ; CX ← 8 AL – AL = 7 AL
MOV AL, BL
CBW
MOV BX, AX
SAL AX, 2 ; AX ← 4 BL
g) Set the rightmost 4 bits of AX, clear the 4 leftmost bits of AX and invert bit
positions 7, 8 and 9 of AX.
OR AX, 000Fh
AND AX, 0FFFh
5
XOR AX, 0380h
11) Two 32 bits numbers, one is stored at DS: 200 and the other at DS: 100.Write a
program to add these two numbers and stores the result at DS: 300
MOV DX, 0
MOV AX, [200]
MOV BX, [100]
ADD AX, BX
ADC DX, [202]
ADD DX, [102]
MOV [300], AX
MOV [302], DX
12) Write a program that add 2 hexadecimal digits stored in memory location DS: 100
as one byte.
13) Write a program that adds an array of unsigned 8-bit numbers starting at 340H and
puts the sum in 400H. The length of the array is in 401H. Run the program with the
following data: [401] = 04H, [340H] =3EH, [341H] =47H, [342H] =F5H, [343H]
=2AH.
MOV [400h], AH
14) Write a program that adds an array of unsigned 16-bit numbers starting at 340H
and puts the sum in 400H. The length of the array is in 401H. Run the program with
the following data: [401] = 02H, [340H]=36H, [341H]=21H, [342H]=97H,
[343H]=18H.
6
MOV CX, [0401h]
MOV SI, [0340h]
CLD
MOV DX, 0
AddArray:
LODSW
ADD DX, AX
LOOP AddArray
MOV [400h], DX
MOV AX, 7
MOV CX, 6
Fact:
MUL CX
LOOP Fact
16) Write a program that stores the BCD numbers in the memory starting at address
340H. Then the program doubles each element and stores the results in the same
memory location.
MOV AL, 0
MOV DI, 340h
CLD
Again1:
STOSB
INC AL
CMP AL, 10
JNE Again1
MOV CX, 10
MOV DI, 340h
MOV SI, DI
Again2:
LODSB
SHL AL, 1
STOSB
LOOP Again2
SHR DI, 1
b) Move all the bits in AL left 1 place making sure a 0 moves into the rightmost bit
position.
SHL AL, 1
7
c) Rotate the bits left 1 place in SI.
ROL SI, 1
RCR DX, 1
e) Move the DH register right 1 place with the sign bit shifted through DH.
SAR DH, 1
18) If AX=6F55H, DS=3245H and CX=0100H, find the change in AX after the execution
of each of the following lines and the value of CX at the end of the program
a) MOV AX,1234H
AX ← 1234h
b) MOV BX ,AX
BX ← 1234h
c) NOT AX
AX ← EDCBh
d) AND AX,BX
e) OR AX ,BX
f) XOR CX,CX
g) MOV CL,02H
CX ←0002h
h) SHL AX,CL
i) SAR AX,CL
8
AX ← 48D02h >> 2 = 1234h
j) RCL AX,CL
Carry is zero
AX ← 1234h << 2 = 48D0h
19) Write an assembly program to sort 50 unsigned numbers stored in memory starting
at address 50C70H in an ascending order.
LOUT:
POP SI
INC SI
INC DI
LOOP LOUT
SAL AX, 2
9
b) 10d
; 10d is 8 + 2
MOV BX, AX
SAL BX, 3 ; BX ← 8AX
SAL AX, 1 ; AX ← 2AX
ADD BX, AX
10