-2

I am trying to learn Assembly(MASM x64) and wrote a simple code.

.code
main proc
mov rax, 1
mov rcx, 2
mov rdx, 0
div rcx
ret 0
main endp
end

Now I started a debugging process to see that it works(as there is no other way to check if something works) and at the line div rcx the debugger started to drag me the whole way through some files called exe_common.inl, file_mode.cpp, back to exe_common.inl, matherr.cpp, again back to exe_common.inl, utility.cpp,and again exe_common.inl(what are those???????). At that point, there is a block of preprocessor code

#ifdef _RTC
_RTC_Initialize();
atexit(_RTC_Terminate);
#endif

and at _RTC_Initialize(); (the second line), the programm shuts down with the message initsect.cpp not found. What on earth does that mean???? Stackoverflow shows a gloryous 0 results about 'initsect.cpp not found' and google is not helpful either. Obviously, the same thing always happens when the line with ret tries to execute. I have erased the 0 after ret, erased everything between main proc and main endp, and erased the ret line. always the same. This happens when the line before main endp tries to execute, no matter what it is. It even happens by the simplest code on the world:

.code
main proc
ret 0
main endp
end

I am using microsoft visual studio 2019 and a AMD x64-x86 CPU.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 1
    Surely you don't have a line of code that just says `mov rdx` ? – Michael Aug 25 '20 at 19:48
  • Can you explain in more detail what you did to build and run the code? – Nate Eldredge Aug 25 '20 at 22:35
  • @Michael I had longer codes either but they all had the same problem so I took the simplest code to exclude the possibility that I made a mistake. And at Nate I ran the local windows debugger, marked the start and end point properly(start and end of the code) and keeped pressing F11. As the mentioned problem was detected, I ran the program by pressing ctrl+F5. the command prompt just says the program exited with code blablabla(just a trash number without any meaning) – Tarelenion Aug 26 '20 at 18:26
  • My point was that `mov rdx` isn't a valid instruction since it's missing the source operand, so I don't see how you can have executed the code in your question. – Michael Aug 26 '20 at 19:00
  • @Michael good question;;; For I have erased the code and cannot find out anymore whether if it was a copy mistake or something else so I'll just erase that part – Tarelenion Aug 26 '20 at 19:11
  • 1
    Does this answer your question? [Why should EDX be 0 before using the DIV instruction?](https://stackoverflow.com/questions/38416593/why-should-edx-be-0-before-using-the-div-instruction) And the other part about the strange files is probably because of how C++ handles that exception, you ended up stepping through all of that mess. (even if you didn't mean to write a C++ program, it's very easy to do it by accident in visual studio) – harold Aug 26 '20 at 19:25
  • @harold Thanks I have forgotten it. But it is not the problem either, for the problem occurs even by the simplest code on the world(without div or anything else). I will update the question. – Tarelenion Aug 26 '20 at 19:36
  • More info needed, I couldn't reproduce it: it just runs and exits with some random exit code (as expected) – harold Aug 26 '20 at 19:47
  • 2
    `rex` isn't a register name so this isn't a [mcve] of your division bug. Did you mean `mov rdx, 0`? – Peter Cordes Aug 26 '20 at 20:22
  • With this code, `div rcx` shouldn't fault. It sounds very unlikely that execution ends up in code from `exe_common.inl` and so on after `div rcx`, before reaching the `ret` in `main`. Having `main` return to its caller (which you don't have source for) would be totally normal, but your question says something else is happening. I think that's unlikely, but it's critical to be precise if you want anyone to understand and explain what you're seeing. – Peter Cordes Aug 28 '20 at 23:55

2 Answers2

1

Whether you've intended it or not, you've linked your program with the Visual Studio C++ runtime. The code you're stepping through performs the start up initialization that the C++ runtime and C++ programs need to run correctly. Eventually once that has completed it will call your main function, just like it would if you had built a C++ program.

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
-1

Ross Ridge was right, the assembler obviously linked the program with C runtime. What he forgot to say was that I need to set an entry point by _START: to avoid this, Thanks all of you for the help(ironical) and especially thank on DFpercush on Youtube(not ironical).