i remember recently disassembling a for loop, i was pretty sure Masm assembler will translate i++ operation into inc [var_i] or something similar, but what it actually did was moving [var_i] to a register, Adding 1 to it and moving it back to [var_i]. anyone knows why? i didnt checked the Compiler optimized executable coz when i add optimization features to my compilation the exe crashes. so i dont know if optimized version actually does uses the inc and dec instructions. maybe Bill Gates doesnt likes those instruction? or they are slower then addition? which i doubt
There's no one to one translation from C\C++ to assembly. The standards for both languages give compilers an enormous amount of freedom in how they choose to translate code. Then the processors themselves will have a preference of one method over another depending on the model, this is why compilers allow you to specify the processor family that you are compiling your code for.
I guess what I'm trying to say is that there are too many factors in play to give you a definitive answer to this type of question. To venture a guess about this specific scenario though, I would say that your prediction was more inline with what a prefix increment operation would do. The code that you are describing seems appropriate for a post-fix operation.
It's good that you are studying the languages this way but don't let it derail you. Remember that they were developed specifically so that you wouldn't have to deal with raw assembly code.
Thanks for the information, and eventually it doesnt matters if the compiler prefers to translate it to:
mov rcx, [var_i]
add rcx, 1
...
i can always change it manualy to
inc [var_i]
or even
mov rcx, [var_i]
inc rcx and NOP the extra bytes that leftover. even if it will not make thing better, its a good practice.
I don't think you are allowed to just increment the memory where i is stored directly; you have to perform that operation on a register. Also if you are not using a debug build, something like a loop counter would probably not even be kept in memory normally and would just stay in a register for it's duration.
I'm dubious that incrementing a memory location directly works as written; all hardware I have ever seen does not work that way. Have you actually tried writing assembly that does that and seeing if the machine code that is actually executed doesn't touch the registers?