News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

i need some help, regarding operators (+,-*,\)

Started by bet_017, February 09, 2013, 07:36:38 PM

Previous topic - Next topic

bet_017

does anyone know here how to use operators, the output is like this
enter the 1st number:
enter the 2nd number:
enter the operator to be used:

if ill put 2 as the 1st number and 4 as the 2nd number and its addition. the answer should be 6, if ill put sub(-) the answer should -2 and if mul 8, and in div. maybe 0. can you help me? please

bet_017

enter the 1st number: 5
enter the 2nd number:5
enter the operator to be used:-

0

another

enter the 1st number: 5
enter the 2nd number:5
enter the operator to be used:*

25


enter the 1st number: 5
enter the 2nd number:5
enter the operator to be used:\


1

hutch--

This sounds suspiciously like a homework assignment.

Show us what code you have tried to write and someone may be able to help you.

bet_017

here sir

main proc

    LOCAL var1:DWORD            ; space for a DWORD variable
      LOCAL var2:DWORD            ; space for a DWORD variable
     
     
    LOCAL str1:DWORD            ; a string handle for the input data
   LOCAL str2:DWORD            ; a string handle for the input data
   

   


    mov var1, sval(input("Enter a number : "))
     mov var2, sval(input("Enter a number : "))


    mov eax, var1              ; copy the IMMEDIATE number 100 into the EAX register
    mov ecx, var2               ; copy the IMMEDIATE number 250 into the ECX register
    add ecx, eax      ; ADD EAX to ECX
    mov ebx,ecx
    print str$(ebx)             ; show the result at the console
    print chr$(13,10,13,10)


i put sub and imux and idiv but they are not working :(

hfheatherfox07

Hello ,
Look here for exmple of subtract ,
Both console and message box :
http://masm32.com/board/index.php?topic=1072.msg9932


If you want user input into console that is a little tricky , I posted example here:

http://masm32.com/board/index.php?topic=1345.msg14037#msg14037


Download 5th post down to see console user inpute:
http://masm32.com/board/index.php?action=dlattach;topic=1345.0;attach=1171
Your code and your skills will be assimilated. Your programming language is irrelevant.
We are the ASM Borg and you will become part of us. Compile and be assembled.

hfheatherfox07

Also I had a little issue with divide with reminder
And I got help here:
http://masm32.com/board/index.php?topic=1226.0

You can read through the posts and see the way to do it :)

Your code and your skills will be assimilated. Your programming language is irrelevant.
We are the ASM Borg and you will become part of us. Compile and be assembled.

bet_017

thanks sir, but i dont understand. in masm 32 tutorial. We'll use that one to have and output like that, the user is the one to put what operator to be used

hutch--

Explain to us this much, entering the numbers is easy, do you also want to enter the operator (+-*/) as well ? If you want to enter the data as a formula "1+1" then you must parse the string to separate the numbers from the operator.

mineiro

Hello bet_017;
in your example, you have created a variable to hold first and second number, but forgot the "operator" variable. Next step is compare if operator is one that you like or is an error.
.if operator == "+"
;sum
.elseif operator == "-"
;...
.else
;its carnival, anything can happen.
.endif

dedndave

in the masm32\help folder, you will find several help files
the hlhelp.chm file has the macros

you are using sval to get the values
that one won't work to get the operator
if you look in the help file, the "getkey" macro waits for a single key and returns it
or, you could use the "input" macro to get a string

bet_017

how could i use else if statement is masm sir? im a newbie in masm32 :)

dedndave

    .if eax>5
        sub     eax,3
    .elseif eax>3
        sub     eax,2
    .else
        add     eax,1
    .endif


there is also .WHILE/.ENDW, .REPEAT/.UNTIL, and i guess .SWITCH/.CASE, that i have never used :P

MASM operators that may be used in .IF are rather unusual
an image of a page from the masm 6.1 reference manual is attached...

MichaelW

FWIW, the CRT scanf function can be used to get all three of the inputs in a single call. The C code for a version that uses postfix notation could be as simple as:

    int x, y, r;
    char op;
    scanf( "%d %d %c", &x, &y, &op );
    switch(op)
    {
        case '+':
            r = x + y;
            break;
        case '-':
            r = x - y;
            break;
        case '*':
            r = x * y;
            break;
        case '/':
            r = x / y;
            break;
    }
    printf( "%d\n", r );

Well Microsoft, here's another nice mess you've gotten us into.

Gunther

Mike,

well done. That's the solution.

bet_017,

what should you do? Take the small C code inside a function, say the compiler to generate the assembly language output (in gcc is that command line switch -S) and you have an idea for your solution. Good luck. And last, but not least: Welcome to the forum.

Gunther
You have to know the facts before you can distort them.

bet_017

thanks sir, for all your help. ill try my best to finish this one, We're using the tutorial number 5, the numbers as a default code. hope i can do it,