Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CoE3DJ4 Digital Systems Design Chapter 7: Assembly Language Programming Assembly language • Assembly language is a computer language lying between extremes of machine language and high level languages (e.g., C or Pascal) • An assembly language program is a program written using labels, mnemonics and so on in which each statement corresponds to a machine instruction • Assembly language programs, often called source code or symbolic code, cannot be executed by a computer • A machine language program (object code), binary bytes representing instructions, is executable by a computer • An assembler translates an assembly language program into a machine language program Assembly language • Many 8051 assemblers are available • Intel’s original MCS-51 family assembler (ASM51) is no longer available but has set the standard for other assemblers • We focus on the assembly language programming using the common features of ASM51 which are supported by most assemblers. Assembly language programs • Assembly language programs contain the following: – Machine instructions • Mnemonics of executable instructions – Assembler directives • Instructions to assembler defining program structure, symbols, data constants and … – Assembler controls • Set assembler modes and direct assembly flow – Comments • Enhance the readability of programs • General format of each line: [label] mnemonic [operand][,operand][…][;comment] Assembly language programs • Label field: represents the address of the instruction that follows – Used in branching to this instruction • Mnemonic field: Instruction mnemonics or assembler directives – Example: ORG, ADD, MOV • Operand field: Contains the address or data used by the instruction – A label may be used to represent the address or a symbol may be used to represent a data constant • Comment field: Remarks to clarify the program Assembly language programs • Special Assembler Symbols: are used for register-specific addressing modes – Example: A, R0, DPTR, PC, C and AR0 to AR7 • AR0 to AR7 represent addresses of R0 through R7 in the current register bank. • $ can be sued to refer to the address of current instruction Example: JNB TI, $ is the same as HERE: JNB TI, HERE Generic Jumps and Calls • Assembler allows programmers to use a generic JMP instead of SJMP, AJMP, or LJMP and CALL can be used instead of ACALL or LCALL. • Assembler converts JMP to SJMP if no forward references are used and the jump destination is within -128 locations • JMP and CALL are converted to AJMP and ACALL if no forward references are used and the instruction following the JMP or CALL instruction is in the same 2K block as the destination instruction • If short or absolute forms cannot be used, the conversion is to the long form Assemble-time expression evaluation • Values and constants in the operand field may be expressed with an expression (e.g., 2+3) • Use of expressions makes assembly language programs more readable and more flexible • When an expression is used, assembler calculates a value and inserts it into instruction • Expression calculations are performed using 16-bit arithmetic, however, either 8 or 16 bits are inserted into the instruction as needed Assemble-time expression evaluation • Number bases: base for numerical constants is indicated by B for binary, O or Q for octal, D or nothing for decimal and H for hexadecimal. MOV A, #15 MOV A,#1111B MOV A, #17Q • Character String: strings of one or two characters may be used as operands. ASCII codes are converted to binary by assembler. Character constants are enclosed in single quotes (‘) Example: MOV DPRT, #’AB’ MOV DPRT, #4142H Assemble-time expression evaluation • Arithmetic operators: addition, subtraction, multiplication, division and modulo – Example: MOV A, #25 MOD 7 is the same as MOV A, #4 • Logical operators: OR, AND, XOR and NOT – Example: MOV A, #’9’ AND 0FH is the same as MOV A, #9 • Special operators: shift right (SHR), shift left (SHL), highbyte (HIGH) , low byte (LOW) – Example: MOV A, #HIGH 1234H and MOV A, #12H are the same Assemble-time expression evaluation • Relational operators: When a relational operator is used between two operands, result is always false (0000H) or true (FFFFH). The operators are: EQ NE LT LE GT GE = <> < <= > >= – Example: MOV A, #5 NE 4 equals not equals less than less than or equal greater than greater than or equal is the same as MOV A, FFH Assemble-time expression evaluation • Operator precedence: from highest to lowest is () HIGH, LOW *, /, MOD, SHL, SHR +, EQ, NE< LT, LE, GT, GE, NOT AND OR, XOR Example: ‘A’ OR ‘A’ SHL 8 will result in 4141H Assembler directives • Assembler directives are instructions to assembler • They are placed in mnemonic field of program • They have no effect on the content of memory (except DB and DW) • Assembler directives: – – – – – Assembler state control Symbol definition Storage initialization/reservation Segment selection Program linkage Assembler state control • ORG: set origin • Format: ORG expression • Example: ORG 100H • END: last statement in source file • No label is permitted Assembler state control • Some 8051 instructions (i.e., PUSH and POP) allow only register addresses to be used. • AR0 to AR7 are used to represent addresses of R0 through R7 in the current register bank • USING: informs the assembler of the currently active register bank • By combining USING with symbols AR0 to AR7 we can specify the address of any register in register banks. • Format is: USING expression • Example: USING PUSH USING PUSH 3 AR7 1 AR7 – First push will be assembled to PUSH 1FH whereas the second one will be assembled to PUSH 0FH • Note: USING does not actually switch register banks, it only informs the assembler of the active bank Symbol definition • EQU: equate • EQU assigns a numeric value to a specified symbol name • Example: N27 CR EQU EQU 27 0DH Storage initialization/reservation • A segment is a block of code or data memory created by assembler • There are two types of segments: generic and absolute • Location counter: assembler maintains a location counter for each segment • Location counter is a pointer to the address space of active segment and represents an offset for generic segment or the actual address for absolute segment • Memory initialization and reservation directives (i.e., DS, DB, DW or DBIT) change the value of location counter as they allocated memory • ORG sets a new value for location counter Storage initialization/reservation • • • • Generic segments are created using the SEGMENT directive Format: symbol SEGMENT segment_type “symbol” is name of the segment “segment_type” could be: – – – – CODE: code segment XDATA: external data space DATA: internal data space accessible by direct addressing (00H-7FH) BIT: bit space, overlapping byte locations 20H-2FH • Example: EPROM SEGMET CODE • In order to select a segment we use RSEG directive – Example: RSEG EPROM • When RSEG is used to select a segment, that segment becomes active and will be used by assembler until segment is changed with RSEG or with an absolute segment directive Storage initialization/reservation • Absolute segments: reside in a fixed memory location • They are created using BSEG, CSEG, DSEG and XSEG directives • Format: – – – – BSEG CSEG DSEG XSEG [AT [AT [AT [AT • Example: BSEG address] address] address] address] AT 30H Storage initialization/reservation • If an address is provided, assembler terminates the last absolute segment of the specified type (if any) and creates a new segment starting at that address • If an address is not specified, the last address of the specified type is continued • If no absolute segment of this type was previously selected and the address is omitted, a new segment is created starting at location 0 • By default, assembler selects CODE segment as the active segment and initializes counter to 000H. Storage initialization/reservation • Storage initialization and reservation directives initialize and reserve space in word, byte or bit units. • Space is reserved starts at the location indicated by the current value of location counter in the currently active segment • DS (define storage) • Format: [label:] DS expression • DS reserves space in byte units • It can be used in any segment type except BIT • Label represents the address of the first location of memory Storage initialization/reservation • Example: Create a 40 byte data buffer starting at address 30H and fill it with zero. LENGTH: BUFFER: LOOP: DSEG AT 30H EQU 40 DS LENGTH MOV MOV MOV INC DJNZ R7,#LENGTH R0,#BUFFER @R0,#0 R0 R7,LOOP Storage initialization/reservation • • • • • DBIT (define bit) Format: [label:] DBIT expression DBIT reserves space in bit units It can be used only in BIT segment Label (if used) represents the address of the first location of memory • Example: KBFLAG: BSEG DBIT 1 Storage initialization/reservation • DB (define byte) • Format: [label:] DB expression • DB initializes code memory with byte values (places data constants in code memory) • A CODE segment must be active for DB • DB permits character strings • Label (if used) represents the address of the first location of memory Storage initialization/reservation • Example: SQUARES: MESSAGE: CSEG DB DB AT 0100H 0,1,4,9,16,25 ‘Login’ • Resulting memory: address 0100 0101 0102 0103 0104 0105 0106 0107 0108 0109 010A value 00 01 04 09 10 19 4C 6F 67 69 6E Storage initialization/reservation • DW (define word) • Format: [label:] DW expression • DW is the same as DB except two memory locations are assigned to each data • Example: CSEG DW address 0200 0201 0202 0203 0204 0205 0206 0207 AT 200H $,‘A’,1234H,2 value 02 00 00 41 12 34 00 02