Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .file "prime.c"
- .text
- .globl square
- .type square, @function
- square:
- pushl %esp ;should be pushl %ebp
- movl %esp, %ebp ;setup
- movl 12(%ebp), %eax ;copies value from mem location whose address is +12 away from content of ebp into eax
- imull 8(%ebp), %eax ;multiplies the content of mem location whose address is +8 away from content of ebp by eax,
- ;and stores it in eax
- popl %ebp ;finish
- ret
- .size square, .-square
- .globl isPrime
- .type isPrime, @function
- isPrime:
- pushl %ebp ;setup
- movl %esp, %ebp ;setup
- subl $36, %esp ;subtracts the 36 from content in esp and stores it in esp
- movl 8(%ebp), %eax ;copies the content of mem location whose address is +8 away from content of ebp to eax
- andl $1, %eax ;compares content of eax with 1
- testl %eax, %eax ;**Possible error** eax&eax; ZF (zero flag) set when eax&eax == 0, SF (sign flag) when < 0
- jmp .L4 ;jump to .L4
- cmpl $2, 8(%ebp) ;compares the content of mem location whose address is +8 away from ebp to 2
- jne .L6 ;if the numbers are not equal, jump to .L6
- .L4:
- movl 8(%ebp), %eax ;copies the content of mem location whose address is +8 away from ebp into eax
- movl %eax, -28(%ebp) ;copies the content of eax to the mem location -32 away from ebp
- movl $1431655766, -32(%ebp) ;I have a feeling this is wrong, copies 1431655766 to the mem location -32 away from ebp
- movl -32(%ebp), %eax ;copies the content of mem location whose address is -32 away from ebp to eax
- imull -28(%ebp) ;I also feel this is wrong, imull requires 2 arguments no?
- movl %edx, %ecx ;copies the contents of edx to ecx
- movl -28(%ebp), %eax ;copies the content of mem location -28 away from ebp to eax
- sarl $31, %eax ;shift the content of eax to the right 31 bits and stores result in eax
- movl %ecx, %edx ;copies content of ecx to edx
- subl %eax, %edx ;subtracts eax from edx and stores the result in edx
- movl %edx, -24(%ebp) ;copies the contents of edx to the mem location whose address is -24 away from ebp
- movl -24(%ebp), %eax ;copies the content of mem location whose address is -24 away from ebp to eax
- addl %eax, %eax ;adds eax to eax and stores it in eax
- addl -24(%ebp), %eax ;adds the contents of the mem location whose address is -24 away from ebp to eax and the result is stored in eax
- movl -28(%ebp), %ecx ;adds the contents of the mem location whose address is -28 away from ebp to eax and the result is stored in eax
- subl %eax, %ecx ;subtracts content of eax from ecx and stores the result in ecx
- movl %ecx, -24(%ebp) ;copies the content of ecx to the mem location whose address is -24 away from ebp
- cmpl $0, -24(%ebp) ;compares the content of the mem location whose address is -24 away from ebp to 0
- jne .L7 ;if it is not 0, jump to .L7
- cmpl $3, 8(%ebp) ;compares the content of the mem location whose address is 8 away from ebp to 3
- jne .L6 ;if not 3, jump to .L6 (?) Not 3/not zero?
- .L7:
- cmpl $1, 8(%ebp) ;compares the content of the mem location whose address is 8 away from ebp to 1
- jg .L9 ;if greater than 1, jump to .L9
- .L6:
- movl $0, -20(%ebp) ;copies 0 to the mem location whose address is -20 from ebp
- jmp .L10 ;jumps to .L10
- .L9:
- movl $1, -8(%ebp) ;copies 1 to the mem location whose address is -8 away from ebp
- jmp .L11 ;jump to .L11
- .L12:
- movl -8(%ebp), %edx ;copies the content of the mem location whose address is -8 from ebp to edx
- movl %edx, %eax ;copies content of edx to eax
- addl %eax, %eax ;adds eax to eax and stores in eax
- addl %edx, %eax ;adds edx to eax and stores in eax
- addl %eax, %eax ;adds eax to eax and stores in eax
- movl %eax, -4(%ebp) ;copies content of eax to mem location whose address is -4 away from ebp
- movl -4(%ebp), %eax ;copies the content of the mem location whose address is -4 from ebp to eax (?)
- subl $1, %eax ;subracts 1 from eax and stores result in eax
- movl 8(%ebp), %edx ;copies the content of the mem location whose address is 8 away from ebp to edx
- movl %eax, %ecx ;copies eax to ecx
- movl %edx, %eax ;copies edx to eax
- sarl $31, %edx ;shifts edx to the right 31 bits and stores result in edx
- idivl %ecx ;?? divides the contents of edx:eax by contents of ecx, quotient into eax and remainder in edx
- movl %edx, %eax ;copies edx to eax
- testl %eax, %eax ;? eax&eax; ZF set when eax&eax == 0, SF when < 0
- jne .L13 ;? ~ZF (not equal/not zero) jump to .L13
- movl -4(%ebp), %eax ;copies contents of mem location whose address is -4 away from ebp to eax
- subl $1, %eax ;subtracts 1 from eax and stores result in eax
- cmpl 8(%ebp), %eax ;compares the content of the mem location whose address is 8 away from ebp to eax; eax(a)-8(ebp)(b); ZF set if a==b, SF set if <0, OF set if two's complement overflow
- je .L13 ;if zero, jump to .L13
- movl $0, -20(%ebp) ;copies 0 to the mem location whose address is -20 from ebp
- jmp .L10 ;jump to .L10
- .L13:
- movl -4(%ebp), %eax ;copies the contents of mem location whose address is -4 from ebp to eax
- addl $1, %eax ;adds 1 to eax;
- movl 8(%ebp), %edx ;copies the content of the mem location whose address is 8 away from ebp to edx
- movl %eax, %ecx ;copies eax to ecx
- movl %edx, %eax ;copies edx to eax
- sarl $31, %edx ;shifts content of edx right 31 bits and stores in edx
- idivl %ecx ;divides contents of edx:eax by by contents of ecx
- movl %edx, %eax ;copies edx to eax
- testl %eax, %eax ;?eax&eax; ZF set when eax&eax == 0, SF when < 0
- jne .L16 ;~ZF not equal/not zero jump to .L16
- movl -4(%ebp), %eax ;copies contents of mem location whose address is -4 from ebp to eax
- addl $1, %eax ;adds 1 to eax
- cmpl 8(%ebp), %eax ;compares the contents of the mem location whose address is 8 from ebp to eax
- je .L16 ;if zero, jump to .L16
- movl $0, -20(%ebp) ;copies 0 to the mem location whose address is -20 from ebp
- jmp .L10 ;jump to .L10
- .L16:
- addl $1, -8(%ebp) ;adds 1 to the mem location whose address is -8 from ebp
- .L11:
- movl -8(%ebp), %edx ;copies the contents on the mem location whose address is -8 from ebp to edx
- movl %edx, %eax ;edx to eax (eax = edx)
- addl %eax, %eax ;adds eax to eax, stores in eax (eax =+ eax)
- addl %edx, %eax ;adds edx to eax, stores in eax
- addl %eax, %eax ;adds eax to eax, stores in eax
- subl $7, %eax ;subtracts 7 from eax
- movl %eax, (%esp) ;copies eax to the mem location esp
- call square ;calls square function
- cmpl 8(%ebp), %eax ;compares the contents of mem location whose address is 8 away from ebp to eax
- jl .L12 ;if less than, jump to .L12
- movl $1, -20(%ebp) ;copies 1 to mem location whose address is -20 from ebp
- .L10:
- movl -20(%ebp), %eax ;copies content of mem location whose address is -20 from ebp to eax
- leave ;finish
- ret ;finish
- .size isPrime, .-isPrime
- .section .rodata
- .LC0:
- .string "Prime %d is %d\n"
- .text
- .globl main
- .type main, @function
- main:
- leal 4(%esp), %ecx ;4+esp stored in ecx (ecx = 4+esp)
- andl $-16, %esp ;? esp = esp&-16
- pushl -4(%ecx) ;puts the contents of the mem location whose address is -4 from ecx onto the stack
- pushl %ebp ;puts ebp on the stack
- movl %ebp, %esp ;esp = ebp
- pushl %ecx ;puts ecx on the stack
- subl $36, %esp ;subtracts 36 from esp, stores in esp
- movl 4(%ecx), %eax ;copies content of mem location whose address is 4 away from ecx to eax
- addl $0, %eax ;?? adds 0 to eax
- movl (%eax), %eax ;?? copies the content of mem location of eax to eax
- movl %eax, (%esp) ;copies eax to the mem location of esp
- call atoi ;calls atoi (converts string to int)
- movl %eax, -16(%ebp) ;copies eax to the mem location whose address is -16 from ebp
- movl $0, -12(%ebp) ;copies 0 into the mem location whose address is -12 from ebp
- movl $1, -8(%ebp) ;copies 1 into the mem location whose address is -8 from ebp
- jmp .L27 ;jump to .L27
- .L23:
- addl $1, -8(%ebp) ;adds 1 to the mem location whose address is -8 from ebp, stores in mem location
- movl -8(%ebp), %eax ;copies the content of the mem location whose address is -8 from ebp to eax
- movl %eax, (%esp) ;copies eax to mem location of esp
- call isPrime ;call isPrime function
- testl %eax, %eax ;eax = eax&eax
- je .L22 ;if equal/zero jumo to .L22
- addl $1, -12(%ebp) ;? add 1 to the mem location whose address is -12 from ebp, store in mem location
- .L22:
- .L27:
- movl -12(%ebp), %eax ;copies the content of mem location whose address is -12 from ebp to eax
- cmpl -16(%ebp), %eax ;compares content of mem location whose address is -16 from ebp to eax
- jl .L23 ;if <, then jump to .L23
- movl -8(%ebp), %eax ;copies content of mem location whose address is -8 from ebp to eax
- movl %eax, 8(%esp) ;copies eax to mem location whose address is 8 from esp
- movl -16(%ebp), %eax ;copies content of mem location whose address is -16 from ebp to eax
- movl %eax, 4(%esp) ;copies eax to mem location whose address is 4 from esp
- movl $.LC0, (%esp) ;copies .LC0 to mem location of esp
- call printf ;print function
- movl $0, %eax ;copies 0 to eax
- addl $36, %esp ;adds 36 to esp, stored in esp
- popl %ecx ;finish
- popl %ebp ;finish
- leal -4(%ecx), %esp ;**possible error** esp = -4+ecx
- ret ;finish
- .size main, .-main
- .ident "GCC: (GNU) 4.1.2 20080704 (Red Hat 4.1.2-51)"
- .section .note.GNU-stack,"",@progbits
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement