Addition of Numbers
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
Example:
Write a program to add two 16-bit numbers. 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
MOV R6, A ;save the low byte sum in R6
MOV A, #3CH ;load the high byte
ADDC A, #3BH ;add with the carry
MOV R7, A ;save the high byte sum
Subtraction of Numbers
- SUB and SUBB (subtract with borrow)
- In the 8051 we have only SUBB
- The 8051 uses adder circuitry to perform the subtraction
- Take the 2’s complement of the subtrahend (source operand)
- Add it to the minuend (A)
- 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
SUBB when CY = 1
- This instruction is used for multi-byte numbers and will take care of the borrow of the lower operand
CLR C
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
Solution:
We have 2762H - 1296H = 14CCH.
Multiplication
MUL AB ;AxB, 16-bit result in B, A
Example:
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
Division
Example:
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.
Solution:
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
2's Complement
To make the 2’s complement of a number
CPL A ;1’s
complement (invert)
ADD A,#1 ;add 1 to make 2’s comp.
LOGIC AND INSTRUCTIONS
ANL destination,source ;dest = dest AND source
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
XOR INSTRUCTIONS
XRL destination,source
;dest = dest XOR source
Example:
Read and test P1 to see whether it has the value 45H. If it does, send 99H to P2; otherwise, it stays cleared.
Solution:
MOV P2,#00 ;clear P2
MOV P1,#0FFH ;make P1 an input port
MOV R3,#45H ;R3=45H
MOV A,P1 ;read P1
XRL A,R3
JNZ EXIT ;jump if A is not 0
MOV P2,#99H
EXIT: ...
Complement Accumulator
CPL A ;complements the register A
- This is called 1’s complement
MOV A, #55H
CPL A ;now A=AAH
;0101 0101(55H)
;becomes 1010 1010(AAH)
- To get the 2’s complement, all we have to do is to to add 1 to the 1’s complement
Compare Instruction
CJNE destination,source,rel. addr.
- 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
CJNE R5,#80,NOT_EQUAL ;check R5 for 80
... ;R5 = 80
NOT_EQUAL:
JNC NEXT ;jump if R5 > 80
... ;R5 < 80
NEXT: ...
Example:
Write a program to read the temperature and test it for the
value 75. According to the test results, place the temperature value into the registers
indicated by the following.
If T = 75 then A = 75
If T < 75 then R1 = T
If T > 75 then R2 = T
Solution:
MOV P1,#0FFH ;make P1 an
input port
MOV A,P1 ;read P1 port
CJNE A,#75,OVER ;jump if A is not 75
SJMP EXIT ;A=75, exit
OVER:
JNC NEXT ;if CY=0 then A>75
MOV R1,A ;CY=1, A<75, save in R1
SJMP EXIT ; and exit
NEXT:
MOV R2,A ;A>75, save it in R2
EXIT: ...
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
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
.
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
MOV A,#72H ;A = 0111 0010
RL A ;A = 1110 0100
RL A ;A = 1100 1001
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
Example:
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
Serializing Data
Serializing data is a way of sending a byte of data one bit at a time through a single pin of microcontroller
- Using the serial port, discussed in Chapter 10
- To transfer data one bit at a time and control the sequence of data and spaces in between them
Example:
Write a program to transfer value 41H serially (one bit at a
time) via pin P2.1. Put two highs at the start and end of the data. Send the byte
LSB first.
Solution:
MOV A,#41H
SETB P2.1 ;high
SETB P2.1 ;high
MOV R5,#8
AGAIN:
RRC A
MOV P2.1,C ;send CY to P2.1
DJNZ R5,AGAIN
SETB P2.1 ;high
SETB P2.1 ;high
Example:
Write a program to bring in a byte of data serially one bit
at a time via pin P2.7 and save it in register R2. The byte comes in with the LSB
first.
Solution:
MOV R5,#8
AGAIN:
MOV C,P2.7 ;bring in bit
RRC A
DJNZ R5, AGAIN
MOV R2,A ;save it
Single-bit Operations with CY
Example:
Assume that bit P2.2 is used to control an outdoor light and
bit P2.5 a light inside a building. Show how to turn on the outside light and turn
off the inside one.
Solution:
SETB C ;CY = 1
ORL C,P2.2 ;CY = P2.2 ORed w/ CY
MOV P2.2,C ;turn it on if not on
CLR C ;CY = 0
ANL C,P2.5 ;CY = P2.5 ANDed w/ CY
MOV P2.5,C ;turn it off if not off
Write a program that finds the number of 1s in a given byte.
Solution:
MOV R1,#0 ;R1 keeps number of 1s
MOV R7,#8 ;counter, rotate 8 times
MOV A,#97H ;find number of 1s in 97H
AGAIN:
RLC A ;rotate
it thru CY
JNC NEXT ;check CY
INC R1 ;if
CY=1, inc count
NEXT:
DJNZ R7,AGAIN ;go thru 8 times
SWAP
SWAP A
- 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)
Using a Lookup Table for ASCII
Example:
Assume that the lower three bits of P1 are connected to
three switches. Write a program to send the following ASCII characters to P2
based on the status of the switches.
000 ‘0’
001 ‘1’
010 ‘2’
011 ‘3’
100 ‘4’
101 ‘5’
110 ‘6’
111 ‘7’
Solution:
MOV DPTR,#MYTABLE
MOV A,P1 ;get SW status
ANL A,#07H ;mask all but lower 3
MOVC A,@A+DPTR ;get data from table
MOV P2,A ;display value
SJMP $ ;stay here
;------------------
ORG 400H
MYTABLE DB ‘0’,‘1’,‘2’,‘3’,‘4’,‘5’,‘6’,‘7’
END
No comments:
Post a Comment