#---------------------------------------------------------------------------- # prime.s # by TC Bressoud #---------------------------------------------------------------------------- # Start with the data section. Define the strings used # by the program. Global integer and float data would have # similar definitions. .data astr: .string "Enter a: " # The .string directive zero terminates s: .string "%d" result: .string "isPrime(%d) = %d\n" bye: .string "Leaving now" # Group together all the executable code in the .text section .text .globl main main: # Prologue pushl %ebp movl %esp, %ebp andl $0xffffff00, %esp # align esp on 16 byte boundary to begin subl $32, %esp movl $astr, 4(%esp) # printf(astr) movl $1, (%esp) call __printf_chk leal 28(%esp), %eax # scanf(s, &a), where a is in the stack frame movl %eax, 4(%esp) movl $s, (%esp) call __isoc99_scanf movl 28(%esp),%eax movl %eax, (%esp) movl $2,4(%esp) call isPrime movl %eax,12(%esp) movl 28(%esp),%eax movl %eax, 8(%esp) movl $result, 4(%esp) movl $1, (%esp) call __printf_chk # Epilogue leave ret .globl isPrime isPrime: #Prologue pushl %ebp movl %esp, %ebp pushl %esi pushl %edi subl $32, %esp # Register usage # %esi: p # %edi: i movl 8(%ebp),%esi movl 12(%ebp),%edi movl %edi,%eax imull %eax,%eax cmpl %eax,%esi js ret1 movl $0,%edx movl %esi,%eax idivl %edi testl %edx,%edx jz ret0 incl %edi movl %edi,4(%esp) movl %esi,(%esp) call isPrime jmp epilogue ret0: movl $0,%eax jmp epilogue ret1: movl $1,%eax epilogue: addl $32,%esp pop %edi pop %esi leave ret