Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

nasm in c - trouble writing c functions in nasm

Posted on 2003-12-06
11
390 Views
Last Modified: 2012-08-13
This is the code:

;;printRecords.asm
bits 32
extern printf

segment .data
   RSIZE db 12
   INTSIZE db 4
   CHARSIZE db 1

segment .text
   global _printRecords

_printRecords:
   pusha
   mov EBP,ESP
   
   mov EDX,[EBP+8]      ; *R
   mov ECX,[EBP+12]     ; num_recs
   dec ECX              ; loop prep
   mov EBX,[EBP+16]     ; sizeof(R)
   xor EAX,EAX

loop_start:
   cmp EAX,ECX
   je loop_end
   push EDX
   call printf
   add ESP,4
   add EDX,EBX
   jmp loop_end

loop_end:
   mov ESP,EBP
   popa
   ret

;;record_cl.h
/*
// record_cl.h
// A definition of the Record class
// John Stewart
*/

#include <iostream>
#include <string>
using namespace std;

#ifndef RECORD_CL_H
#define RECORD_CL_H

class Record {
   public:
      int Id;
      int Age;
      int Sex;
};

#endif

;;record_search.cpp
/*
record_search.cpp
A command-line program to search Records
John Stewart
Last modified: 12/3/2003
*/

#include <iostream>
#include <cstdio>
#include <string>
#include <fstream>
#include "record_cl.h"
using namespace std;

int numRecords (string);
void loadData (string, int, Record []);
void printRecords (Record [], int, int);
bool searchRecord (Record [], int, int);

int main () {
   string fn = "rec_data.txt";
   int num_recs = numRecords(fn);
   int s_age = 0;
   bool found_one = false;

   Record *R;
   Record *S;
   R = new Record[num_recs];
   loadData(fn, num_recs, R);
   printRecords (R, num_recs, sizeof(S));

   cout << "Enter search age: ";
   cin >> s_age;
   found_one = searchRecord(R, num_recs, s_age);

   delete [] R;
}

int numRecords (string fil) {
   ifstream file("rec_data.txt");
   int num;
   file >> num;
   file.close();
}

void loadData (string fn, int num, Record* rec) {
   ifstream file("rec_data.txt");
   file >> rec[0].Id;
   
   for (int i = 0; i < num; i++) {
      file >> rec[i].Id;
      file >> rec[i].Age;
      file >> rec[i].Sex;
   }

   file.close();
}

;;searchRecord.asm
bits 32
extern printf

segment .data
   RSIZ db 12
   NFOUND db "No match found",0
   FOUND db "User ID: %d",0xA,"Age: %d","Sex: %s",0xA,0xA,0

segment .text
   global _searchRecord

_searchRecord:
   pusha
   mov EBP,ESP

   mov ESI,[EBP+8]         ; *R
   mov EDX,[ESI]
   xor EAX,EAX

loop_start:
   cmp EAX,[EBP+12]        ; [EBP+12] = num_recs
   je loop_end
   add EDX,4
   cmp EDX,[EBP+16]      ; [EBP+16] = s_age
   je print_it
r: add EDX,RSIZ
   sub EDX,4

   inc EAX
   jmp loop_start

print_it:
   push dword [EDX]
   push dword [EDX+4]
   push dword [EDX+8]
   push FOUND
   call printf
   add ESP,16
   jmp r

loop_end:
   mov ESP,EBP
   popa
   mov EAX,1
   ret

;;Makefile
record_search: searchRecord.o printRecords.o record_search.o
      g++ -o record_search searchRecord.o printRecords.o record_search.o
record_search.o: record_search.cpp
      g++ -c record_search.cpp
searchRecord.o: searchRecord.asm
      nasm -f elf searchRecord.asm
printRecords.o: printRecords.asm
      nasm -f elf printRecords.asm
clean:
      rm -f *~
      rm -f *.o
      rm -f record_search
all:
      make clean
      make

Here is the linker error:
record_search.o(.text+0x148): In function `main':
: undefined reference to `printRecords(Record*, int, int)'
record_search.o(.text+0x185): In function `main':
: undefined reference to `searchRecord(Record*, int, int)'
collect2: ld returned 1 exit status
make[1]: *** [record_search] Error 1
make[1]: Leaving directory `/home/stewart/project2'
make: *** [all] Error 2

I know there are other bugs, but this one eludes me.  I prototyped the functions, and coded them in assembly, and compiled the asm before the .c, and no go.  Why is this error being generated?
0
Comment
Question by:stubuf612
  • 5
  • 3
  • 3
11 Comments
 
LVL 22

Expert Comment

by:cookre
ID: 9888987
I'm not familiar with g++ or NASM, but it looks like you're declaring the entry point _printRecords but referring to printRecords.  Does g++ prepend the _?

same for _searchRecord
0
 

Author Comment

by:stubuf612
ID: 9889469
Nope, same compile error.
0
 
LVL 22

Expert Comment

by:cookre
ID: 9889649
extern void printRecords (Record [], int, int);
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:stubuf612
ID: 9890289
nope.... same error
0
 
LVL 22

Expert Comment

by:cookre
ID: 9890345
Given my lack of familiarity with these, anything else is pure speculation.  

All I can think of now is bypassing make and doing a manual link (make generating a flakey link?, yeah, sure)
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9891799
>: undefined reference to `printRecords(Record*, int, int)'
>record_search.o(.text+0x185): In function `main':
>: undefined reference to `searchRecord(Record*, int, int)'
>collect2: ld returned 1 exit status

Linker was not able to locate definitions for the above mentioned functions .. i.e. while you have declared the functions, they have not been defined
0
 

Author Comment

by:stubuf612
ID: 9892075
well.... no... because I defined them in printRecords.asm and searchRecord.asm!!!
0
 

Author Comment

by:stubuf612
ID: 9892078
perhaps incorrectly, but still, they are defined in those asm files (the whole point)
0
 

Author Comment

by:stubuf612
ID: 9896562
Ah.... I've discovered the answer:

 extern "C" void printRecords (Record [], int, int);

That language type specification was the trick, but now a segmentation fault... argh!
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 500 total points
ID: 9896567
run it through gdb
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9911049
If you wish to, you can post in community support and seek refund for this question.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Hide vba in gp 7 104
What is wrong with the below insert statement. Getting error when executing. 5 45
Selenium and Interactive Data Language 3 42
Cygwin - GNU GPL License 1 37
I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question