51 Single Chip Microcomputer Programming Skills Combining Assembly and C Language

This article is mainly about the introduction of assembly and C language, and focuses on the difference between assembly and C language and its programming skills.

Assembly and C language

The difference between C language and assembly language is that their advantages and disadvantages are almost opposite.

The advantages and disadvantages of C language and assembly language: take c as an example

advantage:

c has high operating efficiency;

c can achieve direct access to the hardware;

Good portability of c code;

Disadvantages:

The code reusability of c is poor;

C has poor code maintainability;

C has poor scalability, that is, if you want to add new features, you need to modify more code;

C language is poor in readability;

C language is a general-purpose computer programming language with a wide range of applications. The design goal of C language is to provide a programming language that can compile and process low-level memory in a simple way, generate a small amount of machine code, and run without any operating environment support; although C language provides many low-level processing functions, But it still maintains a good cross-platform feature. C language programs written in a standard specification can be compiled on many computer platforms, even including some embedded processors (microcontrollers or MCUs) and operating platforms such as supercomputers.

Assembly language: is a low-level language used in electronic computers, microprocessors, microcontrollers or other programmable devices, also known as symbolic languages. In assembly language, mnemonics are used to replace the opcodes of machine instructions, and address symbols (Symbol) or labels (Label) are used to replace the addresses of instructions or operands. In different devices, assembly language corresponds to different machine language instruction sets, which are converted into machine instructions through the assembly process. Generally speaking, there is a one-to-one correspondence between a specific assembly language and a specific machine language instruction set, and it is not directly portable between different platforms.

51 Single Chip Microcomputer Programming Skills Combining Assembly and C Language

The main relationship between C language and assembly language: mutual calling, mixed programming, etc., for example:

1. C language calls assembly subroutines

At this point, the assembly subroutine needs to be declared as an external program

#include《reg51.h》

extern void delay(void)

sbit pluse=P1^0;

main()

{

while(1)

{

pluse=0;

delay();

pluse=1;

delay();

}

}

Assembly subroutine:

The assembly part needs to be handled in this way.

PUBLIC DELAY

DELAY SEGMENT CODE

RSEG DELAY

DELAY:

MOV R2, #250

DJNZ R2, $

RET

END

2: The assembly is embedded in the C language. This is relatively simple and can be achieved by including the macro instruction.

void delay(void)

{

#pragma asm

mov r2, #250

djnz r2, $

#pragma endasm

}

Talking about how to compile modularized programs with assembly

1. Writing of the main program

51 Single Chip Microcomputer Programming Skills Combined with Assembly and C Language

The above picture is a screenshot of structured programming. The .asm is the extension of the assembly file, and the C language uses .c. In assembly language, the extension of the header file is .inc, while in the C language, the extension of the header file is .h.

Observe the above picture carefully, the format of assembly include header file is $include (full name of header file), while in C language it is #include "full name of header file", the two have very similarities. ORG 0000H; What is the function of LJMP MAIN without my explanation? The ORG instruction is to specify that the first byte of the next instruction or data is stored in the corresponding storage unit according to the address or label behind the ORG. _MAIN_ASM SEGMENT CODE; The function of RSEG _MAIN_ASM is already marked in the figure above, so I don’t need to talk about it. What needs to be explained is that _MAIN_ASM does not have to be written like this, you can write any symbol you like (such as: MAINFUNC, _MAIN_FUNC, ZHUHANSHU, the last example It's a bit vulgar), as long as it conforms to the naming rules of assembly language variables or labels. If you need to be standardized, it is best to write in the above format. Another point is that this section of code for dynamically allocating program addresses can be omitted. Without this section of code, the compiler would not dynamically allocate program addresses. Everyone can definitely analyze the function of the main function, otherwise it would be meaningless to read this tutorial.

Second, the writing of subroutines

51 Single Chip Microcomputer Programming Skills Combining Assembly and C Language

The following is how to write a subprogram. The subprogram is to be called. You must tell the compiler which programs in the subprogram module can be called by other modules. As shown above, DELAY1MS is the delay function that can be called. Variable and function declarations use the keyword PUBLIC, followed by the function name (label name). Dynamic allocation of program addresses can be omitted, as already mentioned in main.asm. Note that the last line of the subroutine module should write an END. Without this sentence, the compiler will not report an error, but a warning will appear, which is prone to problems. In order to standardize programming, you should not be lazy.

51 Single Chip Microcomputer Programming Skills Combining Assembly and C Language

The last point is the role of the header file. This is the same as the role of the C language. Variables or functions can be declared. This program is relatively simple and can be directly declared in the main program, as shown in the following figure:

51 Single Chip Microcomputer Programming Skills Combining Assembly and C Language

Conclusion

This is the end of the related introduction about assembly and C language. Please correct me if there are any deficiencies.

Related reading recommendations: What are the application areas of the c language

Related reading recommendations: from assembly, C language to ten years of FPGA development and design, tell you the experience of growth

600 Puffs Vape

Disposable 600 puffs vape pen are so convenient, portable, and small volume, you just need to take them
out of your pocket and take a puff, feel the cloud of smoke, and the fragrance of fruit surrounding you. It's so great.
We are China's leading manufacturer and supplier of disposable vape pen, 600 puff disposable vape,600 puff vape pen,vape pen 600 puffs,
600 puffs vape kit,600 puff e-cigarette, and e-cigarette kit, and we specialize in Disposable Vapes, e-cigarette vape pens, e-cigarette kits, etc.


600 puff disposable vape,600 puff vape pen,vape pen 600 puffs,600 puffs vape kit,600 puff e-cigarette

Ningbo Autrends International Trade Co.,Ltd. , https://www.ecigarettevapepods.com