├── LICENSE ├── fasm0 └── hello.asm ├── fasm1 └── fasm1.asm ├── fasm2 ├── fasm2.asm └── io.inc └── fasm3 ├── fasm3.asm ├── io.inc └── unistd64.inc /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015, pbohun 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | * Neither the name of fasm-tutorials nor the names of its 15 | contributors may be used to endorse or promote products derived from 16 | this software without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 22 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 25 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | 29 | -------------------------------------------------------------------------------- /fasm0/hello.asm: -------------------------------------------------------------------------------- 1 | format ELF64 executable 3 2 | 3 | segment readable executable 4 | 5 | entry main 6 | 7 | main: 8 | lea rdi, [msg] ; address of msg goes into rdi 9 | mov rax, 14 ; put length of msg into rax 10 | mov rdx, rax ; move rax to rdx 11 | mov rsi, rdi ; move rdi to rsi 12 | mov rdi, 1 ; stdout 13 | mov rax, 1 ; sys_write 14 | syscall 15 | xor rdi, rdi ; exit code 0 16 | mov rax, 60 ; sys_exit 17 | syscall 18 | 19 | segment readable writable 20 | 21 | msg db 'Hello world!', 10, 0 22 | -------------------------------------------------------------------------------- /fasm1/fasm1.asm: -------------------------------------------------------------------------------- 1 | format ELF64 executable 3 2 | 3 | segment readable executable 4 | 5 | entry main 6 | 7 | main: 8 | lea rdi, [msg] ; address of msg goes into rdi 9 | call strlen 10 | mov rdx, rax ; move rax to rdx 11 | mov rsi, rdi ; move rdi to rsi 12 | mov rdi, 1 ; stdout 13 | mov rax, 1 ; sys_write 14 | syscall 15 | xor rdi, rdi ; exit code 0 16 | mov rax, 60 ; sys_exit 17 | syscall 18 | 19 | strlen: 20 | push rdi ; push to stack 21 | push rcx ; push to stack 22 | sub rcx, rcx ; set rcx to 0 23 | mov rcx, -1 ; move -1 to rcx 24 | sub al, al ; set al to 0 25 | cld ; clear the direction flags 26 | repne scasb ; repeat if not equal to al 27 | neg rcx ; negate rcx 28 | sub rcx, 1 ; subtract 1 from rcx 29 | mov rax, rcx ; move value from rcx to rax 30 | pop rcx ; restore original rcx value 31 | pop rdi ; restore original rdi value 32 | ret 33 | 34 | 35 | segment readable writable 36 | 37 | msg db 'Hello World!', 10, 0 38 | -------------------------------------------------------------------------------- /fasm2/fasm2.asm: -------------------------------------------------------------------------------- 1 | format ELF64 executable 3 2 | 3 | segment readable executable 4 | 5 | entry main 6 | 7 | include 'io.inc' 8 | 9 | main: 10 | lea rdi, [msg] ; address of msg goes into rdi 11 | call print 12 | lea rdi, [msg2] ; address of msg2 goes into rdi 13 | call print 14 | xor rdi, rdi ; exit code 0 15 | mov rax, 60 ; sys_exit 16 | syscall 17 | 18 | segment readable writable 19 | 20 | msg db 'Hello World!', 10, 0 21 | msg2 db 'This is my other string.', 10, 0 22 | -------------------------------------------------------------------------------- /fasm2/io.inc: -------------------------------------------------------------------------------- 1 | strlen: 2 | push rdi ; push to stack 3 | push rcx ; push to stack 4 | sub rcx, rcx ; set rcx to 0 5 | mov rcx, -1 ; move -1 to rcx 6 | sub al, al ; set al to 0 7 | cld ; clear the direction flags 8 | repne scasb ; repeat if not equal to al 9 | neg rcx ; negate rcx 10 | sub rcx, 1 ; subtract 1 from rcx 11 | mov rax, rcx ; move value from rcx to rax 12 | pop rcx ; restore original rcx value 13 | pop rdi ; restore original rdi value 14 | ret 15 | 16 | print: 17 | call strlen ; get string length 18 | mov rdx, rax ; move string length to rdx 19 | mov rsi, rdi ; move address of string to rdi 20 | mov rdi, 1 ; stdout 21 | mov rax, 1 ; sys_write 22 | syscall 23 | ret 24 | -------------------------------------------------------------------------------- /fasm3/fasm3.asm: -------------------------------------------------------------------------------- 1 | format ELF64 executable 3 2 | 3 | segment readable executable 4 | 5 | entry main 6 | 7 | include 'io.inc' 8 | include 'unistd64.inc' 9 | 10 | main: 11 | lea rdi, [prompt] 12 | call print 13 | lea rsi, [buf] 14 | mov rdi, 64 ; size of buffer 15 | call read 16 | mov rdi, rsi ; move address of buf to rdi 17 | call print 18 | xor rdi, rdi ; exit code 0 19 | mov rax, sys_exit 20 | syscall 21 | 22 | segment readable writable 23 | 24 | prompt db 'Please type your name: ', 0 25 | buf rb 64 26 | -------------------------------------------------------------------------------- /fasm3/io.inc: -------------------------------------------------------------------------------- 1 | strlen: 2 | push rdi ; push to stack 3 | push rcx ; push to stack 4 | sub rcx, rcx ; set rcx to 0 5 | mov rcx, -1 ; move -1 to rcx 6 | sub al, al ; set al to 0 7 | cld ; clear the direction flags 8 | repne scasb ; repeat if not equal to al 9 | neg rcx ; negate rcx 10 | sub rcx, 1 ; subtract 1 from rcx 11 | mov rax, rcx ; move value from rcx to rax 12 | pop rcx ; restore original rcx value 13 | pop rdi ; restore original rdi value 14 | ret 15 | 16 | print: 17 | call strlen ; get string length 18 | mov rdx, rax ; move string length to rdx 19 | mov rsi, rdi ; move address of string to rdi 20 | mov rdi, 1 ; stdout 21 | mov rax, sys_write 22 | syscall 23 | ret 24 | 25 | read: 26 | mov rdx, rdi ; size of buffer 27 | mov rdi, 0 ; stdin 28 | mov rax, sys_read 29 | syscall 30 | ret 31 | -------------------------------------------------------------------------------- /fasm3/unistd64.inc: -------------------------------------------------------------------------------- 1 | sys_read = 0 2 | sys_write = 1 3 | sys_open = 2 4 | sys_close = 3 5 | sys_stat = 4 6 | sys_fstat = 5 7 | sys_lstat = 6 8 | sys_poll = 7 9 | sys_lseek = 8 10 | sys_mmap = 9 11 | sys_mprotect = 10 12 | sys_munmap = 11 13 | sys_brk = 12 14 | sys_rt_sigaction = 13 15 | sys_rt_sigprocmask = 14 16 | sys_rt_sigreturn = 15 17 | sys_ioctl = 16 18 | sys_pread64 = 17 19 | sys_pwrite64 = 18 20 | sys_readv = 19 21 | sys_writev = 20 22 | sys_access = 21 23 | sys_pipe = 22 24 | sys_select = 23 25 | sys_sched_yield = 24 26 | sys_mremap = 25 27 | sys_msync = 26 28 | sys_mincore = 27 29 | sys_madvise = 28 30 | sys_shmget = 29 31 | sys_shmat = 30 32 | sys_shmctl = 31 33 | sys_dup = 32 34 | sys_dup2 = 33 35 | sys_pause = 34 36 | sys_nanosleep = 35 37 | sys_getitimer = 36 38 | sys_alarm = 37 39 | sys_setitimer = 38 40 | sys_getpid = 39 41 | sys_sendfile = 40 42 | sys_socket = 41 43 | sys_connect = 42 44 | sys_accept = 43 45 | sys_sendto = 44 46 | sys_recvfrom = 45 47 | sys_sendmsg = 46 48 | sys_recvmsg = 47 49 | sys_shutdown = 48 50 | sys_bind = 49 51 | sys_listen = 50 52 | sys_getsockname = 51 53 | sys_getpeername = 52 54 | sys_socketpair = 53 55 | sys_setsockopt = 54 56 | sys_getsockopt = 55 57 | sys_clone = 56 58 | sys_fork = 57 59 | sys_vfork = 58 60 | sys_execve = 59 61 | sys_exit = 60 62 | sys_wait4 = 61 63 | sys_kill = 62 64 | sys_uname = 63 65 | sys_semget = 64 66 | sys_semop = 65 67 | sys_semctl = 66 68 | sys_shmdt = 67 69 | sys_msgget = 68 70 | sys_msgsnd = 69 71 | sys_msgrcv = 70 72 | sys_msgctl = 71 73 | sys_fcntl = 72 74 | sys_flock = 73 75 | sys_fsync = 74 76 | sys_fdatasync = 75 77 | sys_truncate = 76 78 | sys_ftruncate = 77 79 | sys_getdents = 78 80 | sys_getcwd = 79 81 | sys_chdir = 80 82 | sys_fchdir = 81 83 | sys_rename = 82 84 | sys_mkdir = 83 85 | sys_rmdir = 84 86 | sys_creat = 85 87 | sys_link = 86 88 | sys_unlink = 87 89 | sys_symlink = 88 90 | sys_readlink = 89 91 | sys_chmod = 90 92 | sys_fchmod = 91 93 | sys_chown = 92 94 | sys_fchown = 93 95 | sys_lchown = 94 96 | sys_umask = 95 97 | sys_gettimeofday = 96 98 | sys_getrlimit = 97 99 | sys_getrusage = 98 100 | sys_sysinfo = 99 101 | sys_times = 100 102 | sys_ptrace = 101 103 | sys_getuid = 102 104 | sys_syslog = 103 105 | sys_getgid = 104 106 | sys_setuid = 105 107 | sys_setgid = 106 108 | sys_geteuid = 107 109 | sys_getegid = 108 110 | sys_setpgid = 109 111 | sys_getppid = 110 112 | sys_getpgrp = 111 113 | sys_setsid = 112 114 | sys_setreuid = 113 115 | sys_setregid = 114 116 | sys_getgroups = 115 117 | sys_setgroups = 116 118 | sys_setresuid = 117 119 | sys_getresuid = 118 120 | sys_setresgid = 119 121 | sys_getresgid = 120 122 | sys_getpgid = 121 123 | sys_setfsuid = 122 124 | sys_setfsgid = 123 125 | sys_getsid = 124 126 | sys_capget = 125 127 | sys_capset = 126 128 | sys_rt_sigpending = 127 129 | sys_rt_sigtimedwait = 128 130 | sys_rt_sigqueueinfo = 129 131 | sys_rt_sigsuspend = 130 132 | sys_sigaltstack = 131 133 | sys_utime = 132 134 | sys_mknod = 133 135 | sys_uselib = 134 136 | sys_personality = 135 137 | sys_ustat = 136 138 | sys_statfs = 137 139 | sys_fstatfs = 138 140 | sys_sysfs = 139 141 | sys_getpriority = 140 142 | sys_setpriority = 141 143 | sys_sched_setparam = 142 144 | sys_sched_getparam = 143 145 | sys_sched_setscheduler = 144 146 | sys_sched_getscheduler = 145 147 | sys_sched_get_priority_max = 146 148 | sys_sched_get_priority_min = 147 149 | sys_sched_rr_get_interval = 148 150 | sys_mlock = 149 151 | sys_munlock = 150 152 | sys_mlockall = 151 153 | sys_munlockall = 152 154 | sys_vhangup = 153 155 | sys_modify_ldt = 154 156 | sys_pivot_root = 155 157 | sys__sysctl = 156 158 | sys_prctl = 157 159 | sys_arch_prctl = 158 160 | sys_adjtimex = 159 161 | sys_setrlimit = 160 162 | sys_chroot = 161 163 | sys_sync = 162 164 | sys_acct = 163 165 | sys_settimeofday = 164 166 | sys_mount = 165 167 | sys_umount2 = 166 168 | sys_swapon = 167 169 | sys_swapoff = 168 170 | sys_reboot = 169 171 | sys_sethostname = 170 172 | sys_setdomainname = 171 173 | sys_iopl = 172 174 | sys_ioperm = 173 175 | sys_create_module = 174 176 | sys_init_module = 175 177 | sys_delete_module = 176 178 | sys_get_kernel_syms = 177 179 | sys_query_module = 178 180 | sys_quotactl = 179 181 | sys_nfsservctl = 180 182 | sys_getpmsg = 181 183 | sys_putpmsg = 182 184 | sys_afs_syscall = 183 185 | sys_tuxcall = 184 186 | sys_security = 185 187 | sys_gettid = 186 188 | sys_readahead = 187 189 | sys_setxattr = 188 190 | sys_lsetxattr = 189 191 | sys_fsetxattr = 190 192 | sys_getxattr = 191 193 | sys_lgetxattr = 192 194 | sys_fgetxattr = 193 195 | sys_listxattr = 194 196 | sys_llistxattr = 195 197 | sys_flistxattr = 196 198 | sys_removexattr = 197 199 | sys_lremovexattr = 198 200 | sys_fremovexattr = 199 201 | sys_tkill = 200 202 | sys_time = 201 203 | sys_futex = 202 204 | sys_sched_setaffinity = 203 205 | sys_sched_getaffinity = 204 206 | sys_set_thread_area = 205 207 | sys_io_setup = 206 208 | sys_io_destroy = 207 209 | sys_io_getevents = 208 210 | sys_io_submit = 209 211 | sys_io_cancel = 210 212 | sys_get_thread_area = 211 213 | sys_lookup_dcookie = 212 214 | sys_epoll_create = 213 215 | sys_epoll_ctl_old = 214 216 | sys_epoll_wait_old = 215 217 | sys_remap_file_pages = 216 218 | sys_getdents64 = 217 219 | sys_set_tid_address = 218 220 | sys_restart_syscall = 219 221 | sys_semtimedop = 220 222 | sys_fadvise64 = 221 223 | sys_timer_create = 222 224 | sys_timer_settime = 223 225 | sys_timer_gettime = 224 226 | sys_timer_getoverrun = 225 227 | sys_timer_delete = 226 228 | sys_clock_settime = 227 229 | sys_clock_gettime = 228 230 | sys_clock_getres = 229 231 | sys_clock_nanosleep = 230 232 | sys_exit_group = 231 233 | sys_epoll_wait = 232 234 | sys_epoll_ctl = 233 235 | sys_tgkill = 234 236 | sys_utimes = 235 237 | sys_vserver = 236 238 | sys_vserver = 236 239 | sys_mbind = 237 240 | sys_set_mempolicy = 238 241 | sys_get_mempolicy = 239 242 | sys_mq_open = 240 243 | sys_mq_unlink = 241 244 | sys_mq_timedsend = 242 245 | sys_mq_timedreceive = 243 246 | sys_mq_notify = 244 247 | sys_mq_getsetattr = 245 248 | sys_kexec_load = 246 249 | sys_waitid = 247 250 | sys_add_key = 248 251 | sys_request_key = 249 252 | sys_keyctl = 250 253 | sys_syscall_max = sys_keyctl 254 | --------------------------------------------------------------------------------