Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions library-asm/memcopy.asm
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
; ----------------------------------------------------------------

; Emulates both memmove and memcpy C routines
; Blocks will safely copies if they overlap
; Block will be safely copied if they overlap

; HL => Start of source block
; DE => Start of destiny block
Expand All @@ -19,15 +19,16 @@ __MEMCPY:
PROC
LOCAL __MEMCPY2

add hl, bc
push hl
add hl, bc ; addr of last source block byte + 1
or a
sbc hl, de ; checks if DE > HL + BC
add hl, de ; recovers HL. If Carry set => DE > HL
pop hl ; recovers HL. If carry => DE > HL + BC (no overlap)
jr c, __MEMCPY2

; Now checks if DE <= HL

sbc hl, de
sbc hl, de ; Even if overlap, if DE < HL then we can LDIR safely
add hl, de
jr nc, __MEMCPY2

Expand Down
56 changes: 41 additions & 15 deletions library/memcopy.bas
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
'
' Copyleft (k) 2008
' by Jose Rodriguez-Rosa (a.k.a. Boriel) <http://www.boriel.com>
'
' Use this file as a template to develop your own library file
' ----------------------------------------------------------------

#ifndef __LIBRARY_MEMCOPY__
Expand All @@ -13,20 +11,21 @@ REM Avoid recursive / multiple inclusion

#define __LIBRARY_MEMCOPY__

#define memmove memcopy

#pragma push(case_insensitive)
#pragma case_insensitive = True

' ----------------------------------------------------------------
' sub MEMCOPY(sourceaddr, destaddr, blocklength)
' Sub MemMove(sourceaddr, destaddr, blocklength)
'
' Parameters:
' souceaddr: memory address of source block to copy
' destaddr: memory address of destiny block to copy
' length: number of bytes to copy
'
' Copies block of memory from source to dest. Block may overlap
' Copies block of memory safely from source to dest.
' Source and destiny blocks may overlap
' ----------------------------------------------------------------
sub fastcall memcopy(source as uinteger, dest as uinteger, length as uinteger)
sub fastcall MemMove(source as uinteger, dest as uinteger, length as uinteger)
asm
; Emulates both memmove and memcpy C routines
; Blocks will safely copies if they overlap
Expand All @@ -35,22 +34,49 @@ sub fastcall memcopy(source as uinteger, dest as uinteger, length as uinteger)
; DE => Start of destiny block
; BC => Block length

exx
pop hl ; ret addr
exx

pop af ; ret addr
pop de ; dest
pop bc ; length

exx
push hl ; stores ret addr back
exx
push af ; stores ret addr back

jp __MEMCPY
end asm
end sub


' ----------------------------------------------------------------
' Sub MemCopy(sourceaddr, destaddr, blocklength)
'
' Parameters:
' souceaddr: memory address of source block to copy
' destaddr: memory address of destiny block to copy
' length: number of bytes to copy
'
' Copies block of memory from source to dest.
' Source and destiny blocks should not overlap.
' This sub is slighly faster than memmove
' ----------------------------------------------------------------
sub fastcall MemCopy(source as uinteger, dest as uinteger, length as uinteger)
asm
; Emulates both memmove and memcpy C routines
; Blocks will safely copies if they DON'T overlap

; HL => Start of source block
; DE => Start of destiny block
; BC => Block length

pop af ; ret addr
pop de ; dest
pop bc ; length
push af ; stores ret addr back
ldir
end asm
end sub


#require "memcopy.asm"

#pragma pop(case_insensitive)

#endif

Loading