Skip to content

Instantly share code, notes, and snippets.

@ox
Created March 28, 2012 17:17
Show Gist options
  • Save ox/2228371 to your computer and use it in GitHub Desktop.
Save ox/2228371 to your computer and use it in GitHub Desktop.
The Y86 version of the max(int numbers[], int len) function. Finds the greatest number in an array of ints. Comments are... non-standard.
.text
.globl max
max:
pushl %ebp
movl %esp, %ebp
mrmovl 8(%esp), %ecx
rrmovl %ecx, %eax #; pointer to numbers[]
mrmovl (%ecx), %ecx #; ecx holds greatest encountered
#; number
mrmovl 12(%ebp), %edx #; len/counter
irmovl $1, %esi #; incrimenter
irmovl $4, %edi #; another incrimenter
jmp .compare
.new_greatest:
mrmovl (%eax), %ecx #; change greatest to value (%eax)
.loop:
subl %esi, %edx #; decrease counter
addl %edi, %eax #; incriment %eax by 4 to get next num
mrmovl (%eax), %ebx #; ebx holds value (%eax)
subl %ecx, %ebx #; greatest - ebx
jg .new_greatest #; if greater, then %ebx > %ecx
.compare
subl %esi, %edx #; subtract 1 from counter
jg .loop #; if counter > 0, loop again
rrmovl %ecx, %eax #; move highest into return space
popl %ebp
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment