0

I run this code, which is called from a C++ program that just accepts input for the integers num1 and num2 and passes them to sum.asm. I get the errors: A2022: instruction operands must be the same size in sum.asm line 5 MSB3721: The command "mI64.exe/c/nologo /Zi/Fo "x64\Debug\sum.obj" /W3 /errorReport:prompt /Ta" ......\OneDrive\Documents\sum.asm"" exited with code 1. in masm.targets line 70

I tried changing mov rax, eax to movq rax, [eax] because I saw that was like one of the answers to another question like mine, but it gave me another error saying it wasn't the right syntax

main.cpp:

#include <iostream>

extern "C" int sum (int num1, int num2);

int main() {
int num1, num2, answer;

std::cout << "Enter num1:" << std::endl;
std::cin >> num1;
std::cout << "Enter num2:" << std::endl;
std::cin >> num2;

answer = sum(num1, num2);

std::cout << num1 << "+" << num2 << "=" << answer << std::endl;

return 0;
}

sum.asm:

_sum PROC
mov eax, ecx
add eax, edx
mov rax, eax
ret
_sum ENDP
END

This is just supposed to add the 2 numbers passed in from the main c++ project and then return the sum.
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Cindy
  • 66
  • 4
  • `movq` is AT&T syntax, not Intel syntax. It's for a totally different assembler that uses `mov src,dst`. If you did use `mov rax, [eax]`, that would be a 64-bit load from a 32-bit address, truncating a pointer to 32 bits. – Peter Cordes Jul 23 '19 at 21:14
  • You don't need to zero-extend EAX into RAX, `add eax, edx` already does that. Besides, you declared it as `int` so the caller is only going to look for the result in the low 32 bits of RAX (aka EAX). – Peter Cordes Jul 23 '19 at 21:15
  • Just currious; why would you try to implement things in inline asm in this day and age where compilers are as good at optimizing plain (readable) C++ as they are? – Jesper Juhl Jul 23 '19 at 21:23
  • @JesperJuhl It is for a class. I am just learning assembly, it is for a class that is required for 2 of my degrees. – Cindy Jul 23 '19 at 21:29
  • @JesperJuhl: that's not inline asm, MSVC doesn't even support inline asm for x86-64. It's stand-alone asm which is a more sane way to learn about asm. – Peter Cordes Jul 23 '19 at 21:34
  • @PeterCordes Correct. I bow to that. I was wrong. – Jesper Juhl Jul 23 '19 at 21:36
  • Thank y'all for all of y'all's help! I will check out links later, but I have a whole bunch of assignments due this week (6 of them being essays). I really appreciate the input and help, though!! – Cindy Jul 24 '19 at 22:33

1 Answers1

0

I figured it out. I just changed the line: mov rax, eax to: lea rax, [eax] I am just getting an unresolved external symbol for the project's object file. Does anyone know how to fix that since the MASM part is working now?

Cindy
  • 66
  • 4
  • That `lea` is equivalent to `mov eax,eax` but slower and larger code-size. And like I said, you don't need to redo truncation of RAX to 32-bit, `add eax,edx` already did that, and the caller is only going to read EAX anyway. [The advantages of using 32bit registers/instructions in x86-64](//stackoverflow.com/q/38303333) and [Why do x86-64 instructions on 32-bit registers zero the upper part of the full 64-bit register?](//stackoverflow.com/q/11177137). TL:DR: you should just remove that 3rd instruction instead of replacing it. – Peter Cordes Jul 23 '19 at 21:33
  • Your 2nd error sounds like you forgot to tell visual studio to actually link your asm into your executable. [external assembly file in visual studio](//stackoverflow.com/q/33751509). – Peter Cordes Jul 23 '19 at 21:38
  • Thank you for all of the help y'all! I got it figured out. I had already linked the asm file, but I needed to make the asm function public, and I added ```#pragma once``` to the beginning of the cpp file. I will check out those links, though! It is a lot of fun learning this stuff! – Cindy Jul 24 '19 at 22:31