Assembly code (NASM) for mac - hello world


/ Published in: Assembler
Save to your folder(s)



Copy this code and paste it in your HTML
  1. ; Hello World in assembly for mac
  2. ; nasm -f macho hello.asm
  3. ; ld -e _start -o hello hello.o
  4. ;
  5. section .text
  6. global _start ;must be declared for linker (ld)
  7.  
  8. _syscall:
  9. int 0x80 ;system call
  10. ret
  11.  
  12. _start: ;tell linker entry point
  13.  
  14. push dword len ;message length
  15. push dword msg ;message to write
  16. push dword 1 ;file descriptor (stdout)
  17. mov eax,0x4 ;system call number (sys_write)
  18. call _syscall ;call kernel
  19.  
  20. ;the alternate way to call kernel:
  21. ;push eax
  22. ;call 7:0
  23.  
  24. add esp,12 ;clean stack (3 arguments * 4)
  25.  
  26. push dword 0 ;exit code
  27. mov eax,0x1 ;system call number (sys_exit)
  28. call _syscall ;call kernel
  29.  
  30. ;we do not return from sys_exit,
  31. ;there's no need to clean stack
  32. section .data
  33.  
  34. msg db "Hello, world!",0xa ;our dear string
  35. len equ $ - msg ;length of our dear string

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.