Professional Documents
Culture Documents
Direct Addressing
✓ In direct addressing mode, the data is in a RAM memory location whose
address is known, and this address is given as a part of the instruction. For
example
MOV A, 20H ;Transfer the data of 20H to Accumulator
ADD A, 30H
• MOV Rn, A
MOVX A, @Ri
MOVX A, @DPTR
MOVX @Ri, A
MOVX @DPTR, A
MOVC A, @A+DPTR
MOVC A, @A+PC
3160914:MP & MC DR. MUKESH M BHESANIYA 19
PUSH direct
Description:
✓ The Stack Pointer is incremented by one.
✓ The contents of the indicated variable is then copied into the internal RAM
location addressed by the Stack Pointer.
✓ No flags are affected.
Example:
ORG 0H
MOV 30H,#23H
PUSH 30H ;SP=07+1, 08H=23H
POP 20H ;20H=23H, SP=08-1
END
Solution:
Solution:
XCH A, Rn
XCH A, direct
XCH A, @Ri
XCHD
✓ Exchange lower digit of accumulator with the lower digit of the memory
location specified.
XCHD A, @Ri
✓ The lower 4-bits of the accumulator are exchanged with the lower 4-bits of
the internal memory location identified indirectly by the index register.
✓ The upper 4-bits of each are not modified.
Example
✓ 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 010
+ 0BH + 0000 1011
100H 0000 0000
✓ CY =1, since there is a carry out from D7 PF =1, because the number of 1s is zero
(an even number), PF is set to 1. AC =1, since there is a carry from D3 to D4
3160914:MP & MC DR. MUKESH M BHESANIYA 29
Arithmetic Instructions
ADDC
✓ 8-bit addition between the accumulator, a second operand and the previous
value of the CY flag.
✓ Useful for 16-bit addition in two steps.
Example:
1
3C E7
+ 3B 8D
78 74 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
Example:
ORG 0000h
MOV R0, #03H // move the value 3 is register R0//
MOV A, #05H // move the value 5 in accumulator A and assume Cy=0
SUBB A, 00H // A=5-3-cy final value is 2 stored in the Accumulator A //
END
Example
✓ Originally the Accumulator holds the value 80 (50H). Register B holds the
value 160 (0A0H). The instruction,
MUL AB
Example
✓ The Accumulator contains 251 (0FBH or 11111011B) and B contains 18 (12H
or 00010010B). The instruction,
DIV AB
✓ will leave 13 in the Accumulator (0DH or 00001101B) and the value 17 (11H
or 00010001B) in B, since 251 = (13 x 18) + 17. Carry and OV will both be
cleared.
3160914:MP & MC DR. MUKESH M BHESANIYA 34
Examples
Multiplication:
ORG 0000h
MOV B, #03H // move the value 3 is register B//
MOV A, #05H // move the value 5 in accumulator A//
MUL AB // A=5*3 final value is 0FH, stored in the Accumulator A & B=0 //
END
Division:
ORG 0000h
MOV B, #03H // move the value 3 is register B//
MOV A, #15H // move the value 5 in accumulator A//
DIV AB // A=15/3 final value is 5 stored in the Accumulator A & B=0//
END
3160914:MP & MC DR. MUKESH M BHESANIYA 35
Arithmetic Instructions
DA A
✓ Decimal-adjust Accumulator for Addition
✓ DA A adjusts the 8-bit value in the Acc. resulting from the earlier addition of
two variable (each in packed-BCD format), producing two 4-bit digits.
✓ Any ADD or ADDC instruction may have been used to perform the addition.
Example
✓ The Acc. holds the value 56H representing the packed BCD digits of the
decimal number 56. R3 contains the value 67H representing the packed BCD
digits of the decimal number 67. The carry flag is set. The instruction
sequence,
✓ will first perform a standard binary addition, resulting in the value 0BEH
(10111110B) in the Acc. The carry and auxiliary carry flags will be cleared.
✓ The Decimal Adjust instruction will then alter the Accumulator to the value
24H (00100100B), indicating the packed BCD digits of the decimal number 24,
the low-order two digits of the decimal sum of 56, 67, and the carry-in.
✓ The carry flag will be set by the Decimal Adjust instruction, indicating that a
decimal overflow occurred. The true sum 56, 67, and 1 is 124.
3160914:MP & MC DR. MUKESH M BHESANIYA 37
The DA Instruction
Example:
MOV A, #47h ; A=47h first BCD operand
MOV B, #25h ; B=25h second BCD operand
ADD A, B ; hex (binary) addition (A=6Ch)
DA A ; adjust for BCD addition (A=72h)
Hex BCD
47 0100 0111
+ 25 + 0010 0101
6C 0110 1100
+ 6 + 0110
72 0111 0010
3160914:MP & MC DR. MUKESH M BHESANIYA 38
Example
Write a program segment to add two 16-bit numbers. The numbers are 3CE7h
and 3B8Dh.
Place the sum in R7 and R6; R6 should store the lower byte.
Solution:
Examples
RL A
RLC A
✓ Rotates A and the carry flag one bit position to the left
RR A
RRC A
✓ Rotates A and the carry flag one bit position to the right
Note that for RLC and RRC, you have to know the C flag first
C 7 6 5 4 3 2 1 0 Before: 10011100 CY = 0
After: 00111000 CY = 1
7 6 5 4 3 2 1 0 Before: 10011100
After: 01001110
RR A
7 6 5 4 3 2 1 0 C Before: 10011100 CY = 1
After: 11001110 CY = 0
CPL A
SWAP
✓ Swapping the lower-nibble (lower 4 bits) and the higher-nibble (upper 4 bits)
of register A.
7 6 5 4 3 2 1 0
SWAP A
Mnemonic Operation
Solution:
MOV A, 40h ; set A = RAM location 40h
MOV R7, #0 ; set R7 = 0
ADD A, 41h ; add A with RAM location 41h
JNC NEXT ; if CY = 0 don’t accumulate carry
INC R7 ; keep track of carry
NEXT: ADD A, 42h ; add A with RAM location 42h
JNC NEXT1 ; if CY = 0 don’t accumulate carry
INC R7 ; keep track of carry
NEXT1:
END
Solution :
Solution :
MOV R4,#0 ;R4=0
MOV R0,#40H ;load pointer
MOV R2,#05 ;load counter
BACK: CJNE @R0, #65H, NEXT ;compare RAM data with 65
MOV A,R0 ;if 65, save address
MOV R4,A ;if 65, save address
SJMP EXIT ;and exit
NEXT: INC R0 ;increment pointer
DJNZ R2,BACK ;keep check until count=0
EXIT: ...
DJNZ direct,radd Decrement the direct address by I and jump to the relative
address if the result is not O; no flags are affected unless the
direct address is the PSW
JNZ radd Jump to the relative address if A is not 0; the flags and the A
register are not changed
ORG 0000H
MOV DPTR, #300H // STARTING ROM LOCATION
MOV R0, #40H // STARTING RAM LOCATION
MOV R2, #05H // LOAD COUNTER
LOOP: CLR A
MOVC A, @A+DPTR //COPY ROM LOCATION DATA TO ACC
MOV @R0, A // COPY ACC DATA TO RAM LOCATION
INC DPTR
INC R0
DJNZ R2, LOOP
END
3160914:MP & MC DR. MUKESH M BHESANIYA 61
Example
Write an ALP to add 10 data bytes stored at internal RAM locations starting from
40H. Store the result at the location of R5 (LSB of the result) of bank 2 and the
location of R4 (MSB of the result) of bank 2.
ORG 0H
SETB PSW.4
CLR PSW.3
MOV R2,#00H
MOV R0,#0AH
MOV R1,#40H
CLR A
Loop: ADD A,@R1
JNC Skip
INC R2
Skip: INC R1
DJNZ R0,Loop
MOV R4,A
MOV A,R2
MOV R5,A
END
3160914:MP & MC DR. MUKESH M BHESANIYA 62
Software delay in 8051
In an 8051 microcontroller, it requires 12 cycles of the processor clock for
executing a single instruction cycle.
For an 8051 microcontroller clocked by a 12MHz crystal, the time taken for
executing one instruction cycle is 1μS and it is according to the equation, Time for
1 instruction cycle= 12 /12MHz = 1μS.
The shortest instructions will execute in 1μS and other instructions will take 2 or
more micro seconds depending up on the size of the instruction.
Thus a time delay of any magnitude can be generated by looping suitable
instructions a required number of time.
ORG 000H
MOV P1,#00000000B
MOV A,#00000000B
MAIN: MOV R6,#248D
LOOP1: DJNZ R6,LOOP1
CPL A
MOV P1,A
SJMP MAIN
END
ORG 000H
MOV P1,#00000000B
MOV A,#00000000B
MAIN: MOV R6,#2D
LOOP2: MOV R7,#245D
LOOP1: DJNZ R7,LOOP1
DJNZ R6,LOOP2
CPL A
MOV P1,A
SJMP MAIN
END
3160914:MP & MC DR. MUKESH M BHESANIYA 68
ACALL addr11
Function:
✓ Absolute Call
Description:
✓ ACALL unconditionally calls a subroutine located at the indicated address. The
instruction increments the PC twice to obtain the address of the following
instruction, then pushes the 16-bit result onto the stack (low-order byte first)
and increments the Stack Pointer twice.
✓ The subroutine called must start within the same 2k block of the program
memory as the first byte of the instruction following ACALL.
After PUSH 4
0B
0A 88 R4
09 00 Program counter Hight Byte [PCH]
08 0B Program counter Low Byte [PCH]
After PUSH 5
0B 57 R5
0A 88 R4
09 00 Program counter Hight Byte [PCH]
08 0B Program counter Low Byte [PCH]
3160914:MP & MC DR. MUKESH M BHESANIYA 79
BCD, ASCII, and other application programs
In this section we provide some real-world examples on how to use arithmetic
and logic instructions.
We will see their applications in real-world devices covered in future chapters.
ASCII numbers
✓ On ASCII keyboards, when the key “0″ is activated, 30H (0011 0000) is
provided to the computer. Similarly, 31H (0011 0001) is provided for the key
“1″, and so on, as shown in Table.
✓ It must be noted that although ASCII is standard in the United States (and
many other countries), BCD numbers are universal. Since the keyboard,
printers, and monitors all use ASCII, how does data get converted from ASCII
to BCD, and vice versa? These are the subjects covered next.
Solution:
MOV A,#29H ;packed BCD
ANL A,#0FH ;Lower nibble: A=09H
ORL A,#30H ;make it an ASCII, A=39H (‘9’)
MOV R6,A ;R6=39H ASCII char
MOV A,#29H ;
ANL A,#0F0H ;upper nibble: A=20H
SWAP A ;A=02H, equals to ”RR A” 4 times
ORL A,#30H ;A=32H,ASCII char ’2’
MOV R2,A ;R2=32H ASCII char
25H+62H+3FH+52H=118H → sum=18H
Dropping the carry of 1, we have 18H.
25H+62H+3FH+52H+E8H=200H → sum=00H
Dropping the carries, we have 00H.
Data is correct!
(c) If the second byte 62H has been changed to 22H, show how checksum detects
the error.
25H+22H+3FH+52H+E8H=1C0H → sum=C0H
Dropping the carries, we have C0H, which is not 00H.
The ROM data is corrupted
CAL_CHKSUM:
MOV R1,#RAM_ADDR
MOV R2,#COUNT
CLR A
H2: ADD A,@R1 ;calculate the sum
INC R1
DJNZ R2,H2
CPL A ;2’s complement
INC A
MOV @R1,A ;save checksum
RET
3160914:MP & MC DR. MUKESH M BHESANIYA 89
Binary to BCD Conversion
This subroutine converts 8 bit Hexadecimal number into its equivalent BCD value.
BIN_DEC:
MOV A,R0
MOV B,#100
DIV AB
MOV R3,A ; R3 CONTAIN HUNDRED NUMBER
MOV A,B
MOV B,#10
DIV AB
MOV R2,A ; R2 CONTAIN TENTH NUMBER
MOV R1,B ; R1 CONTAIN LEFT DECIMAL NUMBER
RET