I compile 64 bit program in Visual Studio 2015, it has two files:

2.asm

.code ;-------------------------------------------- Addup PROC, Arg1:DWORD, Arg2:DWORD, Arg3:DWORD mov eax, Arg1 add eax, Arg2 add eax, Arg3 ret Addup ENDP ;---------------------------------------------- END 

1.cpp

 #include <iostream> extern "C" int __stdcall Addup(int, int, int); int main() { std::cout << Addup(1, 1, 1); system("pause"); } 

The problem is that instead of the correct value of 3, the program displays some sort of garbage:

enter image description here

How to fix it? Thanks in advance.

Decision:

Here: msdn.microsoft.com/ru-ru/library/dd335933.aspx answer:

In the x64 agreement, the first four integer arguments (from left to right) are passed in 64-bit registers designed specifically for this purpose: RCX: 1st integer argument RDX: 2nd integer argument R8: 3rd integer argument R9: 4- th integer argument The remaining integer arguments are passed through the stack.

2.asm

 .code ;-------------------------------------------- Addup PROC mov rax, rcx add rax, rdx add rax, r8 ret Addup ENDP ;---------------------------------------------- END 
  • one
    I would look under the disassembler, what kind of code does it end up with? Most likely, when compiling for x64, some kind of "special" stdcall is used. - insolor
  • There is no backed suspicion that in the 64-bit model the value returns to RAX, and you have its upper part here in an undefined state. - Alexander Prokoshev
  • Changed all eax to rax, replaced the type of function parameters with qword, the same. - user310775
  • one
    Here: msdn.microsoft.com/ru-ru/library/dd335933.aspx answer: "In the x64 agreement, the first four integer arguments (from left to right) are transmitted in 64-bit registers designed specifically for this purpose: RCX: 1- rdx integer argument: 2nd integer argument R8: 3rd integer argument r9: 4th integer argument The remaining integer arguments are passed through the stack. " - user310775
  • @ user310775, add your solution as an answer, along with a quote from msdn - insolor

2 answers 2

Decision:

Here: msdn.microsoft.com/ru-ru/library/dd335933.aspx answer:

In the x64 agreement, the first four integer arguments (from left to right) are passed in 64-bit registers designed specifically for this purpose: RCX: 1st integer argument RDX: 2nd integer argument R8: 3rd integer argument R9: 4- th integer argument The remaining integer arguments are passed through the stack.

2.asm

 .code ;-------------------------------------------- Addup PROC mov rax, rcx add rax, rdx add rax, r8 ret Addup ENDP ;---------------------------------------------- END 

    Try for 2.asm to specify .model in the spirit

     .MODEL FLAT, STDCALL 
    • Tried, then it is not compiled, writes: syntax error:. - user310775
    • At the beginning of the file added a separate line? - FLCL
    • Exactly. In x86 it works, in x64 it swears on .model - user310775
    • Try to set the correct options for the asm: habr.com/post/252647 starting from "create * .asm file". I have a compile: i.imgur.com/v7xsZ0V.png - FLCL
    • @FLCL in the article in the 32-bit code is compiled, the question clearly states that you need to build a 64-bit program. - insolor