My mem_n_cpy() in Assembly
I wrote it in assembly:
Code:
.486
.model flat, c
.data
;No data defined
.code
mem_n_cpy PROC
; edx reminder
; eax quotient
; esi src
; edi dst
; ecx acts as a copy buffer
; ebx temp
push ebp
mov ebp, esp
push edi
push esi
mov eax, [ebp+16] ;Count parameter
cmp eax, 0 ;
jle RETURN ;Count is zero or negative, invalid parameter
mov edi, [ebp+8] ;dst parameter
mov esi, [ebp+12] ;src parameter
xor edx, edx
mov ebx, 4
div ebx
;Check to see if src is overlapped on dst or not
lea ebx, [edx + 4*eax]
mov ecx, edi
sub ecx, esi
add ecx, ebx
cmp ecx, ebx
jg REVERSE ;Overlapped
QLOOP:
dec eax
mov ecx, [esi]
mov [edi], ecx
add esi, 4
add edi, 4
test eax, eax
jne QLOOP
test edx, edx
je RETURN
RLOOP:
dec edx
mov BYTE PTR cl, [esi]
mov BYTE PTR [edi], cl
inc esi
inc edi
test edx, edx
jne RLOOP
jmp RETURN
REVERSE:
lea ebx, [edx + 4*eax]
add esi, ebx
add edi, ebx
RQLOOP:
dec eax
sub esi, 4
sub edi, 4
mov ecx, [esi]
mov [edi], ecx
test eax, eax
jne RQLOOP
cmp edx, 0
je RETURN
RRLOOP:
dec edx
dec esi
dec edi
mov BYTE PTR cl, [esi]
mov BYTE PTR [edi], cl
test edx, edx
jne RRLOOP
RETURN:
pop esi
pop edi
mov esp, ebp
pop ebp
ret
mem_n_cpy ENDP
END
I appreciate your ideas about it.