/* $begin code-yso */ /* $begin code-ysa */ # This version makes use of the iaddl instruction # Execution begins at address 0 .pos 0 init: irmovl Stack, %esp # Set up Stack pointer irmovl Stack, %ebp # Set up base pointer jmp Main # Execute main program # Array of 4 elements .align 4 array: .long 0xd .long 0xc0 .long 0xb00 .long 0xa000 Main: irmovl $4,%eax pushl %eax # Push 4 irmovl array,%edx pushl %edx # Push array call Sum # Sum(array, 4) halt # int Sum(int *Start, int Count) Sum: pushl %ebp rrmovl %esp,%ebp mrmovl 8(%ebp),%ecx # ecx = Start mrmovl 12(%ebp),%edx # edx = Count irmovl $0, %eax # sum = 0 cmp %eax,%edx jle End Loop: rmxchg %esi,(%ecx) # get *Start addl %esi,%eax # add to sum rmmovl %esi,(%ecx) iaddl $4,%ecx # Sum++ iaddl $-1,%edx # Count-- irmovl $0,%ebx cmp %ebx,%edx jg Loop # Stop when 0 End: popl %ebp ret .pos 0x100 Stack: # The stack goes here /* $end code-ysa */ /* $end code-yso */