# Opening a file and printing it out
# C style
.section .data
filename:
	.asciz "open_file.s"
.section .text
.global main
main:
# File descriptor is above the memory address from malloc
# file descriptor:  8(%rsp)
# address that came from malloc:  (%rsp)
	mov %rsp, %rbp
	sub $16, %rsp
	mov $filename, %rdi
	mov $0, %rsi # O_RDONLY is 0
	call open
	# Now we'll have a file descriptor in %rax
	mov %rax, 8(%rsp)

	# Let's get some memory
	mov $1024, %rdi
	call malloc
	# Our memory address is in rax
	mov %rax, (%rsp)

	mov 8(%rsp), %rdi
	mov %rax, %rsi
	mov $1024, %rdx
	call read
	
	mov (%rsp), %rdi # More direct than pop followed by push
	call puts

	mov (%rsp), %rdi 
	call free

	mov 8(%rsp), %rdi
	call close
	add $16, %rsp

	ret