Skip to content

Instantly share code, notes, and snippets.

@jart
Created Jan 13, 2021
Embed
What would you like to do?
Tiniest lz4 block copier that's binary compatible with i8086 + i386 + x86_64
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
│vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi│
╞══════════════════════════════════════════════════════════════════════════════╡
│ Copyright 2020 Justine Alexandra Roberts Tunney │
│ │
│ Permission to use, copy, modify, and/or distribute this software for │
│ any purpose with or without fee is hereby granted, provided that the │
│ above copyright notice and this permission notice appear in all copies. │
│ │
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
.code16
/ 67 Byte LZ4 Block Decompressor for 80186 / 80386 / x86_64
/
/ @param es:di destination memory
/ @param ds:si compressed data
/ @param dx byte length of compressed data
/ @return ax advanced destination pointer
/ @return di advanced destination pointer
/ @return si advanced source pointer
/ @note this code is completely untested beware
/ @note compressed data should be trustworthy
/ @note this has the same api as mempcpy()
/ @see cosmopolitan/libc/str/lz4cpy.c
lz4cpy: add %si,%dx
0: xor %ax,%ax
lodsb
push %ax
xchg %ax,%cx
shr $4,%cl
cmp $15,%cl
jne 2f
xor %ax,%ax
1: lodsb
add %ax,%cx
cmp $255,%al
je 1b
2: rep movsb
pop %cx
cmp %si,%dx
jbe 9f
lodsw
push %ax
and $15,%cl
cmp $15,%cl
jne 2f
xor %ax,%ax
1: lodsb
add %ax,%cx
cmp $255,%al
je 1b
2: add $4,%cx
pop %ax
push %si
mov %di,%si
sub %ax,%si
rep movsb
pop %si
jmp 0b
9: mov %di,%ax
ret
.type lz4cpy,@function
.size lz4cpy,.-lz4cpy
.globl lz4cpy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment