How to write a dup_space?

dup_string(" ",5) ;" " space(5)

dup_string("A",5) ;"AAAAA"
Posted on 2002-07-22 22:56:20 by fox
db 25 dup (" ") ; 25 spaces
db 2 dup ("ABC") ; "ABCABC"
dup_string MACRO string:REQ, cnt:REQ

db cnt dup(string)
ENDM
; keep your syntax :)
dup_string(" ",5) ;" " space(5)
dup_string("A",5) ;"AAAAA"
; copy string a number of times after itself

dup_string PROC uses esi edi, string:DWORD, count:DWORD
mov edi, string
or ecx, -1
mov esi, edi
repne scasb
not ecx
dec ecx ; length of string
imul ecx, count
rep movsb
ret
dup_string ENDP

Example:

.data
MyString db "ABC",128 dup (0)

.code
invoke dup_string, OFFSET MyString, 2
; MyString is now "ABCABCABC"
This should just about cover whatever you mean. :tongue:
Posted on 2002-07-22 23:50:51 by bitRAKE
thanks!

But I still have some questions!

.386
.model flat, stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib

?Q1:
?dup_string PROTO :DWORD,:DWORD
?OR:
?dup_string MACRO string:REQ, cnt:REQ
? db cnt dup(string)
?ENDM

.data
MyString db "ABC",128 dup (0)

.code
dup_string PROC uses esi edi, string:DWORD, count:DWORD
mov edi, string
or ecx, -1
mov esi, edi
repne scasb
not ecx
dec ecx ; length of string
imul ecx, count
rep movsb
ret
dup_string ENDP

start:
invoke dup_string, OFFSET MyString, 2
?Q2:
?invoke MessageBox, 0, offset dup_string, NULL, MB_OK
invoke ExitProcess,NULL
end start
Posted on 2002-07-23 01:59:43 by fox
Use the function if you want to duplicate a string during runtime.
Use the macro if you want to duplicate a string during assembly time.

If the string to duplicate is static(permanent, you don't want to change it anymore) just declare it the old fashion way

MyString DB 20 DUP(" ").

If the string to duplicate is unknown or depends on an "input" use the function.
Posted on 2002-07-23 02:52:43 by stryker
fox, like stryker said, "Don't use them all at once." There are three solutions posted. :) Sorry, I didn't explain - usually assume people have read the Intel & MASM manual.
Posted on 2002-07-23 06:59:26 by bitRAKE
Thanks bitRAKE,stryker.

I want Dynamic.
I try this.

.386
.model flat, stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib

dup_string PROTO :DWORD,:DWORD

.data
MyString db "ABC",4 dup (0)

.code
dup_string PROC uses esi edi, string:DWORD, count:DWORD
mov edi, string
or ecx, -1
mov esi, edi
repne scasb
not ecx
dec ecx ; length of string
imul ecx, count
rep movsb
ret
dup_string ENDP

start:
invoke dup_string, OFFSET MyString, 2
invoke ExitProcess,NULL
end start

but it not work ,i don't know what's wrong?
Posted on 2002-07-23 21:48:59 by fox
Try this:
[size=9].686

.MODEL FLAT, STDCALL
OPTION CASEMAP:NONE
INCLUDE \masm32\INCLUDE\windows.inc
INCLUDE \masm32\INCLUDE\kernel32.inc
INCLUDE \masm32\INCLUDE\user32.inc
INCLUDElib \masm32\lib\user32.lib
INCLUDElib \masm32\lib\kernel32.lib

.data

pattern DB "ABC", 0

.data?

output DB 1024 DUP(?)

.code

dupf PROC USES esi edi lpszPattern:DWORD, lpszOutput:DWORD, uCount:DWORD

mov esi, lpszPattern
xor edx, edx
mov edi, lpszOutput
mov ecx, uCount

__duplicate:

mov al, BYTE PTR [esi+edx]
or al, al
jnz __copy_it

xor edx, edx
jmp __next

__copy_it:

mov BYTE PTR [edi], al
inc edx
inc edi
jmp __duplicate

__next:

dec ecx
jnz __duplicate
mov BYTE PTR [edi], 0

ret

dupf ENDP

start:

invoke dupf, OFFSET pattern, OFFSET output, 3
invoke MessageBox, 0, OFFSET output, 0, 0
invoke ExitProcess,NULL

end start[/size]
The 1st parameter must be NULL terminated. :)
Posted on 2002-07-23 22:10:46 by stryker
thanks stryker.
Posted on 2002-07-24 00:20:26 by fox