mardi 4 juillet 2017

C - Program skips if statement code. Condition definitely met

I need some help here. I've read the following post with no results.

C Program Skipping if Statements Even When Conditions Are Met

I'm trying to write a simple rpn calculator, but I am stuck on the first if statement of the main function.I run it with ./prog d but instead of calling the debug function it proceeds to output "Text" and exit.

// This is not the full main function that is in my source file, as it
// is too large to include here. These are just the parts which are
// being affected.

int main(int argc, char *argv[]) {
    if (strcmp(argv[1], 'd')) {
        debug();
    }

    printf("Text\n");
    return 0;
}

void debug(void)
{
    char choice;
    int dbgValuePop;

    printf("\t1: push <value>\n\t2: pop\n\t3: display stack and exit\n--# ");
    scanf("%c", &choice);
    getchar();

    switch(choice)
    {
        case '1':
            push(strtok(choice, " "));
            break;

        case '2':
            dbgValuePop = pop();
            printf("%d\n", dbgValuePop);
            break;

        case '3':
            display();
            break;

        default: // Erroneous input
            printf("Invalid input. Try again.\n");
    }
}

I know that it is skipping the code for certain because after debugging with gdb and disassembling main I see this (Note the address that je jumps to if the result of strcmp is equal.):

 0x000000000040077f <+50>:  call   0x400600 <strcmp@plt>
 0x0000000000400784 <+55>:  test   eax,eax
 0x0000000000400786 <+57>:  je     0x40079c <main+79>
 0x0000000000400788 <+59>:  mov    eax,0x0
 0x000000000040078d <+64>:  call   0x4007a3 <debug>
 0x0000000000400792 <+69>:  mov    edi,0x0
 0x0000000000400797 <+74>:  call   0x400650 <exit@plt>
 0x000000000040079c <+79>:  mov    eax,0x0
 0x00000000004007a1 <+84>:  leave  
 0x00000000004007a2 <+85>:  ret

Any help will be greatly appreciated. Feel free to ask for any extra info you want.

Aucun commentaire:

Enregistrer un commentaire