The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: port513 on May 27, 2006, 04:05:38 AM

Title: Now it's time for my next stupid question ;)
Post by: port513 on May 27, 2006, 04:05:38 AM
I downloaded Assembly Optimization Tips by Mark Larson

The first thing in that is to free up all 8 CPU registers for use in my code.

Can someone explain how to make good use of that kind of programming and if there is some example code it's even better.


/Henke
Title: Re: Now it's time for my next stupid question ;)
Post by: asmfan on May 27, 2006, 07:18:24 AM
Port, it is useful according to logic of maximization of registers use and minimizction memory use. Say you write a huge procedure with a bunch of variables so put them in regs not mem/stack...
Title: Re: Now it's time for my next stupid question ;)
Post by: Mark Jones on May 27, 2006, 08:17:38 AM
asmfan knows what he's talking about here. In order, from fastest to slowest: registers, stack, memory. Memory can be 10-1000x slower than registers... so if you can put one of your working values into a register, versus a stack or memory location, then the routine will run much faster. :U

For general assembly programming, do not bother with freeing all the registers. The only improvment this manifests is in a loop or when that particular code is called many times. Keep in mind if that routine is called many times, the process of freeing the registers is fairly slow and might nullify or even reverse the speed gain. If all else fails, benchmark your code!

(And if you have an AMD system, check out AMD's developer pages for the CodeAnalyst... very useful program-bottleneck analysis software.)
Title: Re: Now it's time for my next stupid question ;)
Post by: port513 on May 27, 2006, 03:25:01 PM
Any example code?

I want to know how to use this even if I never will use this etc ;)


/Henke
Title: Re: Now it's time for my next stupid question ;)
Post by: stanhebben on May 27, 2006, 03:41:28 PM
Because you really want it:


LOCAL counter:DWORD
 
  mov counter, 1000000
_loop:
  sub counter, 1

  ... some code ...

  cmp counter, 0
  jnz _loop


is better if rewritten like: (if the looped code doesn't use ecx, of course)


  mov ecx, 1000000
_loop:
  sub ecx, 1

  ... some code which doesn't use ecx ...

  cmp ecx, 0
  jnz _loop


Hope you get the point.
Title: Re: Now it's time for my next stupid question ;)
Post by: port513 on May 27, 2006, 04:07:24 PM
Yup, but free all 8 registers?


/Henke
Title: Re: Now it's time for my next stupid question ;)
Post by: stanhebben on May 27, 2006, 04:18:14 PM
I'm not sure if you can free up esp, but here's some code that could do it. ebp can be used freely if your parameters are in registers and you don't have local variables.


; ebx/esi/edi must preserved, according to the stdcall convention
push esi
push edi
push ebx

; backup ebp and esp
push ebp
mov espbackup, esp

... code that can use all eight registers, but no local vars/stack ....

; restore registers
mov esp, espbackup
pop ebp

pop ebx
pop edi
pop esi


But I recommend limiting yourself to 6 registers or eventually 7 if you don't need local variables.
Title: Re: Now it's time for my next stupid question ;)
Post by: port513 on May 27, 2006, 04:20:04 PM
Thanks!

/Henke