talks

git clone git://code.dwrz.net/talks
Log | Files | Refs

math.s (742B)


      1 .text
      2 	.global _start
      3 
      4 _start:
      5 	mov $0, %rax  # rax = 0
      6 	inc %rax      # rax++
      7 	dec %rax      # rax--
      8 	add $7, %rax  # rax += 7
      9 	sub $5, %rax  # rax -= 5
     10 
     11 	# At this point, rax = 2.
     12 	# Multiplication cannot use immediate addressing.
     13 	# So we store the multiplier in %rbx.
     14 	# mul assumes the multiplicand is in %rax.
     15 	# The product is also stored in %rax.
     16 	mov $12, %rbx
     17 	mul %rbx # rax *= 12
     18 
     19 	# rax = 24.
     20 	# Division also cannot use immediate addressing.
     21 	# Store the divisor in %rbx.
     22 	# div assumes the dividend is in %rax.
     23 	# The quotient is also stored in %rax.
     24 	mov $3, %rbx
     25 	div %rbx
     26 
     27 	# rax = 8
     28 	# Let's use this as our exit code.
     29 	# We're using 64-bits now -- the exit code should be in %rdi.
     30 	mov %rax, %rdi
     31 	mov $231, %rax
     32 	syscall