- NASM Manual - for the syntax
- What are the calling conventions for UNIX & Linux system calls on x86-64 - comparison
- AMD64 ABI reference - A.2.1 - for the actual x86_64 calling convention
- X86 calling conventions - moar calling conventions
- Mac OS X syscalls reference
-
-
Save aktau/4289bb940d5193c95d04 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; /usr/local/bin/nasm -f macho 32.asm && ld -macosx_version_min 10.7.0 -o 32 32.o && ./32 | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
; The .data section is for storing and naming constants. | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
section .data | |
msg: db "Hello world!", 10 | |
.len: equ $ - msg | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;The .text section is for the actual code. | |
;(I assume .text refers to source code being text) | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
section .text | |
global start ; indicate the entry point with global. | |
;OSX expects this to be called "start" | |
start: | |
; we are going to syscall "write" | |
; user_ssize_t write(int fd, user_addr_t cbuf, user_size_t nbyte); | |
; The calling convention for OSX 32bit code is to have the params on | |
; the stack | |
push dword msg.len ; push the length | |
push dword msg ; push the address of the message | |
push dword 1 ; push the file descripter. 1 is stdout | |
mov eax, 4 ; put the syscall number in eax | |
sub esp, 4 ; put some space on the stack, required for syscalls on osx (and bsd) | |
int 0x80 ; execute the syscall | |
add esp,16 ; cleanup the stack. 4 bytes per arg pulse the 4 bytes we sub'd is 16 bytes | |
; now we syscall "exit | |
; void exit(int rval); | |
push dword 0 ; push the rval on the stack | |
mov eax, 1 ; put the syscall number in eax | |
sub esp, 4 ; put some space on the stack, required for syscalls on osx (and bsd) | |
int 0x80 ; execute the sys call. | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; /Usr/local/bin/nasm -f macho64 64.asm && ld -macosx_version_min 10.7.0 -lSystem -o 64 64.o && ./64 | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
; The .data section is for storing and naming constants. | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
section .data | |
msg: db "Hello, world!", 10 | |
.len: equ $ - msg ;$ refers to the address of this constant, so $ - msg is the length of message | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;The .text section is for the actual code. | |
;(I assume .text refers to source code being text) | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
section .text | |
global start ;global is used to tell the kernel where to enter the program. | |
;OSX expects this to be called "start" | |
;You can put underscores in numbers to make them easier to read. They are ignored by nasm. | |
start: | |
mov rax, 0x200_0004 ;The number for the syscall "write": user_ssize_t write(int fd, user_addr_t cbuf, user_size_t nbyte); | |
mov rdi, 1 ;First param for write is the file descripter. 1 is stdout | |
mov rsi, msg ;Second param is a pointer to the msg | |
mov rdx, msg.len ;Third param is the length of the message | |
syscall | |
mov rax, 0x200_0001 ;The number for the syscall "exit": void exit(int rval); | |
mov rdi, 0 ; First param is the return code | |
syscall |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment