Programming assembly for x86-64 bit machines

Following assembly books which are written for x86-32 bit machines on a 64 bit machine can be a pain in the ass. Thankfully I found this site
that really clarified things for me. Thus this is a short example of an assembly program written for an x86-64 bit machines demonstrating how a function can be called. The sample function takes a number a returns the square of that number.

# A program to call a function a compute 
# the square of a number.

.section .data

.section .text

.globl _start


	push $5			#push first argument	

	call square		# call the square function
	movq $1, %rax    #Call to exit

	int $0x80

# Square Function.
# takes a number and squares it.
# returns the squared number.

.type square, @function
	push %rbp			# save old base pointer
	movq %rsp, %rbp     		# make stack pointer the base pointer
	movq 16(%rbp), %rcx   		# Copy the first argument from the
					# stack which is on position two. 
					# Thus its 2 quad-words which requires 16
	imul %rcx, %rcx   		# Square and store the number
	mov %rcx, %rbx			# store the return value
	movq %rbp, %rsp			# restore the stack pointer
	pop %rbp


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s