0% found this document useful (0 votes)
245 views

Sheet 3 Solution

This document contains an introduction to assembly programming lecture material. It includes examples of assembly instructions like MOV, LEA, LDS, LES, PUSHF, IN, and OUT. It provides explanations of differences between related instructions and examples of using instructions like REP, MOVS, and XLAT. It also contains problems to write short assembly programs that perform tasks like filling memory, copying arrays, and performing arithmetic operations.

Uploaded by

Mohamed Alfarash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
245 views

Sheet 3 Solution

This document contains an introduction to assembly programming lecture material. It includes examples of assembly instructions like MOV, LEA, LDS, LES, PUSHF, IN, and OUT. It provides explanations of differences between related instructions and examples of using instructions like REP, MOVS, and XLAT. It also contains problems to write short assembly programs that perform tasks like filling memory, copying arrays, and performing arithmetic operations.

Uploaded by

Mohamed Alfarash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

University of Alexandria

Faculty of Engineering
Division of Communications & Electronics

Subject Name: Microprocessors Lecturer: Dr.Nayera Sadek


Academic Year: 2016-2017 Assistants: Khaled Essam

Third Year – Semester 1

SHEET 3 Introduction to Assembly Programming

1) Describe the function of each of the following instructions:


a) MOV BX, 03FFH
Bx ← 03FF h

b) LEA AX, [DATA]


EA ← DATA (the offset itself)

c) LDS DI, [LIST]


DI ← [LIST+1], [LIST]
DS ← [LIST+3], [LIST+2]

d) LES BX, [LIST]


BX ← [LIST+1], [LIST]
ES ← [LIST+3], [LIST+2]

e) PUSHF
SP ← SP-2
[SP] ← The flag register

f) IN AL,0EEH
AL ← byte from the input device connected at address 0EE h

2) What is the difference between the two following instructions:


a) MOV BX, [DATA] and LEA BX, [DATA]
The first: BX ← word at the label DATA
The second: BX ← DATA itself

b) IN AL, 10H and IN AL, DX


The first: AL ← byte from the input device connected at address 10 h (Direct port
addressing mode)
The second: AL ← byte from the input device connected at the address stored in DX
(Indirect port addressing mode)

c) OUT 10H, AX and OUT DX, AX

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.

MOV AX, 5000h


MOV ES, AX
MOV DI, 0
MOV CX, 1000 h
MOV AL, 77H
CLD
REP STOSB

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.

MOV CX, [0400h]


MOV AX, 0
MOV DS, AX
MOV ES, AX
MOV SI, 0340h
MOV DI, 0360h
CLD
REP MOVSB

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

7) What is wrong with the following instructions?


a) ADD CL, AX
CL is 8 bits and AX is 16 bits wide.
b) INC [BX]
Does BX point to a byte or a word? We don't know because we should use BYTE PTR or
WORD PTR
c) ADD [BX],12H
Is this an Immediate Addressing mode or a Register Indirect mode? We cannot mix the
modes.

8) Find the result of IDIV BL if


a) AX=0010H and BL=FDH

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

b) AX=-16d and BL=3d


-16/3 = - (16/3) = - (15/3 + 1/3) = -(5 + 1/3) = -5 - 1/3
AL ← -5 = FBh
AH ← -1 = FFh

9) Write the instruction that performs the following operations:


a) AND BX with DX and save the result at BX.
AND BX, DX
b) AND the data stored 4 words before the location addressed by SI with DX and
save the result in DX.

AND DX, -8[SI] ; 8 bytes are 4 words

c) OR 1122H with BP.

OR BP, 1122h

d) OR AH with memory location WHEN and save the result in WHEN.

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.

XOR DX, 60[BP] ; 60 bytes are 30 words

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

e) Save the content of AL in BX as word.

CBW
MOV BX, AX

f) Implement the following equation: 7(AL)-5(BL)-(DX)/8 and store the result in


AX. (Assume that DX is divisible by 8).

We can expand it to:


8AL – 4BL– AL – BL – DX / 8

SAR DX, 3 ; DX ← DX/8

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

SUB CX, AX ; CX ← 7AL – 4 BL


SUB CX, BX ; CX ← 7AL – 4 BL – BL = 7AL – 5BL
SUB CX, DX ; CX ← 7AL – 5BL –DX /8
MOV AX, CX

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

h) Scan through a section of byte memory called for a 66H.

MOV CX, NUM


MOV AL, 66h
CLD
REPNE SCASB

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.

MOV AX, [100]


ROR AX, 4
SHR AH, 4
ADD AL, AH

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 CX, [0401h]


MOV SI, [0340h]
CLD
MOV AH, 0
AddArray:
LODSB
ADD AH, AL
LOOP AddArray

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

15) Write a program to calculate factorial N (given N = 7).

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

17) Write the instruction that performs the following operations:


a) Shift DI right 1 place with a 0 moved into the leftmost bit position.

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

d) Rotate through carry DX 1 place to the right.

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

AX ← EDCBh & 1234h = 0000h

e) OR AX ,BX

AX ← 0000h | 1234h = 1234h

f) XOR CX,CX

CX ← 0100h ^ 0100h = 0000h

g) MOV CL,02H

CX ←0002h

h) SHL AX,CL

AX ← 1234h << 2 = 48D0h

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.

; Prepare the address


MOV CX, 50
MOV AX, 5000h
MOV DS, AX
MOV SI, 0C70h
MOV DI, SI

LOUT:

MOV DX, CX ; Counts the number of inner loop iterations


PUSH SI
MOV BL, [SI] ; Hold the the minimum number (assume it's the first number)
LIN: ; Inner Loop

MOV AL, [SI] ; Get AL from [SI]


CMP AL, BL ; Is AL the new minimum?
JGE Skip ; If not then skip
MOV BL, AL ; If yes then make it the new minimum
MOV AL, [DI]; Exchange the new minimum at this SI
MOV [DI], BL
MOV [SI], AL
Skip:
INC SI
DEC DX ; One less iteration
CMP DX, 0 ; Is the inner loop done yet?
JNZ LIN ; If not then loop again

POP SI
INC SI
INC DI
LOOP LOUT

20) Without using MUL/IMUL instruction, write a sequence of instructions that


multiply AX with:
a) 4d

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

You might also like