summaryrefslogtreecommitdiff
path: root/x86_64/exponent.s
blob: e469d26d8f51f32e308885a86cdceb3b09122f3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Compute a^b, where %rbx=a and %rcx=b
#
# Result is added to the accumulator (%rax)


.globl _start

.section .text

_start:
	movq	$2, %rbx # Base
	movq	$5, %rcx # Exponent

	movq	$1, %rax # Initialize accumulator, take advantage of the fact
			 # that anything to the power of 0 is 1.

	# We just need to run this once, as decq will set ZF if %rcx reaches
	# zero, and the jmp instruction does nothing with the ZF
	cmpq	$0, %rcx
	je	complete

mainloop:

	# Multiply %rbx by %rax, and stores the result in %rax
	# Hence %rax is the accumulator register
	mulq	%rbx

	# loopq instruction is very nice, in a single instruction it does:
	#	- decrement %rcx - The counter register -
	#	- compare %rcx to zero
	#	- Jump to the location specified if after decrementing %rcx is not 0
	loopq	mainloop

complete:
	movq	%rax, %rdi
	movq	$60, %rax
	syscall