Skip to content

Instantly share code, notes, and snippets.

@grahamking
Created July 10, 2015 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grahamking/6145d00cf53c9ca220f4 to your computer and use it in GitHub Desktop.
Save grahamking/6145d00cf53c9ca220f4 to your computer and use it in GitHub Desktop.
Example for darkcoding mem allocation post. http://www.darkcoding.net/software/how-memory-is-allocated/
.text
.global _start
_start:
mov $12, %rax # brk syscall number
mov $0, %rdi # 0 is invalid, want to get current position
syscall
mov %rax, %rsi # rsi now points to start of heap mem we'll allocate
mov %rax, %rdi # move top of heap to here ...
add $4, %rdi # .. plus 4 bytes we allocate
mov $12, %rax # brk, again
syscall
# Move "HI\n" to our memory
movb $72, (%rsi) # 'H'
inc %rsi
movb $73, (%rsi) # 'I'
inc %rsi
movb $10, (%rsi) # \n
inc %rsi
movb $0, (%rsi) # \0, C string terminator
# Print it
sub $3, %rsi # Move rsi back to start of string
mov $3, %rdx # String length 3
mov $1, %rax # write system is number 1
mov $1, %rdi # destinatiout is fd 1, stdout
syscall
# Exit
mov $60, %eax # exit syscall is number 60
mov $0, %edi # exit code 0
syscall
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment