Page 1 of 1

while(1) && for(;;) && jmp $ statements

Posted: Sun Apr 15, 2012 9:31 pm
by kammce
Is there a way to make my C code or my Assembly code loop forever without using while(1) or for(;;) in C or jmp $ in Assembly. My only issue is that it uses up a lot of my CPU. I would not have an issue with it if all kernels did this but the GRUB boot loader barely takes any CPU when it runs. Its is not until my kernel is loaded and booted that my CPU starts to run at 50% usage (I have a dual core).

Would some sort of time delay fix this, even thought that time delay would be on a for loop as well?

Thank you for your time.

Re: while(1) && for(;;) && jmp $ statements

Posted: Sun Apr 15, 2012 9:45 pm
by AndrewBuckley
while(1)
{
__asm__ ("hlt");
}

for(;;)
{
__asm__ ("hlt");
}

haltloop:
hlt
jmp haltloop

try any of these, hlt will tell the cpu to wait for interupts.

Re: while(1) && for(;;) && jmp $ statements

Posted: Sun Apr 15, 2012 9:48 pm
by AndrewBuckley
__asm__ __volatile__("hlt"); //will work, but im not sure if the volatile keyword is needed.

Re: while(1) && for(;;) && jmp $ statements

Posted: Sun Apr 15, 2012 9:50 pm
by kammce
PERFECTION!!!

Thank you very much.
I thought that the assembly instruction "hlt" literally halted the CPU completely, similar to rebooting or shutting down a computer.

Re: while(1) && for(;;) && jmp $ statements

Posted: Sun Apr 15, 2012 10:48 pm
by Kazinsal
kammce wrote:I thought that the assembly instruction "hlt" literally halted the CPU completely, similar to rebooting or shutting down a computer.
Only if you disable interrupts ("cli") first. "hlt" halts the processor until it externally woken up -- an interrupt does this, and the PIT sends out an interrupt on IRQ0 frequently (usually at a frequency of 18Hz unless otherwise specified).