1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] How do I reproduce this CRC32 logic from assembly [closed]

Discussão em 'Python' iniciado por Stack, Novembro 25, 2025.

  1. Stack

    Stack Membro Participativo

    You are given an assembly program that generates a CRC-32 lookup table and then computes a CRC-32–style hash over four input bytes.
    The task is to:
    Understand how the table is generated
    Follow the hash routine step-by-step (XOR with CRC low byte, lookup, shift, XOR)
    Reproduce the same logic in your own code
    Compute the final 32-bit value returned in EAX

    ====================================================
    ; ========= CONFIG =========
    ; SASM: Mode = NASM, 64-bit

    section .data
    input_data db 0x4B,0x43,0x43,0x48
    input_len dq 4

    section .bss
    lut resd 256

    section .text
    global _start

    ; ========= ENTRY =========

    _start:
    call hash

    ; print result (in hex) just to verify
    mov rdi, rax
    ; exit
    mov rax, 60
    xor rdi, rdi
    syscall

    ; ========= CRC TABLE GENERATOR =========

    generate_table:
    xor rcx, rcx ; i = 0

    .gen_outer:
    cmp rcx, 256
    je .done_outer

    mov eax, ecx ; t = i

    mov rdx, 0 ; j = 0
    .gen_inner:
    cmp rdx, 8
    je .store

    test al, 1
    jz .no_xor
    shr eax, 1
    xor eax, 0xEDB88320
    jmp .next

    .no_xor:
    shr eax, 1

    .next:
    inc rdx
    jmp .gen_inner

    .store:
    mov dword [lut + rcx*4], eax
    inc rcx
    jmp .gen_outer

    .done_outer:
    ret

    ; ========= CRC HASH FUNCTION =========

    hash:
    call generate_table

    mov rbx, input_data
    mov rcx, [input_len]

    mov eax, 0xffffffff ; CRC

    xor rdx, rdx ; index = 0

    .loop:
    cmp rdx, rcx
    je .done

    mov bl, [input_data + rdx] ; read byte
    xor bl, al ; CRC low byte ^ input
    movzx r8d, bl ; r8d = index (0..255)

    shr eax, 8
    xor eax, dword [lut + r8d*4]

    inc rdx
    jmp .loop

    .done:
    xor eax, 0xffffffff
    ret





    ===================================================================
    My pyhton program
    def generate_table():
    lut = [0] * 256
    for i in range(256):
    t = i
    for _ in range(8):
    if t & 1:
    t = (t >> 1) ^ 0xEDB88320
    else:
    t = t >> 1
    lut = t & 0xFFFFFFFF
    return lut


    def crc32_exact_asm(data_bytes):
    lut = generate_table()
    crc = 0xFFFFFFFF
    index = 0
    length = len(data_bytes)

    while index < length:
    input_byte = data_bytes[index] & 0xFF

    index_val = (input_byte ^ (crc & 0xFF)) & 0xFF

    crc = (crc >> 8) & 0xFFFFFFFF

    crc = (crc ^ lut[index_val]) & 0xFFFFFFFF

    index += 1

    crc = crc ^ 0xFFFFFFFF
    return crc & 0xFFFFFFFF


    data = [0x4B, 0x43, 0x43, 0x48]
    result = crc32_exact_asm(data)

    print(hex(result))

    Continue reading...

Compartilhe esta Página