How to improve the accuracy of C language delay compared to assembly language

Source: Internet
Author: User

As long as the rational use, C can still achieve unexpected results. Many friends complain that C efficiency is much worse than the Assembly, in fact, if the compiler principle of Keil C has a more in-depth understanding, it is possible to use the appropriate syntax, so that the generated C code to achieve optimization. Even though this may seem unlikely, there are some simple principles to follow: 1. Use the unsigned data structure as much as possible. 2. Try to use the char type, it is not enough to use int, then the long. 3. If possible, do not use floating-point type. 4. Use concise code, because by experience, concise C code can often generate concise target code (although not in all cases to be established).

  

void Delay1 (unsigned char i)

{

while (-a);

}

Absent-minded compilation, look at the source:

; FUNCTION _delay1 (BEGIN)

; SOURCE Line # 13

;----Variable "I" Assigned to Register "R7"----

; SOURCE Line # 14

0000? C0004:

; SOURCE Line # 15

0000 Dffe DJNZ R7,? C0004

; SOURCE Line # 16

0002? C0006:

0002 RET

; FUNCTION _delay1 (END)

Day ~ ~ ~ The Miracle appeared ... I think this program should already be able to meet the needs of the general situation. If a table is listed:

I delay TIME/US

1 5

2 7

3 9

.....

When the delay time is calculated, the time of the 2 clock cycles spent in the Lcall statement of the calling function has been counted.

Learn from http://www.elecfans.com/emb/danpianji/20140825352241.html

How to improve the accuracy of C language delay compared to assembly language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.