Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture Set 4 Programming the 8051.

Similar presentations


Presentation on theme: "Lecture Set 4 Programming the 8051."— Presentation transcript:

1 Lecture Set 4 Programming the 8051

2 Addressing Modes Five addressing modes are available:
Immediate Register Direct Indirect Indexed There are three more modes: Relative Absolute Long These are used with calls, branches and jumps and are handled automatically by the assembler.

3 Immediate Addressing The data is directly specified in the instruction. Useful for getting constants into registers. Immediate data must be preceded with a “#” sign. MOV R0, #0F0H ; Load R0 with the value F0H The immediate value is a maximum of 8-bits. One exception, when dealing with the DPTR register it can be 16-bits. MOV DPTR, #2000H ; Load the value 2000H into the DPTR register

4 Register Addressing Mode
Direct access to eight registers – R0 through R7. MOV A, R0 MOV R1, A ADD A, R1 Not all combinations are valid. MOV R2, R1 ; Invalid There are 4 banks of registers accessible through register addressing. Only one bank can be accessed at a time controllable through bit RS0 and RS1 of the PSW. MOV PSW, # B Set RS0:RS1 to 11, therefore, accessing register bank 3.

5 Direct Addressing Direct addressing can access any on-chip hardware register. All on-chip memory locations and registers have 8-bit addresses. Can use the 8-bit address in the instruction. MOV A, 4H ; Amem[04H] Or can use the register name. MOV A, R4 Don’t get confused with Immediate mode. No “#” sign.

6 Indirect Addressing R0 and R1 may be used as pointer registers where their contents indicate an address in internal RAM where the data is to be read or written. MOV R1, #40H ; Make R1 point to location 40 MOV ; Move the contents of 40H to A R1 ; Move contents of R1 into the memory location pointed to by R0.

7 Indirect Addressing Can also be used for accessing external memory:
Can use R0 and R1 to point to external memory locations 00H to FFH. MOVX ; Move contents of external memory location whose address is in R1 into A Can also use DPTR to point to all 64k of external memory. MOVX

8 Indexed Addressing Use a register for storing a pointer to memory and another register for storing an offset. The effective address is the sum of the two: EA = Pointer + Offset MOVC ; Move byte from memory located at DPTR+A to A.

9 Example Program to implement X2 as a lookup table.
Supports 1 through 9 only. ORG 0 ; assembler directive mov DPTR, #LUT ; 300H is the LUT address mov A, #0FFH mov P1, A ; program the port P1 to input data Again: mov A, P1 ; read x movc ; get x2 from LUT mov P2, A ; output x2 to P2 sjmp again ; for (1) loop ORG 300H ;Look-up Table starts at 0x0300 LUT: DB 0, 1, 4, 9, 16, 25, 36, 49, 64, 81

10 Program Control Instructions
Unconditional Branch ajmp addr11 ; absolute jump ljmp addr16 ; long jump sjmp rel ; short jump to relative address ; jump indirect Conditional branch jz, jnz rel ; short conditional jump to rel. addr djnz rel ; decrement and jump if not zero cjne rel ; compare and jump if not equal Subroutine Call acall addr11 ; absolute subroutine call lcall addr16 ; long subroutine call ret ; return from subroutine call reti ; return from ISV

11 Target Address Target address can be,
absolute: A complete physical address addr16: 16 bit address, anywhere in the 64k addr11: 11 bit address, anywhere within 2k page. rel: relative (forward or backward) -128 bytes to +127 bytes from the current code location Target address calculation for relative jumps PC of next instruction + rel address For jump backwards, drop the carry PC = 15H, SJMP 0FEH Address is 15H + FEH = 13H Basically jump to next instruction minus two (current instruction)

12 Conditional Jumps jz, jnz : Conditional on A==0
Checks to see if A is zero jz jumps if A is zero and jnz jumps is A not zero No arithmetic op need be performed (unlike 8086/8085) djnz : dec a byte and jump if not equal to zero djnz Rn, rel djnz direct, rel jnc : Conditional on carry CY flag jc rel jnc rel cjne : compare and jump if not equal cjne A, direct, rel cjne Rn, #data, rel #data, rel

13 Loop using djnz Add 3 to A ten times Loop within loop using djnz
mov A, #0 ; clear A mov R2, #10 ; R2  10, can also say 0AH AGAIN: add A, #03 ; add 3 to A djnz R2, AGAIN ; repeat until R2==0 mov R5, A ; save the result in R5 Loop within loop using djnz mov R3, #100 loop1: mov R2, #10 ; trying for 1000 loop iterations loop2: nop ; no operation djnz R2, loop2 ; repeat loop2 until R2==0 djnz R3, loop1 ; repeat loop1 until R3==0

14 Unconditional Jumps LJMP addr16 SJMP rel Long jump. 3 byte instruction
Jump to a 2byte target address 3 byte instruction SJMP rel Jump to a relative address from PC+127 to PC-128 Jump to PC (00H – 7FH) Jump to PC – 128 (80H – FFH)

15 Call Instructions Subroutines: LCALL addr16 ACALL addr11 RET
Reusable code snippets LCALL addr16 Long call. 3 byte instruction. Call any subroutine in entire 64k code space PC is stored on the stack ACALL addr11 2 byte instruction Call any subroutine within 2k of code space Other than this, same behavior as LCALL Saves code ROM for devices with less than 64K ROM RET Return from a subroutine call, Pops PC from stack


Download ppt "Lecture Set 4 Programming the 8051."

Similar presentations


Ads by Google