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

vivendi

Hello, i want to write a simple app, that store's the value's 1,2,3,4,5 in a different memory location, and while its storing the number '1' in the first location, i need another register that counts all the numbers.
So if the numbers 1 to 5 are stored, then another register should have the result '15', cause thats the total of 1+2+3+4+5.

My main problem is the storing in different memory locations, im not sure how to start with this. So i was hoping someone here could help me on my way.

hutch--

This sounds like homework but we will forgive you this time. Here is how you do it in 32 bit MASM code.


; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

comment * -----------------------------------------------------
                        Build this  template with
                       "CONSOLE ASSEMBLE AND LINK"
        ----------------------------------------------------- *

    .code

start:
   
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    call main
    inkey
    exit

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

main proc

    LOCAL var1  :DWORD
    LOCAL var2  :DWORD
    LOCAL var3  :DWORD
    LOCAL var4  :DWORD
    LOCAL var5  :DWORD

    mov var1, 1
    mov var2, 2
    mov var3, 3
    mov var4, 4
    mov var5, 5

    xor eax, eax
    add eax, var1
    add eax, var2
    add eax, var3
    add eax, var4
    add eax, var5

    print str$(eax),13,10

    ret

main endp

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

end start
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

vivendi

Thanks alot!
I tried this code, but how can i tell what the value of eax is at the end? I see you're using "print str$(eax),13,10", but it doesnt look like it's printing any value... Any idea why?

hutch--

Make sure you build it as CONSOLE, not GUI.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Vortex

#4
Another one :

.386
.model flat,stdcall
option casemap:none

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

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

.data
numbers     dd 1,2,3,4,5        ; we define an array to handle easily the sequence of numbers
message     db 'Sum = %d',0     ; %d will be replaced by the sum of the array members

.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"
.code

start:

    xor     eax,eax
    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,5
@@:
    add     eax,DWORD PTR [edx] ; DWORD PTR [edx] retrieves the value of each integer, edx points
                                ; the address of the array
    add     edx,4               ; edx is a full 32-bit register, so we have to increment edx by 4
                               
    dec     ecx                 ; if ecx == 0 then the ZERO flag is set to TRUE
    jnz     @b                  ; after dec ecx, jump to the nearest anonymous label @@ if the
                                ; ZERO flag is FALSE
    invoke  wsprintf,ADDR buffer,ADDR message,eax
                                ; %d is the only parameter specified in the definition of "message"
                                ; eax holds the sum and wsprintf will dump the the string "Sum = 15"
                                ; to the temporary memory block named "buffer"
    invoke  StdOut,ADDR buffer  ; StdOut prints text to the console, wsprintf writes the formatted string specified by
                                ; "message" to the memory block pointed by ADDR buffer
    invoke  ExitProcess,0       

END start

[attachment deleted by admin]

vivendi

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

Vortex

To build the project as console application :

C:\masm32\bin> buildc.bat filename

build.bat is designed for GUI applications.

vivendi

Thanks again, it works fine now :)

Just a last request though, would you mind explaining some things about the code..?
I've tried to comment it, but could you please help me with the parts that i dont understand.
I know i still have alot to learn, but i think this code is a great help if i inderstand what's going on.

Here's what i've commented.

.data
numbers     dd 1,2,3,4,5          ;Not sure what dd is for, creating some sort of an array?
message     db 'Sum = %d',0       ;Defining a string, where %d is replaced with a decimal value.

.data?                            ;Why .data again but then with a '?'... ???
buffer      db 100 dup(?)         ;Dont know what this is for...

.code

start:

    xor     eax,eax               ;make sure eax is empty/initialised.
    mov     edx,OFFSET numbers    ;Whats happening here? OFFSET? Why not just 'mov edx, numbers'
    mov     ecx,5                 ;the number 5 is placed in ecx for the loop
@@:
    add     eax,DWORD PTR [edx]   ;dont know why you're using DWORD PTR, but you obviously are adding the number of edx to eax everytime it loops
    add     edx,4                 ;Same here, not sure why the number 4 is placed in edx, it thought it already contained 'numbers'
    dec     ecx                   ;DECrease ecx
    jnz     @b                    ;Jump if not zero, but how does it know that its checking for ecx, and what is @b for? I only see a label called @@...
    invoke  wsprintf,ADDR buffer,ADDR message,eax           ;i know that wpsrintf is used to put variables together from the C language, but its kinda unclear here for me, the value of eax is probably placed in %d
    invoke  StdOut,ADDR buffer    ;StdOut, prints text to the screen? Why buffer, and not message...
    invoke  ExitProcess,0       

END start

Vortex

vivendi,

I modified the text of my posting above to display the commented version of the source code.

An additional note, here is a table of value of values , edx vs integers pointed by edx. You can examine it with the help of a debugger like Ollydbg :

edx               integer pointed by edx
======       ==============

00402000      1
00402004      2
00402008      3
0040200C      4
00400210      5

Attached is a screenshot taken from a debugging session with Ollydb

[attachment deleted by admin]

Boucly

Hi, vivendi,

Quotenumbers     dd 1,2,3,4,5          ;Not sure what dd is for, creating some sort of an array?
dd is double word, hence 32-bitor 4 bytes. It is an array declaration indeed. Each value has a memory size of dd

Quote.data?                            ;Why .data again but then with a '?'... ???
Quote
From Iczelion's Win32 Asm tutorial
.DATA    This section contains initialized data of your program.
.DATA?  This section contains uninitialized data of your program. Sometimes you just want to preallocate some memory but don't want to initialize it. This section is for that purpose. The advantage of uninitialized data is: it doesn't take space in the executable file. For example, if you allocate 10,000 bytes in your .DATA? section, your executable is not bloated up 10,000 bytes. Its size stays much the same. You only tell the assembler how much space you need when the program is loaded into memory, that's all.
.CONST  This section contains declaration of constants used by your program. Constants in this section can never be modified in your program. They are just *constant*.

Quotebuffer      db 100 dup(?)         ;Dont know what this is for...
buffer name of the variable (you know that of course). db = double byte. 100 dup(?) 100 copies of what's in the bracket, in other words an array of the value in the bracket. ?, Might have to find someone else to answer that, not sure myself.

Quotexor     eax,eax               ;make sure eax is empty/initialised.
Zero eax. Faster in processing than mov eax, 0.

Quotemov     edx,OFFSET numbers    ;Whats happening here? OFFSET? Why not just 'mov edx, numbers'
OFFSET numbers The location (offset) of the variable numbers. In an array is it the location of the first variable.

Sorry I ran out of time.  :( Good luck  :U

Boucly

Seb

Quote from: Boucly on October 01, 2006, 01:13:36 PM
buffer      db 100 dup(?)         ;Dont know what this is for...

Sorry for going off-topic, but doesn't db mean declare byte? :red

Regards,
Seb

Boucly

#11
Quote from: Seb on October 01, 2006, 02:09:12 PM
Quote from: Boucly on October 01, 2006, 01:13:36 PM
buffer      db 100 dup(?)         ;Dont know what this is for...

Sorry for going off-topic, but doesn't db mean declare byte? :red

Regards,
Seb

Sorry, you're right Seb. Thanks for correcting me. I researched on it and **I was right for the past three months  :red :P

Quote
From http://web.cs.wpi.edu/~jburge/courses/c02/cs2011/lectures/Lecture7.PDF

Data Allocation
Directives
• Data allocation directives
allocate storage based on
several predefined types:
– DB – define byte (1 byte)
–DW – define word (2 bytes)
– DD – define doubleword (4 bytes)
– ... and more for larger data types
(up to 10 bytes)

EDIT: I had learned a lot more by helping others than trying to get pity by being a newbie. And that's somehting I learned in the last month or so.

**EDIT: "I was wrong" not "I was right"

Vortex

buffer      db 100 dup(?)

This statements creates an array named buffer containing 100 uninitialized bytes.

vivendi

Thanks alot for helping me with the code!!
At first it looked pretty hard, but i've been playing around with it after the explanations, and i start to understand most of it now!

So thanks again for helping me out :)

vivendi

Sorry, but im having some trouble again  :red

I'm trying to change the code so that it asks for a user input. All i added was a line to .data?


.data?
  buffer db 100 dup(?)
  gNummers  dd 100 dup(?)    ;this one is new


And i added a line in the .code section


.code


start:

  xor eax, eax ;zero eax

  invoke StdIn,gNummers, LENGTHOF gNummers


When i compile and run the app, then eveything seems fine at first, cause it does ask for an input. I can enter any number i want and then i get the result of the old app i was asking for at first, which should give '15' as a result.
But im getting 18... No matter what number i enter.

And if i remove the two lines i added then everything is back to normal. So why is the output 18 when i add my two lines of code...??? Thats really something i don't understand...