Solved

Display random characters

Posted on 2004-10-10
21
592 Views
Last Modified: 2013-12-03
Hi

I want to display random characters at a designated rectangular.
I am able to generate teh random characters but how can I make them display only within the rectangular starting at say row 5 column 5. Assume that the rectangular runs from row 5 through 15 and column 5 through 10. I have tried using RandomRange to set the number and call Gotoxy.. but it doesn't see to work.
0
Comment
Question by:icysmarty
  • 11
  • 10
21 Comments
 
LVL 11

Expert Comment

by:dimitry
Comment Utility
Can you show your source, please ?
0
 

Author Comment

by:icysmarty
Comment Utility
Here it is..
.data
wheight      byte      80
wwidth      byte      25
numchar      dword   2000      
delayt      dword      3000      ;delay 3 seconds
ldelay      dword      300      ;loop delay

nrow      byte      5
ncol      byte      8

CR = 0dh            ;carriage return
LF = 0Ah            ;line feed
;***************************************************************************
.code
main PROC

;Set text color to balck text on yellow background:

      mov eax, black + (yellow *16)
      call SetTextColor
      call Clrscr      ;clear the screen
      mov ecx, numchar      ;loop counter
      
;Generate 2000 characters random integers between 0 and 99
;Include a 3000 milisecond delay
      

      mov dh, nrow
      mov dl, ncol
L1:      
      call Gotoxy
      
      mov eax,16
      call randomRange
      call settextcolor      

      mov eax,50+1
        call randomRange
      add eax,' '

      
        call WriteChar
      
      mov bh, 5
      call RandomRange
      mov dh, bh

      mov bl, 17
      call RandomRange
      mov dl, bl

      ;call RandomRange
      ;add dl, al

      ;mov eax, nrow
      ;call RandomRange
      ;add dh, ah
      
      loop L1



      mov eax, delayt
      call Delay
      ret


main ENDP
END main


I have made some modification since I posted the last message. Now it is getting worse.
0
 
LVL 11

Expert Comment

by:dimitry
Comment Utility
Where are these functions: Gotoxy, RandomRange, WriteChar ?
0
 

Author Comment

by:icysmarty
Comment Utility
It is defined in the library.
0
 
LVL 11

Expert Comment

by:dimitry
Comment Utility
What is your compiler ? Nasm, Tasm or Masm ?
0
 

Author Comment

by:icysmarty
Comment Utility
Masm615
0
 

Author Comment

by:icysmarty
Comment Utility
Can you please tell me the logic to display characters only in a rectangular area at a entire screen?
The problem I have is , i cannot make the random characters show in that rectangular only.
0
 
LVL 11

Expert Comment

by:dimitry
Comment Utility
So you are trying to create Console application for Win32 ?
What is your compilation usage ?
0
 

Author Comment

by:icysmarty
Comment Utility
what do you mean?
0
 
LVL 11

Expert Comment

by:dimitry
Comment Utility
It is not very complicated from "logical" point of view.
Your screen is 80x25 characters. So your X coordinate can be any number from 1 to 80 and Y - from 1 to 25.
Gotoxy(X,Y) moves your "current" cursor position to (X,Y) and you are printing character in this position.
However, implementation with specific library functions demands accurate use of them.
Can you please give me definitions of these functions: Gotoxy, RandomRange and WriteChar ?
I suspect you may pass not correct values to them.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 11

Expert Comment

by:dimitry
Comment Utility
How do you compile your code ?
0
 

Author Comment

by:icysmarty
Comment Utility
this is the randomRange
;--------------------------------------------------------------
RandomRange PROC
;
; Returns an unsigned pseudo-random 32-bit integer
; in EAX, between 0 and n-1. Input parameter:
; EAX = n.
; Last update: 09/06/2002
;--------------------------------------------------------------
       push  ebx
       push  edx

       mov   ebx,eax  ; maximum value
       call  Random32 ; eax = random number
       mov   edx,0
       div   ebx      ; divide by max value
       mov   eax,edx  ; return the remainder

       pop   edx
       pop   ebx

       ret
RandomRange ENDP

;--------------------------------------------------
Gotoxy PROC
;
; Locate the cursor
; Receives: DH = screen row, DL = screen column
; Last update: 7/11/01
;--------------------------------------------------------
.data
_cursorPosition COORD <>
.code
      pushad

      CheckInit      ; was console initialized?
      movzx ax,dl
      mov _cursorPosition.X, ax
      movzx ax,dh
      mov _cursorPosition.Y, ax
      INVOKE SetConsoleCursorPosition, consoleOutHandle, _cursorPosition

      popad
      ret
Gotoxy ENDP

;------------------------------------------------------
WriteChar PROC
;
; Write a character to standard output
; Recevies: AL = character
; Last update: 10/30/02
; Note: WriteConole will not work unless direction flag is clear.
;------------------------------------------------------
      pushad
      pushfd      ; save flags
      CheckInit

      mov  buffer,al

      cld      ; clear direction flag
      INVOKE WriteConsole,
        consoleOutHandle,      ; console output handle
        OFFSET buffer,      ; points to string
        1,      ; string length
        OFFSET bytesWritten,        ; returns number of bytes written
        0

      popfd      ; restore flags
      popad
      ret
WriteChar ENDP
/***********************************************************

0
 

Author Comment

by:icysmarty
Comment Utility
I compile the code using Masm as well

Here is what I understand.
First i paint the background with yellow color, then I mov the cursor to (5,8) (row 5 column 8)... from there I print the random character. After that , i generate a random coordinate within the

(5,8) -------------(5,25)
|                          |
|                          |
|                          |
|                          |
|                          |
(10,8)------------(10,25)

This is the part I did not get
0
 
LVL 11

Expert Comment

by:dimitry
Comment Utility
Ok,

So in order to get row and colomn you need to do something like this:
L1:    
     call Gotoxy
     
     mov eax,16
     call randomRange
     call settextcolor    

     mov eax,50+1
     call randomRange
     add eax,' '
     call WriteChar
     
     mov eax, 5
     call RandomRange
     mov dl, al
     add  dl, 6

     mov eax, 10
     call RandomRange
     mov dh, al
     add  dh, 6

     loop L1
0
 
LVL 11

Expert Comment

by:dimitry
Comment Utility
I posted code for (5,5) - (15,10)
You need to generate random number between 0 and (15-5) and add to initial coordinate 6. (6 if you want to be inside your rectangle)
0
 

Author Comment

by:icysmarty
Comment Utility
why though?
Now it only runs from row 5,5 - 5,10
0
 
LVL 11

Expert Comment

by:dimitry
Comment Utility
Are you sure ? Can you check real values of dl and dh before Gotoxy call ?
For (5,8) - (10,25) code should be:
L1:    
     call Gotoxy
     
     mov eax,16
     call randomRange
     call settextcolor    

     mov eax,50+1
     call randomRange
     add eax,' '
     call WriteChar
     
     mov eax, 18
     call RandomRange
     mov dl, al
     add  dl, 8

     mov eax, 6
     call RandomRange
     mov dh, al
     add  dh, 5

     loop L1

0
 

Author Comment

by:icysmarty
Comment Utility
It works now..
I put
mov eax,6
call RandomRange
mov dh, 'ah'
add dh, 5

can you please explain to me why
mov eax, 18
     call RandomRange
     mov dl, al
     add  dl, 8

     mov eax, 6
     call RandomRange
     mov dh, al
     add  dh, 5

Isn't it adding add the column to the previous value like
8 then 16 then 24 to the column?
0
 
LVL 11

Accepted Solution

by:
dimitry earned 500 total points
Comment Utility
    mov eax, 18
     call RandomRange    ; You receive value between 0 - 17
     mov dl, al
     add  dl, 8                 ; Now DL gets value between 8 - 25

     mov eax, 6
     call RandomRange    ; You get value between 0 - 5
     mov dh, al
     add  dh, 5                ; Now DH gets value between 5 - 10
0
 

Author Comment

by:icysmarty
Comment Utility
Thank you ... i just figured that out.
0
 
LVL 11

Expert Comment

by:dimitry
Comment Utility
You are welcome
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

HOW TO: Connect to the VMware vSphere Hypervisor 6.5 (ESXi 6.5) using the vSphere (HTML5 Web) Host Client 6.5, and perform a simple configuration task of adding a new VMFS 6 datastore.
A safe way to clean winsxs folder from your windows server 2008 R2 editions
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now