I'm creating a small kernel in C and I need a function that will take a parameter containing the amount of seconds it should wait for.
我在C中创建一个小内核,我需要一个函数来获取一个参数,该参数包含它应该等待的秒数。
I've tried using for loops, but they haven't worked.
我尝试过使用for循环,但它们没有用。
I can't use the C standard library and need a way to tell the kernel to wait(in C). How can I do this?
我不能使用C标准库,需要一种方法来告诉内核等待(在C中)。我怎样才能做到这一点?
Loop:
循环:
int c = 1, d = 1;
for ( c = 1 ; c <= 32767 ; c++ )
for ( d = 1 ; d <= 32767 ; d++ ){}
1 个解决方案
#1
2
Using a loop works, but the time it takes will be highly dependent on the speed of your CPU and any compiler optimisations. It's useful when you know the target hardware (e.g. writing for specific microcontroller).
使用循环可以工作,但所需的时间将高度依赖于CPU的速度和任何编译器优化。当您了解目标硬件(例如,为特定微控制器编写)时,它非常有用。
You should include a NOP in the body of loop. Also check if your compiler supports a #pragma or special comment to disable optimisations on specific blocks of code.
你应该在循环体中包含一个NOP。还要检查编译器是否支持#pragma或特殊注释以禁用特定代码块的优化。
Check your compiler documentation for a definition of NOP. Most compilers define a macro named _nop()
or _nop_()
检查编译器文档以获取NOP的定义。大多数编译器定义一个名为_nop()或_nop_()的宏
You can define your own using:
您可以使用以下定义自己的:
#define Nop() {_asm nop _endasm}
Or by writing asm("nop")
if your compiler supports inline assembly.
或者如果编译器支持内联汇编,则编写asm(“nop”)。
#1
2
Using a loop works, but the time it takes will be highly dependent on the speed of your CPU and any compiler optimisations. It's useful when you know the target hardware (e.g. writing for specific microcontroller).
使用循环可以工作,但所需的时间将高度依赖于CPU的速度和任何编译器优化。当您了解目标硬件(例如,为特定微控制器编写)时,它非常有用。
You should include a NOP in the body of loop. Also check if your compiler supports a #pragma or special comment to disable optimisations on specific blocks of code.
你应该在循环体中包含一个NOP。还要检查编译器是否支持#pragma或特殊注释以禁用特定代码块的优化。
Check your compiler documentation for a definition of NOP. Most compilers define a macro named _nop()
or _nop_()
检查编译器文档以获取NOP的定义。大多数编译器定义一个名为_nop()或_nop_()的宏
You can define your own using:
您可以使用以下定义自己的:
#define Nop() {_asm nop _endasm}
Or by writing asm("nop")
if your compiler supports inline assembly.
或者如果编译器支持内联汇编,则编写asm(“nop”)。