Chapter 4 Assembly Language Programming

Publisher:Zhenai5201314Latest update time:2024-08-09 Source: cnblogs Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Assembly language statement format

1. Instruction format

[Label:] Mnemonic operand 1, operand 2 [; Comment]

Label: It consists of 1 to 8 characters, and the first character must be a letter. It is used to indicate the address of the instruction.

Operands: Binary (ending with B), decimal (ending with D or omitted), hexadecimal (ending with H) numbers and strings (add " ")

Comments: These are non-executable parts that describe the program.

2. Pseudo-instruction format

Pseudo-instructions are non-executable instructions whose function is to provide information to the assembler. Common pseudo-instructions are:


Assembly start address command: ORG

Format: ORG address

Function: Specifies the starting address of the program. If omitted, the starting address starts from 0000H.


Assembly termination command: END

Format: END expression

Function: source program assembly terminates. In the main program module, "expression" is included to give the starting address of the program, and in other modules (subroutines), the expression can be omitted.


Assignment command: EQU

Format: character name EQU expression

Function: Assign the value of an expression to a character name. The expression can be a constant, address, label, or expression. The character name after assignment can be used in the program.


For example:


A1 EQU 20H

B1 EQU A1

C1 EQU 40H+10H


MOV A, #A1

BAC: ADD A,C1

Define data commands: DB, DW

Format: [label:] DB byte data item table

Function: Define byte data in the program memory starting from the specified address (i.e. label).


Format: [label:] DW word data item

Function: Starting from the specified address (i.e. label), define 16-bit word data in the program memory. The high 8-bit data is in the low address unit, and the low 8-bit data is in the high address unit.


For example:


ORG 1000H

TAB1: DB 1BH, 'A', -2, 128

TAB2: DW 302AH, 34H

The storage format is as shown below:


Low High

TAB1 1BH

41H

FEH

80H

TAB2 30H

2AH

00H

34H

Define storage area command: DS

Format: [label:] DS expression

Function: Starting from the specified address (i.e. label), reserve a specified number of byte units (the value of the expression) as a storage area for program execution (for program memory).


For example:


ORG 2000H

TAB: DS 05H

Bit definition command: BIT

Format: character name BIT bit address

Function: Assign a bit address to a character name. The bit address can be an absolute address or a symbolic address.


For example:


HULED BIT P1.0

Branching Programming

A branch program executes different directions depending on whether a given condition is met. The basic structure is divided into single-branch, double-branch and multi-branch programs.


Single branch structure

If the given conditions are met, program segment A is executed, and then the instructions below this instruction are executed; if the conditions are not met, program segment A is not executed, but the instructions below this instruction are executed.


Double branch structure

If the given conditions are met, program segment A is executed, otherwise program segment B is executed.


Multi-branch structure

First, arrange the branches by number, and then implement multi-branch selection according to the value of the sequence number.


Loop Programming

1. Basic structure of loop program

In the basic design, a part of the instructions is controlled to be executed repeatedly several times, and a large number of processing tasks are completed with a short program. This program that is repeatedly executed according to a certain control rule is called a loop program.


Execute first and then judge the structure

The characteristic is that once entering the loop, the loop processing part is executed first, and then the loop control condition is used to determine whether to end the loop. If not, the loop operation continues to be executed; if it is ended, the loop is exited.


Judge before execution structure

The characteristic is that once entering the loop, it first determines whether the loop control condition ends the loop. If it ends, it exits the loop, otherwise it continues to execute the loop operation.


The cycle program consists of the following four basic components:


Set the initial value of the loop. Set the initial value at the beginning of the loop to prepare for the loop. For example, set the initial value of the counter and the working unit.

Loop processing: The content that is repeatedly executed in a loop program.

Cycle modification. Modify the cycle parameters to prepare for the next cycle.

Loop control. Determine whether to end the loop.

2. Example of loop programming

The "counting method" is usually used to control the loop, and the "execute first, then judge" loop structure is selected.


Example: Sum of multiple single-byte numbers

Assume that 10 bytes of unsigned data are stored in the internal RAM starting from 40H, and the results are stored in R3 and R2. In this example, the number of loops is known, and the counting method is used to control the loop.


The source program is as follows:


ORG 8000H

MOV R0, #40H; set data pointer

MOV R7, #10; Set the initial value of the counter

MOV R3, #0; clear the result unit

MOV R2, #0 ;

LOOP1: MOV A, R2; get addend

ADD A, @R0; sum

MOV R2, A; store the lower 8 bits of the result

JNC LOOP2; No carry, transfer to LOOP2

INC R3; if there is a carry, add 1 to the high 8 bits of the result

LOOP2: INC R0 ; modify pointer

DJNZ R7, LOOP1; Unfinished, continue

END

Design a 20ms delay program

The delay program has a great relationship with the execution time of MCS-51 instructions. When using a 12MHz crystal oscillator, one machine cycle is 1μS, and the execution time of a "DJNZ" instruction is 2μS.

20ms = 2μS×10000,

Since the maximum count value of 8 bits is 256, a double loop method can be used.

20ms = 2μS×100×100,


The delay of 20ms is as follows:


D20MS:MOV R4, #100; 20ms=2μS×100×100

; Initial value of outer loop = 100

DY1: MOV R3, #100; initial value of inner loop = 100

DY2: DJNZ R3, DY2; 100×2=200=0.2ms

DJNZ R4, DY1; 0.2×100=20ms

RET

Subroutine design

The main characteristics of a subroutine are that it needs to be called by other programs during execution, and after execution is completed, the execution flow needs to be returned to the program that called the subroutine.


The following two issues must be addressed during the subroutine call:


Calling and returning between programs;

Parameter transfer between the calling program and the called program.

Programming Example

Table Lookup Program

In many cases, table lookup programs can simplify calculations, simplify the multi-branch structure of programs, and improve program efficiency. The data table used in the table lookup is a constant arranged in a certain order and stored in the program memory.


The MCS-51 instruction system has two instructions for table lookup:


MOVC A, @A+DPTR

MOVC A, @A+PC

Boolean Handlers

One of the biggest features of the MCS-5l microprocessor is that it has strong Boolean processing capabilities, that is, the ability to process Boolean variables (bit variables), so it is best at switch quantity control.


Most hardware designs use combinatorial logic to implement complex functions. Although the hardware used is varied, the purpose is only one, that is, to solve the problem represented by the logic function of several Boolean variables.


For example, the most common car head and tail lights, elevator operation, etc. are mainly controlled by switch quantities.


Applications of KEIL C51

C language is a compiled programming language that combines the characteristics of multiple high-level languages ​​and has the functions of assembly language.

Using C language for programming has become a mainstream in software development.

Developing systems using C language can greatly shorten the development cycle, significantly enhance the readability of the program, and facilitate improvement and expansion.

The C language for 8051 has become increasingly mature and has become a professional, practical, high-level language.

1. Features of C-51

C language is widely supported as a very convenient language. The most commonly used language in China is Keil C51.


The C language program itself does not depend on the machine hardware system, and the program can be ported from different microcontrollers basically without modification.


C provides many mathematical functions and supports floating-point operations, with high development efficiency, thus shortening development time and increasing program readability and maintainability.


Compared with ASM-51, C-51 has the following advantages:


It is not required to understand the instruction system of the microcontroller, but only to have a basic understanding of the memory structure of 8051;

Details such as register allocation, addressing of different memories, and data types can be managed by the compiler;

The program has a regular structure and can be divided into different functions. This makes the program structured.

The ability to combine variable selections with special operations improves program readability;

The provided library contains many standard subroutines and has strong data processing capabilities;

Because of the convenient modular programming technology, the compiled programs can be easily transplanted;


C51 Storage Type

Storage Type Addressing space Data length Range
data On-chip direct addressable RAM 8 0~127
idata On-chip indirect addressable RAM 8 0~255
pdata Page addressing of off-chip RAM 8 0~255
xdata Off-chip data storage (64K) 16 0~65535
code On-chip unified addressing ROM (64K) 16 0~65535
bdata On-chip bit-addressable RAM (16-bit) 1 0~127

The general format of C51 data declaration is as follows:

[1] [2]
Reference address:Chapter 4 Assembly Language Programming

Previous article:Chapter 2 Structure and Principle of MCS-51 Microcontroller
Next article:Chapter 5 Timer/Counter

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号