summaryrefslogtreecommitdiff
path: root/PGU/CHAP3/max_value.s
blob: 7025b057c2b6c606007e5e9b6b10079df45d3704 (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Find the maximum value inside a static list

# Variables used:
#
#	- %r11 - Holds index of the data item being examined
#	- %rdi - Largest data item found so far
#	- %rax - Current data item
#
#
# Constant memory locations:
#
# data_items -	contains the item data. (Marks the beginning actualy)
#		So far, 0 is used as terminator
#
# This program is written for x86_64 processors, it won't assemble in x86

.section .data # Specify data section


# Array of data items. This use 8bytes (quad-word) for each location
data_items:
.quad 133,55,50,12,25,77,30,93,222,55,62,250,66,34,69,0


.section .text

# Mark program's starting point so Linux can 'see' it
.globl _start

_start:
movq $0, %r11				# Move 0 into %rdi to indicate
					# the start of the index

movq data_items(,%r11,8), %rax		# Load the first data item (8bytes long)
					# This actually uses indexed access
					# mode, where %rdi is the 'offset' into
					# the data_items array, and 8 is the
					# multiplier.

movq %rax, %rbx				# This is the first item, so, %rax is
					# current the biggest anyway


# Loop over all items
start_loop:
	cmpq $0, %rax			# compare current item with the
	je loop_exit			# terminator 0, and jump to exit
					# if it is equal.

	incq %r11			# Increment index so we can grab next
					# item

	movq data_items(,%r11,8), %rax  # Grab next item once we incremented the
					# index above

	cmpq %rbx, %rax			# If our current item in %rax is smaller
	jle start_loop			# we ignore it and move back to the loop

	movq %rax, %rdi			# Otherwise we store it as our new largest
	jmp start_loop			# item and jump back to the loop unconditionally

loop_exit:
	movq $60, %rax			# Call exit() syscall, our argument is
					# already in %rdi, just setup syscall
	syscall				# number and call kernel