Skip to content

Instantly share code, notes, and snippets.

@jtgi
Created January 20, 2014 17:50
Show Gist options
  • Select an option

  • Save jtgi/8525179 to your computer and use it in GitHub Desktop.

Select an option

Save jtgi/8525179 to your computer and use it in GitHub Desktop.
find max int in an array.
.pos 0x100
main: irmovl bottom, %esp # initialize stack
irmovl a, %edi # address of the first element of a
irmovl alen, %esi
mrmovl (%esi), %esi # number of elements of a
irmovl $0x1, %eax
subl %eax, %esi # last index in a
# ready to call findmax: a --> edi, last --> esi
call findmax
halt
# Find position of the maximum element in an array.
.pos 0x200
findmax:
pushl %edi # save *a
pushl %esi # save n
rrmovl %esi, %edx # edx = n
irmovl $4, %eax # eax = 4
mull %eax, %edx # n * 4
addl %edi, %edx # esi = *a + n
mrmovl (%edx), %ecx # ecx = max = a[n]
pushl %ecx # save max = a[n]
pushl %esi # save pos
irmovl $0, %ecx
addl %ecx, %esi # n <= 0? set result reg.
jle end
loop:
irmovl $1, %edx
subl %edx, %esi # n--
rrmovl %esi, %edx # edx = n
irmovl $4, %eax # 4 * n
mull %edx, %eax
addl %edi, %eax # eax = *a + n
mrmovl (%eax), %edx # edx = a[n]
mrmovl 4(%esp), %ecx # ecx = max
subl %edx, %ecx # ecx = max - x, set result reg
jg check_loop # if max - x > 0, loop
rmmovl %edx, 4(%esp) # max = x
rmmovl %esi, (%esp) # pos = n
check_loop:
irmovl $0, %edx
subl %edx, %esi # n - 0 , set result reg
jg loop
end:
popl %eax # set return val to pos
popl %esi # pop off max
popl %esi # restore args
popl %edi
ret
#
# Array to sort
#
.pos 0x1000
a: .long 30
.long 9
.long 21
.long 13
.long 6
.long 26
.long 35
.long 32
.long 15
.long 17
alen: .long 10
#
# Stack (256 thirty-two bit words is more than enough here).
#
.pos 0x3000
top: .long 0x00000000, 0x100 # top of stack.
bottom: .long 0x00000000 # bottom of stack.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment