Introduction to Micro Controllers Embedded System Design Assembly
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 1
Introduction to Micro Controllers & Embedded System Design • 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 2
Introduction to Micro Controllers & Embedded System Design • 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 3
Introduction to Micro Controllers & Embedded System Design • 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 4
Introduction to Micro Controllers & Embedded System Design • A linker is a program that combines and converts relocatable object program forms into an absolute form. A. R. Hurson 5
Introduction to Micro Controllers & Embedded System Design • 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. OBJ PROGRAM. SRC Assembler PROGRAM. LST A. R. Hurson 6
Introduction to Micro Controllers & Embedded System Design • Aforementioned translation usually takes place in two passes: • In pass 1 source file is scanned and a “symbol table” is generated. • In pass, symbol table generated in phase 1 is used to generate the object file and the listing file. A. R. Hurson 7
Introduction to Micro Controllers & Embedded System Design • Pass 1 – 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 8
Introduction to Micro Controllers & Embedded System Design • Pass 2 – 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 9
Introduction to Micro Controllers & Embedded System Design • 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: ] A. R. Hurson Mnemonic [operand][, operand]…. [: comment] 10
Introduction to Micro Controllers & Embedded System Design • 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 11
Introduction to Micro Controllers & Embedded System Design • 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 12
Introduction to Micro Controllers & Embedded System Design • Example: PAR EQU 500 START: MOV A, #0 FFH • ; “PAR” is a symbol which represents ; the value 500 ; “START” is a label which represents ; the address of the MOV instruction 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). A. R. Hurson 13
Introduction to Micro Controllers & Embedded System Design • 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 14
Introduction to Micro Controllers & Embedded System Design • Special Assembler Symbols includes A, B, R -R , DPTR, PC, C, 0 7 AB, … In addition dollar sign ($) can be used to refer to the current value of the location counter. • Example SETB C INC DPTR JNB TI, $ the last sentence can be written as: HERE: JNB TI, HERE A. R. Hurson 15
Introduction to Micro Controllers & Embedded System Design • Indirect Address • The “at” sign (@) indicates indirect addressing and may only be used with R 0, R 1 of active bank, DPTR, or the PC. ADD A, @R 0 ; Operand is in internal RAM at ; the location indicated by the ; content of R 0 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 16
Introduction to Micro Controllers & Embedded System Design • Immediate data where operand value is in the operand field. Immediate data is designated by a pound sign (#). CONSTANT EQU 100 MOV A, #0 FFH ORL 40 H, #CONSTANT • 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. , 00 H or FFH), otherwise an error message “will not fit in a byte” is generated. A. R. Hurson 17
Introduction to Micro Controllers & Embedded System Design • Immediate data MOV A, #0 FF 00 H MOV A, #00 FFH Are syntactically correct MOV A, #0 FE 00 H MOV A, #01 FFH Are syntactically incorrect A. R. Hurson 18
Introduction to Micro Controllers & Embedded System Design • Immediate data • In case of signed decimal notation, constant can be in the range of -256 to +255 MOV A, #-256 MOV A, #0 FF 0 H Both instructions put 00 H into accumulator A. R. Hurson 19
Introduction to Micro Controllers & Embedded System Design • 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, 45 H MOV A, SBUF A. R. Hurson ; Same as MOV A, 99 H 20
Introduction to Micro Controllers & Embedded System Design • BIT address • There are three ways to specify a bit address in an instruction: a) b) Explicitly by giving the address, c) Using a predefined assembly symbol. Using the “dot operator” between the byte address and the bit position, or SETB JNB A. R. Hurson 0 E 7 H ACC. 7 TI, $ 99 H, $ ; Explicit bit address ; Dot operator, same as above ; TI is a predefined symbol ; Same as above 21
Introduction to Micro Controllers & Embedded System Design • Code address • A code address (mainly a label) is used in the operand field for various jump instructions. HERE: A. R. Hurson SJMP HERE 22
Introduction to Micro Controllers & Embedded System Design • Assemble-Time Expression Evaluation: • Values and constants as an operand can be expressed in three ways: • • • Explicitly (e. g. , 0 EFH), 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 23
Introduction to Micro Controllers & Embedded System Design Examples: MOV DPTR, #04 FFH + 3 MOV DPTR, #0502 H A. R. Hurson ; entire 16 -bit result is used 24
Introduction to Micro Controllers & Embedded System Design • 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. , “ 0 A 5 H” not “A 5 H”). A. R. Hurson 25
Introduction to Micro Controllers & Embedded System Design Examples: MOV MOV MOV A. R. Hurson A, #15 A, #1111 B A, #0 FH A, #17 Q A, #15 DB 26
Introduction to Micro Controllers & Embedded System Design • 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’ MOV DPTR, # ‘AB’ MOV DPTR, # 4142 H A. R. Hurson ; convert ASCII digit to ; binary digit ; as above 27
Introduction to Micro Controllers & Embedded System Design • Assemble-Time Expression Evaluation • Arithmetic operators are: + Addition - Subtraction * Multiplication / Division MOD Modulo A. R. Hurson 28
Introduction to Micro Controllers & Embedded System Design Examples: MOV A, #10 + 10 H MOV A, # 1 AH ; same as above MOV A, #25 MOD 7 MOV A, # 4 ; same as above A. R. Hurson 29
Introduction to Micro Controllers & Embedded System Design • Assemble-Time Expression Evaluation • Logical operators are: OR Logical OR AND Logical AND XOR Logical Exclusive OR NOT Logical NOT (complement) • Note: NOT operator is a unary operator A. R. Hurson 30
Introduction to Micro Controllers & Embedded System Design Examples: MOV A, # ’ 9’ AND 0 FH MOV A, # 9 THREE MINUS_THREE A. R. Hurson ; these two are the same EQU MOV MOV 3 -3 A, # (NOT THREE) + 1 A, # MINUS_THREE A, # 11111101 B ; all these three instructions ; are the same 31
Introduction to Micro Controllers & Embedded System Design • Assemble-Time Expression Evaluation • Special operators are: SHR Shift right SHL Shift left HIGH High-byte LOW Low-byte () Evaluate first A. R. Hurson 32
Introduction to Micro Controllers & Embedded System Design • Examples: MOV A, # 8 SHL 1 MOV A, # 10 H ; these two are the same MOV A, # HIGH 1234 H MOV A, # 12 ; these two are the same A. R. Hurson 33
Introduction to Micro Controllers & Embedded System Design • 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 34
Introduction to Micro Controllers & Embedded System Design • Examples: MOV MOV MOV A, # 5 = 5 A, # NE 4 A, # ‘X’ LT ‘Z’ A, # ‘X’ >= ‘X’ A, # $ > 0 A, # 100 GE 50 • The result for all these operations is the same as: MOV A, # 0 FFH A. R. Hurson 35
Introduction to Micro Controllers & Embedded System Design • Operator Precedence is in the following order from the highest to the lowest: () HIGH, LOW * / MOD SHL SHR + EQ NE LT GT GE = <> < <= > >+ NOT AND OR XOR • Operators of the same order are evaluated from left to right. A. R. Hurson 36
Introduction to Micro Controllers & Embedded System Design • Operator Precedence : • Example: Operation Generated value HIGH (‘A’ SHL 8) HIGH ‘A’ SHL 8 NOT ‘A’ – 1 ‘A’ OR ‘A’ SHL 8 0041 H 0000 H FFBFH 4141 H A. R. Hurson 37
Introduction to Micro Controllers & Embedded System Design • 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 38
Introduction to Micro Controllers & Embedded System Design • 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 100 H ; Set the location counter to 100 H • Each program must be started with ORG directive. A. R. Hurson 39
Introduction to Micro Controllers & Embedded System Design • Directives: Assembler State Control • END should be the last statement in the source file. • Note: END does not have any operand. A. R. Hurson 40
Introduction to Micro Controllers & Embedded System Design • Directives: Assembler State Control • USING directive informs the current active register bank. Its general format is: USING expression A. R. Hurson 41
Introduction to Micro Controllers & Embedded System Design • Directives: Assembler State Control Example: USING 3 PUSH AR 7 USING 1 PUSH AR 7 • The first PUSH instruction assembles to PUSH 1 FH (i. e. , R 7 in bank 3) and the second PUSH instruction translates to PUSH 0 FH (R 7 in bank 1). A. R. Hurson 42
Introduction to Micro Controllers & Embedded System Design • 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 A. R. Hurson SEGMENT CODE 43
Introduction to Micro Controllers & Embedded System Design • Directives: Symbols • EQU assigns a numeric value to a specific symbol name. Its general format is: Symbol EQU expression Example N 27 A. R. Hurson EQU 27 44
Introduction to Micro Controllers & Embedded System Design • 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 45
Introduction to Micro Controllers & Embedded System Design • 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 LENGTH BUFFER A. R. Hurson DSEG AT 30 H EQU 40 DS LENGTH ; put in data segment ; 40 bytes reserved 46
Introduction to Micro Controllers & Embedded System Design • Directives: Storage initialization/reservation • Define Storage (DS) ; The following code clears the 40 bytes ; reserved in the previous example LOOP A. R. Hurson MOV R 7, #LENGTH MOV R 0, #BUFFER MOV @R 0, # 0 DJNZ R 7, LOOP continue 47
Introduction to Micro Controllers & Embedded System Design • 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 48
Introduction to Micro Controllers & Embedded System Design • Define Byte (DB): Example: SQUARE: MESSAGE CSEG AT 0100 H DB 0, 1, 4, 9, 16, 25 DB ‘Login: ’, 0 When the aforementioned code is assembled, we will have the following result in external code memory: A. R. Hurson 49
Introduction to Micro Controllers & Embedded System Design • Define Byte (DB): Example: A. R. Hurson Address Contents 0100 0101 0102 0103 0104 0105 0106 0107 0108 0109 010 A 010 B 010 C 00 01 04 09 10 19 4 C 6 F 67 69 6 E 3 A 00 50
Introduction to Micro Controllers & Embedded System Design • Define Byte (DB): Example: ORG DATA 1: DATA 2: DATA 3: DATA 4: DATA 5: A. R. Hurson 500 H DB 28 DB 00110101 B DB 39 H ORG 510 H DB ‘ 2591’ ORG DB ‘My name is Joe’ ; Decimal, 1 C in Hex ; Binary, 35 in Hex ; ASCII Number ; ASCII Characters 51
Introduction to Micro Controllers & Embedded System Design • 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 52
Introduction to Micro Controllers & Embedded System Design • Define Word (DW): Example: CSEG AT 200 H DB $, ‘A’, 1234 H, 2, ‘BC’ When the aforementioned code is assembled, we will have the following hexadecimal memory assignments: A. R. Hurson 53
Introduction to Micro Controllers & Embedded System Design • Define Word (DW): Example: A. R. Hurson Address Contents 0200 0201 0202 0203 0204 0205 0206 0207 0208 0209 02 00 00 41 12 34 00 02 42 43 54
Introduction to Micro Controllers & Embedded System Design • Example: Assume that bit P 2. 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 P 1. 5 to turn on a buzzer: OVEN_HOT BUZZER BIT P 2. 3 P 1. 5 HERE: JNB ACALL CLR CPL ACALL SJMP OVEN_HOT, HERE DELAY BUZZER DELAY HERE A. R. Hurson 55
Introduction to Micro Controllers & Embedded System Design • Example: Assume that the RAM location 12 H holds the status of whethere 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 12 H is high. If it is low, the LCD should say “No new messages”: PHONEBIT NO: EXIT: YES_MSG: NO_MSG: A. R. Hurson BIT MOV JNC MOV LCALL SJMP MOV LCALL ORG DB ORG 12 H C, PHONEBIT NO DPTR, #400 H DISPLAY EXIT DPTR, #420 H DISPLAY 400 H “New Message”, 0 420 H DB “No new message”, 0 56
Introduction to Micro Controllers & Embedded System Design • 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 57
Introduction to Micro Controllers & Embedded System Design EDITOR Program. asm Assembler Program Note: An lst file lists op. codes, Addresses, and errors detected By the assembler. This file can Be used to detect syntax errors. . lst . obj Linker Program. abs OH Program. hex A. R. Hurson 58
Introduction to Micro Controllers & Embedded System Design • A Running example: • Original program ORG ; Start at location 0 MOV R 5, #25 H ; Load 25 H into R 5 MOV R 7, #34 H ; Load 34 H into R 7 MOV A, #0 ; Initialize accumulator with 0 ADD A, R 5 ; Add content of R 5 to A ADD A, R 7 ; Add content of R 7 to A ADD A, #12 H ; Add 12 H to A HERE: SJMP HERE ; Stay in this Loop END ; End of program A. R. Hurson 59
Introduction to Micro Controllers & Embedded System Design • A Running example: • List file 1 0000 2 0000 3 0002 4 0004 5 0006 6 0007 7 0008 8 000 A 9 000 C ORG 7 D 25 7 F 34 7400 2 DADD 2 F ADD 2412 80 FE END A. R. Hurson ; Start at location 0 MOV R 5, #25 H ; Load 25 H into R 5 MOV R 7, #34 H ; Load 34 H into R 7 MOV A, #0 ; Initialize accumulator with 0 A, R 5 ; Add content of R 5 to A A, R 7 ; Add content of R 7 to A ADD A, #12 H ; Add 12 H to A HERE: SJMP HERE ; Stay in this Loop ; End of program 60
Introduction to Micro Controllers & Embedded System Design • A Running example: • After the program is burned into ROM we have the following image of the program: 0000 0001 0002 0003 0004 0005 0006 0007 0008 0009 000 A 000 B A. R. Hurson 7 D 25 7 F 34 74 00 2 D 2 F 24 12 80 FE 61
Introduction to Micro Controllers & Embedded System Design • Example: Loop and conditional jump • Write a program that adds 3 to accumulator 10 times. AGAIN: • Note: MOV ADD DNJZ MOV A, #0 R 7, #10 A, #3 R 7, AGAIN R 5, A ; Clear accumulator ; Set the loop counter ; Add 3 to accumulator ; Repeat the loop until R 7 = 0 Since size of loop counter (R 7) is 8, the loop cannot be iterated more than 256 times. A. R. Hurson 62
Introduction to Micro Controllers & Embedded System Design • Example: Nested Loops • What if we want to repeat an action more than 256 times? Write a program to complement 55 H 700 times: NEXT: AGAIN: A. R. Hurson MOV A, #55 H MOV R 3, #10 MOV R 2, #70 CPL A DNJZ R 2, AGAIN DNJZ R 2, NEXT (continue) ; initialize accumulator ; Set the outer loop counter ; Set the inner loop counter ; Add 3 to accumulator ; Repeat the loop until R 2 = 0 ; Repeat the loop until R 3 = 0 63
Introduction to Micro Controllers & Embedded System Design • What is the logic behind the following program? N_1: N_2: OVER: A. R. Hurson MOV A, #0 MOV R 5, A ADD A, #79 H JNC N_1 INC R 5 ADD A, #0 F 5 H JNC N_2 INC R 5 ADD A, #0 E 2 H JNC OVER INC R 5 (continue) ; initialize accumulator ; Set R 5 to zero ; Jump if CY = 1 64
Introduction to Micro Controllers & Embedded System Design • Calculating jump forward and backward addresses (short jump): Line 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 A. R. Hurson 18 PC 0000 0002 0004 0006 0007 0008 0009 000 B 000 D 000 E 000 F 0010 0011 0012 0013 0015 0017 operation 7800 7455 6003 08 04 04 2477 5005 E 4 F 8 F 9 FA FB 2 B 50 F 2 80 FE AGAIN: NEXT: OVER: HERE: Mnemonic ORG MOV JZ INC INC ADD JNC CLR MOV MOV ADD JNC SJMP END Operand 0000 R 0, #0 A, #55 H NEXT R 0 A A A, #77 H OVER A R 0, A R 1, A R 2, A R 3, A A, R 3 AGAIN HERE 65
Introduction to Micro Controllers & Embedded System Design • Subroutine call and return BACK: DELAY: AGAIN: A. R. Hurson ORG 0 MOV A, #55 H ; initialize accumulator MOV P 1, A ; Send 55 H to port 1 LCALL DELAY ; Time delay MOV A, #0 AAH MOV P 1, A LCALL DELAY SJMP BACK ; This is the subroutine ORG 300 H MOV R 5, #0 FFH DJNZ R 5, AGAIN RET END 66
Introduction to Micro Controllers & Embedded System Design • Subroutine call and return Line PC 1 0000 2 0000 3 0002 4 0004 5 0007 6 0009 7 000 B 8 000 E 9 0010 10 0300 11 0300 12 0302 13 0304 14 0305 A. R. Hurson operation 7455 F 590 120300 74 AA F 590 120300 80 F 0 7 DFF DDFE 22 Mnemonic Operand ORG 0 BACK: MOV A, #55 H MOV P 1, A LCALL DELAY MOV A, #0 AAH MOV P 1, A LCALL DELAY SJMP BACK ; This is the subroutine ORG 300 H DELAY: MOV R 5, #0 FFH AGAIN: DJNZ R 5, AGAIN RET END ; initialize accumulator ; Send 55 H to port 1 ; Time delay 67
Introduction to Micro Controllers & Embedded System Design • Program branching instructions • bytes long. MOV RL JMP DPTR, #JUMP_TABLE A, #INDEX_NUMBER A @A + DPTR A. R. Hurson 68
Introduction to Micro Controllers & Embedded System Design • Program branching instructions • Jump Tables JUMP_TABLE: A. R. Hurson AJMP CASE 0 CASE 1 CASE 2 CASE 3 69
Introduction to Micro Controllers & Embedded System Design • Program branching instructions Example: Assume the jump table in the previous example starts at memory location 8100 H with the following values: A. R. Hurson Address Content 8100 01 8101 B 8 8102 01 8103 43 8104 41 8105 76 8106 E 1 8107 F 0 70
Introduction to Micro Controllers & Embedded System Design • Program branching instructions a) What is the beginning and ending addresses of the 2 K block of the code memory in which these instructions reside? b) At what addresses do CASE 0 through CASE 3 begin? a) 8000 H to 87 FFH b) CASE 0 begins at address 80 B 8 H CASE 1 begins at address 8043 H CASE 2 begins at address 8276 H CASE 3 begins at address 87 F 0 H A. R. Hurson 71
Introduction to Micro Controllers & Embedded System Design • 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; highbyte first and low-byte second. A. R. Hurson 72
Introduction to Micro Controllers & Embedded System Design • Program branching instructions • Subroutines and Interrupts Example: An LCALL instruction is at address 1000 H-1002 H and the stack pointer contains 20 H, then the LCALL a) Pushes the return address 1003 H into the stack, placing 03 H in 21 H and 10 H in 22 H b) c) Leaves the stack pointer containing 22 H, and Jumps to the subroutine by loading the PC with the address contained in bytes 2 and 3 of the instruction. A. R. Hurson 73
Introduction to Micro Controllers & Embedded System Design • Program branching instructions • Subroutines and Interrupts Example: The following instruction LCALL COSINE Is at address 0240 H through 0206 H and the subroutine COSINE is at address 043 AH. Assume stack pointer contains 3 AH. What internal RAM locations are altered and what are their new values after execution of LCALL instruction? Address 3 BH 3 CH 81 H (stack pointer) A. R. Hurson Contents 02 H 07 H 3 CH 74
Introduction to Micro Controllers & Embedded System Design • 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 75
Introduction to Micro Controllers & Embedded System Design • 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 76
Introduction to Micro Controllers & Embedded System Design • Program branching instructions • Conditional jumps Example: The following code shows a loop that is iterated 10 times. LOOP: A. R. Hurson MOV R 7, #10 (begin loop) (end loop) DNJZ R 7, LOOP 77
Introduction to Micro Controllers & Embedded System Design • 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 03 H. The following code shows this process. SKIP: A. R. Hurson CJNE A, #03 H, SKIP SJMP TERMINATE (continue) 78
- Slides: 78