Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Introduction to Micro Controllers & Embedded System Design Assembly Language Programming Department of Electrical & Computer Engineering Missouri University."— Presentation transcript:

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

2 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

3 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

4 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

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

6 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

7 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

8 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

9 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

10 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

11 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

12 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

13 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 ; “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

14 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

15 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

16 Indirect Address The “at” sign indicates indirect addressing and may only be used with R0, R1 of active bank, DPTR, or the PC. ADD ; Operand is in internal RAM at ; the location indicated by the ; content of R0 MOV ; 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

17 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

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

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

20 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

21 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 H, $ ; Same as above A.R. Hurson

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

23 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

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

25 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

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

27 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

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

29 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

30 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

31 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, # B ; all these three instructions ; are the same A.R. Hurson

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

33 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

34 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

35 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

36 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

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

38 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

39 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

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

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

42 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

43 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

44 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

45 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

46 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

47 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

48 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

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

50 Define Byte (DB): Example: Address Contents 0100 00 0101 01 0102 04
C F 010A 6E 010B 3A 010C 00 A.R. Hurson

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

52 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

53 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

54 Define Word (DW): Example: Address Contents 0200 02 0201 00 0202 00
A.R. Hurson

55 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

56 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 H MOV C, PHONEBIT JNC NO MOV DPTR, #400H LCALL DISPLAY SJMP EXIT NO: MOV DPTR, #420H LCALL DISPLAY EXIT:  ORG H YES_MSG: DB “New Message”, 0 ORG H NO_MSG: DB “No new message”, 0 A.R. Hurson

57 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

58 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

59 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

60 A Running example: List file 1 0000 ORG ; Start at location 0
D25 MOV R5, #25H ; Load 25H into R5 F34 MOV R7, #34H ; Load 34H into R7 MOV A, #0 ; Initialize accumulator with 0 D ADD A, R5 ; Add content of R5 to A F ADD A, R7 ; Add content of R7 to A 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

61 A Running example: After the program is burned into ROM we have the following image of the program: 0000 7D 0002 7F 0006 2D 0007 2F 000A 80 000B FE A.R. Hurson

62 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

63 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

64 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

65 Calculating jump forward and backward addresses (short jump):
Line PC operation Mnemonic Operand ORG MOV R0, #0 MOV A, #55H JZ NEXT INC R0 AGAIN: INC A INC A NEXT: ADD A, #77H 000B JNC OVER 000D E CLR A 000E F MOV R0, A 000F F MOV R1, A 0010 FA MOV R2, A 0011 FB MOV R3, A 0012 2B OVER: ADD A, R3 F JNC AGAIN FE HERE: SJMP HERE END A.R. Hurson

66 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 H DELAY: MOV R5, #0FFH AGAIN: DJNZ R5, AGAIN RET END A.R. Hurson

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

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

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

70 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

71 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

72 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

73 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

74 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

75 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

76 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

77 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

78 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


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

Similar presentations


Ads by Google