x86 Assembly in Linux and Mac OS X

Programming, for all ages and all languages.
Post Reply
no_one
Posts: 17
Joined: Sat Jul 14, 2007 9:47 am

x86 Assembly in Linux and Mac OS X

Post by no_one »

I wrote an assembly program which just substracts 5 from 7. I assembled this in Ubuntu:

Code: Select all

.section .text
.globl _start
_start:
	pushl $5
	pushl $7
	call  sub
	movl  %eax, %ebx
	movl  $1, %eax
	int   $0x80
sub:
	pushl %ebp
	movl  %esp, %ebp
	movl  8(%ebp), %eax
	movl  12(%ebp), %ebx
	subl  %ebx, %eax
	movl  %ebp, %esp
	popl  %ebp
	ret
And returned 2, the correct answer. Then, I assembled this in Mac OS X:

Code: Select all

.text
.globl _start
_start:
	pushl $5
	pushl $7
	call  sub
	movl  %eax, %ebx
	movl  $1, %eax
	int   $0x80
sub:
	pushl %ebp
	movl  %esp, %ebp
	movl  8(%ebp), %eax
	movl  12(%ebp), %ebx
	subl  %ebx, %eax
	movl  %ebp, %esp
	popl  %ebp
	ret
And got 5. What's going on here? The only thing that changes is the assembler directive.
User avatar
Colonel Kernel
Member
Member
Posts: 1437
Joined: Tue Oct 17, 2006 6:06 pm
Location: Vancouver, BC, Canada
Contact:

Post by Colonel Kernel »

int 80 invokes a system call on Linux, right? I'm assuming that's how you display the answer. I'm guessing int 80 does something quite different and unexpected on Mac OS X.
Top three reasons why my OS project died:
  1. Too much overtime at work
  2. Got married
  3. My brain got stuck in an infinite loop while trying to design the memory manager
Don't let this happen to you!
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Post by Alboin »

Mac supports BSD system calls, no? (I'm not 100% sure on this.) If so, 0x80 is he right interrupt, the only thing is you're passing the arguments incorrectly. (BSD pushes it's arguments. Linux puts them in registers.)
C8H10N4O2 | #446691 | Trust the nodes.
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post by Brynet-Inc »

Alboin is correct, UNIX follows a different convention for system calls.. Linux does it the DOS way ;)

IIRC Mac OSX follows the UNIX convention.. So you can find more information about this below.

http://www.freebsd.org/doc/en_US.ISO885 ... calls.html

But I believe this tutorial will be a little more helpful for you... (Although it's using NASM unfortunately..)

http://untimedcode.com/2007/5/20/learn- ... n-mac-os-x
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

Brynet-Inc wrote:But I believe this tutorial will be a little more helpful for you... (Although it's using NASM unfortunately..)

http://untimedcode.com/2007/5/20/learn- ... n-mac-os-x
Intel syntax isn't that hard to convert into AT&T syntax (I've done it for all the assembly code in Bran's tutorial, which isn't really an achievement I guess).


But I do have to say this - Brynet, I seem to remember you being an avid supporter of this small line of code...

Code: Select all

.intel_syntax noprefix
mov eax,5
OR the -mintel-syntax command-line switch.

:D
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post by Brynet-Inc »

I think you misinterpreted my comment.. I was simply stating that unfortunately it uses NASM 8)

I don't like using Intel Syntax personally, but yes I have in the past stated it's supported by GAS..
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

Brynet-Inc wrote:yes I have in the past stated it's supported by GAS..
I messed up trying to say that, basically.
I was simply stating that it unfortunately uses NASM
Ok, I said all that because I assumed that you were making this comment in relation to the given link, which uses Intel syntax, and hence I showed how to use Intel syntax in GAS (which the thread starter appears to be using).

In other words, I completely misinterpreted your post 8)
Post Reply