-1

I am using Visual Studio 2019 Developer Command Prompt. The inline assembly code is simple division and remainder. There is no error in compilation using the command cl /EHsc filename.cpp. Absolutely nothing is displayed on the screen. It takes up to 4 seconds of runtime.

#include<iostream>
using namespace std;
int main()
{
    int x=10,y=20;
    int z=0,r=0;
    //z=x/y
    //r=x%y
    __asm
    {
        MOV EAX,x
        IDIV y
        MOV z,EAX
        MOV r,EDX
    }
    cout<<"z = "<<z<<"\tr = "<<r<<endl;
    return 0;
}
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • I see nothing in this code that would interfere with `cout`. You will just have to trace into the code with a debugger. But why are you using inline assembly at all, and not simply using the `/` and `%` operators in normal C++ code? Let the compiler optimize for you as needed. – Remy Lebeau Feb 03 '20 at 21:53
  • 1
    If you're trying to be efficient, use [`std::div`](https://en.cppreference.com/w/cpp/numeric/math/div) – NathanOliver Feb 03 '20 at 21:56
  • I am learning basic inline assembly and that is why using it. – Mohammad Shahwez Feb 03 '20 at 21:56
  • 1
    1. Use the debugger. 2. Forget inline assembly 3. Forget assembly – Michael Chourdakis Feb 03 '20 at 21:58
  • You might need to tell the compiler that you are clobbering `EAX` and `EDX`. – G. Sliepen Feb 03 '20 at 22:03
  • How do I do that ? @G.Sliepen – Mohammad Shahwez Feb 03 '20 at 22:05
  • Thanks a lot, everyone. Adding just one line `CDQ` just before `IDIV y` did the job. @1201ProgramAlarm – Mohammad Shahwez Feb 03 '20 at 22:17
  • 2
    @MichaelChourdakis "Forget assembly" What about if you want to be a engineer developing drivers or close to the bone protocols on on embedded hardware. Or if you want to develop on OS kernels. Or if you want to write compilers? Or if you want to work on HPC? Or just to get a better understanding on what happens under the hood to write better abstractions? Or what if you want to write a new programming language? – bolov Feb 03 '20 at 22:53
  • 2
    @G.Sliepen: This is MSVC, not GNU C inline asm. MSVC parses your asm and figures out which registers you touch. – Peter Cordes Feb 03 '20 at 23:43

1 Answers1

3

You're missing a CDQ between the MOV EAX,x and the IDIV y instructions. Without it, the EDX register has an unknown value in it, possibly resulting in a divide overflow. The CDQ instruction will sign extend EAX into EDX.

MOV EAX,x
CDQ
IDIV y
1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
  • @MohammadShahwez: Also worth mentioning that this inline asm is totally pointless; the compiler will do this optimization for you (using both outputs of one `idiv`), or better not even using `idiv` for a compile-time-constant divisor. (Even with a runtime variable dividend.) Using MSVC inline asm introduces a store/reload of both outputs. See [What is the difference between 'asm', '\_\_asm' and '\_\_asm\_\_'?](//stackoverflow.com/a/35959859) – Peter Cordes Feb 03 '20 at 23:45
  • @MohammadShahwez: Also worth mentioning: under a better OS, you'd see the program crash from the divide exception. e.g. on a Unix-like OS such as Linux, the OS would kill your process with a SIGFPE arithmetic exception, and the command line shell would report that fact. Under Windows, maybe try Powershell in case it's just `cmd` being bad and not reporting abnormal program termination. – Peter Cordes Feb 04 '20 at 03:28