# Program: given a list or integers and a factor
# Find the max number in the list that the factor divides
# For example: INPUT: [3,9,2750,50]27], factor: 3 | OUTPUT: 27
.section .rodata
nums: .long 3,9,27,50,27,-1
factor: .long 3
.section .data
cur_value: .long -1
# first three args: %edi, %esi, %edx
.section .text
.globl _start
_start:
# SETUP
# %r8 will store the array index
# %r11 will store the max value
# %esi will store the factor/divisor.
mov $0, %r10d
mov $0, %r11d
mov factor, %esi
loop:
# get current value and store it in %rdi
# we'll also update our variable for `cur_value`
mov nums(, %r10d, 4), %edi
cmp $-1, %edi
je exit
movl %edi, cur_value
# Call the function and increment the aray index
call is_divisible_by
inc %r10d
# if it was NOT divisible (rax = False or 0) jump back to the beginning
cmp $0, %rax
je loop
# if it was divisible, check to make sure it's larger than the current max
cmp %r11d, cur_value
jl loop
mov cur_value, %r11d
jmp loop
exit:
mov %r11d, %edi
mov $60, %eax
syscall
is_divisible_by:
# Return 0 (false) if not divisible; 1 (true) if divisible
# A (dividend, %eax) / B (divisor)
# dividend needs to first be moved into eax
mov %edi, %eax
# divide by a register, immediate, or memory address
# this is unsigned (positive), use idiv for signed
div %esi
# the resultant integer quotient goes in %eax, and the remainder goes in %edx
# if %rdx is zero it means A is divisible by B: we don't care about %eax
mov $0, %eax
cmp $0, %edx
jne end
mov $1, %rax
end:
ret