aaaaa

aaaaa
mail@pastecode.io avatar
unknown
assembly_x86
2 months ago
2.0 kB
15
Indexable
Never
.data
output_text:    .string "Max: %d\nMin: %d\nZera: %d\n"
table:          .long 15, -21, 0, 0, 5, 9, 7, 1, 2, -38, 3, -5, 9, 123, 0
count:          .long 15

# Return should be:
# Max = 123
# Min = -38
# Zera = 3
##################################################################
.text
.global main

main:
        PUSH    %rbp

        XOR     %esi, %esi      # Make the max value = 0
        XOR     %edx, %edx      # Make the min value = 0
        XOR     %ecx, %ecx      # Make the zero count value = 0

        MOV     $-1, %eax       # Use %eax as counter (-1 cause for loop increments it later)
for_loop:
        INCL    %eax            # Start at index 1
        CMP     count, %eax
        JE      for_loop_end    # If counter == table size then end the loop
        # Else, the loop continues:
        MOV     table(,%eax,4), %r8d    # Store the current integer into %r8d
        CMP     $0, %r8d                # If current integer = 0, then increment the zero count
        JE      handle_zero
handled_zero:
        CMP     %r8d, %esi              # Check for new max
        JL      handle_max
handled_max:
        CMP     %r8d, %edx              # Check for new min
        JG      handle_min
        JMP     for_loop

handle_max:
        MOV     %r8d, %esi      # Update with the new max
        JMP     handled_max
handle_min:
        MOV     %r8d, %edx      # Update with the new min
        JMP     for_loop
handle_zero:
        INCL    %ecx            # Update the zero count
        JMP     handled_zero

for_loop_end:
        # For the printf function:
        # %rdi has the text format
        # %rsi has the max value        (long so %esi)
        # %rdx has the min value        (long so %edx)
        # %rcx has the zero count       (long so %ecx)
        MOV     $output_text, %rdi
        XOR     %al, %al
        CALL    printf
end:
        XOR     %eax, %eax
        POP     %rbp
        RET
Leave a Comment