Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

NOP (No Operation) Problem

Status
Not open for further replies.

southafrikanse

Junior Member level 1
Joined
Jun 12, 2007
Messages
15
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Amora, Portugal
Activity points
1,418
8051 one clock per instruction cycle nop mov

Greetings

I'm using my microcontroller to work as a RTC.

I'm trying out Google to solve my problem however I can't find my answer.

I have to use C language and I know that in ASSEMBLY we can get the instruction: NOP.

What is the equivalent in C?

Best regards.
 

nop + assembly

For picc it's like this:
Code:
asm("nop");
What compiler do you use?
 

precise timing using nop

The "NOP" assembly language instruction does nothing but eat clock cycles (3 clock cycles per NOP). Often times higher level compilers will remove all the NOP's during the optimize pass, so such instructions are useless for controlling timing. Additionally, controlling timing to the level of an exact number of clock cycles is nearly impossible from a higher level language like C, even when assembly level NOP's are permitted - you don't have control over how the compiler assembles the machine level code.

If all you are trying to do is control timing, you would be better served by using a loop or a series of 'C' instructions that don't contribute to program output (i.e. a series of variable increments that don't get used for visible program variables). For example, you could define the variable "timer", increment it however many times needed to create the necessary delay, reset it, and move on.

If you really need control over the program down to the number of clock cycles, you're going to have to program in assembly language. Higher level languages aren't going to do it for you.
 

nop instruction in keil

southafrikanse said:
z01z said:
What compiler do you use?

I have to use the Keil compiler.
For Keil C51 use this syntax:
#include <intrins.h>

_nop_();

The NOPs are not removed by the optimizer....
 
intrins.h _nop_

House_Cat said:
The "NOP" assembly language instruction does nothing but eat clock cycles (3 clock cycles per NOP).

Whether you say it "does something" or "does nothing at all", a NOP instruction as a whole, involves four clock cycles. In all PIC micros I can recall.

Moreover, any instruction cycle takes four clock cycles.
 

assembly nooperation

atferrari said:
Whether you say it "does something" or "does nothing at all", a NOP instruction as a whole, involves four clock cycles. In all PIC micros I can recall.

Moreover, any instruction cycle takes four clock cycles.

One of the problems with correcting a person is that you need to read the original problem. He said MICROCONTROLLER. He didn't say PIC. How many clock cycles an instruction takes depends on what microcontroller you may be using. I gave a typical controller - not all controllers. Your absolute statement about the time required to execute a NOP is wrong. Look at an 80xx series or a Z8xxxxx series controller, for example.

The fact still remains that NOP is a USELESS instruction in higher level languages like 'C'. It really only has any use at all in assembly language - and then it is sloppy coding. NOP is a tool used by crackers and hackers - real programmers use timing loops or in-line instructions to eat CPU time. A high level compiler will simply optimize a pushed ASM('NOP') completely out of the code - you achieve NOTHING by using it. Not even 4 clock cycles or 3 clock cycles - NOTHING gets coded for the processor.
 

nop machine cycle?

Yes you are right. I didn't realized the question did not say PIC. Yes. You could have had mentioned four instead of three for the case. Clear.

I am curious. Let's say that you want to "waste" the equivalent to 1, or 2, or 3 instruction cycles (1, or 2, or 3 NOPs) do you always have the chance to implement a loop with three lines of code (or equivalent)?

Interested.
 

keil optimizing out the nops

House_Cat said:
A high level compiler will simply optimize a pushed ASM('NOP') completely out of the code - you achieve NOTHING by using it. Not even 4 clock cycles or 3 clock cycles - NOTHING gets coded for the processor.
Sorry House_Cat, that's not always true. Take a look at this simple example:
Code:
#include <intrins.h>
#include <REG52.H>

void main (void)
{
	while(1)
	{
		_nop_();
		_nop_();
		_nop_();
	    P1 ^= 0x01;     		    // Toggle P1.0
		_nop_();
		_nop_();
	    P1 ^= 0x01;     		    // Toggle P1.0
		_nop_();
	}
}

I've compiled it using Keil's C51 (IMHO a high level language compiler) and used the highest optimization level (9). Now take a look at the generated assembly code:
ASSEMBLY LISTING OF GENERATED OBJECT CODE


; FUNCTION main (BEGIN)
; SOURCE LINE # 4
; SOURCE LINE # 5
0000 ?C0001:
; SOURCE LINE # 6
; SOURCE LINE # 7
; SOURCE LINE # 8
0000 00 NOP
; SOURCE LINE # 9
0001 00 NOP
; SOURCE LINE # 10
0002 00 NOP
; SOURCE LINE # 11
0003 639001 XRL P1,#01H
; SOURCE LINE # 12
0006 00 NOP
; SOURCE LINE # 13
0007 00 NOP
; SOURCE LINE # 14
0008 639001 XRL P1,#01H
; SOURCE LINE # 15
000B 00 NOP
; SOURCE LINE # 16
000C 80F2 SJMP ?C0001
; FUNCTION main (END)
You'll recognize that the compiler keeps the NOPs - that's its job.
I also wouldn't say that NOPs are useless. It can be very useful to achieve very short delays of some hundred ns. This can't be done by using loops and timers. Such delays are sometimes necessary to adjust the timing of connected peripheral devices, e.g. LCD displays.
 

nop instruction cycle

Aparently I got an interesting topic created about the NOP.

Let me ask you something:

If NOP does the same as clock cycles in C, why can't you use NOP instead of the clock cycles? Is about the size of the code?
 

assembly nop

You've to distinguish between clock cycles and machine cycles. From Wikipedia (https://en.wikipedia.org/wiki/8051):
The original 8051 core ran at 12 clock cycles per machine cycle, with most instructions executing in one or two machine cycles. With a 12 MHz clock frequency, the 8051 could thus execute 1 million one-cycle instructions per second or 500,000 two-cycle instructions per second.
That means that the controller needs 12 clock (crystal) cycles to execute one machine cycle. A NOP instruction is executed in one machine cycle = 12 clock cycles. So in a classical 8051 no instruction can be processed faster. It's a hardware feature.
 

I was sloppy in my terminology and got called on it - thank you. I should have said "machine cycles".

It looks like I need to look at the Kiel compiler. It's one I haven't used.

My point was that, unless you confine the "NOP" to a section of assembly code, you're not really controlling the microprocessor as accurately as you believe you are. You have no control over how the compiler is going to assemble the compiled code. Therefore, any section of code in which you use "NOP" is going to be of unpredictable length - hence unpredictable execution time.

Any instruction can be used to eat machine cycles when coding in assembly language - it doesn't have to be a string of "NOP". However, you have to confine your precise timing to an assembly code segment. Otherwise, you're wasting your time. (No puns intended)
 

So, another conclusion is, as far I understand it: you can not waste other than full "instruction cycles" whatever it takes, 3 ,4 10 or 12 machine (clock) cycles per instruction according to the design of the micro in use.

If you can not do it in assembly much less you caould do it in any language, right?
 

That would be correct. You are limited to whatever number of machine cycles a particular instruction uses. You can combine a series of instructions with whatever combination of timing you need (mov, and, add, or, etc). You just need to ensure that you don't mess with registers containing valid data while you're killing time.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top