69,373
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
char *power_2(int n, char *pos)
{
static unsigned int d[750000], w=10000;
unsigned int x = n / 18, t=1U<<18, b=0, g=0, i;
d[0] = 1 << (n % 18);
if (d[0] > w) {
d[++b] = d[0]/w;
d[0] %= w;
}
while (x--) {
for (i=0; i<=b; i++) {
d[i] = d[i] * t + g;
g = d[i] / w;
d[i] %= w;
}
while (g) {
d[++b] = g%w;
g /= w;
}
}
i = sprintf(pos, "%u", d[b]);
if (i<2 && b)
sprintf(pos+1, "%u", d[--b]);
pos[2] = 0;
return pos;
}
int main()
{
char pos[16];
puts(power_2(4000000, pos));
return 0;
}
; Listing generated by Microsoft (R) Optimizing Compiler Version 15.00.30729.01
TITLE e:\Projects\Power2\Power2\Power2.c
.686P
.XMM
include listing.inc
.model flat
INCLUDELIB OLDNAMES
PUBLIC ??_C@_02GMHACPFF@?$CFu?$AA@ ; `string'
EXTRN @__security_check_cookie@4:PROC
EXTRN __imp__sprintf:PROC
EXTRN __imp__puts:PROC
; COMDAT ??_C@_02GMHACPFF@?$CFu?$AA@
CONST SEGMENT
??_C@_02GMHACPFF@?$CFu?$AA@ DB '%u', 00H ; `string'
?w@?1??power_2@@9@9 DD 02710H ; `power_2'::`2'::w
PUBLIC _power_2
?d@?1??power_2@@9@9 DD 0b71b0H DUP (?) ; `power_2'::`2'::d
; Function compile flags: /Ogtpy
; File e:\projects\power2\power2\power2.c
; COMDAT _power_2
_TEXT SEGMENT
_x$ = 8 ; size = 4
_pos$ = 8 ; size = 4
_power_2 PROC ; COMDAT
; 4 : {
push ebp
mov ebp, DWORD PTR _pos$[esp]
push esi
push edi
; 5 : static unsigned int d[750000], w=10000;
; 6 : unsigned int x = n / 18, t=1U<<18, b=0, g=0, i;
xor edi, edi
mov eax, 55555 ; 0000d903H
xor esi, esi
; 7 : d[0] = 1 << (n % 18);
mov DWORD PTR ?d@?1??power_2@@9@9, 1024 ; 00000400H
push ebx
npad 5
$LL8@power_2:
; 8 : if (d[0] > w) {
; 9 : d[++b] = d[0]/w;
; 10 : d[0] %= w;
; 11 : }
; 12 : while (x--) {
dec eax
mov DWORD PTR _x$[esp+12], eax
; 13 : for (i=0; i<=b; i++) {
xor ecx, ecx
npad 9
$LL24@power_2:
; 14 : d[i] = d[i] * t + g;
mov eax, DWORD PTR ?d@?1??power_2@@9@9[ecx*4]
shl eax, 18 ; 00000012H
lea edx, DWORD PTR [eax+esi]
mov DWORD PTR ?d@?1??power_2@@9@9[ecx*4], edx
; 15 : g = d[i] / w;
mov eax, -776530087 ; d1b71759H
mul edx
; 16 : d[i] %= w;
mov eax, DWORD PTR ?d@?1??power_2@@9@9[ecx*4]
shr edx, 13 ; 0000000dH
mov esi, edx
xor edx, edx
mov ebx, 10000 ; 00002710H
div ebx
inc ecx
mov DWORD PTR ?d@?1??power_2@@9@9[ecx*4-4], edx
cmp ecx, edi
jbe SHORT $LL24@power_2
; 17 : }
; 18 : while (g) {
test esi, esi
je SHORT $LN25@power_2
$LL3@power_2:
; 19 : d[++b] = g%w;
mov eax, -776530087 ; d1b71759H
mul esi
shr edx, 13 ; 0000000dH
mov ecx, edx
imul ecx, 10000 ; 00002710H
sub esi, ecx
inc edi
mov DWORD PTR ?d@?1??power_2@@9@9[edi*4], esi
; 20 : g /= w;
mov esi, edx
test esi, esi
jne SHORT $LL3@power_2
$LN25@power_2:
; 8 : if (d[0] > w) {
; 9 : d[++b] = d[0]/w;
; 10 : d[0] %= w;
; 11 : }
; 12 : while (x--) {
mov eax, DWORD PTR _x$[esp+12]
test eax, eax
jne SHORT $LL8@power_2
; 21 : }
; 22 : }
; 23 : i = sprintf(pos, "%u", d[b]);
mov edx, DWORD PTR ?d@?1??power_2@@9@9[edi*4]
mov esi, DWORD PTR __imp__sprintf
push edx
push OFFSET ??_C@_02GMHACPFF@?$CFu?$AA@
push ebp
call esi
add esp, 12 ; 0000000cH
pop ebx
; 24 : if (i<2 && b)
cmp eax, 2
jae SHORT $LN23@power_2
test edi, edi
je SHORT $LN23@power_2
; 25 : sprintf(pos+1, "%u", d[--b]);
mov eax, DWORD PTR ?d@?1??power_2@@9@9[edi*4-4]
push eax
lea ecx, DWORD PTR [ebp+1]
push OFFSET ??_C@_02GMHACPFF@?$CFu?$AA@
push ecx
call esi
add esp, 12 ; 0000000cH
$LN23@power_2:
pop edi
pop esi
; 26 : pos[2] = 0;
mov BYTE PTR [ebp+2], 0
; 27 : return pos;
mov eax, ebp
pop ebp
; 28 : }
ret 0
_power_2 ENDP
_TEXT ENDS
PUBLIC __$ArrayPad$
PUBLIC _main
EXTRN ___security_cookie:DWORD
; Function compile flags: /Ogtpy
; COMDAT _main
_TEXT SEGMENT
_pos$ = -20 ; size = 16
__$ArrayPad$ = -4 ; size = 4
_main PROC ; COMDAT
; 31 : {
sub esp, 20 ; 00000014H
mov eax, DWORD PTR ___security_cookie
xor eax, esp
mov DWORD PTR __$ArrayPad$[esp+20], eax
; 32 : char pos[16];
; 33 : puts(power_2(1000000, pos));
lea eax, DWORD PTR _pos$[esp+20]
push eax
call _power_2
push eax
call DWORD PTR __imp__puts
; 34 : return 0;
; 35 : }
mov ecx, DWORD PTR __$ArrayPad$[esp+28]
add esp, 8
xor ecx, esp
xor eax, eax
call @__security_check_cookie@4
add esp, 20 ; 00000014H
ret 0
_main ENDP
_TEXT ENDS
END
; Listing generated by Microsoft (R) Optimizing Compiler Version 15.00.30729.01
TITLE e:\Projects\Power2\Power2\Power2.c
.686P
.XMM
include listing.inc
.model flat
INCLUDELIB OLDNAMES
EXTRN @__security_check_cookie@4:PROC
EXTRN __imp__sprintf:PROC
EXTRN __imp__puts:PROC
?w@?1??power_2@@9@9 DD 02710H ; `power_2'::`2'::w
$SG-5 DB '%u', 00H
ORG $+1
$SG-6 DB '%u', 00H
PUBLIC _power_2
?d@?1??power_2@@9@9 DD 0b71b0H DUP (?) ; `power_2'::`2'::d
; Function compile flags: /Ogtpy
; File e:\projects\power2\power2\power2.c
; COMDAT _power_2
_TEXT SEGMENT
_pos$ = 8 ; size = 4
_power_2 PROC ; COMDAT
; 4 : {
push ebx
push ebp
push esi
push edi
; 5 : static unsigned int d[750000], w=10000;
; 6 : unsigned int x = n / 18, t=1U<<18, b=0, g=0, i;
xor edi, edi
mov ebp, 55555 ; 0000d903H
xor esi, esi
; 7 : d[0] = 1 << (n % 18);
mov DWORD PTR ?d@?1??power_2@@9@9, 1024 ; 00000400H
npad 9
$LL8@power_2:
; 8 : if (d[0] > w) {
; 9 : d[++b] = d[0]/w;
; 10 : d[0] %= w;
; 11 : }
; 12 : while (x--) {
dec ebp
; 13 : for (i=0; i<=b; i++) {
xor ecx, ecx
$LL24@power_2:
; 14 : d[i] = d[i] * t + g;
mov eax, DWORD PTR ?d@?1??power_2@@9@9[ecx*4]
shl eax, 18 ; 00000012H
lea edx, DWORD PTR [eax+esi]
mov DWORD PTR ?d@?1??power_2@@9@9[ecx*4], edx
; 15 : g = d[i] / w;
mov eax, -776530087 ; d1b71759H
mul edx
; 16 : d[i] %= w;
mov eax, DWORD PTR ?d@?1??power_2@@9@9[ecx*4]
shr edx, 13 ; 0000000dH
mov esi, edx
xor edx, edx
mov ebx, 10000 ; 00002710H
div ebx
inc ecx
mov DWORD PTR ?d@?1??power_2@@9@9[ecx*4-4], edx
cmp ecx, edi
jbe SHORT $LL24@power_2
; 17 : }
; 18 : while (g) {
test esi, esi
je SHORT $LN25@power_2
$LL3@power_2:
; 19 : d[++b] = g%w;
mov eax, -776530087 ; d1b71759H
mul esi
shr edx, 13 ; 0000000dH
mov ecx, edx
imul ecx, 10000 ; 00002710H
sub esi, ecx
inc edi
mov DWORD PTR ?d@?1??power_2@@9@9[edi*4], esi
; 20 : g /= w;
mov esi, edx
test esi, esi
jne SHORT $LL3@power_2
$LN25@power_2:
; 8 : if (d[0] > w) {
; 9 : d[++b] = d[0]/w;
; 10 : d[0] %= w;
; 11 : }
; 12 : while (x--) {
test ebp, ebp
jne SHORT $LL8@power_2
; 21 : }
; 22 : }
; 23 : i = sprintf(pos, "%u", d[b]);
mov edx, DWORD PTR ?d@?1??power_2@@9@9[edi*4]
mov ebx, DWORD PTR _pos$[esp+12]
mov esi, DWORD PTR __imp__sprintf
push edx
push OFFSET $SG-5
push ebx
call esi
add esp, 12 ; 0000000cH
; 24 : if (i<2 && b)
cmp eax, 2
jae SHORT $LN23@power_2
test edi, edi
je SHORT $LN23@power_2
; 25 : sprintf(pos+1, "%u", d[--b]);
mov eax, DWORD PTR ?d@?1??power_2@@9@9[edi*4-4]
push eax
lea ecx, DWORD PTR [ebx+1]
push OFFSET $SG-6
push ecx
call esi
add esp, 12 ; 0000000cH
$LN23@power_2:
pop edi
pop esi
pop ebp
; 26 : pos[2] = 0;
mov BYTE PTR [ebx+2], 0
; 27 : return pos;
mov eax, ebx
pop ebx
; 28 : }
ret 0
_power_2 ENDP
_TEXT ENDS
PUBLIC __$ArrayPad$
PUBLIC _main
EXTRN ___security_cookie:DWORD
; Function compile flags: /Ogtpy
; COMDAT _main
_TEXT SEGMENT
_pos$ = -20 ; size = 16
__$ArrayPad$ = -4 ; size = 4
_main PROC ; COMDAT
; 31 : {
sub esp, 20 ; 00000014H
mov eax, DWORD PTR ___security_cookie
xor eax, esp
mov DWORD PTR __$ArrayPad$[esp+20], eax
; 32 : char pos[16];
; 33 : puts(power_2(1000000, pos));
lea eax, DWORD PTR _pos$[esp+20]
push eax
call _power_2
push eax
call DWORD PTR __imp__puts
; 34 : return 0;
; 35 : }
mov ecx, DWORD PTR __$ArrayPad$[esp+28]
add esp, 8
xor ecx, esp
xor eax, eax
call @__security_check_cookie@4
add esp, 20 ; 00000014H
ret 0
_main ENDP
_TEXT ENDS
END
.file "k.c"
.section .rodata.str1.1,"aMS",@progbits,1
.LC0:
.string "%u"
.text
.p2align 4,,15
.globl power_2
.type power_2, @function
power_2:
pushl %ebp
movl $954437177, %edx
movl %esp, %ebp
pushl %edi
xorl %edi, %edi
pushl %esi
pushl %ebx
subl $44, %esp
movl 8(%ebp), %ebx
movl %ebx, %eax
imull %edx
movl %ebx, %eax
sarl $31, %eax
sarl $2, %edx
subl %eax, %edx
movl $1, %eax
leal (%edx,%edx,8), %ecx
movl %ebx, %edx
addl %ecx, %ecx
subl %ecx, %edx
movl %edx, %ecx
sall %cl, %eax
cmpl $10000, %eax
movl %eax, d.1844
jbe .L3
movl $10000, %ecx
xorl %edx, %edx
divl %ecx
movw $1, %di
movl %eax, d.1844+4
movl %edx, d.1844
.L3:
movl %ebx, %eax
movl $954437177, %ecx
imull %ecx
sarl $31, %ebx
sarl $2, %edx
subl %ebx, %edx
movl %edx, -28(%ebp)
movl -28(%ebp), %eax
testl %eax, %eax
je .L15
.p2align 4,,7
.p2align 3
.L8:
xorl %ecx, %ecx
xorl %ebx, %ebx
movl %ecx, %edx
.p2align 4,,7
.p2align 3
.L4:
movl d.1844(,%ebx,4), %esi
movl $-776530087, %eax
sall $18, %esi
leal (%edx,%esi), %esi
mull %esi
shrl $13, %edx
imull $10000, %edx, %eax
subl %eax, %esi
movl %esi, d.1844(,%ebx,4)
addl $1, %ebx
cmpl %ebx, %edi
jae .L4
testl %edx, %edx
movl %edx, %ecx
je .L5
leal 1(%edi), %ebx
leal d.1844(,%ebx,4), %esi
jmp .L6
.p2align 4,,7
.p2align 3
.L16:
addl $1, %ebx
.L6:
movl $-776530087, %eax
movl %ebx, %edi
mull %ecx
shrl $13, %edx
imull $10000, %edx, %eax
subl %eax, %ecx
movl %ecx, (%esi)
addl $4, %esi
testl %edx, %edx
movl %edx, %ecx
jne .L16
.L5:
subl $1, -28(%ebp)
movl -28(%ebp), %eax
testl %eax, %eax
jne .L8
.L15:
movl d.1844(,%edi,4), %eax
movl $.LC0, 4(%esp)
movl %eax, 8(%esp)
movl 12(%ebp), %eax
movl %eax, (%esp)
call sprintf
cmpl $1, %eax
ja .L9
testl %edi, %edi
je .L9
movl d.1844-4(,%edi,4), %eax
movl $.LC0, 4(%esp)
movl %eax, 8(%esp)
movl 12(%ebp), %eax
addl $1, %eax
movl %eax, (%esp)
call sprintf
.L9:
movl 12(%ebp), %edx
movb $0, 2(%edx)
movl %edx, %eax
addl $44, %esp
popl %ebx
popl %esi
popl %edi
popl %ebp
ret
.size power_2, .-power_2
.p2align 4,,15
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
andl $-16, %esp
subl $32, %esp
leal 16(%esp), %eax
movl %eax, 4(%esp)
movl $4000000, (%esp)
call power_2
movl %eax, (%esp)
call puts
xorl %eax, %eax
leave
ret
.size main, .-main
.local d.1844
.comm d.1844,3000000,32
.ident "GCC: (Debian 4.4.2-5) 4.4.2"
.section .note.GNU-stack,"",@progbits