10-CAT 1 Paper Showing-20-02-2024
10-CAT 1 Paper Showing-20-02-2024
Addition
Assume that RAM locations 40 – 44H have the following values. Write a program
to find the sum of the values. At the end of the program, register A should contain
the low byte and R7 the high byte
40 = (7D)
41 = (EB)
42 = (C5)
43 = (5B)
44 = (30)
Solution:
MOV R0,#40H ;load pointer
MOV R2,#5 ;load counter
CLR A ;A=0
MOV R7,A ;clear R7
AGAIN: ADD A,@R0 ;add the byte ptr to by R0
JNC NEXT ;if CY=0 don’t add carry
INC R7 ;keep track of carry
NEXT: INC R0 ;increment pointer
DJNZ R2,AGAIN ;repeat until R2 is zero
Dr. K. Ghosh 79
Example
Write a program to add two 16-bit numbers. The numbers are 3CE7H and
3B8DH. Place the sum in R7 and R6; R6 should have the lower byte.
Solution:
Dr. K. Ghosh 80
Example
Assume that 5 BCD data items are stored in RAM locations starting at 40H,
as shown below. Write a program to find the sum of all the numbers. The
result must be in BCD.
40=(71)
41=(11)
42=(65)
43=(59)
44=(37)
Solution:
MOV R0,#40H ;Load pointer
MOV R2,#5 ;Load counter
CLR A ;A=0
MOV R7,A ;Clear R7
AGAIN: ADD A,@R0 ;add the byte pointer
;to by R0
DA A ;adjust for BCD
JNC NEXT ;if CY=0 don’t
;accumulate carry
INC R7 ;keep track of carries
NEXT: INC R0 ;increment pointer
Dr. K. Ghosh DJNZ R2,AGAIN ;repeat until R2 is 0 81
Subtraction
Dr. K. Ghosh 82
❑ SUBB when CY = 0
1. Take the 2’s complement of the
subtrahend (source operand)
2. Add it to the minuend (A)
3. Invert the carry
CLR C
MOV A,#4C ;load A with value 4CH
SUBB A,#6EH ;subtract 6E from A
JNC NEXT ;if CY=0 jump to NEXT
CPL A ;if CY=1, take 1’s complement
INC A ;and increment to get 2’s comp
NEXT: MOV R1,A ;save A in R1
Solution:
4C 0100 1100 0100 1100
CY=0, the result is positive; - 6E 0110 1110 1001 0010
CY=1, the result is negative
-22 01101 1110
and the destination has the CY =1
2’s complement of the result Invert carry
Dr. K. Ghosh 83
❑ SUBB when CY = 1
➢ This instruction is used for multi-byte numbers
and will take care of the borrow of the lower
operand
Dr. K. Ghosh 84
Multiplication and Division
Dr. K. Ghosh 85
❑ The 8051 supports byte over byte division only
➢ The byte are assumed to be unsigned data
DIV AB ;divide A by B, A/B
CY is always 0 If B
Dr. K. Ghosh 0, OV = 0 86
If B = 0, OV = 1 indicates error
Example
(a) Write a program to get hex data in the range of 00 – FFH from
port 1 and convert it to decimal. Save it in R7, R6 and R5.
(b) Assuming that P1 has a value of FDH for data, analyze program.
Solution:
(a)
MOV A,#0FFH
MOV P1,A ;make P1 an input port
MOV A,P1 ;read data from P1
MOV B,#10 ;B=0A hex
DIV AB ;divide by 10
MOV R7,B ;save lower digit
MOV B,#10
DIV AB ;divide by 10 once more
MOV R6,B ;save the next digit
MOV R5,A ;save the last digit
(b) To convert a binary (hex) value to decimal, we divide it by 10
repeatedly until the quotient is less than 10. After each division the
remainder is saves.
Q R
FD/0A = 19 3 (low digit)
19/0A = 2 5 (middle digit)
2 (high digit)
Therefore, we have FDH=253.
Dr. K. Ghosh 87
Example
In semester, a student has to take six courses. The marks of the student (out of
25) are stored in RAM locations47H onwards. Find the average marks, and
output it on port 1.
Solution:
MOV R1,#06
Dr. K. Ghosh 88
Logic Instructions
AND
ANL DESTINATION,SOURCE
;dest = dest AND source
❑ This instruction will perform a logic AND on the two
operands and place the result in the destination
➢ The destination is normally the accumulator
➢ The source operand can be a register, in memory, or immediate
Dr. K. Ghosh 89
OR
ORL DESTINATION,SOURCE
;dest = dest OR source
❑ The destination and source operands are ORed and
the result is placed in the destination
➢ The destination is normally the accumulator
➢ The source operand can be a register, in memory, or
immediate
MOV A,#04H ;A = 04
ORL A,#68H ;A = 6C
04H 0 0 0 0 0 1 0 0
68H 0 1 1 0 1 0 0 0
6CH 0 1 1 0 1 1 0 0
Dr. K. Ghosh 90
XOR
XRL destination,source
;dest = dest XOR source
❑ This instruction will perform XOR operation on the
two operands and place the result in the destination
➢ The destination is normally the accumulator
➢ The source operand can be a register, in memory, or
immediate
Dr. K. Ghosh 91
XOR
Read and test P1 to see whether it has the value 45H. If it does, send
99H to P2; otherwise, it stays cleared. XRL can be used to
see if two registers
Solution:
MOV P2,#00 ;clear P2 have the same value
MOV P1,#0FFH ;make P1 an input port
MOV R3,#45H ;R3=45H
MOV A,P1 ;read P1
XRL instruction can be
XRL A,R3
JNZ EXIT ;jump if A is not 0
MOV P2,#99H
EXIT: ... If both registers have the same
value, 00 is placed in A. JNZ
and JZ test the contents of the
Dr. K. Ghosh
Department of Computer Scie 92
accumulator.
Rotate Right
RR A ;ROTATE RIGHT A
❑ In rotate right
➢ The 8 bits of the accumulator are rotated
right one bit, and
➢ Bit D0 exits from the LSB and enters into
MSB, D7
MSB LSB
RL A ;ROTATE LEFT A
❑ In rotate left
➢ The 8 bits of the accumulator are rotated
left one bit, and
➢ Bit D7 exits from the MSB and enters into
LSB, D0
MSB LSB
Dr. K. Ghosh 94
Rotate Right Through Carry
MSB LSB CY
CLR C ;make CY = 0
MOV A,#26H ;A = 0010 0110
RRC A ;A = 0001 0011 CY = 0
RRC A ;A = 0000 1001 CY = 1
RRC A ;A = 1000 0100 CY = 1
Dr. K. Ghosh 95
Rotate Left Through Carry
CY MSB LSB
Dr. K. Ghosh 97
BCD Number System
MOV A, #17H
ADD A, #28H
Dr. K. Ghosh 98
BCD Number System
Example:
MOV A,#47H ;A=47H first BCD operand
MOV B,#25H ;B=25H second BCD operand
DA works only ADD A,B ;hex(binary) addition(A=6CH)
after an ADD, DA A ;adjust for BCD addition
but not after INC (A=72H)
72H
The “DA” instruction works only on A. In other word, while the source
can be an operand of any addressing mode, the destination must be in
register A in order for DA to work.
Dr. K. Ghosh 99
BCD Number System
Example:
HEX BCD
29 0010 1001
+ 18 + 0001 1000
41 0100 0001 AC=1
+ 6 + 0110
47 0100 0111