main.cpp
#include <stdio.h>
extern "C" int CalcArraySquares(int* y, const int* x, int n);
int CalcArraySquaresCpp(int* y, const int* x, int n)
{
int sum = 0;
for (int i = 0; i < n; i++)
{
y[i] = x[i] * x[i];
sum += y[i];
}
return sum;
}
int main(int argc, char* argv[])
{
int x[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
const int n = sizeof(x) / sizeof(int);
int y1[n];
int y2[n];
int sum_y1 = CalcArraySquaresCpp(y1, x, n);
int sum_y2 = CalcArraySquares(y2, x, n);
for (int i = 0; i < n; i++)
printf("i: %2d x: %4d y1: %4d y2: %4d\n", i, x[i], y1[i], y2[i]);
printf("\n");
printf("sum_y1: %d\n", sum_y1);
printf("sum_y2: %d\n", sum_y2);
return 0;
}
calcarraysquares.asm
; Name: calcarraysquares.asm
;
; Build: g++ -m32 -c main.cpp -o main.o
; nasm -f elf32 -o calcarraysquares.o calcarraysquares.asm
; g++ -m32 -o calcarraysquares calcarraysquares.o main.o
;
; Source: Modern x86 Assembly Language Programming p.56
global CalcArraySquares
section .text
; extern "C" int CalcArraySquares(int* y, const int* x, int n);
;
; Description: This function cComputes y[i] = x[i] * x[i].
;
; Returns: Sum of the elements in array y.
%define y [ebp+8]
%define x [ebp+12]
%define n [ebp+16]
CalcArraySquares:
push ebp
mov ebp,esp
push ebx
push esi
push edi
; Load arguments
mov edi,y ;edi = 'y'
mov esi,x ;esi = 'x'
mov ecx,n ;ecx = 'n'
; Initialize array sum register, calculate size of array in bytes,
; and initialize element offset register.
xor eax,eax ;eax = sum of 'y' array
cmp ecx,0
jle .emptyArray
shl ecx,2 ;ecx = size of array in bytes
xor ebx,ebx ;ebx = array element offset
; Repeat loop until finished
.l1:
mov edx,[esi+ebx] ;load next x[i]
imul edx,edx ;compute x[i] * x[i]
mov [edi+ebx],edx ;save result to y[i]
add eax,edx ;update running sum
add ebx,4 ;update array element offset
cmp ebx,ecx
jl .l1 ;jump if not finished
.emptyArray:
pop edi
pop esi
pop ebx
pop ebp
ret
build
g++ -m32 -c main.cpp -o main.o
nasm -f elf32 -o calcarraysquares.o calcarraysquares.asm
g++ -m32 -o calcarraysquares calcarraysquares.o main.o