the program find max number from a list of numbers, using assembly language on x86 architecture,
code:
max_num2.s:
# find max number in a list of numbers, loop reverse, # and don't need the register that record total_count, # # registers: # %eax store each number to compare # %ebx the max number # %edi index of number during comparation .section .data num_items: .long 3,67,34,222,45,75,54,34,44,33,22,11,66 num_count: .long 13 .section .text .globl _start _start: movl num_items, %ebx movl num_count, %edi decl %edi loop: cmp $0, %edi # detect whether reach end jl exit movl num_items(,%edi,4), %eax # read next number decl %edi # decrease index by 1 cmp %eax, %ebx jge loop movl %eax, %ebx jmp loop exit: movl $1, %eax int $0x80
compile:
as max_num2.s -o a.o
ld a.o -o a.out
run:
./a.out
get result: (the result is stored in %ebx, then call exit syscall, which read status code from this register)
echo $?