Skip to content

Instantly share code, notes, and snippets.

@Cxarli
Created October 29, 2015 21:08
Show Gist options
  • Save Cxarli/11dcf70b57980383f80b to your computer and use it in GitHub Desktop.
Save Cxarli/11dcf70b57980383f80b to your computer and use it in GitHub Desktop.
;; ---- HELP ----
;; Important functions:
;; syscall Call a system function. ID from `rax`, output to `rax`
;; - See: https://github.com/torvalds/linux/blob/097f70b3c4d84ffccca15195bdfde3a37c0a7c0f/include/linux/syscalls.h
;; - See: http://blog.rchapman.org/post/36801038863/linux-system-call-table-for-x86-64
;;
;; - Examples:
;; * 0 sys_read: Read a value
;; * 1 sys_write: Output a value
;; * 60 sys_exit: Exit
;;
;; C functions:
;; - 0 long sys_read ( unsigned int fd, char __user *buf, size_t count );
;; - 1 long sys_write ( unsigned int fd, const char __user *buf, size_t count );
;; - 60 long sys_exit ( int error_code );
;; Compile and run with
;; ```sh
;; nasm -f elf64 -o hello.o hello.asm
;; ld -o hello hello.o
;; ./hello
;; ```
;; Function variables:
;; rax CPU cache — temporary register. `syscal` uses this Register: Accumulator
;; rdi CPU cache — 1st argument. Register: Destination Index
;; rsi CPU cache — 2nd argument. Register: Source Index
;; rdx CPU cache — 3rd argument. Register: D
;; Other variables:
;; rbx Register: Base index
;; rcx Register: Counter
;; Special variables:
;; $ The current memory location
section .data ;; Constants here
;; msg db "Hello, world!", 10 ;; Hello world variable as `msg`
;; len equ $ - msg ;; Length of `msg`
NL db 10
namein db "Please enter your name", 10
namein_ equ $ - namein
nameout0 db "Hello, "
nameout0_ equ $ - nameout0
nameout1 db " !", 10
nameout1_ equ $ - nameout1
name times 512 db 0 ;; Buffer of 512 bytes
exit db 0
section .bss ;; Variables here
name_ resd 0
help resd 0
help2 resd 0
help3 resd 0
section .text ;; Code here
global _start
_start: ;; Initialize _start, then run
;; Get input and save as `name`
mov rax, 0
mov rdi, 0
mov rsi, name
mov rdx, 512
syscall
;; Get length of input
mov [name_], rax
;; Output name_ as number
mov help, 1
mov help2, 0
mov help3, 0
output_name_:
;; Make the help variable
make_help:
;; help *= 10
mov help2, help
mov help, 0
times 10 add help, help2
;; help <= name_
cmp help, name_
jle make_help
mov rdx, help
div 10
mov help, rdx
mov help2, name_
repeat:
mov rdx, help2
div help
mov help3, rdx
; help2 -= help*help3
; help /= 10
; if help == 0: help = -1
cmp help, 0
jne skip
mov help, -1
skip:
mov help3, help3 + 0x30 ; Convert number to ASCII
; If help2 >= 0: repeat
cmp help2, 0
jge repeat
;; Output nameout0
mov rax, 1
mov rdi, 1
mov rsi, nameout0
mov rdx, nameout0_
syscall
;; Output name
mov rax, 1
mov rdi, 1
mov rsi, name
mov rdx, name_
syscall
;; Output nameout1
mov rax, 1
mov rdi, 1
mov rsi, nameout1
mov rdx, nameout1_
syscall
;; Output msg
;;mov rax, 1
;;mov rdi, 1
;;mov rsi, msg
;;mov rdx, len
;;syscall
eof: ;; End Of File: Exit with 0
;; Exit with statuscode 0
mov rax, 60
mov rdi, 0
syscall
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment