The MASM Forum Archive 2004 to 2012

Miscellaneous Forums => 16 bit DOS Programming => Topic started by: dncprogrammer on December 12, 2006, 09:08:00 PM

Title: Efficiency in addressing?
Post by: dncprogrammer on December 12, 2006, 09:08:00 PM
Im parsing strings and I have written code that works but Im not so sure that I have written it most efficiently which has brought about an issue in my mind. For a good starter, generally speaking which of the following is a better method of indexing?

mov   firstbyte, [si]
         inc si
mov   secbyte, [si]
         inc si
mov   thirdbyte, [si]
         inc si
...ect...
or

mov   firstbyte, [si]
mov   secbyte, [si+1]
mov   thirdbyte, [si+2]
...ect...

I realize that the value of si will not be changed when done with the 2nd example, in case I was trying to maintain a pointer value. That decision can be made as necessary.
Or what if I didn't use inc in the first example but rather add si, 1 for each index. Are there huge penaltys to pay?
jon

Title: Re: Efficiency in addressing?
Post by: stanhebben on December 12, 2006, 09:20:22 PM
Your second version will be faster. Always try to use addressing modes to avoid instructions.

Also, using an add instead of inc will be faster.

The code speedup can be noticeable if you do this in inner loops.
Title: Re: Efficiency in addressing?
Post by: dncprogrammer on January 08, 2007, 05:16:52 PM
Hey guys!
I realize that this is not an addressing question but it is, however, an efficiency question.
Which would be faster within a loop:

or al, al
jz  whatever

or..

cmp al, 0
je whatever

jon
Title: Re: Efficiency in addressing?
Post by: Tedd on January 09, 2007, 02:13:20 PM
I doubt there will be any noticable difference, and it would depend more on the instructions leading up to that.
In general I would go for the least destructive. 'OR' will write back the value, destroying the previous value of al (which was the same, but that's not the point,) whereas CMP doesn't try to modify al.

If only checking for zero, "test al,al" might be better :P (It does 'and al,al' but without writing back the result to al.)