main.cpp
#include <stdio.h>
extern "C" void CalculateSums(int a, int b, int c, int* s1, int* s2, int* s3);
int main(int argc, char *argv[])
{
int a = 3, b = 5, c = 8;
int s1a, s2a, s3a;
CalculateSums(a, b, c, &s1a, &s2a, &s3a);
// Compute the sums again so we can verify the results
// of CalculateSums().
int s1b = a + b + c;
int s2b = a * a + b * b + c * c;
int s3b = a * a * a + b * b * b + c * c * c;
printf("Input: a: %4d b: %4d c: %4d\n", a, b, c);
printf("Output: s1a: %4d s2a: %4d s3a: %4d\n", s1a, s2a, s3a);
printf(" s1b: %4d s2b: %4d s3b: %4d\n", s1b, s2b, s3b);
return 0;
}
callingconvention.asm
; Name: callingconvention.asm
;
; Build: g++ -m32 -c main.cpp -o main.o
; nasm -f elf32 -o callingconvention.o callingconvention.asm
; g++ -m32 -o callingconvention callingconvention.o main.o
;
; Source: Modern x86 Assembly Language Programming p.37
global CalculateSums
section .text
; extern "C" void CalculateSums(int a, int b, int c, int* s1, int* s2, int* s3);
;
; Description: This function demonstrates a complete assembly language
; prolog and epilog.
;
; Returns: None.
;
; Computes: *s1 = a + b + c
; *s2 = a * a + b * b + c * c
; *s3 = a * a * a + b * b * b + c * c * c
%define a [ebp+8]
%define b [ebp+12]
%define c [ebp+16]
%define ptrS1 [ebp+20]
%define ptrS2 [ebp+24]
%define ptrS3 [ebp+28]
CalculateSums:
push ebp
mov ebp, esp
push ecx
mov eax, a
add eax, b
add eax, c ;a+b+c
mov ecx, ptrS1
mov [ecx], eax ;store result in s1
mov ecx, ptrS2 ;result s2
mov eax, a
imul eax, eax ;a*a
mov [ecx], eax ;s1 = a*a
mov ecx, ptrS3 ;result s3
imul eax, a ;a*a*a
mov [ecx], eax ;s3 = a*a*a
mov ecx, ptrS2 ;result s2
mov eax, b
imul eax, eax ;b*b
add [ecx], eax ;s2 = a*a + b*b
mov ecx, ptrS3 ;result s3
imul eax, b ;b*b*b
add [ecx], eax ;s3 = a*a*a + b*b*b
mov ecx, ptrS2 ;result s2
mov eax, c
imul eax, eax ;c*c
add [ecx], eax ;s2 = a*a + b*b + c*c
mov ecx, ptrS3 ;result s3
imul eax, c ;c*c*c
add [ecx], eax ;s3 = a*a*a + b*b*b + c*c*c
pop ecx
mov esp, ebp
pop ebp
ret
build
g++ -m32 -c main.cpp -o main.o
nasm -f elf32 -o callingconvention.o callingconvention.asm
g++ -m32 -o callingconvention callingconvention.o main.o