Skip to content

Instantly share code, notes, and snippets.

@netshade
Created September 6, 2014 19:04
Show Gist options
  • Save netshade/3245b40ff86a101ebfc0 to your computer and use it in GitHub Desktop.
Save netshade/3245b40ff86a101ebfc0 to your computer and use it in GitHub Desktop.
Assembly Experiments
; /Usr/local/bin/nasm -f macho64 64.asm && ld -macosx_version_min 10.7.0 -lSystem -o 64 64.o && ./64
BITS 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
othermsg: db "Goodbye, world!", 10
.len: equ $ - othermsg
callmsg: db "Call, world!", 10
.len: equ $ - callmsg
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;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.
some_method:
mov rax, 0x200_0004
mov rdi, 1
mov rsi, callmsg
mov rdx, callmsg.len
syscall
inc
ret
start:
mov rax, 1
cmp rax, 1
jne printgoodbye
printhello:
call some_method
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
jmp terminate
printgoodbye:
call some_method
mov rax, 0x200_0004
mov rdi, 1
mov rsi, othermsg
mov rdx, othermsg.len
syscall
jmp terminate
terminate:
call some_method
mov rax, 0x200_0001 ;The number for the syscall "exit": void exit(int rval);
mov rdi, 0 ; First param is the return code
syscall
BITS 64
section .data
EQUAL_EACH: equ 1000b
stdout: equ 1
no_arguments: equ 1
no_arguments_msg: db "No arguments", 10
.len: equ $-no_arguments_msg
format: db "Number of arguments to this function: %d", 10, 0
lenFormat: db "Length of string: %d", 10, 0
section .text
global start
extern _printf
writeMsg:
push rsi
push rdi
mov rax, 0x200_0004
mov rdi, stdout
pop rsi
pop rdx
syscall
ret
; ==== strlen ====
strlen_sse42:
; ecx = string
mov rax, -16
mov rdx, rcx
pxor xmm0, xmm0
STRLEN_LOOP:
add rax, 16
PcmpIstrI xmm0, [rdx + rax], EQUAL_EACH
jnz STRLEN_LOOP
add rax, rcx
ret
start:
mov r12, [rsp]
mov r13, rsp
mov rdi, format ; set up registers for c func call, printf. printf arg 1, format
mov rsi, r12 ; printf arg 2, the integer currently at the top of the stack (argc)
and rsp, -16 ; align the stack on 16 bit boundary before making a call to printf, required in mac os x
call _printf ; call printf
pop r8 ; ignore return value
cmp r12, no_arguments ; if no arguments, do nothing
je terminate_no_arguments
xor rbx, rbx
process_arguments:
inc rbx
mov rcx, [r13 + 8 * rbx]
call strlen_sse42
mov rdi, lenFormat
mov rsi, rcx
and rsp, -16
call _printf
check_exit:
cmp rbx, r12
jne process_arguments
jmp terminate
terminate_no_arguments:
mov rdi, no_arguments_msg
mov rsi, no_arguments_msg.len
call writeMsg
jmp terminate
terminate:
mov rax, 0x200_0001
mov rdi, 0
syscall
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment