summaryrefslogtreecommitdiff
path: root/PGU/CHAP3/max_value.s
diff options
context:
space:
mode:
authorCarlos Maiolino <[email protected]>2025-07-10 22:55:07 +0200
committerCarlos Maiolino <[email protected]>2025-07-10 22:56:55 +0200
commitd98f46ce647846b0aa30b2e16a30fd4e152a1bf5 (patch)
tree267474fcc77cf20b428f6f4c7f768ca09f4cfe0e /PGU/CHAP3/max_value.s
parent869e68986aa8f69af6e7842260a68d1e5c6f796f (diff)
Add new code
Signed-off-by: Carlos Maiolino <[email protected]>
Diffstat (limited to 'PGU/CHAP3/max_value.s')
-rw-r--r--PGU/CHAP3/max_value.s65
1 files changed, 65 insertions, 0 deletions
diff --git a/PGU/CHAP3/max_value.s b/PGU/CHAP3/max_value.s
new file mode 100644
index 0000000..7025b05
--- /dev/null
+++ b/PGU/CHAP3/max_value.s
@@ -0,0 +1,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