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

MichaelW

#15
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.
Well Microsoft, here's another nice mess you've gotten us into.

bet_017


MichaelW

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.
Well Microsoft, here's another nice mess you've gotten us into.