Learn how to print "Hello, World!" in assembler on MacOS X using the jmp/call method to get a pointer to the string to print. Happy Hacking!

x86

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

# References:
# http://www.klake.org/~jt/encoder/ 

.text
.globl _main

_main:
    jmp _data

_syscall:
    int     $0x80
    ret

_begin:
    pop     %ebx        #   get ptr of string
    pushl   $14         #   pass the length of the string   
    pushl   %ebx        #   pass the ptr to the hello string
    pushl   $1          #   pass stdout (1)
    movl    $4, %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 

_data:
    call _begin

.asciz "Hello, World!n"

PPC

; By Erik Wrenholt
; This PPC example shows how to use the jmp/call
; method to obtain the ptr to the string.

;gcc hello4.s -o hello4 ; ./hello4 

.globl _main
.text

_main:

    bl      _string     ; branch to location of string

_resume:

    li      r3, 1       ; stdout
    mflr    r4          ; move from lr into r4

    li      r5, 14      ; string length
    li      r0, 4       ; SYS_write
    sc                  ; write(stdout, string, len)
    nop

    li      r3, 0       ; 
    li      r0, 1       ; SYS_exit (r3)
    sc

_string:
    bl  _resume         ; go back to _resume
                        ; the link register holds ptr to string below

    .asciz "Hello, world!n"

Tags: jmp, assembler

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 akin to the PPC version below).

Intel x86

# 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

Tags: asm, assembler