Download presentation
1
CoE3DJ4 Digital Systems Design
Chapter 7: Assembly Language Programming
2
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
3
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.
4
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]
5
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
6
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
7
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
8
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
9
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
10
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), high-byte (HIGH) , low byte (LOW) Example: MOV A, #HIGH 1234H and MOV A, #12H are the same
11
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 = equals NE <> not equals LT < less than LE <= less than or equal GT > greater than GE >= greater than or equal Example: MOV A, #5 NE 4 is the same as MOV A, FFH
12
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
13
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
14
Assembler state control
ORG: set origin Format: ORG expression Example: ORG 100H END: last statement in source file No label is permitted
15
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 3 PUSH AR7 USING 1 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
16
Symbol definition EQU: equate
EQU assigns a numeric value to a specified symbol name Example: N27 EQU 27 CR EQU 0DH
17
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
18
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
19
Storage initialization/reservation
Absolute segments: reside in a fixed memory location They are created using BSEG, CSEG, DSEG and XSEG directives Format: BSEG [AT address] CSEG [AT address] DSEG [AT address] XSEG [AT address] Example: BSEG AT 30H
20
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.
21
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
22
Storage initialization/reservation
Example: Create a 40 byte data buffer starting at address 30H and fill it with zero. DSEG AT 30H LENGTH: EQU 40 BUFFER: DS LENGTH MOV R7,#LENGTH MOV R0,#BUFFER LOOP: INC R0 DJNZ R7,LOOP
23
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: BSEG KBFLAG: DBIT 1
24
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
25
Storage initialization/reservation
Example: CSEG AT 0100H SQUARES: DB 0,1,4,9,16,25 MESSAGE: DB ‘Login’ Resulting memory: address value C F 010A 6E
26
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 AT 200H DW $,‘A’,1234H,2 address value
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.