8051 Microcontroller1
8051 Microcontroller1
Djnz r2, back ; repeat data transfer until all the data are transferred (decrement r2, and jump to back
when r2 is not equal to zero)
Exit: sjmp exit ; specifies logical end of the program
End ; specifies physical end of the program
(to view go to memory location D:0x20, D represents data memory space(refers to internal ram), C represents
code memory space(refers to program memory), eg. If the RAM specification is 128 bytes, then internal
memory value ranges from 0x00 to 0x7F (i.e., 0 to 127 = 128/16 =8-1 = 7F)
Result:
Before Execution:
I:0020h: 01 02 03 04 05 06
I:0040h: 00 00 00 00 00 00
After Execution:
I:0020h: 01 02 03 04 05 06
I:0040h: 01 02 03 04 05 00
2. Write an ALP to move a block of n bytes of data from source (2000h) to destination (2050h) using
External RAM.
; ALP to transfer block of n bytes from source (2000h) to destination (2050h) using External-RAM
Org 0000h ; beginning of the program at 0000h location
Mov dptr,#2000h ; load dptr(one of 16 bit register(user accessible) out of two(program counter &
DPTR) with source location address
Mov r0,#50h ; r0 is loaded with destination location lower byte
; need 16 bit to store external ram memory location(20 is 8 bit and 50 is another 8 bit, I.e., 2050h) but we have
only one 16 bit register i.e., dptr which is already used for source address. So we are using 2, 8 bit registers, 1
for 20h and another for 50h, all together 2050h
Mov r1,#20h ; r1 is loaded with destination location upper byte
Mov r2,#04 ; r2 is loaded with block size
Loop: Movx a,@dptr ; here dptr is loaded with external memory so instead of mov, we should movx
– x is for external memory. Accumulator is loaded with content of address
which is present in dptr(16bit address)
Push dph ; save dptr content into stack (dph is data pointer higher or upper)
;(in order to load to the destination location which is stored in r0 and r1 we have to
take it to dptr, in such condition it will delete source location 2000h to avoid that
push the values to dph and dpl)
Push dpl ; save dptr content into stack (dpl is data pointer lower)
Mov dph,r1 ; load dph with r1 destination content(MSB) upper byte
Mov dpl,r0 ; load dpl with r0 destination content(LSB) lower byte
Movx @dptr,a ; move accumulator content to destination location
After Execution:
X:2000h: 11 22 33 44 55 66
X:2050h: 11 22 33 44 00 00
3. Write an ALP To exchange the source block starting with address 20h, (Internal RAM) containing
N (05) bytes of data with destination block starting with address 40h (Internal RAM).
; ALP to exchange block of N bytes between source (20h) and destination (40h) in Internal-RAM
Org 0000h ; Define code location to start from address 0000h
Mov r0, #20h ; Load R0 with source address
Mov r1, #40h ; Load R1 with destination address
Mov r2, #05h ; Load R2 with the number of bytes to exchange
Back: Mov a, @r0 ; Load accumulator with the content of the source address
Xch a, @r1 ; Exchange accumulator content with the content of the destination address
Mov @r0, a ; Store the accumulator content at the source address
Inc r0 ; Increment source address
Inc r1 ; Increment destination address
Djnz r2, Back ; Decrement R2 and repeat the loop if it's not zero
End ; End of the program
Result:
Before Execution:
I:0020h: 01 02 03 04 05
I:0040h: 11 22 33 44 55
After Execution:
I:0020h: 11 22 33 44 55
I:0040h: 01 02 03 04 05
4. Write an ALP to exchange the source block starting with address 10h (Internal memory), containing
n (06) bytes of data with destination block starting at location 00h (External memory).
Program:
Org 0000h ; defining code location to start from location 0000
Mov r0,#10h ; load r0 with source location address
Mov dptr, #2000h ; load dptr register to hold destination address (or use 00h as per program)
Mov r2,#06h ; load r2 with block size
Back: movx a,@dptr ; load accumulator with the content of the address specified by (@)dptr, here
we are using external memory so we need to use movx, here x mention
external memory
Mov b,@r0 ; load reg b with internal memory(so no x) content
Xch a,b ; xchange accumulator (a) and reg b content
Movx @dptr,a ; from acc, move the content to dptr address [external]
Mov @r0,b ; from reg b move the content to r0 address [internal]
Inc r0 ; internal memory location increment r0 by 1
Inc dptr ; external memory location increment dptr by 1
Djnz r2,back ; decrement reg r2, if not zero jump to label ‘back’
End
Result:
Before Execution:
I:0010h: 01 02 03 04 05 06
X:2000h: 11 22 33 44 55 66
After Execution:
I:0010h: 11 22 33 44 55 66
X:2000h: 01 02 03 04 05 06