News:

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

flipping an array around

Started by bcddd214, May 24, 2011, 08:13:16 PM

Previous topic - Next topic

bcddd214

I have the following program which I am trying to print the array a second time but backwards using push and pop

***************************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

jj2007

Sounds confusing what you want to achieve. And it could be so simple...
include \masm32\include\masm32rt.inc
.data
TheArray dd 100, 101, 102, 103, 104, 105, 106, 107, 108

.code
start: mov esi, offset TheArray
xor ebx, ebx
.Repeat
push [esi+4*ebx]
inc ebx
.Until ebx>=sizeof TheArray/4
.Repeat
pop eax
print str$(eax), 13, 10
dec ebx
.Until Zero?
exit
end start

bcddd214

getting a syntax error with your srt variable
I am using Irvine

Also, am I calling on the start procedure correctly?

Assembling: string-array1.asm
string-array1.asm(91) : error A2008: syntax error : str$

******************************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
TheArray dd 100, 101, 102, 103, 104, 105, 106, 107, 108

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

        .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
            call start
            ret
ShowArray       endp 


start: mov esi, offset TheArray
   xor ebx, ebx
   .Repeat
   push [esi+4*ebx]
   inc ebx
   .Until ebx>=sizeof TheArray/4
   .Repeat
   pop eax
   print str$(eax), 13, 10
   dec ebx
   .Until Zero?
exit
end start

      
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

jj2007

Quote from: bcddd214 on May 24, 2011, 08:52:22 PM
getting a syntax error with your srt variable
I am using Irvine

There ain't no srt in my code. Perhaps you mean str$() - I guess that is WriteInt in irvinespeak...
The rest is plain assembler, must work fine under "irvine".

bcddd214


bcddd214

it doesn't like
print str$(eax), 13, 10

bcddd214

If I remove the line, the entire program appear to not run at all.
executes with no effect.

dedndave

remove this line
print str$(eax), 13, 10

replace it with these 2 lines
        call    WriteInt
        call    CrLf


str$() is a macro that is very flexible with regard to what parameters may be used
in this case, we happen to want to display the value that is in EAX, so WriteInt works perfectly

bcddd214

If I put the code in main, I get tons of garbage??

*******************************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
TheArray dd 100, 101, 102, 103, 104, 105, 106, 107, 108

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

        .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

   mov esi, offset TheArray
      xor ebx, ebx
      .Repeat
      push [esi+4*ebx]
      inc ebx
      .Until ebx>=sizeof TheArray/4
      .Repeat
      pop eax
      ;print str$(eax), 13, 10
      dec ebx
      .Until Zero?


    call ShowArray
;    call DumpRegs     
exit
   
main    ENDP

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

        END     main

bcddd214

Quote from: dedndave on May 24, 2011, 09:52:49 PM
remove this line
print str$(eax), 13, 10

replace it with these 2 lines
        call    WriteInt
        call    CrLf


Nothing I do will allow or force str$
str$  does not work. it is completely broken on my machine.   :(

dedndave

that is why i said to remove it - lol


your program structure is a bit "untidy"

your entry point is "main"
it looks like it may have previously been "start"

the ShowArray routine CALL's "start"
"start" ends with an "exit"
if you want to call it like a routine, it should end with RET
the only "exit" should be at the end of main
very sloppy - makes it hard to follow

bcddd214

I don't see where ShowArray is calling start.
I remove the code and put it into main and it fills the array with junk now??


***********************************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
TheArray dd 100, 101, 102, 103, 104, 105, 106, 107, 108

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

        .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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;   
      mov esi, offset TheArray
      xor ebx, ebx
      .Repeat
      push [esi+4*ebx]
      inc ebx
      .Until ebx>=sizeof TheArray/4
      .Repeat
      pop eax
      ;print str$(eax), 13, 10
      dec ebx
      .Until Zero?
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;   

   
    call    Crlf
    call ShowArray
;    call DumpRegs     
exit
   
main    ENDP

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

        END     main

dedndave

http://www.masm32.com/board/index.php?topic=16750.msg139281#msg139281
ShowArray CALL's start in that post

now, you have replaced it with a hard JMP
i am guessing that it gets stuck in an endless loop, now

spend a little time re-organizing the overall program
make it clear

main should be the entry point
for the most part, the other functions should be called from main
there will be exceptions to this, of course
but for the simple task you are trying to perform, you should not require too much nesting
as it is, it's starting to look like a bowl of spaghetti
no wonder you are having difficulty

bcddd214

Yeah, I did a pretty bad job this time.
It's giving me fits and I am doing what ever I can to get this last part working.
Any assistance would be super appreciated. I seem to have got myself in the weeds with this and unsure how to bail myself out.

:(

qWord

hi,
try this:
Quoteinclude \Irvine\Irvine32.inc

includelib \Irvine\Irvine32.lib
includelib \Irvine\kernel32.lib
includelib \Irvine\user32.lib

GlobalAlloc proto :DWORD,:DWORD
GPTR equ 40h

fn macro fnName:req,args:VARARG
   IFNDEF lbl_cntr
      lbl_cntr = 0
   ENDIF
   rv_args TEXTEQU <>
   FOR arg,<&args>
      IFNB <&arg>
         IF @InStr(1,<&arg>,<!">) EQ 1
            .data
               @CatStr(<sz_rv_>,%lbl_cntr) db arg,0
               rv_args CATSTR rv_args,@CatStr(<,OFFSET sz_rv_>,%lbl_cntr)
               lbl_cntr = lbl_cntr + 1
            .code
         ELSE
            rv_args CATSTR rv_args,<,arg>
         ENDIF
      ELSE
         rv_args CATSTR rv_args,<,arg>
      ENDIF
   ENDM
   invoke fnName rv_args
endm
rv macro fnName,args:VARARG
   fn fnName,args
   EXITM <eax>
endm
chr$ macro args:VARARG
   IFNDEF lbl_cntr
      lbl_cntr = 0
   ENDIF
   .data
      @CatStr(<sz_chr$_>,%lbl_cntr) db args,0
      lbl_cntr = lbl_cntr + 1
   .code
   EXITM @CatStr(<OFFSET sz_chr$_>,%lbl_cntr-1)
endm
print macro args:VARARG
   mov edx,chr$(args)
   invoke WriteString
endm

.code
random_num proc uiLower:DWORD,uiUpper:DWORD
   
   mov eax,uiUpper
   sub eax,uiLower
   call RandomRange
   add eax,uiLower
   
   ret
   
random_num endp

main proc
LOCAL pArray: ptr DWORD
LOCAL nElements:SDWORD
LOCAL lower:DWORD
LOCAL upper:DWORD
LOCAL nColumns:DWORD

   invoke Clrscr     
    invoke Randomize
   
   print "number of elements: "
   mov nElements,rv(ReadInt)
   .if nElements <= 0
      print "invalid input"
      invoke WaitMsg
      exit
   .endif
   
   print "lower limmit for numbers: "
   mov lower,rv(ReadInt)

   print "upper limmit for numbers: "
   mov upper,rv(ReadInt)   
   .if eax == lower
      print "error: zero range"
      invoke WaitMsg
      exit
   .endif
   
   print "columns per row: "
   mov nColumns,rv(ReadInt)   
   .if nColumns <= 0
      print "invalid input"
      invoke WaitMsg
      exit
   .endif

   lea eax,[eax*4] ; buffer size = nElements * SIZEOF DWORD
   mov pArray,rv(GlobalAlloc,GPTR,eax)
   mov edi,eax
   
   ; fill array with random numbers and print them
   xor ebx,ebx
   xor esi,esi
   .while ebx < nElements
      invoke random_num,lower,upper
      mov [edi+ebx*4],eax
      .if esi < nColumns
         call WriteInt
         print 9
      .else
         xor esi,esi
         call Crlf
         call WriteInt
         print 9
      .endif
      inc esi
      inc ebx
   .endw
   print 13,10
   
   print "reversed: ",13,10
   ; print array in reversed order
   mov ebx,nElements
   xor esi,esi
   .while ebx
      mov eax,[edi+ebx*4-4]
      .if esi < nColumns
         call WriteInt
         print 9
      .else
         xor esi,esi
         call Crlf
         call WriteInt
         print 9
      .endif
      inc esi
      dec ebx
   .endw
   print 13,10
   
   
   invoke WaitMsg
   
   exit
   ret
   
main endp
end main
FPU in a trice: SmplMath
It's that simple!