News:

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

what I can do?

Started by dgonzalez, November 15, 2005, 04:50:34 AM

Previous topic - Next topic

dgonzalez

I want to make a array with data entered from the keyboard, but when I am going to print it in screen, it shows something totally different, How I can correct this?

.486                                    ; create 32 bit code
    .model flat, stdcall                    ; 32 bit memory model
    option casemap :none                    ; case sensitive

    include \masm32\include\windows.inc     ; always first
    include \masm32\macros\macros.asm       ; MASM support macros

    include \masm32\include\masm32.inc
    include \masm32\include\gdi32.inc
    include \masm32\include\user32.inc
    include \masm32\include\kernel32.inc

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

    .data?
     vn   DD  4 dup (?)
    .code                       ; Tell MASM where the code starts

    start:                          ; The CODE entry point to the program
      call main                   ; branch to the "main" procedure
    exit

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

    main proc
        LOCAL cnt   :DWORD          ; allocate a loop counter
        LOCAL str1  :DWORD
        LOCAL str2  :DWORD
        LOCAL var   :DWORD

        mov esi, vn
        mov cnt,4
        jmp ciclo

    ciclo:
        mov str1, input("type values of array: ")
        print chr$(" ",10,13)
        mov var, sval(str1)
        print chr$(" ",10,13)
        mov eax,var
        mov vn[esi],eax
        add esi,4
        sub cnt, 1
        cmp cnt,0
        ja ciclo
        je imprime

    imprime:
        mov cnt,4
        mov esi,vn
        jmp cimprime
    cimprime:
        mov eax,vn[esi]
        print str$(eax)
        print chr$(" ",10,13)
        print chr$("the values of array are: ")
        print chr$(" ",10,13)
        add esi,4
        sub cnt,1
        cmp cnt,0
        ja cimprime
        je sale         

    sale:
        ret

    main endp


end start                 

rags

One thing I see is right after your local declarations in the main proc, you have this:

        mov esi, vn
        mov cnt,4
        jmp ciclo

    ciclo:

AT this point in time vn will contain garbage, because it is unitialized.
declaring a variable n the DATA? section as an un-initialized variable does not garentee it will contain a zero.

Secondly why the jmp ciclo, when that is the address immediately following the jmp statement? The jump is not needed.
Hope this helps.

Rags
God made Man, but the monkey applied the glue -DEVO

dgonzalez

thank you rags!!

I already made some changes, but when I want to show the array in the screen, the continuous problem, it's no shows zeros (0) but very rare values.


.486                                    ; create 32 bit code
    .model flat, stdcall                    ; 32 bit memory model
    option casemap :none                    ; case sensitive

    include \masm32\include\windows.inc     ; always first
    include \masm32\macros\macros.asm       ; MASM support macros

    include \masm32\include\masm32.inc
    include \masm32\include\gdi32.inc
    include \masm32\include\user32.inc
    include \masm32\include\kernel32.inc

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

    .data
     vn   DD  4 dup (?)
    .code                       ; Tell MASM where the code starts

    start:                          ; The CODE entry point to the program
      call main                   ; branch to the "main" procedure
    exit

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

    main proc
        LOCAL cnt   :DWORD          ; allocate a loop counter
        LOCAL str1  :DWORD
        LOCAL str2  :DWORD
        LOCAL var   :DWORD
        LOCAL tm   :DWORD


        mov esi, vn
        mov cnt,4
       
    ciclo:
        mov str1, input("type values of array: ")
        print chr$(" ",10,13)
        mov var, sval(str1)
        print chr$(" ",10,13)
        mov eax,var
        mov vn[esi],eax
        add esi,4
        sub cnt, 1
        cmp cnt,0
        ja ciclo
        je imprime

    imprime:
        mov cnt,4
        mov esi,vn
        jmp cimprime
    cimprime:
        mov eax,vn[esi]
        mov tm, eax
        print str$(tm)
        print chr$(" ",10,13)
        print chr$("the values of array are: ")
        print chr$(" ",10,13)
        add esi,4
        sub cnt,1
        cmp cnt,0
        ja cimprime
        je sale         

    sale:
        ret

    main endp


end start           




[attachment deleted by admin]

rags

This does not make vn equal to zeros, if that is what you want:

    .data
     vn   DD  4 dup (?)


That statement should be in the .data? section.
If you have a variable that must be equal to some value at startup, then declare it in the .data section.
Here is an example:
Suppose you have a variable named 'wheels' and want it to be equal to 4 at startup.
This is what it would look like in the program listing:

        .DATA
        wheels dd 4


You still have jumps in your code that are not needed.
You do not have to 'jump' to the address immediately after a jump statement if that is where the program flow will take it anyway.
Example:

       xor ecx, ecx      ; zeroes the ecx register
Again:
        add ecx, 5
       cmp ecx, 15
       jne Again          ; when the condition ecx =15 is met, program flow continues at the done label
                              ; otherwise program execution branches back to the Again label.
Done:
      ret




rtegards,

Rags
God made Man, but the monkey applied the glue -DEVO