Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sub eax,0 - does it do anything?

I just opened a file in IDA Pro and I found some code that looks completely useless. However, I thought it might have some use. Doesn't the sub eax,0 just subtract 0 from eax?

The code:

hinstDLL= dword ptr  4  
fdwReason= dword ptr  8  
lpReserved= dword ptr  0Ch  

mov     eax, [esp+fdwReason]  
sub     eax, 0  
jz      short loc_10001038  
like image 836
Filip Haglund Avatar asked Mar 20 '12 17:03

Filip Haglund


2 Answers

The sub instruction sets flags (OF, SF, ZF, AF, PF, and CF, according to the documentation) - the mov instruction does not. The jz will jump only if the zero flag (ZF) is set, so if you want to jump based on the value in eax that flag has to be set appropriately.

like image 184
Carl Norum Avatar answered Oct 29 '22 01:10

Carl Norum


The sub instruction will set the zero flag if its result is zero. In this case this means that the zero flag will be set if eax is zero.

So these three instructions check if [esp+fdwReason] is zero and jump to loc_10001038 in that case.

like image 31
Uli Schlachter Avatar answered Oct 29 '22 02:10

Uli Schlachter