0% found this document useful (0 votes)
39 views23 pages

10-CAT 1 Paper Showing-20-02-2024

The document discusses arithmetic instructions for the 8051 microcontroller. It describes addition using the ADD instruction, and how the flag register is affected. It also covers subtraction using the SUBB instruction, multiplication using MUL, and division using DIV.

Uploaded by

chandan shekar
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)
39 views23 pages

10-CAT 1 Paper Showing-20-02-2024

The document discusses arithmetic instructions for the 8051 microcontroller. It describes addition using the ADD instruction, and how the flag register is affected. It also covers subtraction using the SUBB instruction, multiplication using MUL, and division using DIV.

Uploaded by

chandan shekar
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/ 23

Arithmetic Instructions

Addition

ADD A,SOURCE ;A = A + SOURCE


❑ The instruction ADD is used to add two operands
➢ Destination operand is always in register A
➢ Source operand can be a register, immediate data, or in
memory
➢ Memory-to-memory arithmetic operations are never allowed in 8051
Assembly language

Show how the flag register is affected by the following instruction.


MOV A,#0F5H ;A=F5 hex
ADD A,#0BH ;A=F5+0B=00
Solution:
F5H 1111 0101
+ 0BH + 0000 1011
100H 0000 0000
Dr. K. Ghosh 78
Example

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:

CLR C ;make CY=0


MOV A,#0E7H ;load the low byte now A=E7H
ADD A,#8DH ;add the low byte now A=74H and CY=1
MOV R6,A ;save the low byte of the sum in R6
MOV A,#3CH ;load the high byte
ADDC A,#3BH ;add with the carry
;3B + 3C + 1 = 78(all in hex)
MOV R7,A ;save the high byte of the sum

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

❑ In many microprocessor there are two different


instructions for subtraction: SUB and SUBB (subtract with
borrow)
➢ In the 8051 we have only SUBB
➢ The 8051 uses adder circuitry to perform the subtraction

SUBB A,source ;A = A – source – CY


❑ To make SUB out of SUBB, we have to make CY=0 prior
to the execution of the instruction
➢ Notice that we use the CY flag for the borrow

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

A = 62H – 96H – 0 = CCH


CLR C CY = 1
MOV A,#62H ;A=62H
SUBB A,#96H ;62H-96H=CCH with CY=1
MOV R7,A ;save the result
MOV A,#27H ;A=27H
SUBB A,#12H ;27H-12H-1=14H
MOV R6,A ;save the result
A = 27H - 12H - 1 = 14H
Solution: CY = 0
We have 2762H - 1296H = 14CCH.

Dr. K. Ghosh 84
Multiplication and Division

❑ The 8051 supports byte by byte multiplication only


➢ The byte are assumed to be unsigned data
MUL AB ;AxB, 16-bit result in B, A

MOV A,#25H ;load 25H to reg. A


MOV B,#65H ;load 65H to reg. B
MUL AB ;25H * 65H = E99 where
;B = OEH and A = 99H

Unsigned Multiplication Summary (MUL AB)


Multiplication Operand1 Operand2 Result
Byte x byte A B B = high byte
A = low byte

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

MOV A,#95 ;load 95 to reg. A


MOV B,#10 ;load 10 to reg. B
DIV AB ;A = 09(quotient) and
;B = 05(remainder)

Unsigned Division Summary (DIV AB)


Division Numerator Denominator Quotient Remainder
Byte / byte A 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

MOV B,#06 ;only B can be used as divisor reg.


MOV R0,#47H
MOV A,#0
BACK: ADD A,@R0
INC R0
DJNZ R1,BACK
DIV AB
MOV P1,A

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

Show the results of the following.


MOV A,#35H ;A = 35H
ANL A,#0FH ;A = A AND 0FH
35H 0 0 1 1 0 1 0 1
0FH 0 0 0 0 1 1 1 1
05H 0 0 0 0 0 1 0 1

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

Show the results of the following.

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

Show the results of the following.


MOV A,#54H
XRL A,#78H
XRL instruction can be
54H 0 1 0 1 0 1 0 0 used to toggle certain
78H 0 1 1 1 1 0 0 0 bits of an operand
2CH 0 0 1 0 1 1 0 0

Dr. K. Ghosh 91
XOR

The XRL instruction can be used to clear the contents of a register by


XORing it with itself. Show how XRL A,A clears A, assuming that
AH = 45H.
45H 0 1 0 0 0 1 0 1
45H 0 1 0 0 0 1 0 1
00H 0 0 0 0 0 0 0 0

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

MOV A,#36H ;A = 0011 0110


RR A ;A = 0001 1011
RR A ;A = 1000 1101
RR A ;A = 1100 0110
RR A ;A = 0110 0011
Dr. K. Ghosh 93
Rotate Left

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

MOV A,#72H ;A = 0111 0010


RL A ;A = 1110 0100
RL A ;A = 1100 1001

Dr. K. Ghosh 94
Rotate Right Through Carry

RRC A ;ROTATE RIGHT THROUGH CARRY


❑ In RRC A
➢ Bits are rotated from left to right
➢ They exit the LSB to the carry flag, and
the carry flag enters the MSB

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

RLC A ;ROTATE LEFT THROUGH CARRY


❑ In RLC A
➢ Bits are shifted from right to left
➢ They exit the MSB and enter the carry flag,
and the carry flag enters the LSB

CY MSB LSB

Write a program that finds the number of 1s in a given byte.


MOV R1,#0
MOV R7,#8 ;count=08
MOV A,#97H
AGAIN: RLC A
JNC NEXT ;check for CY
INC R1 ;if CY=1 add to count
NEXT: DJNZ R7,AGAIN
Dr. K. Ghosh 96
BCD Number System

❑ The binary representation of the digits 0 to 9 is called BCD


(Binary Coded Decimal)
Digit BCD
0 0000
➢ Unpacked BCD
1 0001
▪ In unpacked BCD, the lower 4 bits of the number represent 2 0010
the BCD number, and the rest of the bits are 0 3 0011
▪ Ex. 00001001 and 00000101 are 4 0100
unpacked BCD for 9 and 5 5 0101
6 0110
➢ Packed BCD
7 0111
▪ In packed BCD, a single byte has two BCD number in it, one 8 1000
in the lower 4 bits, and one in the upper 4 bits 9 1001
▪ Ex. 0101 1001 is packed BCD for 59H

Dr. K. Ghosh 97
BCD Number System

❑ Adding two BCD numbers must give a BCD result

MOV A, #17H
ADD A, #28H

The result above should have been 17 + 28 = 45 (0100 0101).


To correct this problem, the programmer must add 6 (0110) to the
low digit: 3F + 06 = 45H.

Adding these two numbers gives


0011 1111B (3FH),
Which is not BCD!

Dr. K. Ghosh 98
BCD Number System

DA A ;DECIMAL ADJUST FOR ADDITION

❑ The DA instruction is provided to correct the aforementioned problem


associated with BCD addition
➢ The DA instruction will add 6 to the lower nibble or higher nibble if
need

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

➢ After an ADD or ADDC instruction


1. If the lower nibble (4 bits) is greater than 9, or if AC=1, add 0110
to the lower 4 bits
2. If the upper nibble is greater than 9, or if CY=1, add 0110 to
the upper 4 bits

Example:
HEX BCD
29 0010 1001
+ 18 + 0001 1000
41 0100 0001 AC=1
+ 6 + 0110
47 0100 0111

Since AC=1 after the


addition, ”DA A” will add 6 to the
lower nibble.
Dr. K. Ghosh The final result is in BCD format. 100

You might also like