Exemple de crc32 en asm

Il s’agit d’une version asm du crc32 présenté dans la rfc gzip (1952).

 
format ELF64 executable 3
entry start
 
include 'include/linux/lin64a.inc'
interpreter '/lib64/ld-linux-x86-64.so.2'
needed 'libc.so.6'
import	printf
 
;-------------------------------------------------------------------------
segment readable executable
;-------------------------------------------------------------------------
 
start:
 
	mov	esi,buffer_len
	mov	edi,buffer
	call	update_crc
 
	mov	esi,eax
	mov	edi,formh
	xor	eax,eax
	call	[printf]
 
   exit:
	xor	edi,edi
	mov	eax,_exit
	syscall
 
 
 
update_crc:
	cmp	dword [crc_table_computed],0
	jnz	@f
	mov	ebp,esi
	call	make_crc_table
	mov	ecx,ebp
     @@:
	xor	eax,eax
	not	eax
	xor	edx,edx
     @@:
	mov	dl,al
	mov	bl,byte [edi]
	xor	dl,bl
	inc	edi
	mov	ebx,dword [esi+edx*4]
	shr	eax,8
	xor	eax,ebx
	dec	ecx
	jnz	@b
	xor	eax,0xffffffff
	ret
 
 
 
make_crc_table:
	mov	esi,crc_table
	xor	ecx,ecx
     A1:
	mov	eax,ecx
	mov	bl,8
     A2:
	mov	dl,al
	and	dl,1
	cmp	dl,1
	jnz	@f
	mov	edx,eax
	shr	edx,1
	mov	eax,0xedb88320
	xor	eax,edx
	jmp	A3
     @@:
	shr	eax,1
     A3:
	dec	bl
	jnz	A2
	mov	dword [esi+ecx*4],eax
	inc	cl
	jnz	A1
	mov	dword [crc_table_computed],1
	ret
 
;-------------------------------------------------------------------------
segment readable writeable
;-------------------------------------------------------------------------
 
crc_table rd 256
crc_table_computed dd 0
 
buffer db 'Life, the Universe and everything'
buffer_len = $-buffer

Le même crc32 mais en version x32 stack, sans buffer.

 
format ELF executable 3
entry start
 
include 'include/linux/lin32a.inc'
interpreter '/lib/ld-linux.so.2'
needed 'libc.so.6'
 
import	printf
 
;-------------------------------------------------------------------------
segment readable executable
;-------------------------------------------------------------------------
 
start:
 
	mov	esi,buffer_len
	mov	edi,buffer
	call	update_crc
 
	invoke	printf,formh,eax
 
	xor	ebx,ebx
	mov	eax,_exit
	int	0x80
 
 
 
update_crc:
	cmp	dword [crc_table_computed],0
	jnz	@f
	mov	ebp,esi
	call	make_crc_table
	mov	ecx,ebp
     @@:
	sub	esp,8
	xor	eax,eax
	not	eax
	xor	edx,edx
     @@:
	mov	dl,al
	mov	bl,byte [edi]
	xor	dl,bl
	inc	edi
 
	neg	edx
	mov	ebx,dword [esp+edx*4]
	neg	edx
 
	shr	eax,8
	xor	eax,ebx
 
	dec	ecx
	jnz	@b
 
	xor	eax,0xffffffff
	add	esp,8
	ret
 
 
make_crc_table:
	xor	ecx,ecx
	sub	esp,4
     A1:
	mov	eax,ecx
	mov	bl,8
     A2:
	mov	dl,al
	and	dl,1
	cmp	dl,1
	jnz	@f
 
	mov	edx,eax
	shr	edx,1
	mov	eax,0xedb88320
	xor	eax,edx
	jmp	A3
     @@:
	shr	eax,1
     A3:
	dec	bl
	jnz	A2
 
	neg	ecx
	mov	dword [esp+ecx*4],eax
	neg	ecx
 
	inc	cl
	jnz	A1
 
	mov	dword [crc_table_computed],1
	add	esp,4
	ret
 
;-------------------------------------------------------------------------
segment readable writeable
;-------------------------------------------------------------------------
 
crc_table_computed dd 0
 
buffer db 'Life, the Universe and everything'
buffer_len = $-buffer

A compiler avec Fasm