News:

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

Array question

Started by bcddd214, April 24, 2011, 03:00:44 PM

Previous topic - Next topic

bcddd214

I want to place the solutions for the following code, each answer and store them into an array.

1+2=3
3->array
4+2=6
6->array
3+3=6
6->array

total 4s = 1
total 6s = 2

etc????



        TITLE   Random

        INCLUDE Irvine32.inc

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

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

COUNT   = 10
COUNT2   = 2
;#########################################################

        .DATA

commaStr BYTE ", ",0
str1 BYTE "Total=  ",0

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

        .CODE

RandLoop2 PROC   dwMinVal2:DWORD,dwMaxVal2:DWORD,COLUMN:DWORD
;this procedure show X columns of random value betwen dwMinVal and dwMaxVal
;this procedure add one colum to show the sum of values
        LOCAL   dwRange2:DWORD
LOCAL VALUE_FINAL:DWORD

mov VALUE_FINAL,0 ;we initialize this variable with zero
;cmp COLUMN,0 ;zero columns?
;mov COUNT2,0 ;set count2 to zero
JE DO_NOTHING ;so do nothing
        mov     eax,dwMaxVal2
        sub     eax,dwMinVal2
        mov     dwRange2,eax
        mov     edx,OFFSET commaStr
;        mov     ecx,COLUMN
      mov     ecx,COUNT2
RLoop1:
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
;        call DumpRegs
      loop    RLoop1
      
      mov edx, offset str1 ;we show the "total= "
        call    WriteString
        mov eax,VALUE_FINAL ;and after we show the sum of the numbers
        call WriteInt
        call    Crlf ;an enter in the end of column

DO_NOTHING:
        ret
RandLoop2 ENDP


main    PROC
        call    Clrscr     
        call    Randomize      ;initialize random generator
      INVOKE  RandLoop2,1,6,0
      INVOKE  RandLoop2,1,6,0
      INVOKE  RandLoop2,1,6,0
      INVOKE  RandLoop2,1,6,0
      INVOKE  RandLoop2,1,6,0
      INVOKE  RandLoop2,1,6,0
      INVOKE  RandLoop2,1,6,0
      INVOKE  RandLoop2,1,6,0
      INVOKE  RandLoop2,1,6,0
      INVOKE  RandLoop2,1,6,0
        call    Crlf
        exit

main    ENDP

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

        END     main

bcddd214

OR

Would it make more sense to put each value into a different stack?
Is this possible?

2+5=7
7->[7_stack]
4+2=6
6->[6_stack]
1+6=7
7->[7_stack]

6_stack=1
7_stack=4


Which is faster or better or possible?

dedndave

it depends on how you are going to use the information, really
the stack is generally used for temporary values with "local" scope
an array created in one of the data sections is more permanent and has "global" scope
the scope refers to how it may be accessed
local scope means that only the routine where the locals are defined can access the data
global scope means the data can be accessed by any part of the program

sounds to me like you may want a global array

.DATA?
MyArray db 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


to use it, move AryPtr into a register
that register may then be used to add items
this example shows how to store a byte
        mov     edx,AryPtr
;
;
        mov     [edx],al      ;store AL into the array
        inc     edx              ;point to next byte

when you are done updating the array, store the pointer so you know where you left off
        mov     AryPtr,edx
now, AryPtr tells you where the next byte goes

jj2007

Have a look at the stosd opcode, really cute for that purpose.

bcddd214

stosd opcode?
when can I get a link?

I am getting some really disgusting hearts and smiley faces in my output....??

        TITLE   Random

        INCLUDE Irvine32.inc

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

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

COUNT   = 10
COUNT2   = 2
;#########################################################

        .DATA?
MyArray db 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

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

        .CODE

RandLoop2 PROC   dwMinVal2:DWORD,dwMaxVal2:DWORD,COLUMN:DWORD
;this procedure show X columns of random value betwen dwMinVal and dwMaxVal
;this procedure add one colum to show the sum of values
        LOCAL   dwRange2:DWORD
LOCAL VALUE_FINAL:DWORD

mov VALUE_FINAL,0 ;we initialize this variable with zero
;cmp COLUMN,0 ;zero columns?
;mov COUNT2,0 ;set count2 to zero
JE DO_NOTHING ;so do nothing
        mov     eax,dwMaxVal2
        sub     eax,dwMinVal2
        mov     dwRange2,eax
        mov     edx,OFFSET commaStr
;        mov     ecx,COLUMN
      mov     ecx,COUNT2
      
      RLoop1:
      mov     edx,AryPtr
      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
      loop    RLoop1
      
      mov edx, offset str1 ;we show the "total= "
        call    WriteString
        mov eax,VALUE_FINAL ;and after we show the sum of the numbers
        call WriteInt
        add [VALUE_FINAL],edx
      mov     [edx],al      ;store AL into the array
      inc     edx              ;point to next byte
      ;        call DumpRegs
        call    Crlf ;an enter in the end of column

DO_NOTHING:
        ret
RandLoop2 ENDP


main    PROC
        call    Clrscr     
        call    Randomize      ;initialize random generator
      INVOKE  RandLoop2,1,6,0
      INVOKE  RandLoop2,1,6,0
      INVOKE  RandLoop2,1,6,0
      INVOKE  RandLoop2,1,6,0
      INVOKE  RandLoop2,1,6,0
      INVOKE  RandLoop2,1,6,0
      INVOKE  RandLoop2,1,6,0
      INVOKE  RandLoop2,1,6,0
      INVOKE  RandLoop2,1,6,0
      INVOKE  RandLoop2,1,6,0
        call    Crlf
        exit

main    ENDP

bcddd214

Hmmmm.
I tried using esi since I did a dumpregs and saw edx in use.
I also tried to put the array increment into a separate procedure. That did nothing to help except make debugging easier.
Granted, I already knew what was jacked up.  :P
:)

some kind of wild buffer error.

Love the site, finally making real progress!

:)

***********************more noobie code********************

        TITLE   Random

        INCLUDE Irvine32.inc

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

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

COUNT   = 10
COUNT2   = 2
;#########################################################

        .DATA?
MyArray db 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

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

        .CODE

RandLoop2 PROC   dwMinVal2:DWORD,dwMaxVal2:DWORD,COLUMN:DWORD
;this procedure show X columns of random value betwen dwMinVal and dwMaxVal
;this procedure add one colum to show the sum of values
        LOCAL   dwRange2:DWORD
LOCAL VALUE_FINAL:DWORD

mov VALUE_FINAL,0 ;we initialize this variable with zero
;cmp COLUMN,0 ;zero columns?
;mov COUNT2,0 ;set count2 to 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,COLUMN
      mov     ecx,COUNT2
      
      RLoop1:

      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
      loop    RLoop1
      
      mov edx, offset str1 ;we show the "total= "
        call    WriteString
        mov eax,VALUE_FINAL ;and after we show the sum of the numbers
        call WriteInt
      add [VALUE_FINAL],edx
      call array1
      call DumpRegs
        call    Crlf ;an enter in the end of column

DO_NOTHING:
        ret
RandLoop2 ENDP

array1 PROC
        mov     [esi],al      ;store AL into the array
      inc     esi              ;point to next byte
      array1 ENDP
      
main    PROC
        call    Clrscr     
        call    Randomize      ;initialize random generator
      INVOKE  RandLoop2,1,6,0
      INVOKE  RandLoop2,1,6,0
      INVOKE  RandLoop2,1,6,0
      INVOKE  RandLoop2,1,6,0
      INVOKE  RandLoop2,1,6,0
      INVOKE  RandLoop2,1,6,0
      INVOKE  RandLoop2,1,6,0
      INVOKE  RandLoop2,1,6,0
      INVOKE  RandLoop2,1,6,0
      INVOKE  RandLoop2,1,6,0
        call    Crlf
        exit

main    ENDP

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

        END     main

RuiLoureiro

array1 PROC
       mov     [esi],al      ;store AL into the array
       inc     esi              ;point to next byte
array1 ENDP

you must end array1 proc with RET ok ?

array1 PROC
       mov     [esi],al      ;store AL into the array
       inc     esi              ;point to next byte
       ret
array1 ENDP

jj2007

Quote from: bcddd214 on April 24, 2011, 05:07:50 PM
stosd opcode?
when can I get a link?

Here is your link: \masm32\help\opcodes.chm

Fascinating lecture, by the way :green2

bcddd214

Lecture?
What, my gross attempt to move from pathetic noob to a simple idiot?
:P

I am trying!

RuiLoureiro

Quote from: bcddd214 on April 24, 2011, 06:09:27 PM
I am trying!
It seems you want to save VALUE_FINAL into that array and not AL byte
Yes or no ?

Every time you INVOKE  RandLoop2 you want to save that VALUE_FINAL
Yes or no ?

dedndave

i thought i gave you a link to Randy Hyde's AoA online
http://www.arl.wustl.edu/~lockwood/class/cs306/books/artofasm/toc.html
chapter 6 has most of the instructions
the string instructions, like STOSD, are covered in chapter 15

bcddd214

Quote from: RuiLoureiro on April 24, 2011, 06:12:28 PM
Quote from: bcddd214 on April 24, 2011, 06:09:27 PM
I am trying!
It seems you want to save VALUE_FINAL into that array and not AL byte
Yes or no ?

Every time you INVOKE  RandLoop2 you want to save that VALUE_FINAL
Yes or no ?


YES
YES

bcddd214

3+5=8
8->array1
2+3=5
5->array

show array

dedndave

so, you want to create a DWORD array - not a BYTE array
make it big enough so you don't overflow
a good idea to check to see if it's full, but it's easier to make a bigger buffer - lol

if you put the value you want to store in EAX, and the current buffer pointer in EDI, then use STOSD,
it will store the value in the array
after that, store the new pointer from EDI

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

.DATA
AryPtr dd MyArray         ;a dword value that tells you the current position in the array
;
;
;
       mov     eax,ValueToStore
       mov     edi,AryPtr
       stosd
       mov     AryPtr,edi

with the STOSD instruction, EDI is automatically updated to point to the next dword - just store it

dedndave

if your function stores 3 dwords into the array, you only need to load edi with the pointer once at the beginning of the routine
then store it back to AryPtr at the end...
        mov     edi,AryPtr
;
;
        stosd
;
;
        stosd
;
;
        stosd
;
;
        mov     AryPtr,edi