tags:

views:

123

answers:

1

Which one is better/faster/preferred

1:

mov eax, 5

push eax

mov eax, [someAddress]

push eax

2:

push 5

push [someAddress]

+4  A: 

#2 is faster because the assembly operation can take a constant, so you avoid the time loading the eax register. Also #2 leaves eax free, which is good if you are holding many values in registers as it can help minimize memory accesses. If you know the values are constants, just use #2.

Paul
Just beware of the bad practice of using magic numbers in your code which is what #2 is drifting towards :)
slugster
@Paul: Thanks it does seem much simply and generates less executable bytes (assuming no optimization)@Slugster: What do you mean by magic numbers?
Daniel
@Daniel: It's often bad practice to scatter constant values throughout your code. For one, it can hide the original purpose of the constant. In addition to improving readability and comprehension, using variables (perhaps labels is the right word here) or macros allows you to tweek the constants more conveniently, especially when used more than once.
Paul
@Paul: Ah yes of course xD push 5 was just some random number for the example
Daniel
@Daniel: The magic numbers Slugster was referring to are those mysterious, undocumented constants.
Paul