Presentation is loading. Please wait.

Presentation is loading. Please wait.

The 8051 Assembly Language Branching & Subroutines

Similar presentations


Presentation on theme: "The 8051 Assembly Language Branching & Subroutines"— Presentation transcript:

1 The 8051 Assembly Language Branching & Subroutines
Part IV The 8051 Assembly Language Branching & Subroutines EE/CS-352: Embedded Microcontroller Systems

2 EE/CS-352: Embedded Microcontroller Systems
Program Flow Control Unconditional jumps (“go to”) Conditional jumps Call and return EE/CS-352: Embedded Microcontroller Systems

3 EE/CS-352: Embedded Microcontroller Systems
Unconditional Jumps SJMP <rel addr> ; Short jump, relative address is 8-bit 2’s complement number, so jump can be up to 127 locations forward, or 128 locations back. LJMP <address 16> ; Long jump AJMP <address 11> ; Absolute jump to anywhere within 2K block of program memory ; jump with an offset EE/CS-352: Embedded Microcontroller Systems

4 EE/CS-352: Embedded Microcontroller Systems
Infinite Loops Start: mov C, p3.7 mov p1.6, C sjmp Start EE/CS-352: Embedded Microcontroller Systems

5 EE/CS-352: Embedded Microcontroller Systems
Re-locatable Code Memory specific (NOT Re-locatable) cseg at 8000h mov C, p1.6 mov p3.7, C ljmp 8000h end Re-locatable cseg at 8000h Start: mov C, p1.6 mov p3.7, C sjmp Start end EE/CS-352: Embedded Microcontroller Systems

6 EE/CS-352: Embedded Microcontroller Systems
Conditional Jumps These instructions cause a jump to occur only if a condition is true. Otherwise, program execution continues with the next instruction. loop: mov a, P1 jz loop ; if a=0, goto loop, ;else goto next ;instruction mov b, a EE/CS-352: Embedded Microcontroller Systems

7 EE/CS-352: Embedded Microcontroller Systems
Conditional jumps Mnemonic Description JZ <rel addr> Jump if a = 0 JNZ <rel addr> Jump if a != 0 JC <rel addr> Jump if C = 1 JNC <rel addr> Jump if C != 1 JB <bit>, <rel addr> Jump if bit = 1 JNB <bit>,<rel addr> Jump if bit != 1 JBC <bir>, <rel addr> Jump if bit =1, clear bit CJNE A, direct, <rel addr> Compare A and memory, jump if not equal EE/CS-352: Embedded Microcontroller Systems

8 Conditional Jumps for Branching
if condition is true goto label else goto next instruction condition false true label jz led_off setb C mov P1.6, C sjmp skipover clr C mov A, P0 if a = 0 is true send a 0 to LED else send a 1 to LED led_off: skipover: EE/CS-352: Embedded Microcontroller Systems

9 More Conditional Jumps
Mnemonic Description CJNE A, #data, <rel addr> Compare A and data, jump if not equal CJNE Rn, #data, <rel addr> Compare Rn and data, jump if not equal #data, <rel addr> Compare Rn and memory, jump if not equal DJNZ Rn, <rel addr> Decrement Rn and then jump if not zero DJNZ direct, <rel addr> Decrement memory and then jump if not zero EE/CS-352: Embedded Microcontroller Systems

10 EE/CS-352: Embedded Microcontroller Systems
Iterative Loops For A = 0 to 4 do {…} clr a loop: ... inc a cjne a, #4, loop For A = 4 to 1 do {…} mov R0, #4 loop: ... ... djnz R0, loop EE/CS-352: Embedded Microcontroller Systems

11 EE/CS-352: Embedded Microcontroller Systems
Call and Return Call is similar to a jump, but Call instruction pushes PC on stack before branching acall <address ll> ; stack  PC lcall <address 16> Return is also similar to a jump, but Return instruction pops PC from stack to get address to jump to ret ; PC  stack EE/CS-352: Embedded Microcontroller Systems

12 EE/CS-352: Embedded Microcontroller Systems
Subroutine - Example 1 EE/CS-352: Embedded Microcontroller Systems

13 Initializing Stack Pointer
The Stack Pointer (SP) is initialized to 0x07 When using subroutines, the stack will be used to store the PC, so it is very important to initialize the stack pointer. EE/CS-352: Embedded Microcontroller Systems

14 EE/CS-352: Embedded Microcontroller Systems
Subroutine - Example 2 $include (c8051f020.inc) GREEN_LED equ P1.6 cseg at 0 ljmp Main cseg at 0x100 Main: mov WDTCN, #0DEh mov WDTCN, #0ADh orl P1MDOUT,#40h mov XBR2, #40h clr GREEN_LED Again: acall Delay cpl GREEN_LED sjmp Again Delay: mov R7, #02 Loop1: mov R6, #00h Loop0: mov R5, #00h djnz R5, $ djnz R6, Loop0 djnz R7, Loop1 ret END reset vector main program subroutine EE/CS-352: Embedded Microcontroller Systems

15 Subroutine – another example
; Program to compute square root of value on Port 3 (bits 3-0) and ; output on Port 1. $INCLUDE (C8051F020.inc) cseg at 0 ljmp Main Main: mov P3MDOUT, #0 ; Set open-drain mode mov P3, #0xFF ; Port 3 is an input mov P1MDOUT, #0xFF ; Port 1 is push/pull mov XBR2, #40h ; Enable crossbar loop: mov a, P3 anl a, #0x0F lcall sqrt mov P1, a sjmp loop sqrt: inc a movc + PC ret squares: db 0,1,1,1,2,2,2,2,2,3,3,3,3,3,3,3 end reset vector main program subroutine data EE/CS-352: Embedded Microcontroller Systems

16 EE/CS-352: Embedded Microcontroller Systems
Why Subroutines? EE/CS-352: Embedded Microcontroller Systems


Download ppt "The 8051 Assembly Language Branching & Subroutines"

Similar presentations


Ads by Google