Page 1 of 1

x86 Assembly in Linux and Mac OS X

Posted: Mon Aug 06, 2007 9:38 am
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.

Posted: Wed Aug 08, 2007 12:50 am
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.

Posted: Wed Aug 08, 2007 9:17 am
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.)

Posted: Wed Aug 08, 2007 10:03 am
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

Posted: Wed Aug 08, 2007 3:17 pm
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

Posted: Wed Aug 08, 2007 3:50 pm
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..

Posted: Wed Aug 08, 2007 3:54 pm
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)