BEC405A Module2
BEC405A Module2
Immediate Addressing
Register Addressing
Direct Addressing
Indirect Addressing
Indexed Addressing
Immediate Addressing: Immediate addressing simply means that the operand (which
immediately follows the instruction opcode) is the data value to be used. For example the
instruction:
MOV A, #99d
Number 99d
Register Addressing: One of the eight general-registers, R0 to R7, can be specified as the
instruction operand. The assembly language documentation refers to a register generically
as Rn. Ex: ADD A, R5; Adds register R5 contents to A (accumulator)
Direct addressing: Means that the data value is obtained directly from the memory
location specified in the operand. For ex: MOV A, 47h. The instruction reads the data
from Internal RAM address 47h and stores this in the accumulator.
Direct addressing can be used to access Internal RAM , including the SFR registers.
Indirect Addressing: In this mode of addressing the instruction performs an operation on
the data whose address is contained in register R0 or R1. Instructions using indirect
addressing are single byte instructions. In 8051 assembly language the symbol @ before
R0 or R1 denotes indirect addressing.
MOV A, @R0
@ symbol indicates that it is indirect addressing mode. R0 contains a value, for example
54h, which is to be used as the address of the internal RAM location, which contains the
operand data. Indirect addressing refers to Internal RAM only and cannot be used to refer
to SFR registers.
Note: Only R0 or R1 can be used as register data pointers for indirect addressing when
using MOV instructions.
Indexed Addressing: With indexed addressing a separate register, either the program
counter, PC, or the data pointer DTPR, is used as a base address and the accumulator is
used as an offset
address. The effective address is formed by adding the value from the base address to the
value from the offset address. Indexed addressing in the 8051 is used with the JMP or
MOVC instructions. Look up tables are easy to implement with the help of index
addressing.
i. Data transfer
Many computer operations are concerned with moving data from one location to another.
The 8051 uses five different types of instruction to move data:
MOV MOVX MOVC PUSH and POP XCH
MOV
In the 8051 the MOV instruction is concerned with moving data internally, i.e. between
Internal RAM, SFR registers, general registers etc. MOVX and MOVC are used in
accessing external memory data. The MOV instruction has the following format:
The instruction copies (copy is a more accurate word than move) data from a defined
source location to a destination location. Example MOV instructions are:
MOVC
MOVX instructions operate on RAM, which is (normally) a volatile
memory. Program tables often need to be stored in ROM since ROM is
non-volatile memory. The MOVC instruction is used to read data from the
external code memory (ROM). Like the MOVX instruction the DPTR
register is used as the indirect address register. The indirect addressing is
enhanced to realize an indexed addressing mode where register A can be
used to provide an offset in the address specification. Like the MOVX
instruction all moves must be done through register A. The following
sequence of instructions provides an example:
Note: for the MOVC the program counter can also be used to form the address.
XCH
The above move instructions copy data from a source location to a
destination location, leaving the source data unaffected. A special XCH
(eXCHange) instruction will actually swap the data between source
and destination, effectively changing the source data. Immediate
addressing may not be used with XCH. XCH instructions must use
register A. XCHD is a special case of the exchange instruction where just
the lower nibbles are exchanged. Examples using the XCH instruction are:
The 8051 can perform addition, subtraction. Multiplication and division operations on 8 bit numbers.
Addition
In this group, we have instructions to
i. Add the contents of A with immediate data with or without carry.
i. ADD A, #45H
ii. ADDC A, #OB4H
ii. Add the contents of A with register Rn with or without carry.
i. ADD A, R5
ii. ADDC A, R2
iii. Add the contents of A with contents of memory with or without carry using direct and indirect
addressing
i. ADD A, 51H
ii. ADDC A, 75H
iii. ADD A, @R1
iv. ADDC A, @R0
Subtraction
In this group, we have instructions to
i. Subtract the contents of A with immediate data with or without carry.
i. SUBB A, #45H
ii. SUBB A, #OB4H
ii. Subtract the contents of A with register Rn with or without carry.
i. SUBB A, R5
ii. SUBB A, R2
iii. Subtract the contents of A with contents of memory with or without carry using direct and indirect
addressing
i. SUBB A, 51H
ii. SUBB A, 75H
iii. SUBB A, @R1
iv. SUBB A, @R0
Multiplication
MUL AB. This instruction multiplies two 8 bit unsigned numbers which are stored in A and B register.
After multiplication the lower byte of the result will be stored in accumulator and higher byte of result
will be stored in B register.
Eg. MOV A,#45H ;[A]=45H
MOV B,#0F5H ;[B]=F5H
MUL AB ;[A] x [B] = 45 x F5 = 4209
;[A]=09H, [B]=42H
1
Division
DIV AB. This instruction divides the 8 bit unsigned number which is stored in A by the 8 bit unsigned
number which is stored in B register. After division the result will be stored in accumulator and remainder
will be stored in B register.
Eg. MOV A,#45H ;[A]=0E8H
MOV B,#0F5H ;[B]=1BH
DIV AB ;[A] / [B] = E8 /1B = 08 H with remainder 10H
;[A] = 08H, [B]=10H
When two BCD numbers are added, the answer is a non-BCD number. To get the result in BCD, we use
DA A instruction after the addition. DA A works as follows.
If lower nibble is greater than 9 or auxiliary carry is 1, 6 is added to lower nibble.
If upper nibble is greater than 9 or carry is 1, 6 is added to upper nibble.
Eg 1: MOV A,#23H
MOV R1,#55H
ADD A,R1 // [A]=78
DA A // [A]=78 no changes in the accumulator after da a
Eg 2: MOV A,#53H
MOV R1,#58H
ADD A,R1 // [A]=ABh
DA A // [A]=11, C=1 . ANSWER IS 111. Accumulator data is changed after DA A
INC increments the value of source by 1. If the initial value of register is FFh, incrementing the value will
cause it to reset to 0. The Carry Flag is not set when the value "rolls over" from 255 to 0.
In the case of "INC DPTR", the value two-byte unsigned integer value of DPTR is incremented. If the initial
value of DPTR is FFFFh, incrementing the value will cause it to reset to 0.
DEC decrements the value of source by 1. If the initial value of is 0, decrementing the value will cause it
to reset to FFh. The Carry Flag is not set when the value "rolls over" from 0 to FFh.
Logical Instructions
Logical AND
ANL destination, source: ANL does a bitwise "AND" operation between source and destination,
leaving the resulting value in destination. The value in source is not affected. "AND" instruction logically
AND the bits of source and destination.
ANL A,#DATA ANL A, Rn
ANL A,DIRECT ANL
A,@Ri
ANL DIRECT,A ANL DIRECT, #DATA
2
Logical OR
ORL destination, source: ORL does a bitwise "OR" operation between source and destination,
leaving the resulting value in destination. The value in source is not affected. " OR " instruction
logically OR the bits of source and destination.
ORL A,#DATA ORL A, Rn
ORL A,DIRECT ORL
A,@Ri
ORL DIRECT,A ORL DIRECT, #DATA
Logical Ex-OR
XRL destination, source: XRL does a bitwise "EX-OR" operation between source and
destination, leaving the resulting value in destination. The value in source is not affected. " XRL "
instruction logically EX-OR the bits of source and destination.
XRL A,#DATA XRL A,Rn
XRL A,DIRECT XRL
A,@Ri
XRL DIRECT,A XRL DIRECT, #DATA
Logical NOT
CPL complements operand, leaving the result in operand. If operand is a single bit then the state of the
bit will be reversed. If operand is the Accumulator then all the bits in the Accumulator will be reversed.
Rotate Instructions
RR A
This instruction is rotate right the accumulator. Its operation is illustrated below. Each bit is shifted one
location to the right, with bit 0 going to bit 7.
RL A
Rotate left the accumulator. Each bit is shifted one location to the left, with bit 7 going to bit 0
RRC A
Rotate right through the carry. Each bit is shifted one location to the right, with bit 0 going into the carry bit in the
PSW, while the carry was at goes into bit 7
RLC A
Rotate left through the carry. Each bit is shifted one location to the left, with bit 7 going into the carry bit in the
PSW, while the carry goes into bit 0.
3
Branch (JUMP) Instructions
Relative Jump
Jump that replaces the PC (program counter) content with a new address that is greater than (the address
following the jump instruction by 127 or less) or less than (the address following the jump by 128 or less)
is called a relative jump. Schematically, the relative jump can be shown as follows: -
4
In 8051, 64 kbyte of program memory space is divided into 32 pages of 2 kbyte each. The hexadecimal
addresses of the pages are given as follows:-
00 0000 - 07FF
01 0800 - 0FFF
02 1000 - 17FF
03 1800 - 1FFF
.
.
1E F000 - F7FF
1F F800 - FFFF
It can be seen that the upper 5bits of the program counter (PC) hold the page number and the lower 11bits
of the PC hold the address within that page. Thus, an absolute address is formed by taking page numbers
of the instruction (from the program counter) following the jump and attaching the specified 11bits to it to
form the 16-bit address.
Applications that need to access the entire program memory from 0000H to FFFFH use long absolute
jump. Since the absolute address has to be specified in the op-code, the instruction length is 3 bytes
(except for JMP @ A+DPTR). This jump is not re-locatable.
Example: -
1. The unconditional jump is a jump in which control is transferred unconditionally to the target location.
a. LJMP (long jump). This is a 3-byte instruction. First byte is the op-code and second and third bytes
represent the 16-bit target address which is any memory location from 0000 to FFFFH
eg: LJMP 3000H
b. AJMP: this causes unconditional branch to the indicated address, by loading the 11 bit address to 0 -
10 bits of the program counter. The destination must be therefore within the same 2K blocks.
c. SJMP (short jump). This is a 2-byte instruction. First byte is the op-code and second byte is the
relative target address, 00 to FFH (forward +127 and backward -128 bytes from the current PC value).
To calculate the target address of a short jump, the second byte is added to the PC value which is
address of the instruction immediately below the jump.
5
2. Conditional Jump instructions.
JBC Jump if bit 1 and clear bit
=
JB Jump if bit 1 =
JNC Jump if CY 0 =
JC Jump if CY 1 =
Bit level JUMP instructions will check the conditions of the bit and if condition is true, it jumps to the
address specified in the instruction. All the bit jumps are relative jumps.
JB bit, rel ; jump if the direct bit is set to the relative address specified. JNB
bit, rel ; jump if the direct bit is clear to the relative address specified.
JBC bit, rel ; jump if the direct bit is set to the relative address specified and then clear the bit.
6
f. [PC10-0]= address (11 bit); the new address of subroutine is loaded to PC. No flags are
affected.
RET instruction
RET instruction pops top two contents from the stack and load it to PC.
g. [PC15-8] = [[SP]] ;content of current top of the stack will be moved to higher byte of PC.
h. [SP]=[SP]-1; (SP decrements)
i. [PC7-0] = [[SP]] ;content of bottom of the stack will be moved to lower byte of PC.
j. [SP]=[SP]-1; (SP decrements again)
8051 has 128 bit addressable memory. Bit addressable SFRs and bit addressable PORT pins. It is possible to perform
following bit wise operations for these bit addressable locations.
1. LOGICAL AND
a. ANL C,BIT(BIT ADDRESS) ; ‘LOGICALLY AND’ CARRY AND CONTENT OF BIT ADDRESS, STORE RESULT IN CARRY
b. ANL C, /BIT; ; ‘LOGICALLY AND’ CARRY AND COMPLEMENT OF CONTENT OF BIT ADDRESS, STORE RESULT IN CARRY
2. LOGICAL OR
a. ORL C,BIT(BIT ADDRESS) ; ‘LOGICALLY OR’ CARRY AND CONTENT OF BIT ADDRESS, STORE RESULT IN CARRY
b. ORL C, /BIT; ; ‘LOGICALLY OR’ CARRY AND COMPLEMENT OF CONTENT OF BIT ADDRESS, STORE RESULT IN CARRY
3. CLR bit
a. CLR bit ; CONTENT OF BIT ADDRESS SPECIFIED WILL BE CLEARED.
b. CLR C ; CONTENT OF CARRY WILL BE CLEARED.
4. CPL bit
a. CPL bit ; CONTENT OF BIT ADDRESS SPECIFIED WILL BE COMPLEMENTED.
b. CPL C ; CONTENT OF CARRY WILL BE COMPLEMENTED.
ASSEMBLER DIRECTIVES.
Assembler directives tell the assembler to do something other than creating the machine code for an
instruction. In assembly language programming, the assembler directives instruct the assembler to
1. Process subsequent assembly language instructions
2. Define program constants
3. Reserve space for variables
ORG (origin)
The ORG directive is used to indicate the starting address. It can be used only when the
program counter needs to be changed. The number that comes after ORG can be either in hex
or in decimal.
Eg: ORG 0000H ;Set PC to 0000.
7
DB (DEFINE BYTE)
The DB directive is used to define an 8 bit data. DB directive initializes memory with 8 bit
values. The numbers can be in decimal, binary, hex or in ASCII formats. For decimal, the 'D' after
the decimal number is optional, but for binary and hexadecimal, 'B' and ‘H’ are required. For
ASCII, the number is written in quotation marks (‘LIKE This).
DATA1: DB 40H ; hex
DATA2: DB 01011100B ;binary
DATA3: DB 48 ; decimal
DATA4: DB ' HELLO W’ ; ASCII
END
The END directive signals the end of the assembly module. It indicates the end of the program to
the assembler. Any text in the assembly file that appears after the END directive is ignored. If the
END statement is missing, the assembler will generate an error message.
8
Mov a, #12h
Mov a, r7
Mov a, 20h
A = 23h, r0 = 45 h
XCH A, r0
A = 45h, r0 23h
XCHD A, r0
A=25h, r0 43h
XCH A, R0 === exchange instruction
Source value (right side) <=> destination (left side)
Example:
A = 23h, r0 = FA h
XCH A, r0
A = FAh, r0 23h
XCH A, r0
XCH A, @r0
A= 23H, r0 = #10h (immediate value) => becomes mem location = 34h
A = 34h, 10h =23h
XCH A, 20H
A = 12h, 20h = 35h
A = 35h, 20h = 12h
Exchange instruction :
Here only lower nibble (4bit) will be exchanged.
XCHD A, @r0 r0=#20h memory location = 13h
A = 25h , r0 (20h)= 13h
Mov r0, #34h here carry is ‘1’ 1 0here carry is usually ‘0’
Mov a, #0f9h 12 34h
Add a, r0 +FB F9h
----------------
Mov r7, a 1 0E 2Dh
answer stored in registers r5 r6 r7
Mov r1, #12h
Mov a, #0fbh
Addc a, r1
Both input and output are playing in
Mov r6, a
registers
JC vijay
Mov r5, #00h
Vijay: inc r5
end
Add two 16bit number 1234 h and FBF9h
Mov r0, #34h here carry is ‘1’ 1 0here carry is usually ‘0’
Mov a, #0f9h 12 34h
Add a, r0 +FB F9h
----------------
Mov 22h, a 1 0E 2Dh
answer stored in registers 20h 21 22h
Mov r1, #12h
Mov a, #0fbh
Addc a, r1
Input in register and output are playing
Mov 21h, a
in memory location
JC vijay
Mov 20h, #00h
Vijay: inc 20h
end
Add two 16bit number 1234 h and FBF9h
Mov r0, #21h here carry is ‘1’ 1 0here carry is usually ‘0’
Mov 21h, a
Both input and output are playing in
JC vijay
Mov 20h, #00h memory location
Vijay: inc 20h
end
Add two 16bit number 1234 h and FBF9h
Mov r0, #21h here carry is ‘1’ 1 0here carry is usually ‘0’
Mov r6h, a
input are in memory location and
JC vijay
Mov r5h, #00h output are playing in register
Vijay: inc r5h
end
1) 5678h
-1234h
----------------
4444h 5678 h – 1234h – CY = 5678 h – 1234 h - 0 4444h hint: HERE CY =0
Final answer 32 CD h
Program for 2byte 16-bit Subtraction
Jc next2
Mov 61h, a
Mov 60h, #00h
47 h + 25 h = 6C h
After hexadecimal
addition >9 +6
add
MUL A B 8 Bit X 8 Bit
12h X 23h
A = 12h, B = 23h
O r A = 23h , B =12h
A f t e r multiplication operation
R e s u l t = 02 76 h
R e s u l t i s stored such that
A = 76h
B = 02h
div
1234 h X ABCD h = 0C 37 4F A4 h 16 B i t X 16 B i t
12 34 h X AB CD h
-------------------------
Carry01 29 A4 CD h X 34 h
0E 6A CD h X 12 h
22 BC AB h X 34 h
0C 06 AB h X 12 h
-------------------------------
0C 37 4F A4
ORL A, #34h
ORL A, r3
ORL A, 20H
ORL A, @R1
ORL 30H, A
ORL 20H, #35H
XRL A, #34h
XRL A, r3
XRL A, 20H
XRL A, @R0
XRL 30H, A
XRL 20H, #35H
CLR A
CPL A
RL A
RLC A
RLC A
RR A
RRC A
And operation: 1111 0010 F2h OR operation: 1111 0010 F2h
0010 1111 2Fh 0010 1111 2Fh
---------------- ----------------
0010 0010 22h 1111 1111 FFh
XOR operation: 1111 0010 F2h CLR Clear operation: A = 1111 0010 F2h
0010 1111 2Fh A = 0000 0000 00H
----------------
1101 1101 CC h
Syntax:
ACALL delay
LCALL delay
Delay : It’s just a name of subroutine or loop
name or subprogram name
LCALL =16 BIT address == 0 to 15, ACALL = 11BIT address = 0 to 10,
PC to SP using CALL
And SP to PC using RET
11200h : mov a, r0
21201h: mov b, #12h
31202h:acall delay SP = 8bit 12 01 h
31203h: add a, b SP + 1 => 01 h
4end SP + 1 => 12 h
12 12
01 01
PC =12 01 h
SP = 07H 08H 09H PC = 12H 01H
01H 12H
CALL RET
Ex: Add a, r0 // A = 00H
JZ next1
JNZ vijay
The JMP instruction transfers execution to the address generated by adding the 8-bit value in the
accumulator to the 16-bit value in the DPTR register.
A= 08 h, DPTR = 1100 h
JMP @A +DPTR
Jump to address == PC = a + dptr = 1108h
STACK IN 8051
• SP =>Stack pointer => 8bit => used to store address or
mem or information.
• It will take Location of last step.
• SP => Starting address = 07h => 08h to 1Fh
• Push => store mem or address or information and
Location to SP are
• Pop=> Retrieve mem. Location from SP
• Push and Pop are playing with only Register bank
• => RB0, RB1, RB2, RB3
Default=> RB0 register bank is used.
Mov r2, #0f1h
Mov r4, #1ah Top of SP SP+1 =0BH 22H Pop 7 r7
Mov r5, #15h Pop 5 r5
SP +1 = 15H
Push 2 r2 SP +1 0F1H
=08H
Push 4 r4
Push 5 r5 SP = 07H =====
Push 7 r7
If I am using different register bank then suppose RB3
RS1 = PSW.4 =1, RS0 = PSW.3 = 1 , SETB, CLR instruction is to be used.
SETB PSW.4
SETB PSW.3
Top of SP SP+1 =0BH 22H Pop 7 r7
Mov r2, #0f1h Pop 5 r5
SP +1 = 15H
Mov r4, #1ah 0AH Pop 4 r4
Mov r5, #15h SP+1 = 1AH Pop 2 r2
Mov r7, #22h 09H
Push 2 r2 SP +1 0F1H
=08H
Push 4 r4
Push 5 r5 SP = 07H =====
Push 7 r7
1. Write a assemble language program to transfer a block of bytes of data from one location to
another location both internally
Data memory Contents Data memory Contents
Org 00h address address
Mov r3, #04h D:0x040 01h D:0x050 00h
Mov r0, #40h
Mov r1, #50h D:0x041 02h D:0x051 00h
ORG 00H
ORG 00H
MOV R3, #64H
MOV R3, #64H
EXPECTED RESULTS:
EXPECTED RESULTS:
NOTE THAT 64H = 99 in decimal
NOTE THAT 64H = 99 in decimal
PORT1: P1: 99 TO 00 displayed
PORT1: P1: 00 TO 99 displayed
Org 00h
Mov r3, #0ah
Mov r1, #20h
Mov a, #00h
Mov @r1, a /// 0
add a,#01h
inc r1
Mov @r1, a ///1
Back: dec r1 //// to 20h
add a, @r1 //// 0 + 1 ,, 20h + 21h = 01
da a
inc r1
inc r1
Mov @r1, a
djnz r3, back
end
1. Eight bit numbers X, NUM1 and NUM2 are stored in internal data RAM locations 20h, 21h and 22H respectively. Write an
assembly language program to compute the following: org 00h
IF X=0; then NUM1 (AND) NUM2, Mov r0, 21h
IF X=1; then NUM1 (OR) NUM2, Mov r1, 22h
IF X=2; then NUM1 (XOR) NUM2, mov a, 20h
ELSE RES =00, RES is 23H RAM location. cjne a,#00h,vijay1
Mov a, 21h
Concept of Problem: Anl a, 22h
20H X, 21HNUM1, 22HNUM2, 23HRES Here: sjmp Here
20h = 00h, 21h and 22 h,, Results stored in Accumulator A mov a,20h
20h = 01h, 21h or 22h,, Results stored in Accumulator A vijay1: cjne a,#01h,vijay2
20h = 02h, 21h XOR 22h, Results stored in Accumulator Mov a, 21h
20h = other than 00h, 01h, 02h, 23 h = 00h. Orl a, 22h
Here1:sjmp Here1
mov a,20h
vijay2:cjne a,#02h,vijay3
Mov a, 21h
Xrl a, 22h
Here2:sjmp Here2
Vijay3: mov 23h, #0ffh
Here3:sjmp here3
end
Write an assembly language program to find the average of 10 students Data memory Contents
marks stored in external RAM memory address 8000H. Load the average address
value in internal RAM memory 30H.
x:0x8000 01h
org 00h
x:0x8001 02h
mov dptr, #8000h
mov r1, #0ah x:0x8002 03h
mov r2, #00h x:0x8003 04h
loop: movx a,@dptr
x:0x8004 05h
add a, r2
mov r2,a /// Finally r2 contains total marks x:0x8005 06h
inc dpl
x:0x8006 07h
djnz r1, loop
mov b, #0ah /// b = 0ah x:0x8007 08h
mov a,r2 //// a = r2 x:0x8008 09h
div ab
mov 30h,a //// quotient value x:0x8009 0Ah
mov 31,b //// remainder value
end
Write an assembly language program to find the factorial of a number. Use
Subroutine programming. org 00h
Input is 04h mov r0,#04h
Output is 04h X 03h X 02h X 01h mov a,r0
acall fact
fact:dec r0
We can show the results in terms of decimal cjne r0,#01,vijay
Value by using da a instruction sjmp stop
Note this program is only for smaller values vijay:mov b,r0
mul ab
da a
acall fact
stop:sjmp stop
end
Write an assembly language program to count
the number of 1’s and 0’s in an 8-bit data Org 00h Org 00h
received from port P1. Store the count of 1’s and Clr c Clr c
0’s in 30h and 31h. Mov a, 90h /// 90h = Port 1 Mov a, 90h /// 90h = Port 1
Mov r3, #08h Mov r3, #08h
P1 => 45h ==> 0100 0101 Back: rlc a
jc loop
Back: rlc a
jnc loop
No. of one’s (1’s) = 03h => stored in 30h Inc 31h Inc 30h
No. of zero’s (0’s) = 05h => stored in 31h Djnz r3, back Djnz r3, back
Cy flag <= 0100 0101 Here:sjmp here Here:sjmp here
With initial carry = 0 using clr c loop:inc 30h loop:inc 31h
RLC => cy data bits Djnz r3, back Djnz r3, back
1 1000 1010 JC LOOP inc 31h Here:sjmp Here Here:sjmp Here
RLC => 2 0001 0100 JC LOOP end end
LOOP:inc 30h
We need to observe first binary bit of any given number to indicate odd or even, 0 = even, 1= odd
Input: Unpacked BCD number = 05h Input: ASCII number = 35
Output: ASCII number = 35 Output: Unpacked BCD number = 05h
Packed BCD to ASCII number program
Input: Packed BCD number = 12h= 0001 0010
Output: ACSCII: 31 32 = 0011 0001 0011 0020
org 00h
mov a, #12h
anl a, #0f0h /// a = 10h Store the final answer in r1,r2 register
swap a /// a = 01h
add a, #30h /// a = 31h
mov r1, a /// r1 = 31h
mov a, #12h
anl a, #0fh ///a = 02h
add a, #30h /// a = 32h
Mov r2, a
end
ASCII number to packed BCD number program
Input: ACSCII: 31 32 = 0011 0001 0011 0020
Output: Packed BCD number = 12h= 0001 0010
org 00h Take the input from 2000h = 31h and 2001h =32h
mov dptr, #2000h Store the final answer in 20h location
movx a, @dptr /// a = 31h
subb a, #30h //// a = 01h
mov r1, a ///// r1 = 01h
Inc dpl //// 2000h becomes 2001h
Movx a,@dptr ///a =32h
subb a, #30 /// a = 02h
mov r2, a /// r2 ==02h since r1 =01h, r2 = 02h, but answer 12h => 20h
mov a, r1 //// a = 01h
swap a //// a = 10h
orl a, r2 //// a = 10h or 02h 0001 0000 0r 0000 0010 = > 0001 0010 12h
mov 20h, a
end
Decimal to Hexadecimal Number
org 00h 16)171(10
mov a,#17 160
mov b,#16 ------
div ab 11
mov r1, b First division A = 10 = 0Ah, B = 11 = 0Bh
add a, #09h Answer => AB h
mov r5, a
mov r2,#01 16)165(10
mov a, r1 160
swap a ------
orl a, r2 05
mov r3, a First division A = 10 = 0Ah, B = 05h
anl a, #0f0h Answer => A5 h
swap a
mov b, #0ah 16)212(13
mul ab 208
mov r1, a ------
mov a,r3 04
anl a, #0fh First division A = 13 = 0Dh, B = 04 = 04h
add a, r1 Answer => D4 h
mov r6, a
mov a, r5
end