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

Programming, for all ages and all languages.
Post Reply
kammce
Posts: 18
Joined: Sun Apr 15, 2012 9:17 pm

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

Post 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.
AndrewBuckley
Member
Member
Posts: 95
Joined: Thu Jan 29, 2009 9:13 am

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

Post 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.
AndrewBuckley
Member
Member
Posts: 95
Joined: Thu Jan 29, 2009 9:13 am

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

Post by AndrewBuckley »

__asm__ __volatile__("hlt"); //will work, but im not sure if the volatile keyword is needed.
kammce
Posts: 18
Joined: Sun Apr 15, 2012 9:17 pm

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

Post 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.
User avatar
Kazinsal
Member
Member
Posts: 559
Joined: Wed Jul 13, 2011 7:38 pm
Libera.chat IRC: Kazinsal
Location: Vancouver
Contact:

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

Post 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).
Post Reply