Become a subscriber (Free)

Join 29,000 other subscribers to receive subscriber sale discounts and other free resources.
Don't worry -- your e-mail address is totally secure. I promise to use it only to send you MicroZine.

Assembler Delay Loop: How To Integrate Assembler And C Code

There are three ways of coding DELAYS in a C Program, one of which is an assembler delay loop:

  1. Loop counting delay.
  2. Loop counting delay in assembler.
  3. interrupt Driven.

In this page I'm going to look at 1 and 2 and save interrupt driven code for another day.  The first two methods provide an easy (and quick) way of generating a delay time whereas when you use interrupts you need to consider the system as a whole i.e. which timer to use how to integrate with other interrupts etc.

Simple Delay Loop

These are the easiest delays to create and at first sight they seem like the ideal solution since all you do is write a loop as follows:

for (i=0;i<1000;i++) ;

...with the semicolon acting as the body of the delay i.e. do nothing.  And that is one of the problems - An optimising compiler will see a 'do nothing' loop and may optimize it away completely and you won't be told.

After all the function of a compiler is to save space to make your program fit into the maximum memory.  It will see a useless function and say 'right lets remove this'.

To get around this there are usually two methods:

1. Declare the variable as volatile - here you are telling the compiler that the variable can be changed at any time by another process e.g. an interrupt so it should not assume that that variable can be altered i.e. it should not optimize it away.  

2. Use pragmas #pragmas control the compiler operation and you can use them to infulence the actions of the compiler.  There is an example below for the SDCC compiler.
There are usually pragmas to control optimzatiion level (different for each compiler).  

Note: some compilers don't provide #pragmas so you have less control from within the C source code itself often using their IDE and project file to setup and store the same information.

Both these methods require experimentation and for you to look at the
assembler output code to find out if the compiler did what you thought it should have done.

The problem with these methods is that you are having to get involved understanding the operation of the compiler and it means that you do not have portable code.  Also an update to the compiler may change the output optimization.

In addition to this compiler changes may change how the loop is coded (e.g. if going beyond a memory bank limit (16F) - may make the compiler add more complex jump code) and so change the loop time that you use - all in all it's a poor coding solution for creating delay.

Assembler Delay Loop Counting

In this method you import assembler code and stitch it into a C function. OK it is non-portable but it is guaranteed to operate with the delay time you specify and it won't change if the compiler changes.

The key is getting hold of the assembler code and a good tool to do it is picloops.  

This is the sort of assembler code you get:

;PIC Time Delay = 0.0010010s with Osc = 4.000000 MHz
       movlw   D'2'
       movwf   CounterB
       movlw   D'74'
       movwf   CounterA
loop    decfsz  CounterA,1
       goto    loop
       decfsz  CounterB,1
       goto    loop

So this is the PIC loop code that will generate a 0.001001s delay i.e. approx 1ms.  There's lots of variables to play with in Picloops to get a better result but note that the higher the main clock (finer resolution) the closer you will get to the exact time.  Also interrupts can generate an exact time (subject to the tolerance on the Xtal of course) - also subject to how much the interrupt has to do i.e. keep the interrupt code small!

This code is normally used only with assembler so now comes the tricky part - you want to convert it to a C function so for SDCC there are two commands




Place your assembler between these C commands (each compiler will use different commands for this action but the basic principle is the same).

Ok that's easy now comes the difficult bit getting access to variables.

Variable Access for Assembler Delay Loops

When you think about compilers, in the end, all of them must convert all code into assembler - it's the only way that the microcontroller works so all compilers must obey assembler coding rules.  

This means at the lowest level ALL variables are global (as much as a C purist hates that !) this means that every variable must be uniquely identified in the assembler.  In C the details of variable names are hidden from view at the C level but they do exist and most compiler documentation (including SDCC) will tell you how they work so you can do what we are doing here i.e. obtain the assembler coding name for a variable.  

If you don't have documentation the look at the assembler output to see how they do it.

Here's the final code for the C function delay_ms that uses the above assembler:

static void _delay_ms(void) {
volatile unsigned char CounterA,CounterB;
CounterA=74; // Avoid unreferenced warning
CounterB=2;  // Avoid unreferenced warning

; from picloops delay loop calculator V2.1
;PIC Time Delay = 0.0010010s with Osc = 4.000000 MHz
;       movlw   D'2'
;       movwf   __delay_ms_CounterB_1_1
;       movlw   D'74'
;       movwf   __delay_ms_CounterA_1_1
       decfsz  __delay_ms_CounterA_1_1,1
       goto    loop_delay_ms
       decfsz  __delay_ms_CounterB_1_1,1
       goto    loop_delay_ms


There's a few things to note

  • retlw the return code - is removed - we let the C function do that bit.
  • Assembler delay loop variables are not initialized in the assembler.  This is because initializing using the C function code makes the compiler think that the variables are used (and so does not flag an error - that would occur if the variables were only initialized in the assembler - the compiler does not process the assembler at all).
  • The "real" (assembler level global name) or useable variable name is composed of the function name (__delay_ms) -note the double underscore - probably to stop name clashes, the variable (CounterA) name plus compiler specific label (1_1), resulting in the final global '__delay_ms_CounterA_1_1'.

Variable names at assembler level:

The function _delay_ms is static and the variable coding name will change if declared non static - it is legal to do this just be aware that the function (assembler code) will fail unless you match the variable name at the assembler level.

The final variable name is __delay_ms_CounterA_1_1

Breakdown of the SDCC variable
1 An additional underscore prefix _
2 then the function name itself _delay_ms
3 it then appends the variable name _CounterA
4 and then some identifiers _1_1.
Final variable : __delay_ms_CounterA_1_1

That's it really  - you can now code up delays that won't change if you C code does!

Note: Remember you need to change the delay code depending on the clock frequency you use.

Assembler delay loop Disadvantages :

  1. The code is generated assuming a specific clock rate.  If you use a different clock speed you'll get a different delay time!
  2. The code is non-portable.

Assembler delay loop Advantages :

  1. Simple code.
  2. Callable as a C function.
  3. Easy to do and easy to change (using picloops software)
  4. Occupies smaller code space than equivalent in compiler (no checks on clock speed etc (this may be true depending on the exact implementation in the compiler).

Note: There are much more complex coding methods involving #defines to specify the clock speed. The delay routine is then created using this information which means the delay routine will work for any clock speed. The disadvantage is that it takes quite a lot of effort to generate this code in the first place - you can see examples in commercial compilers such as mikroC etc.

New! Comments

Have your say about what you just read! Leave me a comment in the box below.

Privacy Policy | Contact | About Me

Site Map | Terms of Use

Visit our Facebook Page:

   Click Here

Recent Articles

  1. How to use the BMP280 Barometric Pressure chip with the Arduino

    How to use the BMP280 for weather pressure measurement or altitude change detection.

    Read more

  2. How to use the SSD1306 OLED LCD.

    Easily use the SSD1306 in any of your projects.

    Read more

  3. Logic Level Converters made easy

    How to easily make a logic level converter e.g. between 3V and 5V.

    Read more

Sign up for MicroZine
''The'' Microcontroller Newsletter

Enter your first Name and primary email address in the form below:

And receive absolutely FREE a full project for:

"Measuring Analogue Voltages
Without An ADC"

(Using only one pin).

Instant Download:
You Can
Get It Right Now

Warning: This project could be  Removed 
at any time.  

It will  NOT be 
available indefinitely SO
To avoid 
disappointment  get it:


Don't worry -- your e-mail address is totally secure. I promise to use it only to send you MicroZine
Remember this is a project with full description and fully debugged C Source code - and it's not available from the main website.

You can only get it through this newsletter.

To get exclusive access Enter your first name Name and primary email address Now in the form above.:

But wait !

There's more...

You'll receive more
free and exclusive reports as well as site information and site product updates

Scroll up to the form above and sign up NOW. Don't forget it's FREE and if you don't like it, you can unsubscribe at any time.

Click Here Now to use the form above to get your Valuable information absolutely free.

Readers Comments

"I wanted to thank
you so so so much
for all the information
you have provided in
your site it's


- Ranish Pottath

"This site really is
the best and my favorite.
I find here many useful
projects and tips."

- Milan


"Awesome site,
very, very easy and nice
to navigate!"

- Matt

Learn Microcontrollers

"Interested in

Sign up for The
Free 7 day guide:


"I am a newbie to PIC
and I wanted to say
 how great your
site has been for me."

- Dave


"Your site is a great
and perfect work.

- Suresh


"I couldn't find the correct
words to define
yourweb site.

Very useful, uncovered,
honest and clear.

Thanks so much for
your time and works.

- Anon

Back to Top