CHAPTER 2
8051 Assembly
Atanu Shome
Computer Science and Engineering Discipline
Khulna University
Confession
■Most of the materials have been collected from Internet.
■Images are taken from Internet.
■Various books are used to make these slides.
■Primary reference book:
I.The 8051 Microcontroller and Embedded System
by M. A. Mazidi
Atanu Shome, CSE, KU 2
This slide is not enough to learn these topics. It’s just for
your guideline. (NAH! Just a reminder for me -which
topics to cover) Reading from the slides only, will be a BIG
mistake.
YOU NEED TO READ THE DAMN BOOK
Atanu Shome, CSE, KU 3
RAM Allocation in 8051
■Register Banks and Stack -32 bytes
■Bit-addressable RAM –16 bytes
■Scratch Pad RAM –80 bytes
Atanu Shome, CSE, KU 4
Register Banks
■These 32 bytes are divided into 4 banks of registers in which each bank has 8
registers, R0-R7
Atanu Shome, CSE, KU 5
Default Register Bank?
■What happens when powered up?
Atanu Shome, CSE, KU 6
Use of Register Banks
■Example 2-5
–MOV R0, #99H ;load RAM location 00 with 99H
–MOV R5, #85H ;load R1 with 85H, RAM location 05 with 99H
■Example 2-6
–MOV 00, #99H ;load R0 with 99H
–MOV 01, #85H ;load R1 with 85H
Atanu Shome, CSE, KU 7
Direct Addressing mode
Change Register Banks
■PSW Bits Bank Selection
Atanu Shome, CSE, KU 8
Bank RS1 (PSW.4) RS0(PSW.3)
BANK0 0 0
BANK1 0 1
BANK2 1 0
BANK3 1 1
Switch Register Banks
■Write instructions to use the register bank 3. And use the same value 05h in all
registers R0 to R3.
SETB PSW.4
SETB PSW.3
MOV R0, #05h
MOV R1, #05h
MOV R2, #05h
MOV R3, #05h
Atanu Shome, CSE, KU 9
STACK
■SP is at 07h when powered up
■SP increases by 1
■PUSH and POP
■The CPU also uses the stack to save the address of the instruction just below the
CALL instruction
Atanu Shome, CSE, KU 10
Atanu Shome, CSE, KU 11
Atanu Shome, CSE, KU 12
Use of Stack beyond Limit
■Write push instructions to push the contents of registers on stack after the execution
of the following set of instructions.
MOV SP, #4Fh
SETB PSW.3
MOV R0, #25h
MOV R1, #0Ch
MOV R2, #05h
MOV A, #0CEh
■Remember, the address for A is 0E0H
Atanu Shome, CSE, KU 13
Sample Question
■Imagine stack is located at its default position. Show the values of stack and SP
after each PUSH and POP operation of the following code. Also show the initial
situation of stack and SP. Show the values of all 8 registers R0 –R7 after the code
executed.
MOV R6, #25h
MOV R1, #12h
MOV R4, #0F3h
PUSH 6
PUSH 1
PUSH 4
POP 3
POP 2
Atanu Shome, CSE, KU 14
Sample Question
■How many bytes are used in each following directives?
–DATA1: DB “AMERICA”
–ASC_DATA: DB “1234”
–MY_DATA DB “ABC123”
Atanu Shome, CSE, KU 15