The MASM Forum

General => The Campus => Topic started by: mikichi on January 10, 2017, 07:10:34 AM

Title: Compare user input with char
Post by: mikichi on January 10, 2017, 07:10:34 AM
Hey,
I'm trying to let the user enter 2 numbers and operand and solve the "problem".
What I'm doing wrong? in the second try the exe got stuck, and if I comment out the print line(almost the last line) It's not stick but I don't know if it did what I want.

main proc

    PUSH eax
    PUSH ebx
    PUSH ecx
   
    MOV eax, input("Enter a number : ")
    MOV ebx, input("Enter a second number : ")
    MOV ecx, input("Choose operand(+/-): ")
   
    ;First Try
    ;.IF ecx == '+'
    ;    ADD eax, ebx
    ;.ELSEIF ecx == '-'
    ;    SUB eax, ebx
    ;.ELSE
    ;    print "Unkown operand", 13, 10 
    ;.ENDIF


    ;Second Try
    CMP ecx, '+'
    je plus
    plus:
        ADD eax, ebx
    CMP ecx, '-'
    je minus
    minus:
        SUB eax, ebx
           
    print eax, 13, 10 ;If I comment the program run well

    POP eax
    POP ebx
    POP ecx
   
    ret

main endp
Title: Re: Compare user input with char
Post by: hutch-- on January 10, 2017, 09:28:28 AM
The problem is you are trying to use registers as simple variables where EAX and ECX are volatile registers that will probably be overwritten by the next "input()" procedure call. Make 3 LOCAL DWORD variables and MOV the results into the variables.

    LOCAL num1 :DWORD
    LOCAL num2 :DWORD

The third data entry is a string operator so it must be written to a memory buffer.