Assembly Language Techniques Programming the MCS51 Microcontroller Background Data Transfer Instructions Data Processing Instructions Program Flow Control Instructions Interrupts Chatchai Jantaraprim (cj@coe.psu.ac.th)
Program Flow Control Instructions Branching Instructions Allow microntroller to take different actions at run time In hardware, it modified Program Counter, defined the next instruction to be fetched Normally next immediate following instruction is executed Braching instruction place different value in Program Counter Braching includes: Jumps Call/Return
Program Flow Control Instructions Unconditional Jump Instructions SJMP�<rel> Short jump, 127 byte ahead, 128 byte behind AJMP�<addr11> Absolute jump, 11-bit (2K Block) LJMP�<addr16> Long jump, 16-bit (Entire 64K address space) JMP��@A+DPTR Long jump, from lookup table org 8000h����org 8000h mov C, P1.0��start:�mov C, P1.0 mov P1.1, C���mov P1.1, C ljmp 8000h����sjmp start
Program Flow Control Instructions Unconditional Jump Instructions (cont.) 8000��1��org�8000h 8000 A290�2�start:�mov�C, P1.0 8002 9291�3��mov�P1.1, C 8004 028000�4��ljmp�start 0000=��5��end 8000��1��org�8000h 8000 A290�2�start:�mov�C, P1.0 8002 9291�3��mov�P1.1, C 8004 80FA�4��sjmp�start 0000=��5��end 8000��1��org�8000h 8000 A290�2�start:�mov�C, P1.0 8002 9291�3��mov�P1.1, C 8004 0100�4��ajmp�start 0000=��5��end
Program Flow Control Instructions Unconditional Jump Instructions (cont.) 9000��1��org�9000h 9000 A290�2�start:�mov�C, P1.0 9002 9291�3��mov�P1.1, C 9004 029000�4��ljmp�start 0000=��5��end 9000��1��org�9000h 9000 A290�2�start:�mov�C, P1.0 9002 9291�3��mov�P1.1, C 9004 80FA�4��sjmp�start 0000=��5��end 9000��1��org�9000h 9000 A290�2�start:�mov�C, P1.0 9002 9291�3��mov�P1.1, C 9004 0100�4��ajmp�start 0000=��5��end
Program Flow Control Instructions Unconditional Jump Instructions (cont.) Indexed Jump jump @a+dptr ... anl a, #3 rl a rl a mov dptr, #jump_table jmp @a+dptr jump_table: ljmp routine_0 nop ljmp routine_1 nop ljmp routine_2 nop ljmp routine_3 ...
Program Flow Control Instructions Unconditional Jump Instructions (cont.) Short jump and Absolute jump is relocatable, but with limited jump range Long jump is not relocatable, but provides program to jump to anywhere Indexed jump provides a convenient means to construct a jump table
Program Flow Control Instructions Conditional Jump Instructions JZ��<rel> JNZ��<rel> JC��<rel> JNC��<rel> JB��<bit>, <rel> JNB��<bit>, <rel> JBC��<bit>, <rel> CJNE�A, direct, <rel> CJNE�A, #data, <rel> CJNE�Rn, #data, <rel> CJNE�@Ri, #data, <rel> DJNZ�Rn, <rel> DJNZ�direct, <rel>
Program Flow Control Instructions Conditional Jump Instructions (cont.) delay: mov r7, #0 mov r6, #0 delay0: djnz r6, $ djnz r7, delay0 ret
Program Flow Control Instructions Call and Return Instructions Call push PC on stack before branch Return pop PC off stack Call and return are necessary to implement subroutines. ACALL�<addr11> branch within 2K block LCALL�<addr16> branch anywhere in 64K RET pop PC RETI pop PC and reset interrupt hardware
Program Flow Control Instructions Call and Return Instructions (cont.) LED_flag�equ�0Fh �org�8000h start: �jb�LED_flag, LED_OFF �setb�LED_flag �clr�P1.0 �lcall�delay �sjmp�start LED_OFF: �clr�LED_flag �setb�P1.0 �lcall�delay �sjmp�start
Program Flow Control Instructions Call and Return Instructions (cont.) Computed goto techniques: �org�8000h �mov�dptr, #next �push�dph �push�dpl �mov�a, #81h �push�acc �clr�a �push�acc �ret next: �ljmp�8000h �org�8100h
Recommend
More recommend