News:

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

counting help

Started by vivendi, September 30, 2006, 02:32:12 PM

Previous topic - Next topic

Vortex

vivendi,

StdIn puts a CR+LF pair to the end of the buffer receiving keyboard input. You need to remove those ASCII characters to convert the string contained in the buffer to a DWORD value. atodw convers a NULL terminated string to a DWORD

Quoteatodw

atodw proc uses edi esi String:PTR BYTE

Note that the parameter String is an address of DWORD size.

Description
atodw converts a decimal string to dword.

Example
invoke atodw,ADDR MyDecimalString

Parameter
   1. String The address of the decimal string to convert

Return Value
The DWORD value is returned in eax.

Notice that StdIn returns the number of keyboard strokes passed to the buffer pointed by this function.

Here is another version of the counter demo :

.data?
buffer          db 100 dup(?)       ; we allocate an uninitialized data block of 100 bytes
                                ; to store the final state of the "message" string processed
                                ; by wsprintf - 100 is an arbitrary value, you could set the
                                ; value depending on the exact final size of "message"
input_buffer    db 100 dup(?)

.code

start:
    invoke  StdIn,ADDR input_buffer,100 ; receive input from keyboard
                                        ; notice that StdIn will write the CR+LF pair to the buffer
    sub     eax,2
    mov     BYTE PTR [input_buffer+eax],0 ; stripping the CR+LF pair, ASCII 13,10
    invoke  atodw,ADDR input_buffer     ; atodw converts a decimal string to dword   
    cmp     eax,5                       ; check the return value stored in eax
    jbe     @f                          ; if eax<=5 then jump to the nearest next anonymous label
    invoke  StdOut,ADDR errmsg          ; eax greater than 5? Display an error message and
    jmp     start                       ; go back to receive another input

@@:
    mov     edx,OFFSET numbers  ; we set a pointer to the array of 5 integers
                                ; "OFFSET numbers" holds the address of the array
                                ; mov edx,numbers -> edx will be simply set to "1" , the value
                                ; of the first array member
    mov     ecx,eax
    xor     eax,eax
    .
    .



[attachment deleted by admin]

vivendi

Thanks alot, you pretty much made my homework :)

I still need to change some things though. What i need, is a program that first asks how much numbers i want to use, so lets say i enter '5'.
Then after that it should ask, enter 5 different numbers, so if i enter, 1,2,3,4,5 then it should give me the sum of those numbers.

Now i've got a good start with the help of you guys, and especially with the sourcecode you just provided. But i wanted to start all over and do the rest myself.

But again, i got stuck iwth the StdIn part.
I have this code right now:


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


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

.code


start:

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

  invoke wsprintf,ADDR buffer,ADDR message, ADDR input_buffer
  invoke StdOut, ADDR buffer

  invoke ExitProcess,0


END start


It asks me to enter a number, and it should display that number right after that, but i get something like this instead: 4206724

I've tried to mov it in eax, tried to change it to BYTE PTR [input_buffer] and some other things, but all failed.
So what am i doing wrong this time... :(

Vortex

Have a look at the comments :

Quote.data
message         db 'The number you entered is %d',0 ; You should put the NULL string terminator

.data?
buffer          db 100 dup(?)
input_buffer    db 100 dup(?) ; db instead of dd

.code


start:

  invoke    StdIn, ADDR input_buffer, 100
  sub       eax, 2
  mov       BYTE PTR [input_buffer+eax],0
  invoke    atodw, ADDR input_buffer ; the result is stored in eax
  invoke    wsprintf,ADDR buffer,ADDR message,eax ; eax holds the number converted to DWORD
  invoke    StdOut, ADDR buffer
  invoke    ExitProcess,0


END start

ecube

Quote from: vivendi on October 01, 2006, 10:15:16 AM
Thanks alot Vortex, thats actually the code i needed. I know this, cause i saw that the teacher had something simulair. He also had this line at the top: "numbers     dd 1,2,3,4,5"

I haven't tried it out yet, not really checked the code out, have to see if i understand whats going on in the code.

BTW, how do i build as console? I used this command to build my asm file:

C:\masm32\bin> build.bat filename


i'm shocked you're learning asm/masm from a teacher, I wish I had that luxury. Even all the asm books i've seen written are dos based. Keep with it asm is a fantastic language and masm32 a excellent package.

vivendi

Quote from: E^cube on October 01, 2006, 10:23:04 PM
Quote from: vivendi on October 01, 2006, 10:15:16 AM
Thanks alot Vortex, thats actually the code i needed. I know this, cause i saw that the teacher had something simulair. He also had this line at the top: "numbers     dd 1,2,3,4,5"

I haven't tried it out yet, not really checked the code out, have to see if i understand whats going on in the code.

BTW, how do i build as console? I used this command to build my asm file:

C:\masm32\bin> build.bat filename


i'm shocked you're learning asm/masm from a teacher, I wish I had that luxury. Even all the asm books i've seen written are dos based. Keep with it asm is a fantastic language and masm32 a excellent package.

I wish it was all that great, my teacher works with a simulator called zep2, which uses instructions like, load and store and org etc. Instructions you dont see with a normal asm language like NASM, MASM etc..
So i choosed to convert all the assignments that are made for the zep2 environment, to MASM code. Which is pretty hard btw! But educational.

Anyway, i've been searching and coding all night long, some things went pretty good on my own, but theres this one thing i couldn't figure out. Not even wiith the help of google.

In the code above i've got that array: numbers dd 1,2,3,4,5

And i know how to store a value with user input. But how do i store an array like that?? A user must be able to enter 5 different numbers, and those 5 must be stored in an array like that. But how can this be done!?


Boucly

vivendi,

I asssume that you uses MASM32. Read the .asm files in C:\masm32\tutorial\console\, especially demo6\ and demo7\. Take a look if you understand. I just learned it myself. :bg Array addressing relies on referencing and addressing and adding numbers after it.

Boucly