6

I used cl command to compile a cpp file:

cl test.cpp  //the generated  test.exe can work well

then I used another way:

cl /Fa /c test.cpp   //generate a test.asm assembly file
ml test.asm   // there failed!!!

why? How to solve it?

source code:

//:test.cpp 

 #include<iostream>
 using namespace std;
 int main()
  {
    cout<<"hello\n";
  }

wrong information:

Assembling: test.asm test.asm(1669) : fatal error A1010: unmatched block nesting

: ??$?6U?$char_trait s@D@std@@@std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@0@AAV10@PBD@Z


today I write another code in c

//test.cpp
#include<stdio.h>
void main()
{
  printf("hello");
}

then I compile the code

cl /Fa /c test.cpp
ml test.asm //ok!

It may be the difference in C and C++. This confuses me a few days. :(

how to solve it? please help me.

Tim Post
  • 33,371
  • 15
  • 110
  • 174
Simon
  • 63
  • 1
  • 7
  • the code you posted compile fine. maybe it's a problem with your compiler? which compiler are you using? how? – Heisenbug Sep 20 '11 at 15:55
  • I use Visual studio 2010 – Simon Sep 20 '11 at 16:17
  • I repro. The _TEXT ENDS above the ENDP is misplaced, it needs to be below it. This happens more than once, it is getting tripped up by the exception handlers you get when you use /EHs (required). You can report to bug at connect.microsoft.com – Hans Passant Sep 20 '11 at 17:06
  • I modified the asm file,put the ENDP below the _TEXT ENDS.but it's the same error. – Simon Sep 20 '11 at 17:18
  • I turn off the C++ exception option,and retry. the same error – Simon Sep 20 '11 at 17:23

1 Answers1

9

The compiler produces an invalid assembly listing when exception handling code is produced. There's a bug open on Microsoft Connect: http://connect.microsoft.com/VisualStudio/feedback/details/556051/cl-facs-generates-bad-masm-for-c-exception-handlers

In a response to the bug, there's a half-hearted "we will consider fixing this" along with a disclaimer that "listing files generated by the C/C++ compiler are for informational purposes".

It looks like you might be able to have a "scriptable" fix for this particular problem:

  • cut the ENDP statement that follows a text$x ENDS statement,
  • paste it just before the previous _TEXT ENDS statement

At least that looks to be the pattern in the asm file generated by your simple program - I don't know if that pattern would hold generally.

Unfortunately, after applying this fix, several new problems crop up with instructions using fs overrides and a couple undefined symbols. Who knows what else you'd run into once you tried this with a more complex program?

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • Thank you very much.I fix the file as you said.solve the unmatched problem.then I fix `fs` overrides by added `assume fs:nothing` at the head of the asm file.but the second prolbem aboout undefined symbols,I can't solve it yet. – Simon Sep 21 '11 at 14:55
  • In 2010 it's [less](https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/4aad9e70-6bb8-4622-a5d9-a3b07b51fc7f) than "half-hearted": *Before the work was undertaken to target x64 with the Microsoft C/C++ compiler we made the call to no longer support assembling C/C++ generated listing files. In other words, the listing files are for informational purposes only. Even though not supported, listing files from the x86 targeting compiler will potentially be able to be assembled. But with the x64 targeting compiler there will be a number of things that will prevent assembling listing files* – David Wohlferd Dec 06 '17 at 03:04