I'm just trying to get this all working right, it's code provided to us by our professor. He gave us some basic instructions (make sure the function name has an underscore for it, use -f macho) for doing this outside of linux but basically left us on our own an I can't get it to work
Here's my command line inputs and the error message I get, how do I get everything to work nicely so the program behaves normally when I ./vecsum ? The same thing occurs when I use the provided makefile (using -macho), but for simplicity I won't include that.
Code:
Evan-Daviss-iMac:lab8 Evan$ g++ -c -o main.o main.cpp
Evan-Daviss-iMac:lab8 Evan$ nasm -f macho -o vecsum.o vecsum.s
Evan-Daviss-iMac:lab8 Evan$ g++ -o vecsum main.o vecsum.o
ld: warning: in vecsum.o, file is not of required architecture
Undefined symbols:
"_vecsum", referenced from:
_main in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
And here's the code:
vecsum.s
Code:
; vecsum.s
;
; Author : Adam Ferrari
; Date : Jan 29, 1998
; Purpose : This file contains the implementation of the function
; vecsum, which adds up a vector of integers.
; Modified for NASM by Aaron Bloomfield on 9 Nov 2007
global _vecsum
section .text
;
; vecsum
; Parameter 1 - the starting address of a sequence of 32-bit integers.
; Parameter 2 - the number of integers in the sequence.
; Return value - the sum of the integers in the sequence.
;
_vecsum:
; Standard prologue
push ebp ; Save the old base pointer
mov ebp, esp ; Set new value of the base pointer
push esi ; Save registers
xor eax, eax ; Place zero in EAX. We will keep a running
; sum of the vector elements in EAX.
mov esi, [ebp+8] ; Put the vector starting address in ESI.
mov ecx, [ebp+12] ; Put the vector size in ECX. We will use
; ECX to indicate how many vector elements
; are left to add into the sum.
cmp ecx, 0 ; If there are not more than zero elemen
jle vecsum_done ; in the array, skip to the end and return
; zero (already in EAX).
vecsum_loop:
mov edx, [esi] ; Put the current vector element into EDX.
add eax, edx ; Add the current vector element into the
; running sum.
add esi, 4 ; Increment ESI to point to the next
; vector element (4 bytes away).
dec ecx ; Decrement ECX, the counter of how many
; left to do.
cmp ecx, 0 ; If there are more than zero elements
jg vecsum_loop ; left to add up, then do the loop again.
vecsum_done:
; At this point, the loop is done, and we have the sum of the
; vector elements in EAX, which is exactly where we want the
; return value to be.
; Standard epilogue
pop esi ; Restore registers that we used.
; Note - no local variables to dealocate.
pop ebp ; Restore the caller's base pointer.
ret ; Return to the caller.
main.cpp
Code:
// main.cpp
#include <iostream>
#include <time.h>
#include <cstdlib>
using namespace std;
extern "C" int vecsum (int *, int);
// Purpose: This main program produces a vector of random
// numbers between 0 and 99, then calls the
// externally defined function 'vecsum' to add
// up the elements of the vector.
// Author: Adam Ferrari
int main () {
int n, *vec, sum;
cout << "Please enter a vector size: ";
cin >> n;
// sanity check the vector size
if (n <= 0) {
cerr << "Vector size must be greater than zero.\n";
return 1;
}
// allocate, initialize, and display vector
vec = new int[n];
// use current time as random seed
srand((unsigned) time(NULL));
for (int i = 0; i < n; ++i) {
vec[i] = rand() % 100;
cout << "\tvec[" << i << "] = " << vec[i] << endl;
}
// sum up the vector and print out results
sum = vecsum(vec, n);
cout << "The sum of all vector elements is " << sum << endl;
return 0;
}