News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

switching from invoke to while, logical error

Started by bcddd214, May 20, 2011, 10:28:18 PM

Previous topic - Next topic

bcddd214

I have the follow code which produces a number 1-100, 20 times but it uses the invoke function.
I want to remove invoke and do the exact same thing using a while loop instead of invoke.

Any thoughts on how to go about doing this task??

**************************code***************************
        TITLE   Random

        INCLUDE Irvine32.inc

RandLoop PROTO  :DWORD,:DWORD
RandLoop2 PROTO  :DWORD,:DWORD,:DWORD

;#########################################################

COUNT2   = 1
PDWORD TYPEDEF PTR DWORD
;#########################################################

        .DATA?
MyArray dd 100 dup(?) ;creates an empty array of 100 bytes

        .DATA
AryPtr dd MyArray         ;a dword value that tells you the current position in the array
commaStr BYTE ", ",0
str1 BYTE "Total=  ",0
Index       dd 0        ; define this Index
VALUE_FINAL dd 0
arrayD DWORD ", ",0   ;initialize arrayD to hold aray data
ptr1 PDWORD arrayD

;#########################################################

        .CODE

RandLoop2 PROC   dwMinVal2:DWORD,dwMaxVal2:DWORD,COLUMN:DWORD
        LOCAL   dwRange2:DWORD
      mov VALUE_FINAL,0 ;we initialize this variable with zero
        mov     esi,AryPtr
      JE DO_NOTHING ;so do nothing
        mov     eax,dwMaxVal2
        sub     eax,dwMinVal2
        mov     dwRange2,eax
        mov     edx,OFFSET commaStr
      mov     ecx,COUNT2
      
   WhileLp:
         mov     eax,dwRange2
         call    RandomRange
         add     eax,dwMinVal2
         add [VALUE_FINAL],eax ;first moment the value_final is zero, second moment is ????????
         call    WriteInt
;         call    WriteString
      WhileDone:

      call array1
        call    Crlf ;an enter in the end of column

DO_NOTHING:
        ret
RandLoop2 ENDP

array1 PROC
      mov     ebx, Index
      mov     eax, VALUE_FINAL
      mov     [esi+ebx], eax    ;store EAX into the array
      add     ebx, 4               ;point to next dword
      mov     Index, ebx
      ret
array1 ENDP


ShowArray       proc
                xor     ebx, ebx
    _next:      cmp     ebx, Index
                jne     @F
                ret
        @@:     mov     eax, [esi+ebx]    ; get EAX from the array
                add     ebx, 4            ; point to next dword
                call WriteInt
;            call Crlf
            jmp     _next
            ret
ShowArray       endp 

      
main    PROC
        call    Clrscr     
        call    Randomize      ;initialize random generator
      INVOKE  RandLoop2,1,100,0
      INVOKE  RandLoop2,1,100,0
      INVOKE  RandLoop2,1,100,0
      INVOKE  RandLoop2,1,100,0
      INVOKE  RandLoop2,1,100,0
      INVOKE  RandLoop2,1,100,0
      INVOKE  RandLoop2,1,100,0
      INVOKE  RandLoop2,1,100,0
      INVOKE  RandLoop2,1,100,0
      INVOKE  RandLoop2,1,100,0
        call    Crlf
      call ShowArray
      call DumpRegs
        exit
   
main    ENDP

;#########################################################

        END     main

qWord

main    PROC
LOCAL cntr:DWORD
    call    Clrscr     
    call    Randomize      ;initialize random generator

    mov cntr,20
    .while cntr
        INVOKE  RandLoop2,1,100,0
        dec cntr
    .endw

    call    Crlf
    call ShowArray
    call DumpRegs
    exit
   
main    ENDP
FPU in a trice: SmplMath
It's that simple!

bcddd214

she kicks back an error about declaring cntr?

where and how should I declare this?

dedndave

qWord has it defined as a local in main
which should be OK - the OS discards the stack on exit

however, you could make it a global DWORD variable in .DATA?

bcddd214

 cntr is not currently declared anywhere.
I tried to just declare it and set it to zero but that failed.

Can you give me a good lead?

dedndave

first, remove this line
LOCAL cntr:DWORD

then, in the .DATA? section, add this line
cntr dd ?

.DATA? is uninitialized data
that means that it will not change the size of the EXE
it is memory assigned when the OS loads the EXE
that means you have to initialize it, which is OK - that is what this line does...
    mov cntr,20

personally, i would not use WHILE   :P
i would make a loop like this...
        mov     ecx,20

loop00: push    ecx
        INVOKE  RandLoop2,1,100,0
        pop     ecx
        loop    loop00

bcddd214

It was also erroring at a dumpregs but she works now.

If I wanted spit the array out again on a new line but backwards, wouldn't pushing them one at a time into the register and then popping them back out, be the easiest and most resourceful, given this program?

*******************************code************************************
        TITLE   Random

        INCLUDE Irvine32.inc

RandLoop PROTO  :DWORD,:DWORD
RandLoop2 PROTO  :DWORD,:DWORD,:DWORD

;#########################################################

COUNT2   = 1
PDWORD TYPEDEF PTR DWORD
;#########################################################

        .DATA?
MyArray dd 100 dup(?) ;creates an empty array of 100 bytes
cntr dd ?
        .DATA
AryPtr dd MyArray         ;a dword value that tells you the current position in the array
commaStr BYTE ", ",0
str1 BYTE "Total=  ",0
Index       dd 0        ; define this Index
VALUE_FINAL dd 0
arrayD DWORD ", ",0   ;initialize arrayD to hold aray data
ptr1 PDWORD arrayD

;#########################################################

        .CODE

RandLoop2 PROC   dwMinVal2:DWORD,dwMaxVal2:DWORD,COLUMN:DWORD
        LOCAL   dwRange2:DWORD
      mov VALUE_FINAL,0 ;we initialize this variable with zero
        mov     esi,AryPtr
      JE DO_NOTHING ;so do nothing
        mov     eax,dwMaxVal2
        sub     eax,dwMinVal2
        mov     dwRange2,eax
        mov     edx,OFFSET commaStr
      mov     ecx,COUNT2
      
   WhileLp:
         mov     eax,dwRange2
         call    RandomRange
         add     eax,dwMinVal2
         add [VALUE_FINAL],eax ;first moment the value_final is zero, second moment is ????????
         call    WriteInt
;         call    WriteString
      WhileDone:

      call array1
        call    Crlf ;an enter in the end of column

DO_NOTHING:
        ret
RandLoop2 ENDP

array1 PROC
      mov     ebx, Index
      mov     eax, VALUE_FINAL
      mov     [esi+ebx], eax    ;store EAX into the array
      add     ebx, 4               ;point to next dword
      mov     Index, ebx
      ret
array1 ENDP


ShowArray       proc
                xor     ebx, ebx
    _next:      cmp     ebx, Index
                jne     @F
                ret
        @@:     mov     eax, [esi+ebx]    ; get EAX from the array
                add     ebx, 4            ; point to next dword
                call WriteInt
;            call Crlf
            jmp     _next
            ret
ShowArray       endp 

      
main    PROC
        call    Clrscr     
        call    Randomize      ;initialize random generator
    mov cntr,20
    .while cntr
        INVOKE  RandLoop2,1,100,0
        dec cntr
    .endw

    call    Crlf
    call ShowArray
;    call DumpRegs     
exit
   
main    ENDP

;#########################################################

        END     main