Skip to content

Instantly share code, notes, and snippets.

@hiboma
Created July 2, 2012 13:19
Show Gist options
  • Save hiboma/3033226 to your computer and use it in GitHub Desktop.
Save hiboma/3033226 to your computer and use it in GitHub Desktop.
hello-world.S - nasm on mac osx snow leopard
;;; nasm -f macho mac-nasm.S -o hoge
;;; gcc -Wall -Werror -m32 -mmacosx -version-min=10.6 -o hello hoge
;;; gcc -Wall -Werror -arch i386 -mmacosx -version-min=10.6 -o hello hoge

section .data
msg:        db "hello,world", 10
len:        equ $-msg

section .text
global _main
_main:
        push dword len          ; 3) len
        lea eax,[msg]           ; 2) char *
        push dword eax
        push dword 1            ; 1) fd

        mov eax, 4              ; @) write(2)
        push dword eax
        int 0x80

        mov eax,1               ; exit
        int 0x80
@hiboma
Copy link
Author

hiboma commented Jul 2, 2012

linux i386, nasm

section .data
msg:        db "hello,world", 10
len:        equ $-msg

section .text
global _start
_start:
        mov eax, 4              ; @) write(2)
        mov ebx, 1              ; 1) fd
        lea ecx,[msg]           ; 2) char *
        mov edx, len            ; 3) len
        int 0x80

        mov ebx,0               ; exit code
        mov eax,1               ; exit
        int 0x80
$ nasm -f elf test.S -o hoge
$ ld -m elf_i386 -s -o hello hoge
$ ./hello

ok?

@hiboma
Copy link
Author

hiboma commented Jul 3, 2012

objdump -S ./hello 

./hello:     file format elf32-i386


Disassembly of section .text:

08048080 <.text>:
 8048080:       b8 04 00 00 00          mov    $0x4,%eax
 8048085:       bb 01 00 00 00          mov    $0x1,%ebx
 804808a:       8d 0d a4 90 04 08       lea    0x80490a4,%ecx
 8048090:       ba 0c 00 00 00          mov    $0xc,%edx
 8048095:       cd 80                   int    $0x80
 8048097:       bb 00 00 00 00          mov    $0x0,%ebx
 804809c:       b8 01 00 00 00          mov    $0x1,%eax
 80480a1:       cd 80                   int    $0x80

@hiboma
Copy link
Author

hiboma commented Jul 3, 2012

$ objdump -s ./hello 

./hello:     file format elf32-i386

Contents of section .text:
 8048080 b8040000 00bb0100 00008d0d a4900408  ................
 8048090 ba0c0000 00cd80bb 00000000 b8010000  ................
 80480a0 00cd80                               ...             
Contents of section .data:
 80490a4 68656c6c 6f2c776f 726c640a           hello,world.    

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment