Assembly Programming Exam Problems and Solutions
Assembly Programming Exam Problems and Solutions
Problem I
Write a program in assembly that performs the following operations:
Loading the following data into memory
• 32h, 0Ah, 89h, 47h, BCh, FFh, 11h
• 84h, BAh, 90h, B7h, 6Ah, F0h, 00h
Comparison between two elements of the same column.
3. Assignment of the largest number to the stack.
The comparison must start from 1eracolumn:
32, 0A, 89, 47, BC, FF, 11
84h, BAh, 90h, B7h, 6Ah, F0h, 00h
[Link] to DOS
Problem II
The interfacing between an LCD display and the parallel port of a PC is done as shown in the
figure below:
Parallel Port
D0 D0
D1 D1
D2 D2
D3 D3
D4 D4 LCD display
D5 D5
D6 D6 Vcc
D7 D7
E R/W RS Vcc
strobe
Select
printer
With:
E≡ (Enable) walking on descending front allowing the input of the data to be displayed.
RS≡ (Select Register) works at a low level validating the display.
a. Create a flowchart to display the series 00h - FFh. The display of each
data lasts 1s. Justify the different steps as well as the initialization values of
records.
b. Translate the flowchart into C language.
Sidi Mohamed Ben Abdellah University
Faculty of Sciences and Techniques
Department of Industrial Engineering
Solution
Problem I:
DOSSEG
.MODELSMALL
.STACK0100H
.DATA
["T1DB32H","0AH","89H","47H","0BCH","0FFH","11H"]
Solution
DOSSEG
MODELSMALL
.CODE
START:
MOVCX, 05H
LOOP:CALL INPUT
MOVE DL, AL
CALLSAISIE
ADDDL,AL
MOVE AH, 02H
INT21H
LOOP
MOVAX, 4C00H
INT21H
SAISIEPROCNEAR
MOVAH,01H
INT21H
RET
SAISIEENDP
ENDSTART
Sidi Mohamed Ben Abdellah University
Faculty of Sciences and Techniques
Department of Industrial Engineering
ASCII code represents each character on 8 bits (1 byte). The most significant bit
is always equal to 0. The codes for lowercase alphabetical letters follow and
variant from 61h to 7Ah. When the 6th bit goes to 0 the character takes the form
uppercase.
Example: The ASCII code of a is 61h: 0 110 0 0 01
When A is capitalized, the code becomes 41h: 0 100 0 0 0 1
3. Return to DOS.
We will put the transformation of a character and its display in the form of a sub
program.
Sidi Mohamed Ben Abdellah University
Faculty of Sciences and Techniques
Industrial Engineering Department
Solution:
DOSSEG
.MODELSMALL
.STACK0100H
.CODE
START:
MOVAL, 61H
SAUT:PUSHAX
INCAX
CMPAL,7AH
JBESAUT
MOVSI,02H
LOOP:CALLTRANSFORMATION
ADDSI,02H
CMPDL,41H
JABOUCLE
MOVAX, 4C00h
INT21H
TRANSFORMATIONPROCNEAR
MOVE BASE POINTER, STACK POINTER
MOVDL, [BP+SI]
ANDDL,0DFH
MOVE AH, 02H
INT21H
RET
TRANSFORMATIONENDP
ENDSTART
Sidi Mohamed Ben Abdellah University
Faculty of Sciences and Techniques
Department of Industrial Engineering
2. Perform the following operations, on 4 bits, and specify the values of the indicators.
CF, ZF, SF, OF, and PF:
Solution:
EXERCISE 4:
DOSSEG
.MODELSMALL
.STACK0100H
.DATA
T1DB18H, 11H, 29H, 04H, 20H
.CODE
START:
MOVAX, @DATA
MOVDS, AX
MOVE,00H
MOVBX, OFFSET T1
MOVCX,4
MOVAL, [BX]
LOOP:
CMPAL, [BX+1]
JBS1
MOVAL, [BX+1]
S1:INCBX
LOOP
PUSHAX
MOVAX,4C00H
INT21H
ENDSTART
Sidi Mohamed Ben Abdellah University
Faculty of Sciences and Techniques
Department of Industrial Engineering
Solution:
DOSSEG
.MODELSMALL
.STACK0100H
.DATA
T1DB01H, 00H, 02H, 00H, 03H, 00H, 04H, 00H
.CODE
START:
MOVAX, @DATA
MOVDS, AX
MOVAH,00H
MOVBX, OFFSET T1
MOVAL,08H
MOVCX,AL
OUTPUT:CMP[BX],00H
JNZS1
JMPS2
S1:DECAL
S2:INCBX
LOOPSORTIE
PUSHAX
MOVAX, 4C00H
INT21H
ENDSTART
University Sidi Mohamed Ben Abdellah
Faculty of Sciences and Techniques
Department of Industrial Engineering
1. Among the following instructions, indicate those that are incorrect and correct them.
the:
PUSH AL
ROL AX, 2
MOV AX, var
MOVE AX, BL
MOV AX, [0001]
MOVE BX, AX
MOV AX, 03h
AND AX, [SI+6]
Solution 1:
EXERCISE 4:
DOSSEG
.MODELSMALL
.STACK0100H
.DATA
T1DB01H, 00H, 02H, 00H, 03H, 00H, 04H, 00H
.CODE
START:
MOVAX, @DATA
MOVDS,AX
MOVAH,00H
MOVBX, OFFSET T1
MOVAL,8
MOVCX,AL
OUTPUT:CMP[BX],00H
JES1
JMPS2
S1:DECAL
S2:INCBX
LOOPSORTIE
MOVAX,4C00H
INT21H
ENDSTART
Sidi Mohamed Ben Abdellah University
Faculty of Sciences and Techniques
Industrial Engineering Department
Solution 2:
EXERCISE 4:
DOSSEG
.MODELSMALL
.STACK0100H
.DATA
T1DB01H, 00H, 02H, 00H, 03H, 00H, 04H, 00H
.CODE
START:
MOVAX, @DATA
MOVDS,AX
MOVAH, 00H
MOVBX, OFFSET T1
MOVAL,8
MOVCX,AL
OUTPUT: CMP[BX], 00H
JNES2
DECAL
S2:INCBX
LOOPSORTIE
MOVAX,4C00H
INT21H
ENDSTART
Sidi Mohamed Ben Abdellah University
Faculty of Sciences and Techniques
Industrial Engineering Department
1. Provide the position of indicators CF, ZF, SF, PF, and OF at the end of the execution of the
following instructions:
• Add up all the columns and sort the elements into the stack.
columns that result in the value 6.
Sidi Mohamed Ben Abdellah University
Faculty of Sciences and Techniques
Department of Industrial Engineering
Solution:
EXERCISE 3:
DOSSEG
.MODELSMALL
.STACK0100H
.DATA
T1DB01H, 00H, 02H, 00H, 03H, 00H, 02H, 00H
T2DB01H, 02H, 02H, 01H, 03H, 00H, 04H, 00H
.CODE
START:
MOVAX, @DATA
MOVDS, AX
MOVAH,00H
MOVBX, OFFSET T1
MOVCX,8
OUTPUT:MOVAL, [BX]
ADDAL, [BX+8]
CMPAL,06H
JNES1
PUSH[BX]
PUSH[BX+8]
S1:INCBX
LOOPSORTIE
MOVAX, 4C00H
INT21H
ENDSTART
Sidi Mohamed Ben Abdellah University
Faculty of Sciences and Techniques
Industrial Engineering Department
Exercise I:
Exercise II:
Solution:
Exercise II:
DOSSEG JAS2
.MODELSMALL MOVAL, VAR3
.STACK0100 H S2:
.DATA PUSHAX
VAR1DB01H MOVAL, VAR1
VAR2DB04H ADDAL, VAR2
VAR3DB03H ADDAL, VAR3
.CODE PUSHAX
START: MOVBX,OFFSETVAR1
MOVAX, @DATA MOVCX,3
MOVDS, AX S3:
MOVAH,00H MOVAL,[BX]
MOVAL, VAR1 PUSHAX
CMPAL, VAR2 INCBX
JAS1 LOOPS3
MOVAL, VAR2 MOVAX, 4C00H
S1: INT21H
CMPAL, VAR3 ENDSTART
SP F6h
0003
SPF8h
0004
SPFAh
0001
SPFCh
0008
SPFEh
0003
SP100h
Sidi Mohamed Ben Abdellah University
Faculty of Sciences and Techniques
Industrial Engineering Department
Exercise I:
What is the difference between a hardware interrupt and an interrupt?
software?
2. Give the steps for handling a hardware interruption
3. Indicate the type of addressing mode of the following instructions by explaining it.
data entry method
Exercise II:
Create a program in assembly that displays the letters of the alphabet in lowercase and
IN UPPER CASE AS SHOWN IN THE EXAMPLE BELOW:
aAbBcC…zZ
Solution:
Exercise II:
DOSSEG DOSSEG
.MODELSMALL .MODELSMALL
.CODE .CODE
START: START:
MOVBL,61H MOVE AL, 61H
SAUT: SAUT:
MOVE DL, BL MOVDL,AL
CALL DISPLAY DISPLAYCALL
CALLTRANSFORMATION CALLTRANSFORMATION
DISPLAY CALL CALL DISPLAY
INCBL INCAL
CMPBL,7AH CMPAL,7AH
JBESAUT JBESAUT
MOVAX,4C00H MOVAX, 4C00H
INT21H INT21H
DISPLAYPROCNEAR DISPLAYPROCNEAR
MOVAH,02H MOVE,02H
INT21H INT21H
RET RET
DISPLAYENDP DISPLAYENDP
TRANSFORMATIONPROCNEAR TRANSFORMATIONPROCNEAR
ANDDL,0DFH ANDDL,0DFH
RET RET
TRANSFORMATIONENDP TRANSFORMATIONENDP
ENDSTART ENDSTART
University Sidi Mohamed Ben Abdellah
Faculty of Sciences and Techniques
Department of Industrial Engineering
Exercise I:
1. Specify the role of each of the following registers:
CS, DS, SP, and IP
Exercise II:
• Read the data from the stack by comparing them. Sort the highest value.
big in the DX register
Solution:
Exercise II:
DOSSEG
.MODELSMALL
.STACK0100H
.DATA
T1DB18H, 11H, 29H, 04H, 20H
.CODE
START:
MOVAX, @DATA
MOVDS, AX
MOVAH,00H
MOVBX, OFFSET T1
MOVCX,5
OUTPUT:MOVAL, [BX]
INCBX
PUSHAX
LOOPEXIT
POPDX
S1:CMPSP,100H
JZFIN
POPAX
CMPDL,AL
JAS1
MOVDL,AL
JMPS1
FIN
MOVAX,4C00H
INT21H
ENDSTART
Sidi Mohamed Ben Abdellah University
Faculty of Sciences and Techniques
Department of Industrial Engineering
PART I :
1. Briefly recall all the 80x86 registers, specifying the role of each.
records.
2. Provide the addressing mode of the following instruction and indicate the steps of
AND AX, [BX+SI+7]
3. Why do memory segments have a size of 64 KB?
4. Provide the architecture of the memory space by specifying the addresses and the mode
of addressing the microcontroller 80C535.
5. What is the difference between a general register and an SFR?
6. Citer 3 exemples de registres généraux et 3 exemples de registres SFR.
PART II :
PART III :
Write a program to load into the microcontroller, which offers the two
following possibilities according to the operating mode described above:
If the switch is in the high state, we choose period 1.
If the switch is in the low state, we choose period 2
Justify the choice of values to be placed in the registers to achieve the desired period.
Sidi Mohamed Ben Abdellah University
Faculty of Sciences and Techniques
Department of Industrial Engineering
Solution:
PART II:
DOSSEG
.MODELSMALL
.CODE
START:
MOVDL 00H
MOVCX 04H
LOOP:CALL ENTRY
CMPDL,AL
JASAUT
MOVDL,AL
SAUT:
LOOP
MOVAH,02H
INT21H
MOVAX,4C00H
INT21H
SAISIEPROCNEAR
MOVAH,01H
INT21H
RET
SAISIEENDP
ENDSTART
Sidi Mohamed Ben Abdellah University
Faculty of Sciences and Techniques
Department of Industrial Engineering
PART III:
Solution 1:
CPU 80515
INCLUDE stddef51
USING bank0
SEGMENT code
ORG 0000H
START:
MOV R7, #80H
LOOP: CPL P1.7
HOLD CALL Start
JB P4.7 START
MOVE R7, #40H
SJMP LOOP R7← 80H
WAITING :
SAUT2: MOVE R6, #00H
CPL P1.7
SAUT1: DJNZ R6, SAUT1
DJNZ R7, SAUT2
RET
WAITING
END
Solution 2:
CPU 80515
INCLUDE stddef51
USING bank0
SEGMENT code
ORG 0000H
START:
CPL P1.7
JB P4.7 HIGH
CALL WAITING2
SJMP BASE
SKIN : CALL WAITING1 Start
BAS : SJMP START
WAITING1:
CPL P1.7
MOV R7, #80H
SAUT2: MOVE R6, #00H
SAUT1: DJNZ R6, SAUT1
DJNZ R7, SAUT2 P4.7
RET
WAITING2:
MOVE R7, #40H
WAITING1 WAITING2
SAUT4: MOVE R6, #00H
SAUT3: DJNZ R6, SAUT3
DJNZ R7, SAUT4
RET
END
Sidi Mohamed Ben Abdellah University
Faculty of Science and Technology
Industrial Engineering Department
EXERCISE I:
Write the assembly program that performs the following operations:
• Créer le tableau : 01h, 00h, 02h, 00h, 03h, 00h, 04h, 00h.
• Count the number of null values.
• Put this number in the AX register and then on the stack.
EXERCISE II:
Here is the program of the microcontroller 80C535:
CPU 80515
INCLUDE stddef51
USING bank0
SEGMENT code
ORG 0000H
START :
move R6, #30h
move 35h, #2Fh
move P1, #FFh
move the value 50h into register R0
Solution :
EXERCISE I :
DOSSEG
.MODELSMALL
.STACK0100H
.DATA
T1DB01H, 00H, 02H, 00H, 03H, 00H, 04H, 00H
.CODE
START:
MOVAX, @DATA
MOVE DATA TO AX
MOVE,00H
MOVBX, OFFSET T1
MOVAL,08H
MOVCX,AL
OUTPUT:CMP[BX],00H
JNZS1
JMPS2
DECAX
S2:INCBX
LOOPSORTIE
PUSHAX
MOVAX,4C00H
INT21H
ENDSTART
Sidi Mohamed Ben Abdellah University
Faculty of Sciences and Techniques
Industrial Engineering Department
EXERCISE II :
; Main program
START:
move R6, #30h Put 30H in the register R6
nbr : orl AR6, #01010101B Store the result of the logical operation
Or in R6
END
Sidi Mohamed Ben Abdellah University
Faculty of Sciences and Techniques
Industrial Engineering Department
Solution:
EXERCISE 3:
DOSSEG
.MODELSMALL
.STACK0100H
.DATA
T1DW1001H, 0000H, 2002H, 0000H, 0A003H, 0000H, 1F04H, 0000H
.CODE
START:
MOVAX, @DATA
MOVDS, AX
MOVE, 00H
MOVBX, OFFSET T1
MOVAL,8
MOVCX,AX
OUTPUT:CMP[BX],0000H
JNES1
DECAL
S1:ADDBX,2
LOOPSORTIE
MOVAX,4C00H
INT21H
ENDSTART
Sidi Mohamed Ben Abdellah University
Faculty of Science and Techniques
Department of Industrial Engineering
Exercise I:
What is the difference between a hardware interrupt and an interrupt?
software?
2. Provide the steps for handling a hardware interrupt
3. For an 80C535 microcontroller, specify the location in memory of the
general register bases (banks) by stating their number.
4. Give some examples of SFR registers.
Exercise II:
Create a program in 80x86 assembly that displays the letters of the alphabet.
lowercase and uppercase as shown in this example:
aAbBcC…zZ
Exercise III :
EXERCISE II:
Solution1 : Solution2 :
DOSSEG DOSSEG
.MODELSMALL .MODELSMALL
.CODE .CODE
START: START:
MOVAL,61H MOVAL,61H
SAUT: SAUT:
MOVE AL MOVDL,AL
DISPLAY CALL CALL DISPLAY
ADDDL,20H ANDDL,0DFH
DISPLAY CALL CALL DISPLAY
INCAL ADDAL,20H
CMPAL,7AH INCAL
JBESAUT CMPAL,7AH
MOVAX,4C00H JBESAUT
INT21H MOVAX,4C00H
DISPLAYPROCNEAR INT21H
MOVAH, 02H DISPLAYPROCNEAR
INT21H MOVAH,02H
RET INT21H
DISPLAYENDP RET
ENDSTART DISPLAYENDP
ENDSTART
Sidi Mohamed Ben Abdellah University
Faculty of Sciences and Techniques
Department of Industrial Engineering
Exercise III:
Main program
2.1. Attente1 : 33,55 s
Start
Start
P1.7← 0B
R7← 80H
WAITING1
R6← 00H
CPL P1.7
2.2. Attente2 : 32,80
R5← 00H
Start
WAITING2
R5← R5 - 1 No
R7← 40H R5 = 0
Yes
R6← 00H
R6← R6 - 1 No
R6 = 0
No R6← R6 - 1 Yes
R6 = 0
R7← R7 - 1 No
Yes R7 = 0
No R7← R7 - 1
Yes
R7 = 0
Return to
Yes program
Return to
program
Sidi Mohamed Ben Abdellah University
Faculty of Sciences and Techniques
Department of Industrial Engineering
3.
CPU 80515
INCLUDE stddef51
USING bank0
SEGMENT code
ORG 0000H
START:
MOV P1.7, #0B
HOLD WAITING1
CPL P1.7
HOLD2
SJMP START
WAITING1 :
MOVE R7, #80H
SAUT3: MOVE R6, #00H
SAUT2: MOVE R5, #00H
SAUT1: DJNZ R5, SAUT1
DJNZ R6, SAUT2
DJNZ R7, SAUT3
RET
WAITING2 :
MOVE R7, #40H
SAUT5: MOVE R6, #00H
SAUT4: DJNZ R6, SAUT4
DJNZ R7, SAUT5
RET
END
Sidi Mohamed Ben Abdellah University
Faculty of Sciences and Techniques
Department of Industrial Engineering
EXERCISE I :
EXERCISE II:
Sidi Mohamed Ben Abdellah University
Faculté desSciences etTechniques
Department of Industrial Engineering
Supplementary Exercise:
We consider 8 LED diodes connected to the outputs of port P1.
Solution
CPU 80515
INCLUDE stddef51
USING bank0
SEGMENT code
ORG 0000H
START:
MOVE A, #FEH
SAUT3: MOV R7, #80H
SAUT4 : MOVE P1, A
HOLD CALL
JB P4.6 SAUT1
RL A
SJMP JUMP2
SAUT1 : RR A
SAUT2 : JB P4.7, SAUT3
MOV R7, #40H
SJMP JUMP4 Start
WAITING :
S2: MOV R6, #00H A← 11111110B
S1: DJNZ R6, S1
DJNZ R7, S2
RET R7← 80H
END
P1← A
WAITING
Left Right
P4.6
RL A RR A
Index register: generally indicates the physical address that one wishes.
to reach.
DI (Destination Pointer): it points to the destination in a segment, often ES.
where the CPU performs a write.
SI (Source Index): it points to the source in a segment, often DS, where the CPU
execute a reading
SP (Stack Pointer): it contains the offset of the address in a stack area. It is a
stack pointer
BP (Base Pointer): it is a base pointer register usually used with the
SS register.
Sidi Mohamed Ben Abdellah University
Faculty of Sciences and Techniques
Industrial Engineering Department
Work register:
IP (Instruction Pointer): This register is associated with the CS segment register for
indicate the next instruction to be executed. This register can never be modified
directly through jump instructions, programs, and interrupts.
X X X X OF DF IF TF SF ZF X AF X PF X CF
UniversitéSidiMohamedBenAbdellah
Faculty of Science and Technology
Department of Industrial Engineering
ARITHMETIC INSTRUCTIONS
ADD Addition
ADC Addition with carry
INC Increment of 1
AAA ASCII Adjustment for Addition
DAA BCD adjustment for addition
SUB Subtraction
SBB Subtraction with borrowing
DEC Decrement by 1
NEG Two's complement
CMP Comparison
AAS ASCII adjustment for subtraction
THE BCD Adjustment for Subtraction
CMP Comparison
NOT Logical inversion
AND ET logic
OR Inclusive or
XOR Exclusive OU
TEST ET logic without result (positions the indicators)
Shifts:
Rotations :
WIRING INSTRUCTIONS
Conditional connections:
Indicators
Unsigned arithmetic
Signed arithmetic
Unconditional branches:
Loops:
LOOP Loop
LOOPE/LOOPZ Loop if equal / if zero
LOOP/LOOPNZ Loop if not equal / if not zero
JCXZ Connection if CX = 0
Interruptions :
INT Interruption
INTO Interruption overflow
IRET Return from interruption
Sidi Mohamed Ben Abdellah University
Faculty of Sciences and Techniques
Industrial Engineering Department
MOV Transfer
PUSH Loading the battery
POP Discharging the battery
XCHG Exchange
XLAT Octet translation
Addresses :
Indicators:
Entries – exits :