Microprocessors and
Microcontrollers
(EE-231)
Main Objectives
The arithmetic and Logic Instructions and
Programs
Addition, Subtraction, Multiplication, Division
Rotation and Other Logical operations
Checksum
BCD-ASCII conversions
ADD Instruction
ADD A, Source ; A= A+ Source
The instruction ADD is used to add two operands
Destination operand is always register A
Source operand can be a register, immediate data, or memory
Memory-to-memory arithmetic operations are never allowed in 8051
Assembly language (e.g. ADD R,1,R2 is not allowed)
ADD Instruction
Example
ADDC Instruction
How can we add two-16 bit Numbers ?
When adding two 16-bit data operands, the propagation of a carry
from lower byte to higher byte is concerned.
Example
BCD Number System
The binary representation of the digits 0 to 9 is called
BCD (Binary Coded Decimal)
Unpacked BCD
In unpacked BCD, the lower 4 bits of the number
represent the BCD number, and the rest of the bits are
0
Ex. 00001001 and 00000101 are unpacked BCD for 9
and 5
Packed BCD
In packed BCD, a single byte has two BCD number in it,
one in the lower 4 bits, and one in the upper 4 bits
Ex. 0101 1001 is packed BCD for 59H
BCD Number System
Adding two BCD numbers must give a BCD result
DA Instruction
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
needed
Example
DA Instruction
After an ADD or ADDC instruction
If the lower nibble (4 bits) is greater than 9, or if AC=1, add 0110 (6) to
the lower 4 bits
If the upper nibble is greater than 9, or if CY=1, add 0110 (6) to the
upper 4 bits
Example
DA Instruction
Example
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 (borrow in
this case) prior to the execution of the instruction
Subtraction
SUBB when CY = 1
This instruction is used for multi-byte numbers and will take care of the
borrow of the lower operand
Example
Multiplication
The 8051 supports byte by byte multiplication only
The bytes are assumed to be unsigned data
MUL AB ; AxB, 16-bit result in B, A
Division
The 8051 supports byte over byte division only
The byte are assumed to be unsigned data
DIV AB ;divide A by B, A/B
Division
Example
Signed 8-bit Numbers
D7 (MSB) is the sign and D0 to D6 are the magnitude of the number
If D7=0, the operand is positive, and if D7=1, it is negative
Positive numbers are 0 to +127
Negative number representation (2s complement)
Write the magnitude of the number in 8-bit binary (no sign)
Invert each bit
Add 1 to it
Signed 8-bit Numbers
Example
Overflow
In 8-bit signed number operations, OV is set to 1 if either occurs:
There is a carry from D6 to D7, but no carry out of D7 (CY=0)
There is a carry from D7 out (CY=1), but no carry from D6 to D7
Example
Overflow
Example
Example
Note
In unsigned number addition, we must
monitor the status of CY (carry)
Use JNC or JC instructions
In signed number addition,
the OV (overflow) flag must be monitored by the programmer
JB PSW.2 or JNB PSW.2
Logical Instructions
Logical AND
ANL destination,source ; dest = dest AND source
ANL is often used to mask (set to 0) certain bits of an operand Logical OR
Logical OR
ORL destination,source ; dest = dest OR source
ORL instruction can be used to set certain bits of an operand to 1
Logical XOR
XRL destination,source ; dest = dest XOR source
XRL instruction can be used to toggle certain bits of an operand
It is also used for complementing the bits. E.g. XRL A,A clears A
Note:
The destination is normally the accumulator
The source operand can be a register, indirect-memory, or immediate
Logical Instructions
Example
Compare Instructions
CJNE destination , source , Label
The actions of comparing and jumping are combined into a single
instruction called CJNE (compare and jump if not equal)
The CJNE instruction compares two operands, and jumps if they are not
equal
The destination operand can be in the accumulator or in one of the Rn
registers
The source operand can be in a register, in memory, or immediate
The operands themselves remain unchanged
It changes the CY flag to indicate if the destination operand is larger or
smaller
Compare Instructions
START:
CJNE R5 , #80 , NOT_EQUAL ; check R5 for 80
MOV P1 , R5 ;R5 = 80 [Perform the tasks if found equal]
NOT_EQUAL:
JNC GREATER ;jump if R5 > 80
MOV P2 , R5 ;R5 < 80 [Perform the tasks if found lesser]
GREATER:
MOV P3 , R5; R5 > 80 [Perform the tasks if found greater]
Compare Instructions
Example
Rotate Instruction
Rotate Right
RR A ; rotate right A
In rotate right, Bit D0 exits from the LSB and enters into MSB, D7
E.g., 0001 1011 after rotation becomes 1000 1101
Rotate Left
RL A ; rotate left A
In rotate left, Bit D7 exits from the MSB and enters into LSB, D0
E.g., 1110 0100 becomes 1100 1001
Rotate through Carry
Rotate Right through Carry
RRC A ; rotate right A through carry
In RRC, The bits exit the LSB to the carry flag, and the carry enters the MSB
Rotate Left through Carry
RLC A ; rotate left A through carry
In RLC, The bits exit the MSB to enter the carry, the carry enters the LSB
Rotate through Carry
Example
How will you do it with RR or RL instruction ?
AGAIN: JNB ACC.7 , NEXT
INC
R1
NEXT: RL A
DJNZ R7 , AGAIN
Data Serialization
We can Serialize data through rotate instruction
We can transfer a byte of data serially by
Moving CY to any pin of ports P0 P3 using rotate instruction
Example
Data Serialization
Example
SWAP instruction
It swaps the lower nibble and the higher nibble
In other words, the lower 4 bits are put into the higher 4 bits and the
higher 4 bits are put into the lower 4 bits
SWAP works only on the accumulator (A)
How would you swap without swap instruction ?
Rotate 4 times
ASCII and BCD
ASCII and BCD
Devices like printers, LCDs etc. receive data in the form of ASCII.
Systems like computer, servers and embedded systems (like
microcontrollers) have a built-in clock that keeps track of time it is called
(RTC). RTCs are present in almost any electronic device which needs to
keep accurate time.
The RTC provides the time of day (hour, minute, second) and the date
(year , month , day) continuously, regardless of whether the power is on or
off
However this data is provided in packed BCD
To be displayed on an LCD or printed by the printer, it must be in ACSII
format
ASCII to packed BCD Conversion
It is first converted to unpacked BCD (to get rid of the 3)
Combined to make packed BCD
ASCII to packed BCD Conversion
Example
Using a Look-Up table for ASCII
Example
Checksum Byte in ROM
1.
2.
1.
2.
3.
To ensure the integrity of the ROM contents, every system must perform
the checksum calculation
The process of checksum will detect any corruption of the contents of
ROM
The checksum process uses what is called a checksum byte
The checksum byte is an extra byte that is tagged to the end of series of
bytes of data.
To calculate the checksum byte of a series of bytes of data
Add the bytes together and drop the carries
Take the 2s complement of the total sum, and it becomes the last byte of
the series
To perform the checksum operation,
add all the bytes, including the checksum byte
The result must be zero
If it is not zero, one or more bytes of data have been changed
Checksum Byte in ROM
Example
Checksum Byte in ROM
Example