5

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
Community
  • 1
  • 1
Z boson
  • 32,619
  • 11
  • 123
  • 226
  • are you using debug build on MSVC? – joy Jan 17 '14 at 10:40
  • no, release mode "Compile it in 64 bit mode with /FA /O2..." – Z boson Jan 17 '14 at 10:42
  • Are you sure you want to modify the output of the compiler? Will adding a function implemented in assembly work? – Daniel Jan 17 '14 at 11:18
  • 4
    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 Jan 17 '14 at 12:12
  • @HansPassant, yeah, that seems to be my conclusion now. – Z boson Jan 17 '14 at 12:47
  • @Zboson, The solar system has not blown up quite yet. One more chance: Do you have an editor (e.g., a text editor) that can change the physical bytes to physical hexadecimal values that you specify ? e.g., can you change a byte from 3Fh to E7h or whatever ? – User.1 Jan 17 '14 at 20:40
  • @User.1, yes, of course. If you have a suggestion write an answer. But I'm not sure I can just change a hex value. I need to add instructions, not just overwrite them with different ones of the same size. – Z boson Jan 17 '14 at 21:11
  • Generally the easier thing to do is write with intrinsics and then massage the source until the compiler generates basically what you'd write in assembly. It's a lot more portable and robust over time that way... – Chuck Walbourn Nov 15 '14 at 07:30
  • 1
    @ChuckWalbourn, [except that sometimes intrinsics don't do what you want](https://stackoverflow.com/questions/21134279/difference-in-performance-between-msvc-and-gcc-for-highly-optimized-matrix-multp) 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](https://stackoverflow.com/questions/4770918/converting-c-object-file-from-linux-o-to-windows-obj/21212320#21212320). – Z boson Nov 15 '14 at 12:46

0 Answers0