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

extern "C" int CalcArraySum(const int* x, int n);

int CalcArraySumCpp(const int* x, int n)
{
	int sum = 0;
	
	for (int i = 0; i < n; i++)
		sum += *x++;
	
	return sum;
}

int main(int argc, char* argv[])
{
	int x[] = {1, 7, -3, 5, 2, 9, -6, 12};
	int n = sizeof(x) / sizeof(int);
	
	printf("Elements of x[]\n");
	for (int i = 0; i < n; i++)
		printf("%d ", x[i]);
	printf("\n\n");
	
	int sum1 = CalcArraySumCpp(x, n);
	int sum2 = CalcArraySum(x, n);
	
	printf("sum1: %d\n", sum1);
	printf("sum2: %d\n", sum2);
	return 0;
}
calcarraysum.asm
; Name:     calcarraysum.asm
;
; Build:    g++ -m32 -c main.cpp -o main.o
;           nasm -f elf32 -o calcarraysum.o calcarraysum.asm
;           g++ -m32 -o calcarraysum calcarraysum.o main.o
;
; Source:   Modern x86 Assembly Language Programming p.56

global  CalcArraySum

section .text

; extern "C" int CalcArraySum(const int* x, int n);
;
; Description:  This function sums the elements of a signed
;               integer array.

%define x    [ebp+8]
%define n    [ebp+12]

CalcArraySum:
    push    ebp
    mov     ebp,esp
    ; Load arguments and initialize sum
    mov     edx,x                   ;edx = 'x'
    mov     ecx,n                   ;ecx = 'n'
    xor     eax,eax                 ;eax = sum
    ; Make sure 'n' is greater than zero
    cmp     ecx,0
    jle     .invalidCount
    ; Calculate the array element sum
.l1:
    add     eax,[edx]               ;add next element to sum
    add     edx,4                   ;set pointer to next element
    dec     ecx                     ;adjust counter
    jnz     .l1                     ;repeat if not done
.invalidCount:
    pop     ebp
    ret
build
g++ -m32 -c main.cpp -o main.o
nasm -f elf32 -o calcarraysum.o calcarraysum.asm
g++ -m32 -o calcarraysum calcarraysum.o main.o