The MASM Forum

General => The Campus => Topic started by: requiem31 on May 10, 2014, 09:33:37 AM

Title: printing an array
Post by: requiem31 on May 10, 2014, 09:33:37 AM
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
Title: Re: printing an array
Post by: jj2007 on May 10, 2014, 10:08:42 AM
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".
Title: Re: printing an array
Post by: dedndave on May 10, 2014, 10:09:55 AM
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
Title: Re: printing an array
Post by: requiem31 on May 10, 2014, 10:12:42 AM
.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


Title: Re: printing an array
Post by: dedndave on May 10, 2014, 10:17:24 AM
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
Title: Re: printing an array
Post by: requiem31 on May 10, 2014, 10:20:08 AM
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.
Title: Re: printing an array
Post by: dedndave on May 10, 2014, 10:22:43 AM
well - the \masm32\hlhelp.chm and the other files in that folder will get you a long ways
Title: Re: printing an array
Post by: qWord on May 10, 2014, 10:35:59 AM
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:
Title: Re: printing an array
Post by: dedndave on May 10, 2014, 10:40:23 AM
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:
Title: Re: printing an array
Post by: jj2007 on May 10, 2014, 10:50:06 AM
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 (http://masm32.com/board/index.php?topic=94.0)
.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) (http://www.webalice.it/jj2006/MasmBasicQuickReference.htm#Mb1143) ;-)
      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)
Title: Re: printing an array
Post by: Gunther on May 10, 2014, 07:39:43 PM
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
Title: Re: printing an array
Post by: dedndave on May 10, 2014, 08:01:25 PM
yah - i am not a C kinda guy - lol
Title: Re: printing an array
Post by: Vortex on May 10, 2014, 08:05:23 PM
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
Title: Re: printing an array
Post by: Gunther on May 10, 2014, 09:27:57 PM
Erol,

good approach. Should work for beginners.  :t

Gunther
Title: Re: printing an array
Post by: MichaelW on May 10, 2014, 10:35:00 PM
There is also the CRT  _putch (http://msdn.microsoft.com/en-US/library/azb6c04e(v=vs.80).aspx) 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