News:

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

Array problem (simple I think)

Started by j00n, February 25, 2011, 07:38:34 PM

Previous topic - Next topic

j00n

; array stuff - Friday, February 25, 2011 - 3:01:44 AM


include \masm32\include\masm32rt.inc


intw     typedef     sword            ;typedef examples
char     typedef     byte


;initialized variables

.data
    pi          db          3.14
    myString    db          "this is my string",0
    x           real4       1.5
    y           real8       1.0e-25
    z           real10      -1.2594e+10
    myInt       intw         2
    squares  char   0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10


    ary DWORD 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 \
             ,21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40


;uninitialized variables

.data?
    bytevar         db ?
    wordvar         dw ?
    dwordvar        dd ?
    quadword        dq ?
    firstword       dd ?
    secondword      dd ?
    arr2            dd      40 dup (?)

.code


start:


mov ebx, 0                                          ; start at 0
.while ebx < sizeof ary                             ; loop until we reach sizeof ary
    add ebx, 2                                      ; count up by 2
    mov eax, [ary+ebx]                              ; store item to eax
    mov secondword, eax                             ; store eax in secondword
    print addr secondword, 13, 10                   ; print secondword - carriage / line feed
.endw


inkey "press any key to quit"


exit
end start


This works but it prints the symbol instead of the number I want... how do I print the actual contents in this case? 1, 2, 3 of ary ?

qWord

uses the udword$() macro for converting the dword to an string. Also take care of operand sizes.
FPU in a trice: SmplMath
It's that simple!

Vortex

Hi j00n,

A qucik example for you :


include PrintArray.inc

NUMB_OF_ELEMENTS = 10

.data

array   dd 1,2,3,4,5,6,7,8,9,10
format1 db '%d',13,10,0

.code

start:

    call    PrintArray         
    invoke  ExitProcess,0

PrintArray  PROC USES ebx esi

LOCAL buffer[8]:BYTE

    mov     esi,OFFSET array
    mov     ebx,NUMB_OF_ELEMENTS
@@:   
    invoke  wsprintf,ADDR buffer,ADDR format1,DWORD PTR [esi]
    invoke  StdOut,ADDR buffer
   
    add     esi,SIZEOF DWORD    ; =4 , each element has a size of four bytes
    dec     ebx
    jnz     @b
    ret

PrintArray  ENDP

END start

j00n

hey thanks for the help I added some comments to see if you would clarify for me...
include PrintArray.inc

NUMB_OF_ELEMENTS = 10                                           ; is this a constant? I haven't seen this used before

.data

array   dd 1,2,3,4,5,6,7,8,9,10
format1 db '%d',13,10,0

.code

start:

    call    PrintArray         
    invoke  ExitProcess,0

PrintArray  PROC USES ebx esi                                   ; uses - doesn't this preserve the registers?

LOCAL buffer[8]:BYTE

    mov     esi,OFFSET array                                    ; I forgot the difference of offset / addr, why offset in this case?
    mov     ebx,NUMB_OF_ELEMENTS
@@:   
    invoke  wsprintf,ADDR buffer,ADDR format1,DWORD PTR [esi]   ; could you explain the last bit for me?
    invoke  StdOut,ADDR buffer
   
    add     esi,SIZEOF DWORD                                    ; =4 , each element has a size of four bytes
    dec     ebx
    jnz     @b                                                  ; jump back                         
    ret

PrintArray  ENDP

END start



Vortex

Hi j00n,

NUMB_OF_ELEMENTS is a constant to make more readable the source code.

mov     ebx,NUMB_OF_ELEMENTS is more easy to understand compared to

mov     ebx,10

USES ebx esi will preserve only ebx and esi. Have a look at here for more details.

About ADDR :

QuoteThe addr operator is used to pass the address of a label to the function. It's valid only in the context of invoke directive. You can't use it to assign the address of a label to a register/variable, for example. You can use offset instead of addr in the above example. However, there are some differences between the two:

addr cannot handle forward reference while offset can.

Have a look at Iczelion's tutorial MessageBox for an example.

About wsprintf , from win32.hlp :

QuoteThe wsprintf function formats and stores a series of characters and values in a buffer. Any arguments are converted and copied to the output buffer according to the corresponding format specification in the format string. The function appends a terminating null character to the characters it writes, but the return value does not include the terminating null character in its character count.

The purpose is to print a number followed by a CR+LF pair, ASCII 13+10

wsprintf will replace %d with each elements of the array. %d is user for signed decimal integer arguments. esi points the number array and DWORD PTR [esi] holds the value of each element. Suppose that esi = 402000h :

402000h -> 1
402004h -> 2
402008h -> 3
.
.