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
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
This sounds suspiciously like a homework assignment.
Show us what code you have tried to write and someone may be able to help you.
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 :(
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
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 :)
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
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.
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
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
how could i use else if statement is masm sir? im a newbie in masm32 :)
.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...
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 );
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
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,
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.
thanks for the reply sir, godbless.
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.