Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

INCLUDE Irvine32.inc .data prompt byte \"Enter a string: \",0 buffer byte 20 dup

ID: 3570592 • Letter: I

Question

INCLUDE Irvine32.inc

.data

prompt byte   "Enter a string: ",0
buffer byte   20 dup(0),0
buffer2 byte   20 dup(0),0
message   byte   "The string copy is: ",0

.code

strput PROC
              
push   ebp          
mov   ebp, esp      

mov   edx, [ebp+8]      
push   edx
call   writeString
  
pop   edx
pop   ebp

ret   4

strput ENDP

strlen PROC

push    ebp
mov     ebp, esp
push    ecx
push ebx
mov ecx, [ebp+8]

count:  
mov     bl, [ecx + eax]
cmp    bl, 0
je    print
add     eax, 1
jmp     count

print:
pop    ebx
pop     ecx
pop     ebp

ret     4

strlen ENDP

strcpy PROC

push   ebp
mov   ebp, esp

push   esi
push   edi

mov   esi, [ebp+8]      
mov   edi, [ebp+12]          

cpy:
mov   al, [esi]
mov   [edi], al
inc esi
inc edi
cmp   al, 0
je myloop
jmp   cpy

myloop:  
pop   ebp
pop   esi
pop   edi

ret   8

strcpy   ENDP

main PROC

push   offset prompt  
call   strput
  
mov   ecx,sizeof buffer  
mov edx,offset buffer  
call readstring

call crlf

push offset message
call strput

push offset buffer
push offset buffer2
call strcpy

push offset buffer2
call strput

call crlf

exit
main ENDP
END main

I want to copy one string to another string but my strcpy is not working. Can anyone help?

Explanation / Answer

Option1 with actual function defn:

INCLUDE Irvine32.inc
;---------------------------------------------------------
Str_length PROC USES edi,
   pString:PTR BYTE   ; pointer to string
;
; Return the length of a null-terminated string.
; Receives: pString - pointer to a string
; Returns: EAX = string length
;---------------------------------------------------------
   mov edi,pString
   mov eax,0    ; character count
L1:
   cmp BYTE PTR [edi],0   ; end of string?
   je L2   ; yes: quit
   inc edi   ; no: point to next
   inc eax   ; add 1 to count
   jmp L1
L2: ret
Str_length ENDP

;---------------------------------------------------------
Str_copy PROC USES eax ecx esi edi,
    source:PTR BYTE,        ; source string
    target:PTR BYTE       ; target string
;
; Copy a string from source to target.
; Returns: nothing
; Requires: the target string must contain enough
; space to hold a copy of the source string.
;----------------------------------------------------------
   INVOKE Str_length,source        ; EAX = length source
   mov ecx,eax       ; REP count
   inc ecx        ; add 1 for null byte
   mov esi,source
   mov edi,target
   cld        ; direction = up
   rep movsb         ; copy the string
   ret
Str_copy ENDP


.data
string_1 BYTE "ABCDEFG",0
string_2 BYTE 10 DUP(?)

.code
main PROC
   call Clrscr
INVOKE Str_copy,   ; copy string_1 to string_2
   ADDR string_1,
   ADDR string_2

   mov edx,OFFSET string_2
   call WriteString
   call Crlf
   exit
main ENDP

END main

Option2 using built-in function:

INCLUDE Irvine32.inc
Str_copy PROTO,
    source:PTR BYTE,        ; source string
    target:PTR BYTE       ; target string

Str_length PROTO,
   pString:PTR BYTE       ; pointer to string

.data
string_1 BYTE "ABCDEFG",0
string_2 BYTE 10 DUP(?)

.code
main PROC
   call Clrscr
INVOKE Str_copy,   ; copy string_1 to string_2
   ADDR string_1,
   ADDR string_2

   mov edx,OFFSET string_2
   call WriteString
   call Crlf
   exit
main ENDP

END main