Here is my question that I can't manage to google and solve. I wrote a simple C++ program that adds two numbers. However, how do I call an assembly code to do this addition. It will be easier to show what I have so here it is.

Here is my file called Adding.cpp

//============================================================================
// Name        : Adding.cpp
// Author      : Casey
// Version     : Eclipse Galileo
// Copyright   : 
// Description : 
//============================================================================

#include <iostream>
using namespace std;

int main() {
	int x;
	extern int sum(int a1, a2);
	cout << "Enter Numbers\n";
	cin >> a1, a2;
	x = sum(a1, a2);

	cout << x << endl;


	return 0;

Now here is my assembly language code in a different file called Add.s

//
//Add.s
//Created on: Sep 14, 2011
//Author: Casey

Sum:

pushl %ebp,
movl %ebp, %esp

movl 8(%ebp), %eax
movl 12(%ebp), %ecx
addl %eax, %ecx

pop %ebp
ret

I have multiple errors on the lines 'extern' and 'cin' and I'm not 100% sure I wrote the code correctly. How do I put these two together?

Recommended Answers

All 9 Replies

you have to use extern "C" in the file scope.
like this.

extern "C" {
int sum(int a1, int a2);
}

and also as I could remember you need to use global directive to label Sum.
like

.global _Sub

_Sum:

The underscore is added by the C compiler when it naming.so you have to
_Sum instead of _Sum,

No it didn't work. "expected unqualified-id before string constant."
This is what I did.

#include <iostream>
using namespace std;

int main() {
	int x;
	extern "C" {
	int Sum(int a1, a2);
	}
	cout << "Enter Numbers\n";
	cin >> Sum(a1, a2);
	x = Sum(a1, a2);

	cout << x << endl;


	return 0;
}

and

//
//Add.s
//Created on: Sep 14, 2011
//Author: Casey
.global _Sum
Sum:

pushl %ebp,
movl %ebp, %esp

movl 8(%ebp), %eax
movl 12(%ebp), %ecx
addl %eax, %ecx

pop %ebp
ret

Here's an example that works on my AMD/64 bit machine.

testit.cpp

#include <iostream>

extern "C" int sum(int, int);//to avoid name mangling

int main()
{
	std::cout << sum(3, 4) << std::endl;
	return 0;
}

asm_file.s

.section .data

.section .bss

.section .text
	.global sum
sum:
	pushq	%rbp
	movq	%rsp, %rbp

	xorq	%rax, %rax
	addl	%edi, %eax
	addl	%esi, %eax

	movq	%rbp, %rsp
	popq	%rbp
	ret

objdump -D testit.o>testfile

testfile

testit.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <main>:
   0:	55                   	push   %rbp
   1:	48 89 e5             	mov    %rsp,%rbp
   4:	be 04 00 00 00       	mov    $0x4,%esi //the values and registers we want
   9:	bf 03 00 00 00       	mov    $0x3,%edi //the values and registers we want
   e:	e8 00 00 00 00       	callq  13 <main+0x13>
  13:	89 c6                	mov    %eax,%esi
  15:	bf 00 00 00 00       	mov    $0x0,%edi
  1a:	e8 00 00 00 00       	callq  1f <main+0x1f>
  1f:	be 00 00 00 00       	mov    $0x0,%esi
  24:	48 89 c7             	mov    %rax,%rdi
  27:	e8 00 00 00 00       	callq  2c <main+0x2c>
  2c:	b8 00 00 00 00       	mov    $0x0,%eax
  31:	c9                   	leaveq 
  32:	c3                   	retq

And the compile lines.

g++ testit.cpp -c
as asm_file.s -o asm_file.o
g++ asm_file.o testit.o -o testit

Hmm... Your code I copied/pasted and it still didn't work. On line 7 of your code,I get the error 'undefined reference to sum'.

Hmm... Your code I copied/pasted and it still didn't work. On line 7 of your code,I get the error 'undefined reference to sum'.

How did you compile it?

Im using Eclipse Galileo and I clicked run which is pretty much what I do for every program I make. I have build all automatically selected.

In case your compiler is fussy, try changing line 3 to:

extern "C" {
  int sum(int, int);//to avoid name mangling
}

Are your .cpp and .s files both included as dependencies of your executable?

Are your .cpp and .s files both included as dependencies of your executable?

Not sure how to quote here but to answer you probably not. what do I do to do that?

Since you say:

I have build all automatically selected.

I imagine you have to go into the portion of the interface which deals with setting up your build. I'm not familiar with the Galileo Eclipse IDE, so I can't help with any details, but there should be a way to add additional source files to your build, and (if necessary) a way to specify how to assemble your .s file into a .o file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.