Welcome to Soft32 Forums!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Intel MAC assembly examples

 
   Soft32 Home -> Mac -> Programmer Help RSS
Next:  Mount a Network Drive for Time Machine?  
Author Message
Aaron Spiteri

External


Since: Jan 04, 2009
Posts: 7



(Msg. 1) Posted: Mon Apr 06, 2009 9:20 am
Post subject: Intel MAC assembly examples
Archived from groups: comp>sys>mac>programmer>help (more info?)

Does anyone know where I can find some examples of assembly programs in
'as' for intel OSX
--
.....
"When asked what he would like on his tomb stone he said more bass"
Back to top
Login to vote
Reinder Verlinde

External


Since: Jul 23, 2007
Posts: 258



(Msg. 2) Posted: Tue Apr 07, 2009 3:20 pm
Post subject: Re: Intel MAC assembly examples [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article <49d9f0a6$0$20975$afc38c87@news.optusnet.com.au>,
Aaron Spiteri <azzmosphere(at)gmail.com> wrote:

> Does anyone know where I can find some examples of assembly programs in
> 'as' for intel OSX

Take any C or C++ source file, and do:

gcc -S filename.c

This produces a filename.s file containing the as translation of
filename.c

Reinder
Back to top
Login to vote
Aaron Spiteri

External


Since: Jan 04, 2009
Posts: 7



(Msg. 3) Posted: Wed Apr 08, 2009 9:20 am
Post subject: Re: Intel MAC assembly examples [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 2009-04-08 04:29:54 +1000, Reinder Verlinde <reinder RemoveThis @verlinde.invalid> said:

> In article <49d9f0a6$0$20975$afc38c87@news.optusnet.com.au>,
> Aaron Spiteri <azzmosphere(at)gmail.com> wrote:
>
>> Does anyone know where I can find some examples of assembly programs in
>> 'as' for intel OSX
>
> Take any C or C++ source file, and do:
>
> gcc -S filename.c
>
> This produces a filename.s file containing the as translation of
> filename.c
>
> Reinder

Thanks for that gave it a try with a hello world program and I am
reviewing the output now.
--
.....
"When asked what he would like on his tomb stone he said more bass"
Back to top
Login to vote
Aaron Spiteri

External


Since: Jan 04, 2009
Posts: 7



(Msg. 4) Posted: Wed Apr 08, 2009 9:20 am
Post subject: Re: Intel MAC assembly examples [Login to view extended thread Info.]
Archived from groups: comp>sys>mac>programmer>help, others (more info?)

On 2009-04-08 22:41:17 +1000, Aaron Spiteri <azzmosphere(at)gmail.com> said:

> On 2009-04-08 04:29:54 +1000, Reinder Verlinde <reinder.TakeThisOut@verlinde.invalid> said:
>
>> In article <49d9f0a6$0$20975$afc38c87@news.optusnet.com.au>,
>> Aaron Spiteri <azzmosphere(at)gmail.com> wrote:
>>
>>> Does anyone know where I can find some examples of assembly programs in
>>> 'as' for intel OSX
>>
>> Take any C or C++ source file, and do:
>>
>> gcc -S filename.c
>>
>> This produces a filename.s file containing the as translation of
>> filename.c
>>
>> Reinder
>
> Thanks for that gave it a try with a hello world program and I am
> reviewing the output now.

I wrote a small C program "Hello World" and tested that it compiled.
Produced the ASM output and got the following:

.cstring
LC0:
.ascii "Hello World\0"
.text
..globl _main
_main:
pushl %ebp
movl %esp, %ebp
pushl %ebx
subl $20, %esp
call ___i686.get_pc_thunk.bx
"L00000000001$pb":
leal LC0-"L00000000001$pb"(%ebx), %eax
movl %eax, (%esp)
call L_puts$stub
movl $0, %eax
addl $20, %esp
popl %ebx
popl %ebp
ret
.section
__IMPORT,__jump_table,symbol_stubs,self_modifying_code+pure_instructions,5
L_puts$stub:


.indirect_symbol _puts
hlt ; hlt ; hlt ; hlt ; hlt
.subsections_via_symbols
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
..weak_definition ___i686.get_pc_thunk.bx
..private_extern ___i686.get_pc_thunk.bx
___i686.get_pc_thunk.bx:
movl (%esp), %ebx
ret

All though the program compiled to a executable, When I tried to link
it using the following command:

as -o hello.o hello.s
ld -o hello hello.o

I keep getting the message:

ld: Undefined symbols:
_puts

I imagine that this has something to do with the line:

L_puts$stub:

but can not understand why it compiles using gcc but not when ld is
call directly. Any idea's would be greatly appreciated.
--
.....
"When asked what he would like on his tomb stone he said more bass"
Back to top
Login to vote
Aaron Spiteri

External


Since: Jan 04, 2009
Posts: 7



(Msg. 5) Posted: Wed Apr 08, 2009 9:20 am
Post subject: Re: Intel MAC assembly examples [Login to view extended thread Info.]
Archived from groups: comp>sys>mac>programmer>help (more info?)

On 2009-04-08 22:41:17 +1000, Aaron Spiteri <azzmosphere(at)gmail.com> said:

> On 2009-04-08 04:29:54 +1000, Reinder Verlinde <reinder RemoveThis @verlinde.invalid> said:
>
>> In article <49d9f0a6$0$20975$afc38c87@news.optusnet.com.au>,
>> Aaron Spiteri <azzmosphere(at)gmail.com> wrote:
>>
>>> Does anyone know where I can find some examples of assembly programs in
>>> 'as' for intel OSX
>>
>> Take any C or C++ source file, and do:
>>
>> gcc -S filename.c
>>
>> This produces a filename.s file containing the as translation of
>> filename.c
>>
>> Reinder
>
> Thanks for that gave it a try with a hello world program and I am
> reviewing the output now.

I have tried your suggestion and all worked well but when I went to
compile the ASM code into a executable kept on getting the message:

ld: Undefined symbols:
_puts


I was using the following commands:

as -o hello.o hello.s
ld -o hello hello.o

The code compiled correctly under C

gcc -o hello hello.c

Here is a copy:

.cstring
LC0:
.ascii "Hello World\0"
.text
..globl _main
_main:
pushl %ebp
movl %esp, %ebp
pushl %ebx
subl $20, %esp
call ___i686.get_pc_thunk.bx
"L00000000001$pb":
leal LC0-"L00000000001$pb"(%ebx), %eax
movl %eax, (%esp)
call L_puts$stub
movl $0, %eax
addl $20, %esp
popl %ebx
popl %ebp
ret
.section
__IMPORT,__jump_table,symbol_stubs,self_modifying_code+pure_instructions,5
L_puts$stub:


.indirect_symbol _puts
hlt ; hlt ; hlt ; hlt ; hlt
.subsections_via_symbols
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
..weak_definition ___i686.get_pc_thunk.bx
..private_extern ___i686.get_pc_thunk.bx
___i686.get_pc_thunk.bx:
movl (%esp), %ebx
ret

I am assuming that it has something to do with the puts function but
can not figure out what. My assumption would be this is the very same
assembly that C is getting translated into. Should I be including
anything or am I missing some sort of critical option?

--
.....
"When asked what he would like on his tomb stone he said more bass"
Back to top
Login to vote
Andrew Gabriel

External


Since: Apr 08, 2009
Posts: 1



(Msg. 6) Posted: Wed Apr 08, 2009 11:20 am
Post subject: Re: Intel MAC assembly examples [Login to view extended thread Info.]
Archived from groups: comp>sys>mac>programmer>help, others (more info?)

In article <49dc9e60$0$12942$afc38c87@news.optusnet.com.au>,
Aaron Spiteri <azzmosphere(at)gmail.com> writes:
>
> All though the program compiled to a executable, When I tried to link
> it using the following command:
>
> as -o hello.o hello.s
> ld -o hello hello.o
>
> I keep getting the message:
>
> ld: Undefined symbols:
> _puts
>
> I imagine that this has something to do with the line:
>
> L_puts$stub:
>
> but can not understand why it compiles using gcc but not when ld is
> call directly. Any idea's would be greatly appreciated.

Probably because you aren't calling ld the way gcc would.
For example, you started with C, so you probably need the
C library for puts(), but ld won't know that.

Try: ld -o hello -lc hello.o

Alternatively, use the gcc command to link it, and it will
call ld with the correct parameters.

--
Andrew Gabriel
[email address is not usable -- followup in the newsgroup]
Back to top
Login to vote
George Peter Staplin

External


Since: Apr 08, 2009
Posts: 1



(Msg. 7) Posted: Wed Apr 08, 2009 8:30 pm
Post subject: Re: Intel MAC assembly examples [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Aaron Spiteri <azzmosphere(at)gmail.com> wrote:

> On 2009-04-08 22:41:17 +1000, Aaron Spiteri <azzmosphere(at)gmail.com>
> said:
>
>> On 2009-04-08 04:29:54 +1000, Reinder Verlinde <reinder.DeleteThis@verlinde.invalid>
>> said:
>>
>>> In article <49d9f0a6$0$20975$afc38c87@news.optusnet.com.au>,
>>> Aaron Spiteri <azzmosphere(at)gmail.com> wrote:
>>>
>>>> Does anyone know where I can find some examples of assembly programs in
>>>> 'as' for intel OSX
> I wrote a small C program "Hello World" and tested that it compiled.
> Produced the ASM output and got the following:
>
> .cstring
> LC0:
> .ascii "Hello World\0"
> .text
> .globl _main
> _main:
> pushl %ebp
> movl %esp, %ebp
> pushl %ebx
> subl $20, %esp
> call ___i686.get_pc_thunk.bx
> "L00000000001$pb":
> leal LC0-"L00000000001$pb"(%ebx), %eax
> movl %eax, (%esp)
> call L_puts$stub
> movl $0, %eax
> addl $20, %esp
> popl %ebx
> popl %ebp
> ret
> .section
> __IMPORT,__jump_table,symbol_stubs,self_modifying_code+pure_instructions,5
> L_puts$stub:
>
>
> .indirect_symbol _puts
> hlt ; hlt ; hlt ; hlt ; hlt
> .subsections_via_symbols
> .section __TEXT,__textcoal_nt,coalesced,pure_instructions
> .weak_definition ___i686.get_pc_thunk.bx
> .private_extern ___i686.get_pc_thunk.bx
> ___i686.get_pc_thunk.bx:
> movl (%esp), %ebx
> ret
>
> All though the program compiled to a executable, When I tried to link
> it using the following command:
>
> as -o hello.o hello.s
> ld -o hello hello.o
>
> I keep getting the message:
>
> ld: Undefined symbols:
> _puts
>
> I imagine that this has something to do with the line:
>
> L_puts$stub:
>
> but can not understand why it compiles using gcc but not when ld is
> call directly. Any idea's would be greatly appreciated.


Try running gcc -v hello.s

That will tell you how gcc is using the linker. You're probably also
missing the crt*.o files (C runtime/startup files). The crt*.o files are
very important if you want to use shared libraries AFAIK, and generally
have the program startup properly. This applies to most unix-like systems
as well. You may not need all of them though. If you just want your start
symbol to be called, and would prefer to not have a .globl main entry
point, you could probably get away with much less.

Also, this is extremely useful when analyzing assembly code:

gcc -S -fverbose-asm file.c

I've been porting a compiler I wrote that generates assembly code. It runs
on another unix-like system now, so I've been spending my spare time
porting it to the Mac. I don't have much spare time though, so things are
slowly progressing. I managed to get some code running, but some syscalls
are failing, because of alignment differences.

A few things I have noticed that are different:

1. there is no .global alias, just .globl
2. there is no .section .rodata, use .cstring
3. C API functions have a _ prefix
4. .equ doesn't seem to work in the Mac assembler (I used a Tcl script for a
sort of scripted assembly instead for my runtime library to work around
some things I couldn't sanely do with cpp macros).
5. sections in general are very different (as your gcc -S output
demonstrated).

I think that's about it.

By the way, there is an official Mac guide to assembly programming online.

-George
Back to top
Login to vote
Aaron Spiteri

External


Since: Jan 04, 2009
Posts: 7



(Msg. 8) Posted: Fri Apr 10, 2009 7:20 am
Post subject: Re: Intel MAC assembly examples [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 2009-04-09 00:01:53 +1000, andrew DeleteThis @cucumber.demon.co.uk (Andrew
Gabriel) said:

> In article <49dc9e60$0$12942$afc38c87@news.optusnet.com.au>,
> Aaron Spiteri <azzmosphere(at)gmail.com> writes:
>>
>> All though the program compiled to a executable, When I tried to link
>> it using the following command:
>>
>> as -o hello.o hello.s
>> ld -o hello hello.o
>>
>> I keep getting the message:
>>
>> ld: Undefined symbols:
>> _puts
>>
>> I imagine that this has something to do with the line:
>>
>> L_puts$stub:
>>
>> but can not understand why it compiles using gcc but not when ld is
>> call directly. Any idea's would be greatly appreciated.
>
> Probably because you aren't calling ld the way gcc would.
> For example, you started with C, so you probably need the
> C library for puts(), but ld won't know that.
>
> Try: ld -o hello -lc hello.o
>
> Alternatively, use the gcc command to link it, and it will
> call ld with the correct parameters.

This seems on the right track. I got an executable but when I try
running it I get an illegal instrcution. I think that it may be the
wrong library that I am using.
--
.....
"When asked what he would like on his tomb stone he said more bass"
Back to top
Login to vote
Aaron Spiteri

External


Since: Jan 04, 2009
Posts: 7



(Msg. 9) Posted: Fri Apr 10, 2009 7:20 am
Post subject: Re: Intel MAC assembly examples [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 2009-04-09 12:30:30 +1000, George Peter Staplin
<georgeps.DeleteThis@xmission.com> said:

> Aaron Spiteri <azzmosphere(at)gmail.com> wrote:
>
>> On 2009-04-08 22:41:17 +1000, Aaron Spiteri <azzmosphere(at)gmail.com>
>> said:
>>
>>> On 2009-04-08 04:29:54 +1000, Reinder Verlinde <reinder.DeleteThis@verlinde.invalid>
>>> said:
>>>
>>>> In article <49d9f0a6$0$20975$afc38c87@news.optusnet.com.au>,
>>>> Aaron Spiteri <azzmosphere(at)gmail.com> wrote:
>>>>
>>>>> Does anyone know where I can find some examples of assembly programs in
>>>>> 'as' for intel OSX
>> I wrote a small C program "Hello World" and tested that it compiled.
>> Produced the ASM output and got the following:
>>
>> .cstring
>> LC0:
>> .ascii "Hello World\0"
>> .text
>> .globl _main
>> _main:
>> pushl %ebp
>> movl %esp, %ebp
>> pushl %ebx
>> subl $20, %esp
>> call ___i686.get_pc_thunk.bx
>> "L00000000001$pb":
>> leal LC0-"L00000000001$pb"(%ebx), %eax
>> movl %eax, (%esp)
>> call L_puts$stub
>> movl $0, %eax
>> addl $20, %esp
>> popl %ebx
>> popl %ebp
>> ret
>> .section
>> __IMPORT,__jump_table,symbol_stubs,self_modifying_code+pure_instructions,5
>> L_puts$stub:
>>
>>
>> .indirect_symbol _puts
>> hlt ; hlt ; hlt ; hlt ; hlt
>> .subsections_via_symbols
>> .section __TEXT,__textcoal_nt,coalesced,pure_instructions
>> .weak_definition ___i686.get_pc_thunk.bx
>> .private_extern ___i686.get_pc_thunk.bx
>> ___i686.get_pc_thunk.bx:
>> movl (%esp), %ebx
>> ret
>>
>> All though the program compiled to a executable, When I tried to link
>> it using the following command:
>>
>> as -o hello.o hello.s
>> ld -o hello hello.o
>>
>> I keep getting the message:
>>
>> ld: Undefined symbols:
>> _puts
>>
>> I imagine that this has something to do with the line:
>>
>> L_puts$stub:
>>
>> but can not understand why it compiles using gcc but not when ld is
>> call directly. Any idea's would be greatly appreciated.
>
>
> Try running gcc -v hello.s

I got the following output:
Using built-in specs.
Target: i686-apple-darwin8
Configured with: /private/var/tmp/gcc/gcc-5250.obj~12/src/configure
--disable-checking -enable-werror --prefix=/usr --mandir=/share/man
--enable-languages=c,objc,c++,obj-c++
--program-transform-name=/^[cg][^.-]*$/s/$/-4.0/
--with-gxx-include-dir=/include/c++/4.0.0 --build=powerpc-apple-darwin8
--with-arch=pentium-m --with-tune=prescott --program-prefix=
--host=i686-apple-darwin8 --target=i686-apple-darwin8
Thread model: posix
gcc version 4.0.1 (Apple Computer, Inc. build 5250)
/usr/libexec/gcc/i686-apple-darwin8/4.0.1/cc1 -E -lang-asm -quiet -v
-D__DYNAMIC__ hello.s -fPIC -march=pentium-m -mtune=prescott -o
/var/tmp//cce0s7vu.s
ignoring nonexistent directory
"/usr/lib/gcc/i686-apple-darwin8/4.0.1/../../../../i686-apple-darwin8/include"
#include

"..." search starts here:
#include <...> search starts here:
/usr/local/include
/usr/lib/gcc/i686-apple-darwin8/4.0.1/include
/usr/include
/System/Library/Frameworks
/Library/Frameworks
End of search list.
as -arch i386 -force_cpusubtype_ALL -o /var/tmp//ccEhgz9s.o
/var/tmp//cce0s7vu.s
/usr/libexec/gcc/i686-apple-darwin8/4.0.1/collect2 -dynamic -arch i386
-weak_reference_mismatches non-weak -o a.out -lcrt1.o
/usr/lib/gcc/i686-apple-darwin8/4.0.1/crt2.o
-L/usr/lib/gcc/i686-apple-darwin8/4.0.1
-L/usr/lib/gcc/i686-apple-darwin8/4.0.1
-L/usr/lib/gcc/i686-apple-darwin8/4.0.1/../../.. /var/tmp//ccEhgz9s.o
-lgcc -lSystem

From this the most significant line appears to be

as -arch i386 -force_cpusubtype_ALL -o /var/tmp//ccEhgz9s.o
/var/tmp//cce0s7vu.s
/usr/libexec/gcc/i686-apple-darwin8/4.0.1/collect2 -dynamic -arch i386
-weak_reference_mismatches non-weak -o a.out -lcrt1.o
/usr/lib/gcc/i686-apple-darwin8/4.0.1/crt2.o
-L/usr/lib/gcc/i686-apple-darwin8/4.0.1
-L/usr/lib/gcc/i686-apple-darwin8/4.0.1
-L/usr/lib/gcc/i686-apple-darwin8/4.0.1/../../.. /var/tmp//ccEhgz9s.o
-lgcc -lSystem


>
> That will tell you how gcc is using the linker. You're probably also
> missing the crt*.o files (C runtime/startup files). The crt*.o files are
> very important if you want to use shared libraries AFAIK, and generally
> have the program startup properly. This applies to most unix-like systems
> as well. You may not need all of them though. If you just want your start
> symbol to be called, and would prefer to not have a .globl main entry
> point, you could probably get away with much less.
>
> Also, this is extremely useful when analyzing assembly code:
>
> gcc -S -fverbose-asm file.c
>
> I've been porting a compiler I wrote that generates assembly code. It runs
> on another unix-like system now, so I've been spending my spare time
> porting it to the Mac. I don't have much spare time though, so things are
> slowly progressing. I managed to get some code running, but some syscalls
> are failing, because of alignment differences.
>
> A few things I have noticed that are different:
>
> 1. there is no .global alias, just .globl
> 2. there is no .section .rodata, use .cstring
> 3. C API functions have a _ prefix
> 4. .equ doesn't seem to work in the Mac assembler (I used a Tcl script for a
> sort of scripted assembly instead for my runtime library to work around
> some things I couldn't sanely do with cpp macros).
> 5. sections in general are very different (as your gcc -S output
> demonstrated).
>
> I think that's about it.
>
> By the way, there is an official Mac guide to assembly programming online.
>
> -George


After trying your suggestion I noticed that the a.out file appears that
is the correct executable. I have a fair bit of reading ahead of me to
figure out why this is and exactly what is going on. Thank you very
much for your advice. I found the following URI:

http://developer.apple.com/DOCUMENTATION/DeveloperTools/Reference/Asse...er/010-

Is

this the online programming guide that you where refering too?

--
.....
"When asked what he would like on his tomb stone he said more bass"
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Intel Assembly on Mac - Hi, I have an assignment that needs to be finished by tomorrow, an assembly program for intel-architecture. It's quite...

Differences between AIX powerPC assembly and OS X assembly? - We are in the process of starting to port PopLog to OS X. PopLog is already available for AIX. As part of the port, we....

NSMutableArray examples? - I'm just starting with Objective C and Cocoa, and I'm looking for some simple examples of working with data in an..

NSMatrix examples? - Are there some good examples somewhere of how to use NSMatrix? "Learning Cocoa with Objective-C" doesn't cove...

is = { 0 } the only .c zero struct of Apple /Developer/Exa.. - I notice some .c of /Developer/Examples says = { 0 } to mean zero a struct. I notice gcc -Wall -W complains, "war...

Applescript support for Carbon application. Are there any .. - I'm writing a console application for Mac OS X and I would like to expose some of my application objects for..
       Soft32 Home -> Mac -> Programmer Help All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum

Categories:
 Windows
 Linux
  Mac
 PDA


[ Contact us | Terms of Service/Privacy Policy ]