Hey, i know i can manually make an array in te .data section, like this:

.data
  arrNum dd 1,2,3,4,5

but now i want a user to enter multiple numbers in console (using StdIn), and store these in an array just like that.
Only i dont know how to do this, can someone please help me out with this.
Posted on 2006-10-01 14:29:44 by w00
Okay, maybe i wasn't making myself clear, so here's an example of what i have.


.386
.model flat,stdcall
option casemap:none

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

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

.data
  numbers dd 1,2
  message db 'Total is %d'


.data?
  buffer dd 100 dup(?)
  input_buffer dd 100 dup(?)

.code


start:

  invoke StdIn, ADDR input_buffer, 100
  sub eax, 2
  mov BYTE PTR ,0
  invoke atodw, ADDR input_buffer

  invoke wsprintf,ADDR buffer,ADDR message, eax
  invoke StdOut, ADDR buffer

  invoke ExitProcess,0


END start


This code asks the user for an numeric input (numeric), so lets say the user enters the number 5, then it perectly displays the number 5.

But what i want now is that it asks for a number then when i enter a number, then it should ask again, to enter a number.
Both of these numbers should get stored in 1 variable, so an array would seem best for this.

But thats my problem, i know how to store 1 number, but not more then 1 number in the same variable.

Hopefully someone can help me out with this.
Posted on 2006-10-02 07:50:07 by w00
You can store the result ( eax ) of atodw in your array :


mov esi,OFFSET my_array_of_numbers
.
.
mov BYTE PTR ,0
invoke atodw, ADDR input_buffer
mov DWORD PTR ,eax ; store it in
add esi,4 ; increment esi by 4 , every member of the array contains a 32-bit value
.
.
; extra code for the loop




Posted on 2006-10-02 08:42:05 by Vortex
Hey, i've tried alot of things, but still couldn't figure it out. I have this code right now which asks for a number, but it doesn't display a number... Its not even acting as an array.
Any idea what im doing wrong???



.386
.model flat,stdcall
option casemap:none

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

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

.data
    ;nummer    dd 1,2,3,4,5  **** Normally i would've used this one
    nummers    dd 0    ;But now im creating the 'array' without hardcoding their values in it.
    message    db "Totaal is %d",0
   
.data?
    buffer      db 100 dup(?)

.code

start:
    xor    eax, eax
    mov    ecx, 5
    invoke StdIn,ADDR nummers,100  ;This asks for a number, so if i enter '12345' here
                                    ;then why doesn't this acts as an array???

    mov    edx, OFFSET nummers

Count:
    add    eax, DWORD PTR
    add    edx, 4                  ;verhogen met 4 om naar het volgende nummer te gaan. 32bit is +4
    dec    ecx
    jnz    Count

    invoke  wsprintf,ADDR buffer, ADDR message, eax
    invoke  StdOut, ADDR buffer
    invoke  ExitProcess, 0

END start
Posted on 2006-10-04 08:30:04 by w00
i think what your asking is how do i define an array of empty dwords, if so you do it like this in masm

myArray dd 256 dup(0) ;would define a 256 member array init'd to zero
explaination:
myArray/dd/256/dup(0)
Name/membersize/number of members/initial value

In the code you provided you only define ONE dword so ,if you try writing to the next dword in the list, it doesn't exist! you are infact overwriting your next bit of data which in this case is
message    db "Totaal is %d",0
which would probably give you errors on wsprintf function (if it manages to get that far)
Posted on 2006-10-04 08:42:19 by Nice Eddie
Im not sure if this is what you mean, but i've tried this:
I defined an array in the .data? section like this


.data
    message    db "Totaal is %d",0
.data?
    buffer      db 100 dup(?)
    nummers    dd 250 dup(?) ;this would be the array where im gonna store the numbers in



And then asking for an input like i did before

invoke  StdIn, ADDR nummers, 250


Basically all i changed was the .data sections. And the rest remains the same.
But im probably doung it wrong cause i still have no output... :(
Posted on 2006-10-04 09:04:36 by w00
First, you get ASCII TEXT input from the user. Then, you convert this ASCII TEXT to a machine integer representation (if you're using m32lib, look at atodw or whatever). Then, you can do computations/whatever.
Posted on 2006-10-04 09:16:01 by f0dder
aaah! i think i understand now

;This asks for a number, so if i enter '12345' here
;then why doesn't this acts as an array???

because your program is treating this as 1 single number.
You will have to manually seperate each number and place it in the array yourself as a unique value
You should understand the difference between ascii text and decimal numbers.

ascii zero is represented as : decimal 48

so to convert a single digit i would use

lea edx,buffer ;give edx ptr to text
lea ecx,myArray ;give ecx ptr to array
xor eax,eax  ;wipe eax

@@:
mov al,byte ptr ;get the first byte from the string
sub al,48                ;turn it into a proper number from its ascii version
mov ,eax          ;store the value in the array

add ecx,4                ;move the array ptr to the next member
inc edx                    ;move the string ptr to the next ascii character
xor eax,eax            ;wipe eax

cmp byte ptr ,al ;see if it is the last ascii byte
jnz @b                    ;if not repeat the process


this would load the array with with single numbers convered from ascii byte to usable dwords
BUT NOTE! :this little routine has NO error checking but ill leave that for you to work on.
Try looking at some uppercase and lower casing code and play with it a bit to get used to ascii.
Posted on 2006-10-04 09:27:28 by Nice Eddie
I've converted it to a dword, but still no luck.
My code from the .data sections with comments where i changed it


.data
    ;nummers    dd 2000000001,2000000002,2000000003,2000000004,2000000005
    ;nummers    dd 1,2,3,4,5  ;**** Normally i would've used this one
    ;nummers    dd 0    ;But now im creating the 'array' without hardcoding their values in it.
    message    db "Totaal is %d",0
   
.data?
    buffer      db 100 dup(?)
    nummers    db 250 dup(?)  ;Tried this as dd and db

.code

start:
    xor    eax, eax
    mov    ecx, 5
   
    invoke  StdIn, ADDR nummers, 250    ;i enter: 12345 in console
    sub    eax, 2                     
    mov    BYTE PTR ,0    ;strip off ,10,13 'CR+LF'
    invoke  atodw, nummers              ;convert to dword

    mov    edx, OFFSET nummers

Count:
    adc    eax, DWORD PTR
   
    add    edx, 4
    dec    ecx
    jnz    Count

    invoke  wsprintf,ADDR buffer, ADDR message, eax
    invoke  StdOut, ADDR buffer
    invoke  ExitProcess, 0

END start


@Nice Eddie
I didnt understand that code, yet, i just quickly read through it, ill try to sort that out later. Going home now :)
Posted on 2006-10-04 09:36:21 by w00
:roll:
okey dokey mate. :D
Like most things in asm its not hard when you understand...
If you get stuck pm a yahoo/M$mess id and ill talk you through it....
Posted on 2006-10-04 09:40:59 by Nice Eddie