Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: "if" Statement Not Responding Correctly


This question is answered.


Permlink Replies: 2 - Last Post: Aug 20, 2016 2:03 PM Last Post By: Earl Staley
Earl Staley

Posts: 99
Registered: 4/9/07
"if" Statement Not Responding Correctly  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 20, 2016 1:11 PM
I have an 'if' statement that is not working as expected. The statement is:

if (faNew->TableName != NULL){
...
}


The 'if' statement code is executed even if faNew->TableName is NULL.

I have a watch set on faNew->TableName and when the program reaches the 'if' statement, the watch window value for faNew->TableName is "". If I click the '+' sign next to faNew->TableName, all zeros are shown.

I have a structure like this:

struct finAsset {
	...
	char TableName[80];
	...
	finAsset *Next;
};


The structure is accessed via a linked list.

Here is the CPU view when the program breaks at the 'if' statement:

MainFormFinSum.cpp.248: if (faNew->TableName != NULL) {
00409C6E 8B45BC mov eax,[ebp-$44]
00409C71 8B9064040000 mov edx,[eax+$00000464]
00409C77 81C288040000 add edx,$00000488
00409C7D 85D2 test edx,edx
00409C7F 0F8499010000 jz $00409e1e

When when faNew->TableName has a name, the edx register at the breakpoint shows a value (778). When faNew->TableName is NULL, the edx register is 0 until line 00409C71 is executed.

I am not familiar with "test edx,edx". Is 'test' similar to 'cmp'? And why are both arguments to the test statement the same (edx,edx)?

Why is the 'if' statement code being executed when faNew->TableName is NULL?

Thank you for your help...
Earl Staley

Edited by: Earl Staley on Aug 20, 2016 1:12 PM
Antonio Estevez

Posts: 665
Registered: 4/12/00
Re: "if" Statement Not Responding Correctly [Edit]
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 20, 2016 1:36 PM   in response to: Earl Staley in response to: Earl Staley
El 20/08/2016 a las 22:12, Earl Staley escribió:
I have an 'if' statement that is not working as expected. The statement is:

if (faNew->TableName != NULL){
...
}


The 'if' statement code is executed even if faNew->TableName is NULL.

I have a watch set on faNew->TableName and when the program reaches the 'if' statement, the watch window value for faNew->TableName is "". If I click the '+' sign next to faNew->TableName, all zeros are shown.

I have a structure like this:

struct finAsset {
	...
	char TableName[80];
	...
	finAsset *Next;
};


The structure is accessed via a linked list.

Here is the CPU view when the program breaks at the 'if' statement:

MainFormFinSum.cpp.248: if (faNew->TableName != NULL) {
00409C6E 8B45BC mov eax,[ebp-$44]
00409C71 8B9064040000 mov edx,[eax+$00000464]
00409C77 81C288040000 add edx,$00000488
00409C7D 85D2 test edx,edx
00409C7F 0F8499010000 jz $00409e1e

When when faNew->TableName has a name, the edx register at the breakpoint shows a value (778). When faNew->TableName is NULL, the edx register is 0 until line 00409C71 is executed.

faNew->TableName never can be NULL.
faNew->TableName returns the memory address where is located the first char of the TableName array ( faNew pointer +
TableName offset in the finAsset struct)

Try this:
if (faNew->TableName[0] != 0){
...
}


I am not familiar with "test edx,edx". Is 'test' similar to 'cmp'? And why are both arguments to the test statement the same (edx,edx)?

'test' is a 'and' operator without storing the result in the register, only the flag register is affected.

test edx,edx is usually used to check if edx is 0 or not.



Why is the 'if' statement code being executed when faNew->TableName is NULL?

faNew->TableName never can be NULL.
Earl Staley

Posts: 99
Registered: 4/9/07
Re: "if" Statement Not Responding Correctly [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 20, 2016 2:03 PM   in response to: Antonio Estevez in response to: Antonio Estevez
Antonio,

Thank you for the reply and education into assembly language. You nailed the problem.

Regards...
Earl Staley
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02