CSC231 Hello World on Mac

From dftwiki3
Jump to: navigation, search

--D. Thiebaut 19:40, 7 September 2010 (UTC)


; Hello.asm
; D. Thiebaut
;
; taken from http://untimedcode.com/2007/5/20/learn-nasm-assembly-on-mac-os-x
; Simple Hello World that runs on a Mac or on a Linux machine
;
; to assemble, link and run on a Mac:   
;
; nasm -f macho hello.asm -DMAC
; ld -e _start -o hello hello.o
; ./hello
;
; to assemble, link and run on Linux:
;
; nasm -f elf -F stabs hello.asm 
; ld -o hello hello.o
; ./hello

%macro  int80   0
%ifdef  MAC        
        push    edx
        push    ecx
        push    ebx
        push    eax
%endif
        int     0x80           ; call OSX        call   _syscall

%ifdef  MAC
        add     esp, 16
%endif                
%endm
                        
;;; -------------------------------------------------
;;; data section
;;; -------------------------------------------------
        section .data                

msg     db       "Hello, World!",0xa    ; string with a carriage-return
LEN     equ      $ - msg                ; string length in bytes

;;; -------------------------------------------------
;;; code section
;;; -------------------------------------------------
        section  .text                  ; start of the code indicator
        global   _start                 ; make the main function externally visible
  
_start:                                 ; entry point for linker

;;; write our string to standard output

        mov      eax,    4       ; system call for writing
        mov      ebx,    1       ; to stdout
        mov      ecx,    msg     ; the address of the string
        mov      edx,    LEN     ; the # of chars
        int80


;;; exit
        mov      ebx,     0      ; exit code
        mov      eax,     0x1    ; system call number (sys_exit)
        int80