Skip to content

Instantly share code, notes, and snippets.

@aktau
Forked from desertmonad/32.asm
Created August 31, 2014 09:01
Show Gist options
  • Save aktau/4289bb940d5193c95d04 to your computer and use it in GitHub Desktop.
Save aktau/4289bb940d5193c95d04 to your computer and use it in GitHub Desktop.
; /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.
; /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