Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Linking C & ASM (Mac OS 10.6)
Index -> General Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
DtY




PostPosted: Wed Feb 17, 2010 8:44 pm   Post subject: Linking C & ASM (Mac OS 10.6)

I'm having trouble linking C and Assembly, I seem to be getting different target processors on both.

The files I'm using:
driver.c
C:
#include <stdio.h>

int main(void) {
    int ret_status;
    ret_status = asm_main();
    return ret_status;
}

first.asm
Asm:
; first.asm
; Asks for two integers, displays their sum
;
; To create executable:
;   nasm -f elf first.asm
;   gcc -o first first.o driver.c asm_io.o

%include "asm_io.inc"

; initialized data goes in the .data segment
segment .data
prompt1 db "Enter a number: ", 0
prompt2 db "Another: ", 0
outmsg1 db "You entered: ", 0
outmsg2 db " and ", 0
outmsg3 db ", the sum of both is ", 0

; uninitialized data goes in the .bss segment
segment .bss
input1 resd 1
input2 resd 1

; Code is put in the .text segment
segment .text
    global _asm_main
_asm_main:
(and the main body, which I can post, if it would help)

asm_io.inc
This is from the book I'm using: http://www.drpaulcarter.com/pcasm/

To compile:
Bash:
nasm -f elf first.asm #Gives no output
gcc -arch i386 -o first first.o driver.c asm_io.inc

Gives me:
code:
ld: warning: in first.o, file is not of required architecture
ld: warning: in asm_io.inc, file is not of required architecture
Undefined symbols:
  "_asm_main", referenced from:
      _main in ccetlbX9.o
ld: symbol(s) not found
collect2: ld returned 1 exit status


I'm pretty sure the problem is with the -arch flag, but I tried i386, i486 and i686, but none worked.

[edit] Whoops, have to assemble asm_io.inc
Problem persists though
Sponsor
Sponsor
Sponsor
sponsor
DtY




PostPosted: Thu Feb 18, 2010 4:22 pm   Post subject: RE:Linking C & ASM (Mac OS 10.6)

So apparently, there were two errors in here that I worked out today (1) Mac OS X doesn't use ELFs, it uses macho (2) asm_io.inc is not the actual library, it's just a header.

So now, it links fine, but I get a seg fault when I run it.

Here's the complete first.asm
asm:
; first.asm
; Asks for two integers, displays their sum
;
; To create executable:
;   nasm -f macho first.asm
;   gcc -o first first.o driver.c asm_io.o

%include "asm_io.inc"

; initialized data goes in the .data segment
segment .data
prompt1 db "Enter a number: ", 0
prompt2 db "Another: ", 0
outmsg1 db "You entered: ", 0
outmsg2 db " and ", 0
outmsg3 db ", the sum of both is ", 0

; uninitialized data goes in the .bss segment
segment .bss
input1 resd 1
input2 resd 1

; Code is put in the .text segment
segment .text
    global _asm_main
_asm_main:
    ; setup routine
    enter 0, 0
    pusha
    ; print out prompt
    mov eax, prompt1
    call print_string
   
    call read_int ; read integer
    mov [input1], eax ; store into input1

    mov eax, prompt2 ; second prompt
    call print_string

    call read_int
    mov [input2], eax

    mov eax, [input1] ; eax = *input1
    add eax, [input2] ; eax += *input2
    mov ebx, eax ; ebx = eax

    dump_regs 1
    dump_mem 2, outmsg1, 1

    ; Output
    mov eax, outmsg1
    call print_string
    mov eax, [input1]
    call print_int
    mov eax, outmsg2
    call print_string
    mov eax, [input2]
    call print_int
    mov eax, outmsg3
    call print_string
    mov eax, ebx
    call print_int
    call print_nl

    popa
    mov eax, 0
   
    leave
    ret
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: