News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

printing an array

Started by requiem31, May 10, 2014, 09:33:37 AM

Previous topic - Next topic

requiem31

Hi I am new to this forum and trying to learn masm32 assembly. I have a procedure for printing the contents of the array but I am having trouble accessing individual elements. Right now it prints the entire array. displayBoard PROC
    lea esi, dArray
    mov edi, 144

    .while edi > 0
        xor edx, edx
        mov eax, edi
        mov ecx, 12
        div ecx
       
        .if edx == 0
            print " ", 0dh, 0ah
        .endif
       
        print esi
        sub edi, 1
    .endw
   
    mov eax, input()

displayBoard ENDP

jj2007

First things first: Welcome to the Forum :icon14:

What you posted is not sufficient to judge your problem. Please post your complete code, from "include..." to "end start".

dedndave

ESI contains a pointer to the array
the print macro wants a pointer
so - the value in ESI points to the entire array and never changes

what you probably want to display is the contents at a specific address
i guess it's a byte array ?
what type of values are in the array ?

an example might be something like this
    movzx   eax,byte ptr [esi]
    print   uhex$(eax)

in this case, we get one byte from [ESI]
the byte is zero-extended into EAX (the upper 3 bytes of EAX are 0)
then, we display the hexadecimal representation of that dword (always 8 hex digits)
if i wanted to be fancy....
    print   right$(uhex$(eax),2)
this will display the right-most 2 characters of the 8-character hex string

requiem31

.486
.model flat, stdcall
option casemap :none

include \masm32\include\masm32rt.inc

displayBoard proto

.data
count dd 0
dArray byte 144 dup("~")

.code
main PROC

invoke displayBoard

ret
main ENDP

displayBoard PROC
    lea esi, dArray
    mov edi, 144

    .while edi > 0
        xor edx, edx
        mov eax, edi
        mov ecx, 12
        div ecx
       
        .if edx == 0
            print " ", 0dh, 0ah
        .endif

        print esi
        sub edi, 1
    .endw
   
    mov eax, input()

displayBoard ENDP

END main



dedndave

so, the array contains ASCII characters ?

unfortunately, the masm32 lib doesn't really give a clean way to display a single char - lol
here's what i sometimes do
    movzx   eax,byte ptr [esi]
    push    eax
    print   esp          ;display the string that is on the stack
    pop     eax


then, you just need to adjust the pointer in ESI on each pass, so it points to the next char

because the upper bytes of EAX are 0, it creates a temporary null-terminated string of each char

requiem31

As I was typing how to convert 7E hex to ascii you answered the question damn you fast mate. Do you know a good tutorial/doc for masm32 its pretty hard to find anything.

dedndave

well - the \masm32\hlhelp.chm and the other files in that folder will get you a long ways

qWord

Quote from: dedndave on May 10, 2014, 10:17:24 AMunfortunately, the masm32 lib doesn't really give a clean way to display a single char - lol
printf("%c",byte ptr [esi])
clean enough?  :biggrin:
MREAL macros - when you need floating point arithmetic while assembling!

dedndave

it's ok, i guess - lol
i hate functions that require a format string
printing it from the stack seems ok to me   :P

well - anything qWord writes is bound to be good   :biggrin:

jj2007

Quote from: dedndave on May 10, 2014, 10:17:24 AM
unfortunately, the masm32 lib doesn't really give a clean way to display a single char - lol

include \masm32\MasmBasic\MasmBasic.inc      ; download
.data
somestring      db "This is a test", 0

  Init
  xor ecx, ecx
  .While 1
      movzx eax, somestring[ecx]
      .Break .if !eax
      Print Chr$(".", eax, "_")   ; clean enough? If not, use Chr$(al) ;-)
      inc ecx
  .Endw
  Exit
end start


Output: .T_.h_.i_.s_. _.i_.s_. _.a_. _.t_.e_.s_.t_

Use
      or eax, 20h
      Print Chr$("(", al, ") ")

to get (t) (h) (i) (s) ( ) (i) (s) ( ) (a) ( ) (t) (e) (s) (t)

Gunther

Hi requiem31,

welcome to the forum.

Quote from: dedndave on May 10, 2014, 10:40:23 AM
i hate functions that require a format string

That has to do with your C horror.  :lol:

Quote from: dedndave on May 10, 2014, 10:40:23 AM
well - anything qWord writes is bound to be good   :biggrin:

That's for sure.  :t

Gunther
You have to know the facts before you can distort them.

dedndave

yah - i am not a C kinda guy - lol

Vortex

Here is another attempt :

include     \masm32\include\masm32rt.inc

.data

message     db 'This is a test.',0

.code

start:

    call    main
    invoke  ExitProcess,0

main PROC uses esi

LOCAL hHandle:DWORD
LOCAL bWritten:DWORD

    invoke  GetStdHandle,STD_OUTPUT_HANDLE
    mov     hHandle,eax

    mov     esi,OFFSET message
@@:
    movzx   eax,BYTE PTR [esi]
    test    eax,eax
    jz      @f

    invoke  WriteFile,hHandle,esi,1,\   ; print one character
            ADDR bWritten,NULL

    inc     esi
    jmp     @b
@@:
    ret

main ENDP

END start

Gunther

Erol,

good approach. Should work for beginners.  :t

Gunther
You have to know the facts before you can distort them.

MichaelW

There is also the CRT _putch function.

include     \masm32\include\masm32rt.inc

.data

message     db 'This is a test.',0

.code

start:

    call    main
    invoke  ExitProcess,0

main PROC uses esi

    mov     esi,OFFSET message
@@:
    movzx   eax,BYTE PTR [esi]
    test    eax,eax
    jz      @f

    invoke crt__putch, eax

    inc     esi
    jmp     @b
@@:
    ret

main ENDP

END start

Well Microsoft, here's another nice mess you've gotten us into.