I am trying to put a random string into a variable.

I only have partial code so far:

randomText1 db "Nice Job", 0
randomText1 db "Good Job", 0
randomText1 db "You did it", 0
randomText1 db "All Done!", 0

randomText db ? ; is this correct?
(possibly more)

MsgCaption db "Your results",0

At this point, I would like to just MsgBox one of the random strings.

invoke MessageBox, NULL,ADDR RandomText, ADDR MsgCaption, MB_OK

Can anyone help with some sample code? I sure would appreciate the nudge. I am new at this, and I have to say...so far it's fun!

Thanks
Trope
Posted on 2004-11-15 06:43:51 by TropE
There are two ways, firstly create an array of pointers to the strings. Then pick a pointer from your list.

Secondly, make all strings the same length (padding with zeros), and multiply the selected number by the length and add the offset.

For the second option the following macro may help:


StrArr MACRO string:REQ, num:REQ, name
LOCAL tmp

IFNB <name>
name db string, 0
db (num - ($ - name)) DUP (0)
ELSE
tmp db string, 0
db (num - ($ - tmp)) DUP (0)
ENDIF
ENDM


Mirno
Posted on 2004-11-15 07:24:54 by Mirno
Wow , looks like this is more complicated than I thought. Thanks for your reply, but I think the whole "array of pointers is a bit too deep at the moment. Just got into this about 2 weeks ago.

I'd love to study the source code of the example you described if you don't mind (if you have the time to post it sometime).

Thanks again. APpreciate it.
Posted on 2004-11-15 07:52:33 by TropE
Both ways done below, change "UseArrays" to conditionally assemble as fixed width strings.



.386
.model flat,stdcall
option casemap:none

.nolist
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
.list

StrArr MACRO string:REQ, num:REQ, name
LOCAL tmp

IFNB <name>
name db string, 0
db (num - ($ - name)) DUP (0)
ELSE
tmp db string, 0
db (num - ($ - tmp)) DUP (0)
ENDIF
ENDM

UseArrays EQU 1

IF UseArrays
.data
rStr0 db "Test string 1", 0
rStr1 db "Test string 2", 0
rStr2 db "1234567890123456", 0

prStr dd OFFSET rStr0, OFFSET rStr1, OFFSET rStr2

.code
start:
; Use your favorite Random Number Generator here
; invoke RandomNumberGenerator

; Clear edx, and divide eax by number of random strings
; This may not be necessary if your RNG supports a range!
xor edx, edx
mov ecx, 3
div ecx

mov edx, [prStr + edx*4]

invoke MessageBox, NULL, edx, NULL, MB_OK
invoke ExitProcess, 0
ELSE
.data
StrArr "Test string 1", 32, randomString
StrArr "Test string 2", 32
StrArr "1234567890123456", 32

.code
start:
; Use your favorite Random Number Generator here
; invoke RandomNumberGenerator

; Clear edx, and divide eax by number of random strings
; This may not be necessary if your RNG supports a range!
xor edx, edx
mov ecx, 3
div ecx

; edx is the modulus of RNG / number of strings
; Multiply edx by 32 as that is the length of our string (including pads)
shl edx, 5

add edx, OFFSET randomString
invoke MessageBox, NULL, edx, NULL, MB_OK
invoke ExitProcess, 0
ENDIF
end start


Mirno
Posted on 2004-11-15 08:20:02 by Mirno
Each time I run this, I get the same string. Am I doing something wrong?
Posted on 2004-11-15 08:52:33 by TropE
I've not got any random number generator function, so the invoke is commented out. To try the idea, just below the invoke add a "mov eax, 1" and re-assemble.

You can search the board for a RNG function.

Mirno
Posted on 2004-11-15 09:40:22 by Mirno
for Random stuff i wrote for me a little proc some time ago.
it returns a random number as ascii code "1" - "4". the rest is easy and short. maybe not optimized, fast solution (the RandomChar proc) but its short.

.586p	

.model flat, stdcall
option casemap :none

include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
include \masm32\include\masm32.inc
include \masm32\macros\macros.asm

includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib

include RandomChar.asm

;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

.data
randomText1 db "Nice Job",0
randomText2 db "Good Job",0
randomText3 db "You did it",0
randomText4 db "All Done!",0

;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
.code
main:

invoke RandomChar,chr$("1234") ;return one of this Chars in AL
.if al=="1"
mov eax,offset randomText1
.elseif al=="2"
mov eax,offset randomText2
.elseif al=="3"
mov eax,offset randomText3
.else
mov eax,offset randomText4
.endif

invoke MessageBox,NULL,eax,chr$("Good Boy"),MB_RETRYCANCEL
.if eax==IDCANCEL
invoke ExitProcess,NULL
.endif
jmp main

end main
Posted on 2004-11-15 09:46:49 by diablo2oo2
Perfect! Thanks so much.
Posted on 2004-11-15 12:05:17 by TropE