Timestretch Logo Atom Visuals
 Home  Site News  Articles  Desktop Pictures  Erik's Artwork  Music  Software 

Site News
+ Articles
12 inch PowerBook G4 Review + Airport Extreme
Apple Vrs Dell Notebooks
Apple, Please Open up the iPod API!
Asm Programming Using Jmp/Call
- Assembler Programming for Intel and PPC on OSX
Building SpiderMonkey Javascript 1.6 on MacOS X
Canon ZR10 Quick Review
iPod Review... (2)
OS X & Linux Features Compared
PHP, MySQL, and Smarty Programming Intro
Spam Graph
Web Development with MySQL and PHP4 and MacOS X
Writing Secure PHP Applications
Desktop Pictures... (4)
Erik's Artwork
Music
Software... (4)

Search:
Site News:
· Asm Programming Using Jmp/Call
05/04/2008
· LidWakeOff: prevent Macs from Accidently Waking
12/09/2007
· Building SpiderMonkey Javascript 1.6 on MacOS X
05/26/2007
· Assembler Programming for Intel and PPC
05/14/2007
· Url2thumb - Generate Thumbnails from URLs
09/22/2006
· Ruby, Io, Python, Java, C Benchmark
09/22/2005
· Linux on iPod review from xlr8yourmac
12/27/2004
· Writing Secure PHP Applications
03/29/2004
 

Site News | RSS


Home > Articles >

Assembler Programming for Intel and PPC on OSX

Intel x86

On Intel macs, you need to push arguments for the syscall function onto the stack. On PPC, you use registers starting with r3 to pass arguments to syscall. Other differences are the comments: on Intel, you use a pound sign, and on PPC, you use a semi-colon. The instruction order on the Intel version uses AT&T style syntax rather than the Intel syntax (more a kin to the PPC version below).

# Hello World x86 asm - MacOS X
# By Erik Wrenholt
# gcc hello.s -o hello ; ./hello ; echo $?

# References:
#   http://forums.osxfaq.com/viewtopic.php?t=13976
#   http://www.zathras.de/angelweb/blog-intel-assembler-on-mac-os-x.htm

hello:
    .ascii "Hello World!\n"
    len = . - hello

.text
.globl _main

_syscall:
    int     $0x80   #   call syscall
    ret

_main:
    pushl   $len        #   pass the length of the string
    pushl   $hello      #   pass the ptr to the hello string
    pushl   $1          #   pass stdout (1)
    movl    $0x4, %eax  # SYS_write (4)
    call    _syscall

    add     $12, %esp   #   clear stack (we pushed 3 args)
    pushl   $0          #   we want to call exit(0), push 0
    movl    $1, %eax    # SYS_exit=1
    call    _syscall

    leave
    ret

PowerPC

; Hello World PPC asm - MacOS X
; By Erik Wrenholt
; gcc hello.s -o hello ; ./hello ; echo $?

; References:
;   http://www-128.ibm.com/developerworks/library/l-ppc/
;   http://www.ultraviolent.com/ppc/demos/asm/hello/

.data
msg:
    .asciz "Hello World!\n"
    len = . - msg

.text
.globl _main

_main:

    li      r0,4            ; SYS_write = 4
    li      r3,1            ; stdout = 1
    lis     r4,hi16(msg)    ; push address of msg
    addi    r4,r4,lo16(msg) ; push address of msg
    li      r5,len          ; push len

    sc                      ; syscall
    nop                     ; skipped if syscall was successful
    
    li      r0,1            ; SYS_exit = 1
    li      r3,0            ; exit(0)
    sc                      ; syscall
    nop                     ; skipped if syscall was successful
    
    blr                     ; return

Comments

No comments were found.

Leave a Comment

Show Comment Form

© 1996-2008 Timestretch.com
About