I am writing an assembly loop to get the max number in an array. It loops like this:
start_loop:
# Get the current element in the array and move it to %rax
# movz --> (1) b(yte-1), w(ord-2), l(long-4), q(uad-8)
movzwq data_items(,%rdi,2), %rax
# Check if the current element value is zero, if it is, jump to the end
cmp $0, %rax
jz exit
# Increment the array index as we want to continue the loop at the end
inc %rdi
# Compare the current value (rax) to the current max (rbx)
# WARNING: The `cmp` instruction is always backwards with ATT syntax!
# It reads as, "With respect to %rbx, the value of %rax is...(greater|less) than"
# So to see if a > b, do:
# cmp b, a
# jg
# Reference: https://stackoverflow.com/a/26191257/12283168
cmp %rbx, %rax
jge update_value
jmp start_loop
update_value:
mov %rax, %rbx
jmp start_loop
exit:
mov $1, %rax
int $0x80
My question is this part of the comparison code here:
jge update_value
jmp start_loop
update_value:
mov %rax, %rbx
jmp start_loop # <== can I get rid of this part?
Is there a way to not have to specify the jmp start_loop
in the update_value section? For example, in a high level language I could do:
while (1) {
if (a > b)
update_value();
// continue
}
And not have to "jump back to while
from the update_value function, I could just 'continue'. Is it possible to do something like this in assembly, or am I thinking about this incorrectly?