String Concatenation Help

Please support our Assembly advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jan 2008
Posts: 9
Reputation: gothicmisery85 is an unknown quantity at this point 
Solved Threads: 0
gothicmisery85's Avatar
gothicmisery85 gothicmisery85 is offline Offline
Newbie Poster

String Concatenation Help

 
0
  #1
Nov 25th, 2008
I am working on a program, and I can't figure out what I'm doing wrong. Any help would be appreciated. Here is what I have to do:


Write a procedure named St_concat that concatenates a source string to the end of a target string. Sufficient space must exist in the target string to accommodate the new characters. Pass pointers to the source and target strings. Here is a sample call:


  1. .data
  2. targetStr BYTE "ABCDE",10 DUP(0)
  3. sourceStr BYTE "FGH",0
  4. .code
  5. INVOKE Str_concat, ADDR targetStr, ADDR sourceStr



Here is what I have so far:

TITLE String Concatenate                (StrConcat.asm)

; This program concatenates a source
; string to the end of a target string.


INCLUDE Irvine32.inc
INCLUDE Bsearch.inc

ARRAY_SIZE = 1

.data
array DWORD ARRAY_SIZE DUP(?)
targetStr BYTE "ABCDE",10 DUP(0)
sourceStr BYTE "FGH",0

LOWVAL = targetStr
HIGHVAL = sourceStr


.code
main PROC
; Fill an array with random signed integers
INVOKE FillArray, ADDR array, ARRAY_SIZE, LOWVAL, HIGHVAL

; Display the array
INVOKE PrintArray, ADDR array, ARRAY_SIZE
	call WaitMsg

; Perform a bubble sort and redisplay the array
INVOKE BubbleSort, ADDR array, ARRAY_SIZE
INVOKE PrintArray, ADDR array, ARRAY_SIZE

	call Str_concat

	exit
main ENDP

;------------------------------------------------------------
BubbleSort PROC USES eax ecx esi,
	pArray:PTR DWORD,		; pointer to array
	Count:DWORD			; array size
;
; Sort an array of 32-bit signed integers in ascending
; order, using the bubble sort algorithm.
; Receives: pointer to array, array size
; Returns: nothing
;------------------------------------------------------------

	mov ecx,Count
	dec ecx				; decrement count by 1

L1:	push ecx			; save outer loop count
	mov esi,pArray			; point to first value

L2:	mov eax,[esi]			; get array value
	cmp [esi+4],eax			; compare a pair of values
	jge L3				; if [ESI] <= [EDI], no exchange
	xchg eax,[esi+4]		; exchange the pair
	mov [esi],eax

L3:	add esi,4			; move both pointers forward
	loop L2				; inner loop

	pop ecx				; retrieve outer loop count
	loop L1				; else repeat outer loop

L4: 	ret

BubbleSort ENDP

;------------------------------------------------------------
FillArray PROC USES eax edi ecx edx,
	pArray:PTR DWORD,		; pointer to array
	Count:DWORD,			; number of elements
	A:SDWORD,	       		; ABCDE
	B:SDWORD   			; FGH
;
; Fills an array with a random sequence of 32-bit signed
; integers between LowerRange and (UpperRange - 1).
; Returns: nothing
;------------------------------------------------------------
	mov edi,pArray			; EDI points to the array
	mov ecx,Count			; loop counter
	mov edx,A
	sub edx,B			; EDX = absolute range (0..n)

L1:	mov eax,edx			; get absolute range
	add eax,B			; bias the result
	stosd				; store EAX into [edi]

	mov edx,OFFSET targetStr
	call WriteString

	loop L1

	ret

	call Str_concat
FillArray ENDP

;------------------------------------------------------------
PrintArray PROC USES eax ecx edx esi,
	pArray:PTR DWORD,		; pointer to array
	Count:DWORD			; number of elements
;
; Writes an array of 32-bit signed decimal integers to
; standard output, separated by commas
; Receives: pointer to array, array size
; Returns: nothing
;------------------------------------------------------------
.data
comma BYTE ", ",0

.code
	mov esi,pArray
	mov ecx,Count
	cld				; direction = forward

L1: 	lodsd				; load [ESI] into EAX

	loop L1

	call Crlf
	ret
PrintArray ENDP

;------------------------------------------------------------
Str_concat PROC USES eax ecx esi,
	pArray:PTR DWORD,		; pointer to array
	Count:DWORD			; array size
;
; Sort an array of 32-bit signed integers in ascending
; order, using the bubble sort algorithm.
; Receives: pointer to array, array size
; Returns: nothing
;------------------------------------------------------------
.code
INVOKE Str_concat, ADDR targetStr, ADDR sourceStr

	mov ecx,Count
	dec ecx				; decrement count by 1

L1: 	push ecx			; save outer loop count
	mov esi,pArray			; point to first value

L2:	mov eax,[esi]			; get array value
	cmp [esi+4],eax			; compare a pair of values
	jge L3				; if [ESI] <= [EDI], no exchange
	xchg eax,[esi+4]		; exchange pair
	mov [esi],eax

	mov edx,OFFSET sourceStr
	call WriteString


L3:	add esi,4			; move both pointers forward
	loop L2				; inner loop

	pop ecx				; retrieve outer loop count
	loop L1				; else repeat outer loop

L4:	ret

Str_concat ENDP

END main
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 9
Reputation: gothicmisery85 is an unknown quantity at this point 
Solved Threads: 0
gothicmisery85's Avatar
gothicmisery85 gothicmisery85 is offline Offline
Newbie Poster

Re: String Concatenation Help

 
0
  #2
Nov 26th, 2008
Nevermind. I came up with the solution.

If anybody is interested, here is what I got for my answer:

  1. TITLE String Concatenate (StrConcat.asm)
  2.  
  3. ; This program concatenates a source
  4. ; string to the end of a target string.
  5.  
  6.  
  7. INCLUDE Irvine32.inc
  8. INCLUDE Bsearch.inc
  9.  
  10. STRING_SIZE = 10
  11.  
  12.  
  13. Str_concat PROTO,
  14. source:PTR BYTE, ; source string
  15. target:PTR BYTE ; target string
  16.  
  17.  
  18. .data
  19. targetStr BYTE "ABCDE",10 DUP(0)
  20. sourceStr BYTE "FGH",0
  21.  
  22. .code
  23. main PROC
  24.  
  25. ; Display
  26. mov edx,OFFSET targetStr
  27. call WriteString
  28.  
  29. call Crlf
  30.  
  31. call WaitMsg
  32.  
  33. mov eax,LENGTHOF sourceStr
  34.  
  35. INVOKE Str_concat, ADDR sourceStr, ADDR targetStr
  36.  
  37. mov edx,OFFSET targetStr
  38. call WriteString
  39.  
  40. call Crlf
  41.  
  42. exit
  43. main ENDP
  44.  
  45. ;------------------------------------------------------------
  46. FillString PROC USES eax edi ecx edx,
  47. pString:PTR DWORD, ; pointer to array
  48. Count:DWORD, ; number of elements
  49. string_1:SDWORD, ; ABCDE
  50. string_2:SDWORD ; FGH
  51. ;
  52. ; Fills the string with "ABCDE", then concatenates, "FGH" onto it.
  53. ; Returns: nothing
  54. ;------------------------------------------------------------
  55. mov edi,pString ; EDI points to the string
  56. mov ecx,Count ; loop counter
  57. mov edx,string_1
  58. sub edx,string_2 ; EDX = absolute range (0..n)
  59.  
  60. L1: mov eax,edx ; get absolute range
  61. add eax,string_2 ; bias the result
  62. stosd ; store EAX into [edi]
  63.  
  64. loop L1
  65.  
  66. ret
  67.  
  68. FillString ENDP
  69. ;------------------------------------------------------------
  70. Str_concat PROC USES eax ecx esi edi,
  71. source:PTR BYTE, ; source string
  72. target:PTR BYTE ; target string
  73. ;------------------------------------------------------------
  74. mov ecx,eax ; EAX = length source
  75.  
  76. mov esi,source
  77. mov edi,target
  78. add edi,5
  79. cld ; direction = forward
  80. rep movsb ; copy the string
  81.  
  82. ret
  83.  
  84. Str_concat ENDP
  85.  
  86. END main
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 23
Reputation: low_coder is an unknown quantity at this point 
Solved Threads: 2
low_coder low_coder is offline Offline
Newbie Poster

Re: String Concatenation Help

 
0
  #3
Nov 26th, 2008
;Take a look at this one too:
  1. str_cat proc strBase:DWORD, strAdd:DWORD
  2. mov edi, strBase
  3. mov al, 0
  4. repne scasb
  5. dec edi
  6. mov esi, strAdd
  7. @@:
  8. mov al, [esi]
  9. mov [edi], al
  10. inc esi
  11. inc edi
  12. test al, al
  13. jnz @B
  14. ret
  15. str_cat endp
;an example is in attach.
Last edited by low_coder; Nov 26th, 2008 at 8:19 am. Reason: wrong attach.
Attached Files
File Type: zip str_cat.zip (1.6 KB, 10 views)
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1
Reputation: crunchy_frog is an unknown quantity at this point 
Solved Threads: 0
crunchy_frog crunchy_frog is offline Offline
Newbie Poster

Re: String Concatenation Help

 
0
  #4
Dec 5th, 2008
why do you need the FillString procedure? you not even using it. Also Why include Bsearch.inc and String_Size?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Assembly
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC