Académique Documents
Professionnel Documents
Culture Documents
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
1).
Question:
Add two 8 bit numbers from 40h and 45h, and store the 16 bit result in 50h (MSB), 51h (LSB)
Code:
ORG 0h
MOV A, 40h
MOV B, 45h
ADD A, B
MOV 51h, A
JNC X
X:
END
Result:
Conclusion:
After adding two 8 bit numbers when the answer is having a carry it triggers the carry flag and with JNC
and JC functions we can access carries.
2).
Question:
Multiply two 8 bit numbers from 40h and 45h, and store the 16 bit result in 50h (MSB), 51h
(LSB)
Code:
ORG 0h
MOV A, 40h
MOV B, 45h
MUL AB
MOV 51h
,A
MOV 50H, B
END
Result:
Conclusion:
When we multiply two 8 bit numbers it always gives a 16 bit number such that the first 8 bit stores in B
register and last 8 bit stores in a register.
2).
Question:
Divide two 8 bit numbers from 40h and 45h, and store the 16 bit result in 50h (MSB), 51h (LSB)
Code:
ORG 0h
MOV A, 40h
MOV B, 45h
DIV AB
MOV 51h, A
MOV 50H, B
END
Result:
Conclusion:
When we divide two hexadecimal 8 bit numbers the quotient stores in a register and remainder
stores in B register.
3).
Question:
Find the complement of the number stored in 40h and store the result in 50h.
Code:
ORG 0h
MOV A, 40h
CPL A
MOV 50H, A
END
Result:
Conclusion:
We can find the complement of a number stored in a particular address using CPL function
whose result gets stored in Accumulator.
4).
Question:
ADD two 8 bit numbers from code memory, and store the result in address 40h.
Code:
ORG 0h
MOV A, #0H
MOVC A,@A+DPTR
MOV B, A
MOV A, #010H
MOVC A,@A+DPTR
ADD A, B
MOV 40H, A
END
Result:
Conclusion:
We can add two numbers from the code memory by using MOVC command and then for
address we can use DPTR command also.
5)
Question:
Repeat the Question4 and store the result in external memory 0000h.
Code:
ORG 0h
MOV A, #0H
MOVC A,@A+DPTR
MOV B, A
MOV A, #010H
MOVC A,@A+DPTR
ADD A, B
MOVX @DPTR, A
END
Result:
Conclusion:
For storing the value in external memory we can use MOVX command.
6).
Question:
Add two 16 bit numbers that has stored in internal memory 30,31,32,33 then store the
24bit result in addresses 36, 35, 34. (Results may be in 24 bit size).
CODE:
ORG 0h ORG 0h
JNC Y
MOV 34H, #01H
Y:
END
Conclusion:
Add two 16 biota numbers with the help of ADDC command or without ADDC
command.
7)
Question:
Subtract two 8 bit numbers that has borrowed.
Code:
ORG 00H
MOV A, #0AAh
MOV B, #0BCh
SUBB A, B
CPL A
MOV B, #01h
ADD A, B
END
Result:
Conclusion:
Here we are using two’s complement for subtraction of two numbers.
8)
Question:
Find BCD of hexadecimal number.
Code:
ORG 00H
MOV A, #0aAh
DA A
END
Result:
Conclusion:
DA commands add 6 to invalid bad numbers to make it valid.
9).
Question:
Add two BCD numbers.
Code:
ORG 00H
MOV A, #72h
MOV B, #42h
ADD A, B
DA A
MOV 10H, A
JNC X
ADD A, #01H
X:
END
Result:
Conclusion:
Here we added two bad numbers.
10)
Question:
Find the sum and average of an array starting from address 40h to 49h.
Code:
ORG 00H
MOV A, #00H
MOV B, #0AH
MOV A, #00H
X:
ADDC A,@R1
INC R1
DJNZ R0, X
MOV R0, A
DIV AB
END
Result:
Conclusion:
With the help of DJNZ, commands we are able to loop over add the numbers stored in
array and by dividing it we get the average.
11)
Question:
Find the sum of an array (50h to 59h), that has 8 bit numbers, and stores the result in
address 61h (MSB), 60h (LSB).
Code:
ORG 00H
MOV A, #00H
X:
ADD A,@R1
JNC Y
INC R2
Y:
INC R1
DJNZ R0, X
MOV 60H, R2
MOV 61H, A
END
Result:
Conclusion: We did the sum of array of 8 bit numbers and stored it in 60h and 61h.
12)
Question:
Move a block of memory from 30h-39h to 40-49h.
Code:
ORG 00H
MOV B, #0AH
X:
MOV A,@R1
MOV @R0, A
INC R0
INC R1
DJNZ B, X
END
Result:
Conclusion:
We moved the values from one addresses to other addresses with the help of this
algorithm.
13)
Question:
Move a block of memory from 30h-39h to 40-49h IN REVERSE
ORDER.
Code:
ORG 00H
MOV B, #0AH
MOV R1, #030h
MOV R0, #049H
X:
MOV A,@R1
MOV @R0, A
DEC R0
INC R1
DJNZ B, X
END
RESULT:
Conclusion:
We have moved the values from one address to another address in
reverse order.
14).
Question:
Move a block of 10 bytes of memory from the address (30h-39h) to
(35h-3Eh).
Code:
ORG 0H
END
RESULT:
CONCLUSION:
To move the data from 30h-39h to 35h-3Eh, first move the data from 35h-39h to 3Ah-3Eh. For
that make a loop in which move the data from 35h to 3Ah and increment both value 5 times.
And then for remaining data make another loop i.e. 30h-3h to 35
15).
Question:
Find the sum of an array of 10 bytes (30h to 39h), and store the result in 51h (LSB), 50h
(MSB). If carry is there, does the multiplication operation between the final carry (MSB) and the
result of sum (LSB), STORE THE RESULT IN 55h (LSB), 54h (MSB).
Code:
ORG 00H
MOV B, #00H
MOV A, #00H
X:
ADD A,@R1
JNC Y
INC B
Y:
INC R1
DJNZ R0, X
MOV 50H, B
MOV 51H, A
MUL AB
MOV 54H, B
MOV 55H, A
END
Conclusion:
Here we are using one loop to do the summation of the given numbers stored in the addresses
from 30h to 39h.
16)
Question:
Multiply 16 bit number with 8 bit number.
Code:
ORG 00H
MOV A, 41H
MOV B, 31H
MUL AB
MOV 52H, A
MOV R0, B
MOV A, 41H
MOV B, 30H
MUL AB
ADD A, R0
MOV 51H, A
MOV 50H, B
END
Result:
Conclusion:
For multiplying 16 bit number with 8 bit number we first multiplied the first 8 bit and then add
the result in second eighth bit and then multiplied it with the second eighth bit.
17).
Question:
Find the factorial of a number less than value 5h.
Code:
ORG 00H
MOV A, #01H
X:
MOV B, R0
MUL AB
DJNZ R0, X
END
Result:
Conclusion:
For finding the factorial of a given number we are using one single loop to multiply
iteratively.
18).
QUESTION
Find the square of 16 bit number.
CODE
ORG 0H
MOV A, R1
MOV B, R1
MUL AB
MOV 47H, A
MOV R3, B
MOV A, R0
MOV B, R1
MUL AB
ADD A, R3
MOV 46H, A
JNC X
INC B
X:
MOV 45H, B
MOV A, R1
MOV B, R0
MUL AB
MOV 57H, A
MOV R3, B
MOV A, R0
MOV B, R0
MUL AB
ADD A, R3
MOV 56H, A
JNC Y
INC B
Y: MOV 55H, B
MOV A, 57H
ADD A, 46H
MOV 28H, A
MOV A, 56H
ADDC A, 45H
MOV 27H, A
MOV A, 55H
ADDC A, #00H
MOV 26H, A
END
RESULT
CONCLUSION
QUESTION
Find the basic logic gate operation (XOR, AND, OR, XNOR). Between the values stored in
address 40h and 41h.
CODE
ORG 0h
MOV A, R1
MOV B, R0
ANL A, B
MOV 40H, A
MOV A, R1
MOV B, R0
ORL A, B
MOV 41H, A
MOV A, R1
MOV B, R0
XRL A, B
MOV 42H, A
CPL A
MOV 43H, A
END
RESULT
CONCLUSION
In this program, Logical operation like AND, OR & NOT are done using ANL, ORL & CPL
respectively
For Logical XOR operation XRL instruction is used and for XNOR operation compliment
of XOR is the output.
20).
QUESTION
Compare two numbers, that has stored in address 30h and 31h.
CODE
Org 00h
MOV A, 30H
MOV B, 31H
CJNE A, B, NOT_EQUAL
ORL A, B
MOV 35H, A
SJMP EXIT
XRL A, B
CPL A
MOV 35H, A
SJMP EXIT
NEXT:ANL A,B
MOV 35H,A
EXIT:
END
RESULT
CONCLUSION
In this question we aware able to figure out he comparison operations with the help of
CJNE command so when the source is less than or equal to destination the carry flag is
not activated and else it is activated.
21).
QUESTION
Store the 8 bit value in addresses 30h and 31h as nibble and find the sum of the
nibble.
CODE
org 00h
MOV A,29H
MOV B,31H
DIV AB
MOV 30H,A
MOV 31H,B
ADD A,B
END
RESULT
CONCLUSION
Here we are storing the two different nibbles in two different addresses and we are getting it
by dividing the number by #010H.
22).
Question
String of data is stored in XRAM starting from the address 0000H to 0009H, count the
number of zeroes and non-zero numbers and store the result in 000AH and 000BH respectively.
CODE
ORG 00H
JZ SOURCE
INC R0
INC DPTR
SJMP Z
SOURCE: INC R1
INC DPTR
SJMP Z
MOV A, R1
MOVX @DPTR, A
INC DPTR
MOV A, R0
MOVX @DPTR, A
END
RESULT
CONCLUSION
We have used the MOVX command as we are dealing with external memory. The program can
then be successfully completed by using JZ command
23).
Question
CODE
ORG 00H
INC R0
MOV B, A
MOV A,@R1
INC R1
ADD A, B
MOV @R1, A
END
RESULT
CODE
Each number in this case is a simple addition of the previous 2 numbers. This can
be done with the use of an appropriate counter value and INC command
24).
Question
String of data is stored in XRAM starting from the address 000H to 0009H,
find the greatest number and store it in address 000AH
CODE
ORG 00H
MOV R2, #0AH
MOV 40H, #00H
MOV DPTR, #0000H
DO:
MOVX A,@DPTR
MOV 30H, A
INC DPTR
MOV A, 40H
CJNE A, 30H, X
SJMP Y
X:
JNC Z
XCH A, 30H
MOV 40H, A
Z:
NOP
Y:
NOP
DJNZ R0, DO
MOV DPTR, #000AH
MOV A, 40H
MOVX @DPTR, A
END
RESULT
CODE
We have successful compared a string of numbers and stored the final output in a
given memory location by the use of CJNE and XCH command.
25).
Question
String of data is stored in XRAM starting from the address 000H to 0009H, find the least number and
store it in address 000BH
CODE
ORG 00H
DO:
MOVX A,@DPTR
MOV 30H, A
INC DPTR
MOV A, 40H
CJNE A, 30H, X
SJMP Y
X:
JC Z
XCH A, 30H
MOV 40H, A
Z:
NOP
Y:
NOP
DJNZ R0, DO
MOVX @DPTR, A
END
RESULT
CODE
We follow the same commands used in the previous
26).
Question
CODE
ORG 00H
SETB TR0
CPL P1.1
CLR TR0
CLR TF0
SJMP AGAIN
RESULT
CODE
We learn about the basics of TMOD register and how to generate a square wave using Timer 0
Mod-1, which is used in 16 bit.
27).
Question
CODE
ORG 00H
SETB TR1
CPL P1.4
CLR TR1
CLR TF1
SJMP AGAIN
RESULT
CODE
Here, instead of timer 0, we have used timer 1 which changes the value of TMOD register
28).
Question
Generate a square wave of 100 Hz using timer 0(mode 1) at port 1.0 while pressing the switch at port
2.0
CODE
ORG 00H
Z: JNB P2.0, Z
SETB TR0
CPL P1.0
CLR TR0
CLR TF0
SJMP AGAIN
END
RESULT
CONCLUSION
We have learnt how to generate a square wave which is generate only when a specific switch is selected
and remains 0 which it is not selected.
29).
Question
Generate a square wave of 200 Hz using timer 1(mode 1) at port 1.3 while pressing the switch at port
2.3
CODE
ORG 00H
Z: JNB P2.3, Z
SETB TR1
CPL P1.3
CLR TR1
CLR TF1
SJMP AGAIN
END
RESULT
CONCLUSION
It is the same as the previous question, only difference being we have used timer 1 instead of timer 0,
which changes the value of TMOD register
30).
QUESTION
Generate a square wake of 10 kHz using timer 1 by auto reload mode (mode 2) in port 1.3, while
pressing switch at port 2.4
CODE
ORG 00H
AGAIN:
Z: JNB P2.4, Z
SETB TR1
CPL P1.3
CLR TR1
CLR TF1
SJMP AGAIN
END
RESULT
CODE
We have learnt about Mod-2 operation of the timer, also known as Auto reload mode. This is used as 8
bit mod.
31).
Question
Generate a PWM of 1 kHz, that has duty cycle of 70 percentage using timer 0 (mode 1) in port 1.0, while
pressing switch at port 2.0
CODE
ORG 00H
Z: JNB P2.0, Z
ACALL DELAY
CPL P1.0
ACALL DELAY
CPL P1.0
SJMP X
CLR TR0
CLR TF0
RET
END
RESULT
CONSLUSION
We have learnt how to generate a square wave where the on and off percentage is not 50% each. This
can be done by using different count values in TH0 and TL0.
32).
Question
Generate a PWM of 2 kHz, that has duty cycle of 10 percentage using timer 1 (mode 1) in port 1.5, while
pressing switch at port 2.3
CODE
ORG 00H
Z: JNB P2.3, Z
ACALL DELAY
CPL P1.5
ACALL DELAY
CPL P1.5
SJMP X
CLR TR1
CLR TF1
RET
END
RESULT
CONSLUSION
Here, we have used timer 1 to generate a PWM instead of timer 0 which changes the value of TMOD
register.
33).
Question
Generate a square wave of 1 kHz, using timer 0 mode 1 interrupt in P1.1, while pressing switch
a P2.1.
CODE
ORG 0000H
LJMP MAIN
ORG 000BH
CPL P1.1
RETI
ORG 0030H
MAIN:
MOV A, #00H
MOV P2, A
X: JNB P2.1, X
BACK:
SETB TR0
SJMP BACK
END
RESULT
CODE
Here we have learnt how to use the timer in interrupt mode. When overflow is generated, program flow
is stopped and ISR is generated.
34).
Question
Switch is connected at P2.1, monitor the state of the switch and generate following wave of 50% duty
cycle at P1.1 using timer 1(mode 1) as mentioned
CODE
ORG 00H
SETB TR1
X: JNB TF1, X
CLR TR1
CLR TF1
CPL P1.1
JB P2.1, MAIN
SETB TR1
CLR TR1
CLR TF1
CPL P1.1
JB P2.1, MAIN
END
RESULT
CONCLUSION
Here, we have to check the status of the switch (port P2.1) after every cycle. Hence we have to
prepare separate subroutines for the corresponding time delay.
35).
Question
CODE
ORG 0000H
LJMP MAIN
ORG 001BH
CPL P1.1
RETI
ORG 0030H
MAIN:
MOV A, #00H
MOV P2, A
X: JNB P2.1, X
BACK:
SETB TR1
SJMP BACK
END
RESULT
CONCLUSION
When overflow is generated program flow is stopped and ISR is generated.
36).
Question
CODE
ORG 0000H
BACK:
JB P2.1, X
LCALL FIVE
SJMP Y
X:
LCALL ONE
Y:
SJMP BACK
ONE:
: ON
SETB TR0
G: JNB TF0, G
CPL P1.1
CLR TF0
CLR TR0
; OFF
SETB TR0
H: JNB TF0, H
CPL P1.1
CLR TF0
CLR TR0
RET
FIVE:
; ON
SETB TR0
F: JNB TF0, F
CPL P1.1
CLR TF0
CLR TR0
; OFF
SETB TR0
J: JNB TF0, J
CPL P1.1
CLR TF0
CLR TR0
RET
END
RESULT
CONCLUSION
Here the duty cycle which is to be generated depends upon the position of the switch.
37).
Question
Generate a PWM signal of 10 kHz and duty cycle 70% at P1.1 using timer 0 interrupt in auto reload
mode.
CODE
ORG 0H
LJMP START
ORG 000BH
CLR TR0
CPL P1.1
RETI
ORG 0030H
START:
SETB P1.1
BACK:
SETB TR0
SETB TR0
LJMP BACK
END
RESULT
CONCLUSION
Here we have used mode 2 timer in interrupt mode.
38).
Question
Generate a PWM signal of 10 kHz and duty cycle 20% at P1.1 using timer 1 interrupt in auto reload mode.
CODE
ORG 0000H
LJMP START
ORG 001BH
DJNZ R0, X
CLR P1.1
SJMP X
OFF:
DJNZ R1, X
SETB P1.1
X:
RETI
ORG 0030H
START:
SETB P1.1
SETB TR1
BACK: SJMP BACK
END
RESULT
CONCLUSION
This question is similar to the previous question, but timer 1 is used instead of timer 0.
39).
Question
Generate a PWM signal of 1 kHz and duty cycle 60% at P1.4 using timer 1 interrupt in mode-1(16 bit mode)
CODE
ORG 0000H
LJMP START
ORG 001BH
JNB P1.4, ON
SJMP X
ON:
X:
CPL P1.1
RETI
ORG 0030H
START:
SETB P1.4
SETB TR1
BACK: SJMP BACK
END
RESULT
CONCLUSION
Here, timer is used in mode -1 unlike the previous questions where it was operated in auto reload mode
0, #0FEH
SETB TR0
CLR TR0
CLR TF0
CPL P1.1
JB P2.1, DELAY2
END