Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
esrrhs committed Apr 13, 2020
1 parent 1c30643 commit 9d0809f
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 117 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
```

# 编译
* 先写c,然后编译成汇编main-src.s
* 下载最新gcc9.3,先写c,然后编译成汇编main-src.s
```
docker pull gcc:latest
docker run --rm -v "$PWD":/home/project/selfmd5 -w /home/project/selfmd5 gcc:latest gcc -S main-src.c -Os -mavx -msse -mavx2 -ffast-math -fsingle-precision-constant -fno-verbose-asm -fno-unroll-loops -fno-asynchronous-unwind-tables
gcc -S main-src.c -Os -mavx -msse -mavx2 -ffast-math -fsingle-precision-constant -fno-verbose-asm -fno-unroll-loops -fno-asynchronous-unwind-tables
```
* 然后把main.s的依赖libc的部分干掉,换成直接syscall
* 精简main-src.s的无用代码,编译,并sstrip
Expand Down
49 changes: 19 additions & 30 deletions main-src.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,41 +29,33 @@ static long double fsin_my(long double a) {
return (res) > 0 ? res : -res;
}

int main(int argc, char *argv[]) {
int filedesc = open(argv[0], O_RDONLY, 400);
typedef unsigned int v4si __attribute__ ((vector_size (16)));

int main(int argc, char *argv[]) {
char data[1024];
int len = read(filedesc, data, sizeof(data));
register short len = read(open(argv[0], 0, 0), data, sizeof(data));

unsigned int hash[STATE_LEN];
v4si hash = {(unsigned int) (0x67452301), (unsigned int) (0xEFCDAB89), (unsigned int) (0x98BADCFE),
(unsigned int) (0x10325476)};

hash[0] = (unsigned int) (0x67452301);
hash[1] = (unsigned int) (0xEFCDAB89);
hash[2] = (unsigned int) (0x98BADCFE);
hash[3] = (unsigned int) (0x10325476);
int new_len = ((((len + 8) / 64) + 1) * 64) - 8;
register short new_len = ((((len + 8) / 64) + 1) * 64) - 8;
data[len] = 0x80;
*(unsigned long long *) (data + new_len) = len << 3;
int off = 0;
register short off = 0;
for (off = 0; off < new_len; off += BLOCK_LEN) {
unsigned int *m = (unsigned int *) &data[off];

unsigned int tmp[4];
register unsigned int *m = (unsigned int *) &data[off];

tmp[0] = hash[0];
tmp[1] = hash[1];
tmp[2] = hash[2];
tmp[3] = hash[3];
v4si tmp = hash;

const char p1[] = {0, 3, 2, 1};
const char mmstart[] = {0, 1, 5, 0};
const char mmstep[] = {1, 5, 3, 7};
const char ss[] = {7, 12, 17, 22, 5, 9, 14, 20, 4, 11, 16, 23, 6, 10, 15, 21};
for (int i = 0; i < 64; ++i) {
unsigned int inc;
unsigned int b = tmp[p1[(i + 3) % 4]];
unsigned int c = tmp[p1[(i + 2) % 4]];
unsigned int d = tmp[p1[(i + 1) % 4]];
register unsigned int inc;
register unsigned int b = tmp[p1[(i + 3) % 4]];
register unsigned int c = tmp[p1[(i + 2) % 4]];
register unsigned int d = tmp[p1[(i + 1) % 4]];
switch (i / 16) {
case 0:
inc = F(b, c, d);
Expand All @@ -79,23 +71,20 @@ int main(int argc, char *argv[]) {
break;
}

unsigned int mm = m[(mmstart[i / 16] + (i % 16) * mmstep[i / 16]) % 16];
unsigned int s = ss[(i / 16) * 4 + (i % 4)];
unsigned int t = (unsigned int) ((unsigned long long) 4294967296 * fsin_my(i + 1));
register unsigned int mm = m[(mmstart[i / 16] + (i % 16) * mmstep[i / 16]) % 16];
register unsigned int s = ss[(i / 16) * 4 + (i % 4)];
register unsigned int t = (unsigned int) ((unsigned long long) 4294967296 * fsin_my(i + 1));

tmp[p1[i % 4]] += inc + mm + t;
tmp[p1[i % 4]] = b + ROTLEFT(tmp[p1[i % 4]], s);
}

hash[0] += tmp[0];
hash[1] += tmp[1];
hash[2] += tmp[2];
hash[3] += tmp[3];
hash += tmp;
}

unsigned char *buf = (unsigned char *) &hash;
register unsigned char *buf = (unsigned char *) &hash[0];
for (unsigned char i = 0; i < 32; i++) {
int a = (buf[i / 2] >> (4 * (1 - i % 2))) & 0xF;
register char a = (buf[i / 2] >> (4 * (1 - i % 2))) & 0xF;
char c = a >= 10 ? 'a' + (a - 10) : '0' + a;
write(1, &c, 1);
}
Expand Down
165 changes: 81 additions & 84 deletions main-src.s
Original file line number Diff line number Diff line change
Expand Up @@ -7,146 +7,137 @@ _start:

#pushq %r14
#movq %rsi, %r8
mov $400, %dx
xorl %edx, %edx
xorl %esi, %esi
#pushq %r13
xorl %eax, %eax
#xorl %eax, %eax
#pushq %r12
mov $16, %r12b
mov $16, %r12b
#pushq %rbp
#xorl %ebp, %ebp
#pushq %rbx
subq $1104, %rsp
#movq (%r8), %rdi
leaq 80(%rsp), %rbx
mov $2, %al
syscall #call open
syscall #syscall#call open

mov $1024, %dx
movq %rbx, %rsi
leaq 80(%rsp), %rsi
movl %eax, %edi
mov $0, %al
syscall #call read
mov $0, %al
syscall #syscall#call read

movl $64, %esi
movq %rbx, %r11
movabsq $1445102447882210311, %r8
movabsq $1517442620720155396, %r9
movq %rax, %rcx
movabsq $-1167088121787636991, %rax
movq %rax, 32(%rsp)
movabsq $1167088121787636990, %rax
movq %rax, 40(%rsp)
vmovdqa .LC0(%rip), %xmm0
movabsq $1445102447882210311, %r10
movabsq $1517442620720155396, %r11
movswl %ax, %ecx
leal 8(%rcx), %eax
vmovaps %xmm0, 32(%rsp)
cltd
idivl %esi
sal $6, %ax
leal 56(%rax), %ebp
sall $6, %eax
leal 56(%rax), %r8d
movslq %ecx, %rax
sal $3, %cx
sall $3, %ecx
movb $-128, 80(%rsp,%rax)
movslq %ecx, %rcx
movslq %ebp, %rax
movswq %r8w, %rax
movq %rcx, 80(%rsp,%rax)
.L2:
movl %r11d, %eax
subl %ebx, %eax
cmpl %eax, %ebp
jle .L19
movq 32(%rsp), %rax
movq %r8, 64(%rsp)
xorl %edi, %edi
cmpw %r8w, %bp
jge .L19
vmovdqa 32(%rsp), %xmm1
movq %r10, 64(%rsp)
xorl %r9d, %r9d
movswq %bp, %r13
movl $16909056, 20(%rsp)
movq %rax, 48(%rsp)
movq 40(%rsp), %rax
movl $327936, 24(%rsp)
movq %rax, 56(%rsp)
movl $117638401, 28(%rsp)
movq %r9, 72(%rsp)
movq %r11, 72(%rsp)
vmovaps %xmm1, 48(%rsp)
.L7:
leal 3(%rdi), %eax
movl %edi, %r13d
movl %edi, %ecx
and $3, %eax
sarl $4, %r13d
leal 3(%r9), %eax
movl %r9d, %r14d
movl %r9d, %ecx
andl $3, %eax
sarl $4, %r14d
movsbq 20(%rsp,%rax), %rax
movl 48(%rsp,%rax,4), %r10d
leal 2(%rdi), %eax
incl %edi
and $3, %eax
movl 48(%rsp,%rax,4), %ebx
leal 2(%r9), %eax
incl %r9d
andl $3, %eax
movsbq 20(%rsp,%rax), %rax
movl 48(%rsp,%rax,4), %esi
movl %edi, %eax
and $3, %eax
movl %r9d, %eax
andl $3, %eax
movsbq 20(%rsp,%rax), %rax
movl 48(%rsp,%rax,4), %eax
cmp $2, %r13d
cmpl $2, %r14d
je .L3
cmp $3, %r13d
cmpl $3, %r14d
je .L4
cmp $1, %r13d
cmpl $1, %r14d
je .L5
xorl %eax, %esi
andl %r10d, %esi
andl %ebx, %esi
jmp .L17
.L5:
movl %r10d, %edx
movl %ebx, %edx
xorl %esi, %edx
andl %edx, %eax
jmp .L17
.L3:
xorl %r10d, %esi
xorl %ebx, %esi
jmp .L17
.L4:
notl %eax
orl %r10d, %eax
orl %ebx, %eax
.L17:
movslq %r13d, %r14
movslq %r14d, %rdi
xorl %eax, %esi
movl %ecx, %eax
and $3, %ecx
movsbl 28(%rsp,%r14), %edx
andl $15, %eax
movl %edi, 8(%rsp)
movl %r9d, 8(%rsp)
movsbl 28(%rsp,%rdi), %edx
fildl 8(%rsp)
andl $15, %eax
imull %edx, %eax
movsbl 24(%rsp,%r14), %edx
movsbl 24(%rsp,%rdi), %edx
addl %edx, %eax
cltd
idivl %r12d
movslq %edx, %rax
movslq %ecx, %rdx
leal (%rdx,%r13,4), %ecx
movl (%r11,%rax,4), %eax
movslq %ecx, %rcx
movsbl 64(%rsp,%rcx), %ecx
leaq 1104(%rsp,%rax,4), %rax
movl -1024(%r13,%rax), %edi
movl %ecx, %eax
andl $3, %eax
leal (%rax,%r14,4), %edx
movslq %edx, %rdx
movsbl 64(%rsp,%rdx), %ecx
#APP
# 24 "main-src.c" 1
fsin

# 0 "" 2
#NO_APP
fabs
fmuls .LC0(%rip)
movsbq 20(%rsp,%rdx), %rdx
addl 48(%rsp,%rdx,4), %eax
addl %eax, %esi
fmuls .LC1(%rip)
cltq
movsbq 20(%rsp,%rax), %rax
addl 48(%rsp,%rax,4), %edi
fisttpq 8(%rsp)
movq 8(%rsp), %r14
addl %r14d, %esi
movq 8(%rsp), %rdx
addl %edi, %esi
addl %edx, %esi
roll %cl, %esi
addl %esi, %r10d
movl %r10d, 48(%rsp,%rdx,4)
cmpl $64, %edi
addl %esi, %ebx
movl %ebx, 48(%rsp,%rax,4)
cmpl $64, %r9d
jne .L7
movl 48(%rsp), %eax
addq $64, %r11
addl %eax, 32(%rsp)
movl 52(%rsp), %eax
addl %eax, 36(%rsp)
movl 56(%rsp), %eax
addl %eax, 40(%rsp)
movl 60(%rsp), %eax
addl %eax, 44(%rsp)
vmovdqa 32(%rsp), %xmm2
vpaddd 48(%rsp), %xmm2, %xmm0
addl $64, %ebp
vmovaps %xmm0, 32(%rsp)
jmp .L2
.L19:
xorl %ebx, %ebx
Expand All @@ -163,22 +154,28 @@ _start:
sarl %cl, %eax
andl $15, %eax
leal 48(%rax), %edx
cmpl $9, %eax
cmpb $9, %al
jle .L11
leal 87(%rax), %edx
.L11:
movb %dl, 64(%rsp)
leaq 64(%rsp), %rsi
mov $1, %dl
mov $1, %dl
incl %ebx
mov $1, %dil
mov $1, %edi
mov $1, %al
syscall #call write
syscall#call write
cmpb $32, %bl
jne .L12
xorl %edi, %edi
mov $60, %al
syscall #call exit
.align 4
syscall#call exit
.align 16
.LC0:
.long 1732584193
.long -271733879
.long -1732584194
.long 271733878
.align 4
.LC1:
.long 1333788672
Binary file modified selfmd5
Binary file not shown.

0 comments on commit 9d0809f

Please sign in to comment.