Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive
 
main.cpp
#include <stdio.h>

extern "C" int CalcSum(int a, int b, int c);

int main(int argc, char *argv[])
{
    int a=17, b=11, c=14;
    int sum = CalcSum(a,b,c);
    printf("  a:   %d\n", a);
    printf("  b:   %d\n", b);
    printf("  c:   %d\n", c);
    printf("  sum: %d\n", sum);
    return 0;
}
calcsum.asm
; Name:     calcsum.asm
;
; Build:    g++ -m32 -c main.cpp -o main.o
;           nasm -f elf32 -o calcsum.o calcsum.asm
;           g++ -m32 -o calcsum calcsum.o main.o
;
; Source:   Modern x86 Assembly Language Programming p.29

global  CalcSum

section .text

; extern "C" int CalcSum(int a, int b, int c);
;
; Description:  This function demonstrates passing arguments between
;               a C++ function and an assembly language function in
;               Linux with gcc and nasm.
;
; Returns:      a + b + c

%define a   [ebp+8]
%define b   [ebp+12]
%define c   [ebp+16]

CalcSum:
    push    ebp
    mov     ebp,esp
    mov     eax,a
    add     eax,b
    add     eax,c
    pop     ebp
    ret
build
g++ -m32 -c main.cpp -o main.o
nasm -f elf32 -o calcsum.o calcsum.asm
g++ -m32 -o calcsum calcsum.o main.o