4
votes

I would like to modify some assembly lines from C++ code generated by MSVC. The reason I want to do this can be found here Difference in performance between MSVC and GCC for highly optimized matrix multplication code

So I tried to rum masm with the assembly output but it get's a bunch of errors. Instead I just tried a "hello world" example.

include <stdio.h>
int main() {
    printf("asdf\n");
}

Compile it in 64 bit mode with /FA /O2...see the output below

When I run ml64 /c Source.asm I get the following error

Source.asm(35) : error A2006:undefined symbol : FLAT
Source.asm(17) : error A2006:undefined symbol : $LN3
Source.asm(18) : error A2006:undefined symbol : $LN3

After spending too much time on this I found these two threads http://social.microsoft.com/Forums/en-US/e0e541d9-5421-4297-8018-7c6a0f12ae62/compile-assembly-generated-by-cl?forum=whatforum and http://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/4aad9e70-6bb8-4622-a5d9-a3b07b51fc7f/c-compiler-creates-assembler-directives-that-ml64-doesnt-understand?forum=windowssdk where microsoft says

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.

Can someone explain another method to generator the assembly code from C++ code in MSVC2012 so that I can modify a few lines and then recompile/assemble it?

Edit: Here are some more useful comments a problem with cl.exe and ml.exe and MS Visual Studio 2010 how to use the .asm generated file

Here is the assembly output

; Listing generated by Microsoft (R) Optimizing Compiler Version 17.00.50727.1 

;include listing.inc

INCLUDELIB OLDNAMES

PUBLIC  ??_C@_05DBBGCHPA@asdf?6?$AA@            ; `string'
EXTRN   __imp_printf:PROC
EXTRN   __security_check_cookie:PROC
;   COMDAT ??_C@_05DBBGCHPA@asdf?6?$AA@
CONST   SEGMENT
??_C@_05DBBGCHPA@asdf?6?$AA@ DB 'asdf', 0aH, 00H    ; `string'
CONST   ENDS
PUBLIC  main
;   COMDAT pdata
pdata   SEGMENT
$pdata$main DD  imagerel $LN4
    DD  imagerel $LN4+24
    DD  imagerel $unwind$main
pdata   ENDS
;   COMDAT xdata
xdata   SEGMENT
$unwind$main DD 010401H
    DD  04204H
xdata   ENDS
; Function compile flags: /Ogtpy
;   COMDAT main
_TEXT   SEGMENT
main    PROC                        ; COMDAT
; Line 2
$LN4:
    sub rsp, 40                 ; 00000028H
; Line 3
    lea rcx, OFFSET FLAT:??_C@_05DBBGCHPA@asdf?6?$AA@
    call    QWORD PTR __imp_printf
; Line 4
    xor eax, eax
    add rsp, 40                 ; 00000028H
    ret 0
main    ENDP
_TEXT   ENDS
END
are you using debug build on MSVC?neagoegab
no, release mode "Compile it in 64 bit mode with /FA /O2..."Z boson
Are you sure you want to modify the output of the compiler? Will adding a function implemented in assembly work?Dani
There's just no point in modifying the generated assembly code. You'll do it a second time, maybe a third. The forth time you'll give up because you got sick of it. If you need your own assembly code injected into the program then just link to it.Hans Passant
@ChuckWalbourn, except that sometimes intrinsics don't do what you want and so you either have to write assembly or find a compiler which does what you want converting-c-object-file-from-linux-o-to-windows-obj.Z boson