
-----------------------------------
DtY
Wed Feb 17, 2010 8:44 pm

Linking C &amp; 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
#include 

int main(void) {
    int ret_status;
    ret_status = asm_main();
    return ret_status;
}
first.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:
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[/code]

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

-----------------------------------
DtY
Thu Feb 18, 2010 4:22 pm

RE:Linking C &amp; 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
; 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 
