Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

nested for loop?

Status
Not open for further replies.

Gaudalier

Programmer
Jul 3, 2000
75
0
0
MX
Hello, again, hello!!

Still transporting from C code to Asm ;)

Sorry, but cannot find in TEACH YOURSELF C IN 21 DAYS an explanation to this code:
Code:
void ClearScreen()
{
 WORD xpos,ypos;
 for (ypos=0;ypos<Yimage;ypos+=8)
  for (xpos=0;xpos<Ximage;xpos+=8)
   {
     ColorPixel(xpos,ypos,COLOR_BLACK)
   }
}
Question: Is Second For Loop NESTED in First For Loop???

(In Above "Orignal" code semicolon are missing ";")


Thank in advance!


-----
 
yes that is correct

as in a sense the second loop acts as a
single operation in regards to the first.

as in

for (int x = 0; x < 190; x++)
do_something ();

the second line does not need the curly things
as it is single and self contained.

in effect the second for loop is self contained
and therfor does not need the braces.

all three nested loops below will behave the same
I think. and if I am wrong some one will rub
my nose in it.

Code:
for (int x = 0; x < 190; x++)
{
   for (ypos=0;ypos<Yimage;ypos+=8)
   {   
      for (xpos=0;xpos<Ximage;xpos+=8)
      {
         ColorPixel(xpos,ypos,COLOR_BLACK)
      }
   }
}


Code:
for (int x = 0; x < 190; x++)
{
   for (ypos=0;ypos<Yimage;ypos+=8)     
      for (xpos=0;xpos<Ximage;xpos+=8)
      {
         ColorPixel(xpos,ypos,COLOR_BLACK)
      }   
}


Code:
for (int x = 0; x < 190; x++)
   for (ypos=0;ypos<Yimage;ypos+=8)      
      for (xpos=0;xpos<Ximage;xpos+=8)      
         ColorPixel(xpos,ypos,COLOR_BLACK);



the for loop needs to contain in the body either

{
do somthing ....
}

or an operation with a termintating ';' character

for (int x = 0; x < 190; x++)
y++;

this

for (int x = 0; x < 190; x++);

is a do nothing loop. it dont do nothing except eat up
clock cycles. which can in some instances be used as a sort of timer.


TC
 
Gaudalier, do you mind if I mention something? You are translating a graphic routine from C to asm? Presumably the aim is to make it faster. I strongly suspect you're going to be disappointed in this approach...

I also assume you're probably using a VGA or similar graphics system. If so, then each generic pixel write first has to prepare the VGA card using a number of port "out" instructions, which are outrageously slow. Therefore if you clear the whole screen in a nested loop like this, you make as many of these "outs" as there are pixels. If you need to speed it up, the better approach in asm is to do one set-up outside the nested loop, and then do all the screen writing within the loop.

Of course there's a balance between generic code (the for-loop to Ximage is as generic as you can get; it doesn't even need to know the screen dimensions) and hardware-near efficiency. But if you're moving from C to asm, you must be interested in speed rather than genericness??

For interest, in 16-bit assembly for VGA you'd be doing something like:
{ set up all the VGA registers by out statements. I can't remember the addresses of VGA ports and their meaning withoug consulting suitable reference, but you'll have lots of statements like:
mov dx, 03CEh
mov ax, 0305h ;mode 3 is where I usually do drawing
out dx, ax }
....
mov di, size_of_screen
mov ax, 0A000h ; location of screen
mov es, ax
sub ax, ax
label:
mov es:di, ax
dec di
jnz label
mov es:di, ax
-all done--- (I could have burst into string instructions, but didn't because I'm lazy; in practice it won't make much difference to speed)

If you're not writing for VGA, please disregard nearly all I've written! Note that in VGA you can also cover 8 pixels with one write, which is useful for setting large areas of screen to one colour.


 
by the way, dont say too loud

"TEACH YOURSELF C IN 21 DAYS"

It hurts some of the more sensitize ears.

TC ;-)
 
he, he..

by the way, dont say too loud

"TEACH YOURSELF C IN 21 DAYS"

It hurts some of the more sensitize ears.

Long time ago that I don't touch C languaje, so I read my old c book.

By the way LionelHill, yes, actually I'm working in a Imaginery Library, that really need some speed (Clarion, my former DB Languaje) does not do the job!

Thank you Guys!

-----
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top