copy string procedure assembly

8.1k views Asked by At

I wrote a copy string procedure in masm32

coppystring proc uses esi edi  ecx source:dword,dest:dword
                        mov  esi, OFFSET source
                        mov  edi, OFFSET dest
                        mov  ecx, SIZEOF source
                    Lx:
                        mov  al,[esi]          
                        mov  [edi],al           
                        inc  esi              
                        inc  edi
                        loop Lx

coppystring endp

This code gives me error

A2098 invalid operand for OFFSET

The expression following the OFFSET operator must be a memory expression or an immediate expression. But still I don't know how to fix my proc

1

There are 1 answers

0
JBES On

You are getting those errors because the memory address of source and dest are not known at compile time. You need to pass the address to the proc. Also, as commented, you cannot use SIZEOF and should check for the null terminator or get the length another way.

invoke coppystring,offset str1,offset str2 ; Push the offsets here

coppystring proc uses esi edi source:dword,dest:dword
    ; Generally only need to preserve esi, edi and ebx

mov  esi, source
mov  edi, dest

Lx:
    mov  al,[esi]          
    mov  [edi],al           
    inc  esi              
    inc  edi
    cmp byte ptr [esi],0   ; Check for null terminator
    jne Lx                 ; loop if not null

ret
coppystring endp