3

I am attempting to run the following code in terminal on my Mac:

.section, .data

format_string:
   .asciz "My favorite number is %d!"

number:
  .long 786

.section, .text
.globl main

main:
  pushl number
 pushl $format_string  
 call printf 
  addl $8, %esp

  pushl $0
  call exit

This code is in a file named favorite.s

I used the command "gcc favorite.s -m32" and am seeing the following message:

Undefined symbols for architecture i386:
  "_main", referenced from:
      start in crt1.10.6.o
  "exit", referenced from:
      main in ccUKdD8O.o
  "printf", referenced from:
      main in ccUKdD8O.o
ld: symbol(s) not found for architecture i386
collect2: ld returned 1 exit status

What am I doing wrong here? Thanks.

zProgrammer
  • 727
  • 4
  • 10
  • 22

1 Answers1

5

Symbols have an underscore prepended to them on Mac OS X. Add a _ in front of main, exit, and printf in your program and try again!

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • After adding that I am now getting an output that says "Illegal instruction: 4" :/ – zProgrammer Feb 13 '13 at 06:54
  • Sounds like a typo somewhere. Can you show your updated code? – Carl Norum Feb 13 '13 at 06:58
  • I tried a simpler program and it produces that same message: .section, .text .globl _main _main: # Pass one parameter to exit. pushl $27 call _exit – zProgrammer Feb 13 '13 at 07:00
  • Wait - at compile time or runtime? Are you running into this problem: http://stackoverflow.com/questions/10177038/illegal-instruction-4-shows-up-in-os-x-lion? – Carl Norum Feb 13 '13 at 07:03
  • It compiles. The message comes up when i do ./a.out. I tried adding that -mmacosx-version-min=10.6 when i did gcc favorite.s -m32, but it didn't change anything. – zProgrammer Feb 13 '13 at 07:09
  • Does the disassembly match your source? What happens when you step through in your debugger? I'm afraid I don't have a computer handy, so I can't test it out for you here. – Carl Norum Feb 13 '13 at 07:10