The MASM Forum

General => The Campus => Topic started by: bet_017 on February 09, 2013, 07:36:38 PM

Title: i need some help, regarding operators (+,-*,\)
Post by: bet_017 on February 09, 2013, 07:36:38 PM
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
Title: Re: i need some help, regarding operators (+,-*,\)
Post by: bet_017 on February 09, 2013, 07:49:29 PM
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
Title: Re: i need some help, regarding operators (+,-*,\)
Post by: hutch-- on February 09, 2013, 07:53:57 PM
This sounds suspiciously like a homework assignment.

Show us what code you have tried to write and someone may be able to help you.
Title: Re: i need some help, regarding operators (+,-*,\)
Post by: bet_017 on February 09, 2013, 08:20:06 PM
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 :(
Title: Re: i need some help, regarding operators (+,-*,\)
Post by: hfheatherfox07 on February 09, 2013, 09:06:15 PM
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
Title: Re: i need some help, regarding operators (+,-*,\)
Post by: hfheatherfox07 on February 09, 2013, 09:20:46 PM
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 :)

Title: Re: i need some help, regarding operators (+,-*,\)
Post by: bet_017 on February 09, 2013, 11:41:12 PM
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
Title: Re: i need some help, regarding operators (+,-*,\)
Post by: hutch-- on February 10, 2013, 01:19:56 AM
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.
Title: Re: i need some help, regarding operators (+,-*,\)
Post by: mineiro on February 10, 2013, 01:39:48 AM
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
Title: Re: i need some help, regarding operators (+,-*,\)
Post by: dedndave on February 10, 2013, 02:15:30 AM
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
Title: Re: i need some help, regarding operators (+,-*,\)
Post by: bet_017 on February 10, 2013, 09:53:33 AM
how could i use else if statement is masm sir? im a newbie in masm32 :)
Title: Re: i need some help, regarding operators (+,-*,\)
Post by: dedndave on February 10, 2013, 10:08:14 AM
    .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...
Title: Re: i need some help, regarding operators (+,-*,\)
Post by: MichaelW on February 10, 2013, 10:23:17 AM
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 );

Title: Re: i need some help, regarding operators (+,-*,\)
Post by: Gunther on February 10, 2013, 11:19:34 AM
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
Title: Re: i need some help, regarding operators (+,-*,\)
Post by: bet_017 on February 10, 2013, 03:18:54 PM
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,
Title: Re: i need some help, regarding operators (+,-*,\)
Post by: MichaelW on February 10, 2013, 05:48:14 PM
bet_017,

The difficulty of what you are trying to do depends on how you do it. Using a high-level language it can be very easy. Using MASM it will be more difficult, but here too the difficulty depends on how you do it. If using the sval macro is OK, then since it depends on the CRT atol function, I would think that using the CRT scanf function directly should also be OK. So by using scanf, in combination with the MASM32 switch$ macro, you could create a MASM version almost as easily as I created the C version above. But that said, if this is any sort of school work then I think you should take a more low-level approach. I'm not sure how low-level your input and output code should be, but I think the switch-case functionality should be done entirely in your own assembly code, probably using comparisons, jumps, and labels. And it should include a way to bypass invalid operators.

Or another method, that would be a little cleaner, would be to do what the Microsoft C compiler did and use a jump table addressed by an index derived from the character code of the operator. Here is a list of the valid operators and their character codes.

* = 42
+ = 43
- = 45
/ = 47


You could construct the table in your initialized data section from 6 dword members, all containing the address of an appropriate label, with the index value ranging from 42-42=0 to 47-42=5. So the dword at index 0 would point to the label for the multiply routine, the dword at index 1 would point to the label for the addition routine, the dword at index 2 would point to the label used to bypass invalid operators, and so on. And the only actual comparisons required would be to ensure that the index was in the range 0 to 5. So assuming that the table was properly set up, and an operator index in the range 0 to 5 was in ECX, you could jump to the correct routine with:

jmp [table+ecx*4]


The scale factor 4 is necessary to correctly address the table of dwords, so an index of 0 will access the first dword at offset 0 in the table, an index of 1 the second dword at offset 4 in the table, and so on.
Title: Re: i need some help, regarding operators (+,-*,\)
Post by: bet_017 on February 10, 2013, 10:30:02 PM
thanks for the reply sir, godbless.
Title: Re: i need some help, regarding operators (+,-*,\)
Post by: MichaelW on February 10, 2013, 11:00:32 PM
FWIW, I implemented a working jump-table version using sval(input()) to read number1 and number2, input() to read the operator, and the printf macro to display the result, in a compact 33-line source code.