0

I'm learning the basics of x86 via this free book.

Keep in mind this is specific to macOS x86 compared to Linux x86.

Its made for GNU Linux, so I have to change some of the code which is probably where I went wrong. I took this code snippet:

.section .data
 .section .text
 .globl _start
_start:
movl $1, %eax
movl $0, %ebx
int $0x80

After a bit of googling about x86 on macOS I turned that bit of code into this:

    .data
    .text
    .globl _main
_main:
    movl $1, %eax
    movl $0, %ebx
    int $0x80

I compiled this using gcc test.s which compiles it into a.out. When trying to run it using ./a.out I get the error [1] 17301 illegal hardware instruction ./a.out.

Any help is appreciated, thanks!

perryprog
  • 111
  • 2
  • 13
  • 5
    The osx system call convention is different, including the syscall numbers. You should just grab an osx example instead. – Jester Jan 03 '17 at 20:34
  • @Jester OK, I'll check that out. Do you know any good resources? EDIT: Nvm, I found this site: http://www.idryman.org/blog/2014/12/02/writing-64-bit-assembly-on-mac-os-x/ Do you want to answer or should I? – perryprog Jan 03 '17 at 20:35
  • 2
    Consider calling into the libc to do system calls instead of executing an interrupt. That's more portable (you can possibly run the same assembly platform on many Unices) and easier to program. – fuz Jan 03 '17 at 21:04
  • @fuz Yeah, probably. This was my first x86 program so I wanted to with something "basic". – perryprog Jan 03 '17 at 21:55
  • Actually, calling into the libc is much much simpler than doing system calls yourself. No magic numbers to remember and no hassle understanding the difference between the system call and the C function. – fuz Jan 03 '17 at 22:39
  • 1
    Possible duplicate of [assembly language in os x](http://stackoverflow.com/questions/6990885/assembly-language-in-os-x) – Joshua Jan 04 '17 at 05:25
  • @Joshua I think that's different. I already saw that question and it didn't solve my problem. This would be helpful to people who don't realize that x86 on a Mac is different than Linux. If anyone agrees, I'll accept it as a dupe. – perryprog Jan 04 '17 at 13:24
  • Everyone wants to do something "basic", the problem is they think "basic" is calling a system function to do something like generate on-screen output. There is nothing basic about that. – Cody Gray - on strike Jan 04 '17 at 16:10
  • 1
    @Cody Gray: Calling a system function is basic. Calling a system function for the wrong OS doesn't work. – Joshua Jan 04 '17 at 16:18
  • 1
    Something that is OS-dependent, rather poorly documented, and requires understanding of system-level programming that has nothing in particular to do with the assembly code you're trying to write (or what is documented in Intel's manuals or any other assembly language guide) is not what I would describe as "basic". Perhaps your definition differs. – Cody Gray - on strike Jan 04 '17 at 16:23
  • OK, get rid of the word "basic". I just want to learn x86 for macOS. – perryprog Jan 04 '17 at 17:16

1 Answers1

-1

@Jester helped me out. You can view the comment on my question, but basically call convention is different for macOS. I found this resource which helped me out.

perryprog
  • 111
  • 2
  • 13