Introduction to Micro Controllers & Embedded System Design Assembly Language Programming Department of Electrical & Computer Engineering Missouri University.

Slides:



Advertisements
Similar presentations
The 8051 Microcontroller and Embedded Systems
Advertisements

1 Chapter 3 Jump, Loop, and Call Instructions. 2 Sections 3.1 Loop and Jump Instructions 3.2 Call Instructions 3.3 Time Delay Generation and Calculation.
Msc. Ivan A. Escobar Broitman Microprocessors 1 1 The 8051 Instruction Set.
TK 2633 Microprocessor & Interfacing
TK 2633 Microprocessor & Interfacing Lecture 3: Introduction to 8085 Assembly Language Programming (2) 1 Prepared By: Associate Prof. Dr Masri Ayob.
Room: E-3-31 Phone: Dr Masri Ayob TK 2633 Microprocessor & Interfacing Lecture 1: Introduction to 8085 Assembly Language.
Room: E-3-31 Phone: Dr Masri Ayob TK 2633 Microprocessor & Interfacing Lecture 1: Introduction to 8085 Assembly Language.
8051 ASSEMBLY LANGUAGE PROGRAMMING
Microcontroller Intel 8051
MICROCONTROLLER INSTRUCTION SET
Lecture 6 Assembler Directives. 2  Code generation flow  Assembler directives—Introduction  Segment control  Generic segment (SEGMENT, RSEG)  Absolute.
CoE3DJ4 Digital Systems Design Chapter 3: instruction set summary.
Dr Masri Ayob TK 2633: Microprocessor & Interfacing Lecture 7: Assembly Language.
1/2002JNM1 Basic Elements of Assembly Language Integer Constants –If no radix is given, the integer is assumed to be decimal. Int 21h  Int 21 –A hexadecimal.
Chapter 3 Elements of Assembly Language. 3.1 Assembly Language Statements.
CoE3DJ4 Digital Systems Design
The 8051 Assembly Language Branching & Subroutines
The x86 PC Assembly Language, Design, and Interfacing By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey © 2010, 2003, 2000, 1998 Pearson.
Assembler MCS51 - machine language. Structure of programme In general a single assembler programme line has following structure: for example: go_here:adda,r0.
Ass. Prof. Dr Masri Ayob TK 6123 Lecture 13: Assembly Language Level (Level 4)
The 8051 Microcontroller and Embedded Systems
Lecture Set 4 Programming the 8051.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Assembly Language Programming of 8085 BY Prof. U. V. THETE Dept. of Computer Science YMA.
JUMP, LOOP, AND CALL INSTRUCTIONS
1 EKT 225 MICROCONTROLLER I CHAPTER ASSEMBLY LANGUAGE PROGRAMMING.
1 Contents: 3.1 Instruction format and Addressing Modes 3.2 Instruction Introduction Chapter 3 Instruction system.
Microprocessors I 8051 Addressing Modes CS Prof. Msc. Ivan A. Escobar
Computer Architecture. Instruction Set “The collection of different instructions that the processor can execute it”. Usually represented by assembly codes,
Assembly language programming
PROGRAMMING THE BASIC COMPUTER
CHAPTER ADDRESSING MODES.
Assembler Directives Code generation flow
Format of Assembly language
Assembly Language (continue)
Classification of Instruction Set of 8051
Assembly Language Programming of 8085
Microprocessor T. Y. B. Sc..
Symbol Definition—CODE, DATA, IDATA, XDATA
PROGRAMMING THE BASIC COMPUTER
The 8051 Microcontroller and Embedded Systems
Lecture Set 5 The 8051 Instruction Set.
Assembler Directives Code generation flow
Subroutines and the Stack
William Stallings Computer Organization and Architecture 8th Edition
Introduction to Micro Controllers & Embedded System Design Interrupt
Data Processing Instructions
Computer Organization and Assembly Language (COAL)
Introduction to Micro Controllers & Embedded System Design Stored Program Machine Department of Electrical & Computer Engineering Missouri University.
Introduction to Assembly Language
SCHOOL OF ELECTRONICS ENGINEERING Electronics and Communication
BIC 10503: COMPUTER ARCHITECTURE
Introduction to Micro Controllers & Embedded System Design Timer Operation Department of Electrical & Computer Engineering Missouri University of Science.
Lecture 6 Assembler Directives.
Branching Instructions
Introduction to Micro Controllers & Embedded System Design Instruction set Department of Electrical & Computer Engineering Missouri University of Science.
Introduction to Micro Controllers & Embedded System Design Addressing Mode Department of Electrical & Computer Engineering Missouri University of Science.
Subroutines and the Stack
Chapter 8 Central Processing Unit
Shift & Rotate Instructions)
Introduction to Micro Controllers & Embedded System Design Instruction set Department of Electrical & Computer Engineering Missouri University of Science.
68000 Architecture, Data Types and Addressing Modes
DMT 245 Introduction to Microcontroller
Introduction to Micro Controllers & Embedded System Design
Instruction encoding We’ve already seen some important aspects of processor design. A datapath contains an ALU, registers and memory. Programmers and compilers.
8051 ASSEMBLY LANGUAGE PROGRAMMING
Subroutines and the Stack
Chapter 6 Programming the basic computer
Introduction to C Programming
Addressing Modes in 8051 MC S. Lourduraj Asst. Prof. of Physics
Presentation transcript:

Introduction to Micro Controllers & Embedded System Design Assembly Language Programming Department of Electrical & Computer Engineering Missouri University of Science & Technology hurson@mst.edu A.R. Hurson

An assembly language program is a program written using labels, mnemonics, and so on, in which a statement corresponds to a machine instruction. It is also called source code or symbolic code. Note: An assembly language program is not executable by a computer, it must undergo translation by “assembler” to machine language. Note: In this course, in places, I use the terms “assembly program” and “machine program” interchangeably. A.R. Hurson

A machine language program is the binary representation of assembly language program. It is also called object code and is executable by a computer. A.R. Hurson

An assembler is a compiler that translates assembly language program to an equivalent machine language program. Machine program may be in “absolute form” or “relocatable form”. In the latter case, “linking” is required to set the absolute addresses for execution. A.R. Hurson

A linker is a program that combines and converts relocatable object program forms into an absolute form. A.R. Hurson

In short an assembler receives a source file as input (e. g. , PROGRAM In short an assembler receives a source file as input (e.g., PROGRAM.SRC) and generates an object file (PROGRAM.OBJ) and listing file (PROGRAM.LST) as output. PROGRAM.SRC PROGRAM.LST PROGRAM.OBJ Assembler A.R. Hurson

Aforementioned translation usually takes place in two passes: In pass1 source file is scanned and a “symbol table” is generated. In pass, symbol table generated in phase1 is used to generate the object file and the listing file. A.R. Hurson

Pass1 – creation of Symbol table The location counter defaults to 0 or set by the ORG directive is initialized. As the source file is scanned, the location counter is incremented by the length of each instruction, length of defined data directives (DB or DW), and length of reserved memory directives (DS). Each time a label is detected, it is placed in the symbol table along with the content of the location counter. Symbols defined by equate directives (EQU) are also placed in the symbol table along with the equated value. A.R. Hurson

Pass2 – creation of Object and listing files To create the object file: Mnemonics are converted to op.codes. Symbols appear in the operand field are replaced with their values retrieved from the symbol table and correct data and addresses are calculated. The listing file consist of ASCII text for both the source program and the machine language program is generated. A.R. Hurson

Assembly Language Program Format An assembly language program is a collection of assembly language instructions and contains the following: Machine instructions Assembler directives Assembler controls, and Comments An assembly instruction has the following general format: [label:] Mnemonic [operand][,operand]…. [:comment] A.R. Hurson

Comments enhance readability of the program. Assembly instructions are mnemonics of executable instructions ( e.g., INC DPTR) Assembler directives are instructions to the assembler that define program structure: symbols, data, constants, and son (e.g., ORG). Note that no machine instruction will be generated for directives. Assembly controls set assembler modes and direct assembly flow (e.g., $TITLE). Comments enhance readability of the program. A.R. Hurson

A Label represents the address of the instruction that follows A Label represents the address of the instruction that follows. Label must be terminated with a colon (:). In a more general term we can talk about symbols where label is a type of symbol with the requirements that it must terminate with a colon. Symbols are assigned values or attributes using directives such as EQU, SEGMENT, BIT, DATA, … Symbols may be addresses, data constants, names of segments, or other constructs conceived by the programmer. A.R. Hurson

Example: A symbol 1) must begin with a letter, question mark, or underscore, 2) must be followed by a letter, digit, ?, or _, 3) can be of length 31 characters, and 4) may use either upper or lower case characters (reserve words may not be used). PAR EQU 500 ; “PAR” is a symbol which represents ; the value 500 START: MOV A, #0FFH ; “START” is a label which represents ; the address of the MOV instruction A.R. Hurson

Mnemonic Field includes directives (i. e Mnemonic Field includes directives (i.e., ORG, EQU, DB, …) and instruction mnemonics (e.g., ADD, MOV, DIV, …). Operand Field contains the address or data values used by the instruction. A label nay be used to represent the address of the data or a symbol may be used to represent a data constant. Some operations such as RET do not have any operand, whereas, others may have one or more operand(s) separated by commas. Comment must begin with a semicolon (;). Subroutines or large segments of a program may begin with a comment block. A.R. Hurson

Special Assembler Symbols includes A, B, R0-R7, DPTR, PC, C, AB, … In addition dollar sign ($) can be used to refer to the current value of the location counter. Example the last sentence can be written as: SETB C INC DPTR JNB TI, $ HERE: JNB TI, HERE A.R. Hurson

Indirect Address The “at” sign (@) indicates indirect addressing and may only be used with R0, R1 of active bank, DPTR, or the PC. ADD A, @R0 ; Operand is in internal RAM at ; the location indicated by the ; content of R0 MOV A, @A+PC ; Operand is in external code ; memory at an address which is ; formed by adding content of the ; accumulator to the program counter A.R. Hurson

Immediate data where operand value is in the operand field Immediate data where operand value is in the operand field. Immediate data is designated by a pound sign (#). All immediate data operations except (MOV DPTR, #data) require 8 bits of data. Immediate data are evaluated as a 16- bit constant and then the low-byte is used. Note; all bits in the high-byte must be the same (i.e., 00H or FFH), otherwise an error message “will not fit in a byte” is generated. CONSTANT EQU 100 MOV A, #0FFH ORL 40H, #CONSTANT A.R. Hurson

Immediate data MOV A, #0FF00H MOV A, #00FFH Are syntactically correct MOV A, #0FE00H MOV A, #01FFH Are syntactically incorrect A.R. Hurson

Immediate data In case of signed decimal notation, constant can be in the range of -256 to +255 MOV A, #-256 MOV A, #0FF0H Both instructions put 00H into accumulator A.R. Hurson

Data address Many instructions use direct addressing to refer to an operand. This could be either an on-chip data memory or an SFR address. MOV A, 45H MOV A, SBUF ; Same as MOV A, 99H A.R. Hurson

BIT address There are three ways to specify a bit address in an instruction: Explicitly by giving the address, Using the “dot operator” between the byte address and the bit position, or Using a predefined assembly symbol. SETB 0E7H ; Explicit bit address SETB ACC.7 ; Dot operator, same as above JNB TI, $ ; TI is a predefined symbol JNB 99H, $ ; Same as above A.R. Hurson

Code address A code address (mainly a label) is used in the operand field for various jump instructions. HERE:   SJMP HERE A.R. Hurson

Assemble-Time Expression Evaluation: Values and constants as an operand can be expressed in three ways: Explicitly (e.g., 0EFH), With a predefined symbol (e.g., ACC), or With an expression (e.g., 2 + 3). When an expression is used, the assembler calculates a value and inserts it into the instruction. All expression calculations are performed using 16-bit arithmetic, however, either 8 or 16 bits are inserted into the instruction, as needed. A.R. Hurson

Examples: MOV DPTR, #04FFH + 3 MOV DPTR, #0502H ; entire 16-bit result is used A.R. Hurson

Assemble-Time Expression Evaluation Number representation: Constant must be followed with: “B” for binary “O” or “Q” for octal, “D” for decimal, and “H” for hexadecimal Note: A digit must be the first character for a hexadecimal constant to be differentiated from a label (i.e., “0A5H” not “A5H”). A.R. Hurson

Examples: MOV A, #15 MOV A, #1111B MOV A, #0FH MOV A, #17Q MOV A, #15DB A.R. Hurson

Assemble-Time Expression Evaluation Character representation: Strings of one or two characters may also be used. The ASCII code is converted to binary by the assembler. Character constants must be enclosed in single quotes (‘). Examples: CJNE A, # ‘Q’, AGAIN SUBB A, # ‘0’ ; convert ASCII digit to ; binary digit MOV DPTR, # ‘AB’ MOV DPTR, # 4142H ; as above A.R. Hurson

Assemble-Time Expression Evaluation Arithmetic operators are: + Addition - Subtraction * Multiplication / Division MOD Modulo A.R. Hurson

Examples: MOV A, #10 + 10H MOV A, # 1AH ; same as above MOV A, #25 MOD 7 MOV A, # 4 ; same as above A.R. Hurson

Assemble-Time Expression Evaluation Logical operators are: Note: NOT operator is a unary operator OR Logical OR AND Logical AND XOR Logical Exclusive OR NOT Logical NOT (complement) A.R. Hurson

Examples: MOV A, # ’9’ AND 0FH MOV A, # 9 ; these two are the same THREE EQU 3 MINUS_THREE EQU -3 MOV A, # (NOT THREE) + 1 MOV A, # MINUS_THREE MOV A, # 11111101B ; all these three instructions ; are the same A.R. Hurson

Assemble-Time Expression Evaluation Special operators are: SHR Shift right SHL Shift left HIGH High-byte LOW Low-byte ( ) Evaluate first A.R. Hurson

Examples: MOV A, # 8 SHL 1 MOV A, # 10H ; these two are the same MOV A, # HIGH 1234H MOV A, # 12 ; these two are the same A.R. Hurson

Assemble-Time Expression Evaluation Relational operators are binary operators with the result of either true or false. They are: EQ = Equals NE <> Not equals LT < Less than LE <= Less than or equal to GT > Greater than GE >= Greater than or equal to A.R. Hurson

The result for all these operations is the same as: MOV A, # 0FFH Examples: The result for all these operations is the same as: MOV A, # 0FFH MOV A, # 5 = 5 MOV A, # NE 4 MOV A, # ‘X’ LT ‘Z’ MOV A, # ‘X’ >= ‘X’ MOV A, # $ > 0 MOV A, # 100 GE 50 A.R. Hurson

Operator Precedence is in the following order from the highest to the lowest: Operators of the same order are evaluated from left to right. ( ) HIGH, LOW * / MOD SHL SHR + - EQ NE LT GT GE = <> < <= > >+ NOT AND OR XOR A.R. Hurson

Operator Precedence : Example: Operation Generated value HIGH (‘A’ SHL 8) 0041H HIGH ‘A’ SHL 8 0000H NOT ‘A’ – 1 FFBFH ‘A’ OR ‘A’ SHL 8 4141H A.R. Hurson

Assembler Directives are instructions to assembler Assembler Directives are instructions to assembler. With the exception of DB and DW, they do not have any direct effect on the contents of memory (i.e., no executable code is generated during the translation). There are several categories of directives: Types Assembler State Control ORG, END, USING Symbol Definition SEGMENT, EQU, SET, DATA, IDATA, XDATA, BIT, CODE Storage Initialization DS, DBIT, DB, DW Program Linkage PUBLIC, EXTRN, NAME Segment Selection RSEG, CSEG, DSEG, ISEG, BSEG, XSEG A.R. Hurson

Directives: Assembler State Control Set Origin (ORG) alters the location counter to set a new program origin for statements to follow. Its general format is: ORG expression Example: ORG 100H ; Set the location counter to 100H Each program must be started with ORG directive. A.R. Hurson

Directives: Assembler State Control END should be the last statement in the source file. Note: END does not have any operand. A.R. Hurson

Directives: Assembler State Control USING directive informs the current active register bank. Its general format is: USING expression A.R. Hurson

Directives: Assembler State Control Example: USING 3 PUSH AR7 USING 1 The first PUSH instruction assembles to PUSH 1FH (i.e., R7 in bank 3) and the second PUSH instruction translates to PUSH 0FH (R7 in bank 1). A.R. Hurson

Directives: Symbols Create symbol that represent segments, registers, numbers, and addresses. SEGMENT gives name to a relocatble segment and determines its type. Its general format is: Symbol SEGMENT segment-type Example EPROM SEGMENT CODE A.R. Hurson

Directives: Symbols EQU assigns a numeric value to a specific symbol name. Its general format is: Symbol EQU expression Example N27 EQU 27 A.R. Hurson

Directives: Storage initialization/reservation These directives initialize and reserve space in either word, byte, or bit units. The space reserved starts at the location indicated by the current value of the location counter in the currently active segment. A.R. Hurson

Directives: Storage initialization/reservation Define Storage (DS) reserves space in byte units. Its general format is: [label:] DS expression Example: This code reserves 40 bytes buffer in the internal data segment DSEG AT 30H ; put in data segment LENGTH EQU 40 BUFFER DS LENGTH ; 40 bytes reserved A.R. Hurson

Directives: Storage initialization/reservation Define Storage (DS) ; The following code clears the 40 bytes ; reserved in the previous example MOV R7, #LENGTH MOV R0, #BUFFER LOOP MOV @R0, # 0 DJNZ R7, LOOP continue A.R. Hurson

Directives: Storage initialization/reservation Define BIT (DBIT) reserves space in bit. Its general format is: [label:] DBIT expression Define Byte (DB) initializes code memory with byte values. Its general format is: [label:] DB expression [,expression] […] A.R. Hurson

Define Byte (DB): Example: When the aforementioned code is assembled, we will have the following result in external code memory: CSEG AT 0100H SQUARE: DB 0, 1, 4, 9, 16, 25 MESSAGE DB ‘Login:’, 0 A.R. Hurson

Define Byte (DB): Example: Address Contents 0100 00 0101 01 0102 04 0100 00 0101 01 0102 04 0103 09 0104 10 0105 19 0106 4C 0107 6F 0108 67 0109 69 010A 6E 010B 3A 010C 00 A.R. Hurson

Define Byte (DB): Example: ORG 500H DATA1: DB 28 ; Decimal, 1C in Hex DATA2: DB 00110101B ; Binary, 35 in Hex DATA3: DB 39H ; Hex ORG 510H DATA4: DB ‘2591’ ; ASCII Number ORG DATA5: DB ‘My name is Joe’ ; ASCII Characters A.R. Hurson

Directives: Storage initialization/reservation Define Word (DW) is similar to DB, except it initializes code memory with word values (16 bits each). Its general format is: [label:] DW expression [,expression] […] A.R. Hurson

Define Word (DW): Example: When the aforementioned code is assembled, we will have the following hexadecimal memory assignments: CSEG AT 200H DB $, ‘A’, 1234H, 2, ‘BC’ A.R. Hurson

Define Word (DW): Example: Address Contents 0200 02 0201 00 0202 00 0200 02 0201 00 0202 00 0203 41 0204 12 0205 34 0206 00 0207 02 0208 42 0209 43 A.R. Hurson

Example: Assume that bit P2 Example: Assume that bit P2.3 is an input and represent the condition of an oven. If it goes high, it indicates that the oven is hot. Monitor it continuously, whenever, it goes high, send a low-to-high pulse to port P1.5 to turn on a buzzer: OVEN_HOT BIT P2.3 BUZZER BIT P1.5 HERE: JNB OVEN_HOT, HERE ACALL DELAY CLR BUZZER CPL BUZZER SJMP HERE A.R. Hurson

Example: Assume that the RAM location 12H holds the status of whether there has been a phone call or not. If it is high, it means there has been a new call since it was checked the last time. Write a program to display “New Messages” on an LCD if bit RAM 12H is high. If it is low, the LCD should say “No new messages”: PHONEBIT BIT 12H MOV C, PHONEBIT JNC NO MOV DPTR, #400H LCALL DISPLAY SJMP EXIT NO: MOV DPTR, #420H LCALL DISPLAY EXIT:   ORG 400H YES_MSG: DB “New Message”, 0 ORG 420H NO_MSG: DB “No new message”, 0 A.R. Hurson

Assembling and Running an 8051 program A given assembly language program is a sequence of assembly instructions and directives. Instructions specify actions and directives direct assembler: 1) An editor is used (i.e., MS-DOS EDIT) to type an assembly language program in order to generate a source file. 2) The source file is fed to assembler in order to generate a machine code (i.e., object file) and a list file. 3) A linker program is used to convert the object file (s) into an absolute object file. 4) Finally, an absolute object file is fed to object-to-hex converter to create a file ready to be burn into ROM. A.R. Hurson

Note: An lst file lists op.codes, Addresses, and errors detected EDITOR Program Assembler Linker OH .asm .obj .abs .hex .lst Note: An lst file lists op.codes, Addresses, and errors detected By the assembler. This file can Be used to detect syntax errors. A.R. Hurson

A Running example: Original program ORG ; Start at location 0 MOV R5, #25H ; Load 25H into R5 MOV R7, #34H ; Load 34H into R7 MOV A, #0 ; Initialize accumulator with 0 ADD A, R5 ; Add content of R5 to A ADD A, R7 ; Add content of R7 to A ADD A, #12H ; Add 12H to A HERE: SJMP HERE ; Stay in this Loop END ; End of program A.R. Hurson

A Running example: List file 1 0000 ORG ; Start at location 0 2 0000 7D25 MOV R5, #25H ; Load 25H into R5 3 0002 7F34 MOV R7, #34H ; Load 34H into R7 4 0004 7400 MOV A, #0 ; Initialize accumulator with 0 5 0006 2D ADD A, R5 ; Add content of R5 to A 6 0007 2F ADD A, R7 ; Add content of R7 to A 7 0008 2412 ADD A, #12H ; Add 12H to A 8 000A 80FE HERE: SJMP HERE ; Stay in this Loop 9 000C END ; End of program A.R. Hurson

A Running example: After the program is burned into ROM we have the following image of the program: 0000 7D 0001 25 0002 7F 0003 34 0004 74 0005 00 0006 2D 0007 2F 0008 24 0009 12 000A 80 000B FE A.R. Hurson

Example: Loop and conditional jump Write a program that adds 3 to accumulator 10 times. Note: Since size of loop counter (R7) is 8, the loop cannot be iterated more than 256 times. MOV A, #0 ; Clear accumulator MOV R7, #10 ; Set the loop counter AGAIN: ADD A, #3 ; Add 3 to accumulator DNJZ R7, AGAIN ; Repeat the loop until R7 = 0 MOV R5, A A.R. Hurson

Example: Nested Loops What if we want to repeat an action more than 256 times? Write a program to complement 55H 700 times: MOV A, #55H ; initialize accumulator MOV R3, #10 ; Set the outer loop counter NEXT: MOV R2, #70 ; Set the inner loop counter AGAIN: CPL A ; Add 3 to accumulator DNJZ R2, AGAIN ; Repeat the loop until R2 = 0 DNJZ R2, NEXT ; Repeat the loop until R3 = 0 (continue) A.R. Hurson

What is the logic behind the following program? MOV A, #0 ; initialize accumulator MOV R5, A ; Set R5 to zero ADD A, #79H JNC N_1 ; Jump if CY = 1 INC R5 N_1: ADD A, #0F5H JNC N_2 N_2: ADD A, #0E2H JNC OVER OVER: (continue) A.R. Hurson

Calculating jump forward and backward addresses (short jump): Line PC operation Mnemonic Operand 0000 ORG 0000 0000 7800 MOV R0, #0 0002 7455 MOV A, #55H 0004 6003 JZ NEXT 0006 08 INC R0 0007 04 AGAIN: INC A 0008 04 INC A 0009 2477 NEXT: ADD A, #77H 000B 5005 JNC OVER 000D E4 CLR A 000E F8 MOV R0, A 000F F9 MOV R1, A 0010 FA MOV R2, A 0011 FB MOV R3, A 0012 2B OVER: ADD A, R3 0013 50F2 JNC AGAIN 0015 80FE HERE: SJMP HERE 0017 END A.R. Hurson

Subroutine call and return ORG 0 BACK: MOV A, #55H ; initialize accumulator MOV P1, A ; Send 55H to port1 LCALL DELAY ; Time delay MOV A, #0AAH MOV P1, A LCALL DELAY SJMP BACK ; This is the subroutine ORG 300H DELAY: MOV R5, #0FFH AGAIN: DJNZ R5, AGAIN RET END A.R. Hurson

Subroutine call and return Line PC operation Mnemonic Operand 0000 ORG 0 0000 7455 BACK: MOV A, #55H ; initialize accumulator 0002 F590 MOV P1, A ; Send 55H to port1 0004 120300 LCALL DELAY ; Time delay 0007 74AA MOV A, #0AAH 0009 F590 MOV P1, A 000B 120300 LCALL DELAY 000E 80F0 SJMP BACK 0010 ; This is the subroutine 0300 ORG 300H 0300 7DFF DELAY: MOV R5, #0FFH 0302 DDFE AGAIN: DJNZ R5, AGAIN 0304 22 RET 0305 END A.R. Hurson

Program branching instructions bytes long. MOV DPTR, #JUMP_TABLE MOV A, #INDEX_NUMBER RL A JMP @A + DPTR A.R. Hurson

Program branching instructions Jump Tables JUMP_TABLE: AJMP CASE0 AJMP CASE1 AJMP CASE2 AJMP CASE3 A.R. Hurson

Program branching instructions Example: Assume the jump table in the previous example starts at memory location 8100H with the following values: Address Content 8100 01 8101 B8 8102 8103 43 8104 41 8105 76 8106 E1 8107 F0 A.R. Hurson

Program branching instructions a) What is the beginning and ending addresses of the 2K block of the code memory in which these instructions reside? b) At what addresses do CASE0 through CASE3 begin? 8000H to 87FFH CASE0 begins at address 80B8H CASE1 begins at address 8043H CASE2 begins at address 8276H CASE3 begins at address 87F0H A.R. Hurson

Program branching instructions Subroutines and Interrupts There are two variations of CALL: ACALL and LCALL using absolute and long addressing, respectively. Either instruction pushes the value of the program counter into the stack and loads the program counter with the address specified in the instruction. The PC is pushed into the stack, low-byte first and high-byte second. The bytes are popped from the stack in reverse order; high- byte first and low-byte second. A.R. Hurson

Program branching instructions Subroutines and Interrupts Example: An LCALL instruction is at address 1000H-1002H and the stack pointer contains 20H, then the LCALL Pushes the return address 1003H into the stack, placing 03H in 21H and 10H in 22H Leaves the stack pointer containing 22H, and Jumps to the subroutine by loading the PC with the address contained in bytes 2 and 3 of the instruction. A.R. Hurson

Program branching instructions Subroutines and Interrupts Example: The following instruction LCALL COSINE Is at address 0240H through 0206H and the subroutine COSINE is at address 043AH. Assume stack pointer contains 3AH. What internal RAM locations are altered and what are their new values after execution of LCALL instruction? Address Contents 3BH 02H 3CH 07H 81H (stack pointer) 3CH A.R. Hurson

Program branching instructions Subroutines and Interrupts Subroutines should end with a RET instruction. This instruction pops the stack into the program counter to allow the execution of the instruction after CALL. RETI instruction is used to return from an interrupt service routine (ISR). RETI also signals the interrupt control system that the interrupt in progress is done. If there is no other pending interrupt at the time RETI is executed, the RETI is functioning the same as RET instruction. A.R. Hurson

Program branching instructions Conditional jumps 8051 offers a variety of conditional jump instructions, all specify the destination addressing using relative addressing, hence it is limited to a jump distance of -128 to +127 bytes from the instruction following the conditional jump instruction. Note: The DJNZ and CJNE instructions are for loop control. For example, to execute a loop N times, load a counter byte with N and terminate the loop with a DJNZ to the beginning of the loop. A.R. Hurson

Program branching instructions Conditional jumps Example: The following code shows a loop that is iterated 10 times. MOV R7, #10 LOOP: (begin loop)  (end loop) DNJZ R7, LOOP A.R. Hurson

Program branching instructions Conditional jumps Example: Suppose a character is read from the serial port and it is desired to jump to an instruction designated as “TERMINATE”, if it is 03H. The following code shows this process. CJNE A, #03H, SKIP SJMP TERMINATE SKIP: (continue) A.R. Hurson