Skip to content

Instantly share code, notes, and snippets.

@joshtwo
Created May 30, 2014 12:41
Show Gist options
  • Save joshtwo/13e76640db31b5ae2dc9 to your computer and use it in GitHub Desktop.
Save joshtwo/13e76640db31b5ae2dc9 to your computer and use it in GitHub Desktop.
Finds the maximum element of the array using AT&T (as) syntax and Intel (nasm) syntax
; find the maximum number in a list
;
; variables
; edi - Holds the index of the array
; ebx - Largest number
; eax - Current element
section .data
array:
dd 3,67,34,222,45,75,54,34,44,33,22,11,66,0
section .text
global _start
_start:
mov edi, 0 ; set the index to 0
mov eax, [edi * 4 + array] ; set eax to the first element
mov ebx, eax ; set the max to the first element to start with
start_loop:
cmp eax, 0 ; see if we're at the last element, 0
je loop_exit ; if we are, jump to the loop exit
inc edi ; increment the index
mov eax, [edi * 4 + array] ; set the current element to the new element
cmp eax, ebx ; compare eax to ebx
jle start_loop ; if the new element is less than the max, continue the looping
mov ebx, eax ; otherwise set the new max to the current element
jmp start_loop ; whether or not we executed the previous instruction, we
; still need to go back to the beginning of the loop
loop_exit: ; return the max element using the exit system call
mov eax, 1 ; set the system call to exit (1)
int 80h ; ebx is already set to the max element
# find the maximum number in a list
#
# variables
# edi - Holds the index of the array
# ebx - Largest number
# eax - Current element
.section .data
array:
.long 3,67,34,222,45,75,54,34,44,33,22,11,66,0
.section .text
.globl _start
_start:
movl $0, %edi # set the index to 0
movl array(,%edi,4), %eax # set to the first index (array + edi * 4)
movl %eax, %ebx # the largest number starts out as the first element
start_loop: # the loop for searching the array and setting the max element
cmpl $0, %eax # check to see if we're at the end (0)
je loop_exit # if so exit the loop
incl %edi
movl array(,%edi,4), %eax # load the current element
cmpl %ebx, %eax # compare eax to ebx
jle start_loop
movl %eax, %ebx # set the current element to the new largest index
jmp start_loop
loop_exit:
# return the maximum number as the exit code
movl $1, %eax
int $0x80
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment