The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: kevinr on March 16, 2009, 01:19:09 AM

Title: Array to buffer
Post by: kevinr on March 16, 2009, 01:19:09 AM
Hello,
Im new to assmebly (just started learning it) and im kinda  confused on how to do something.

So i an array like this:
somearray SDWORD 3, 5, 7, 44, -23, 34

etc. Now how do i convert that to buffer so that its like:
"3 5 7 44 -23 34"?

Any help would be great. Thx!
Title: Re: Array to buffer
Post by: PBrennick on March 16, 2009, 01:33:04 AM
I am assuming you do not want a string so it should be expressed this way:

somebuffer SDWORD 3, 20h, 5, 20h, 7, 20h, 44, 20h, -23, 20h, 34

Not having any idea what the heck you are trying to do, that is my guess.

Paul
Title: Re: Array to buffer
Post by: kevinr on March 16, 2009, 01:40:24 AM
Thanks for reply.

I need to convert something like:
32, 44 (sdword)

to:
'3', '2', ' ', '4', '4' (byte)

is that even possible? I know '3' is actually 48+3 (since 48 - 57 represent numbers)
Title: Re: Array to buffer
Post by: ChrisLeslie on March 16, 2009, 01:56:42 AM
Are you actually trying to split a sdword up into an ascii representation of each digit used in a base 10 representation?  And then do the same for each element of the "somearry" buffer?
Title: Re: Array to buffer
Post by: kevinr on March 16, 2009, 02:00:10 AM
Yes :)

I'm new and have noooo idea how to even start this. (i managed to go in reverse direction but dont know this)
Title: Re: Array to buffer
Post by: askm on March 16, 2009, 04:22:46 AM
According to my desk reference
ISBN 0-13-091013-9
3.4.5
"The DWORD (define doubleword) and SDWORD (define signed doubleword) directives allocate storage for one or more 32-bit integers.
val1 DWORD 12345678h    ; unsigned
val2 SDWORD -2147483648    ; signed
val3 DWORD 20 DUP (?)    ; unsigned array

You can still use DD to define both ..."
Title: Re: Array to buffer
Post by: BogdanOntanu on March 16, 2009, 05:04:11 AM
Quote from: kevinr on March 16, 2009, 01:19:09 AM
Hello,
Im new to assmebly (just started learning it) and im kinda  confused on how to do something.

So i an array like this:
somearray SDWORD 3, 5, 7, 44, -23, 34

etc. Now how do i convert that to buffer so that its like:
"3 5 7 44 -23 34"?

Any help would be great. Thx!

Here you have a conceptual overview of the algorithmic steps needed for this:

1) Define an array of numbers (as you did above).
  1.1) you will also need a "number of elements in array" variable or a special value (maybe zero) to act as a sequence terminator
2) Reserve a buffer big enough to keep the ASCII results.
  2.1) consider the number of elements in your array and the maximum number of ascii digits in one number for calculating the size of this buffer
3) In a loop preform the following actions:
  3.1) Read one value from your input array
  3.2) Convert the value to it's ASCII representation (hint: use dwtoa <dword to ascii> or dwtoh like functions)
  3.3) Add 4 (size of a dword) to input pointer. Add (size of ASCII string)+1 to the output pointer
  3.4) decrement loop counter, jump back to start of loop if there are more elements to process
4) Done.

As you will notice programming is all about finding a series of clear and simple steps to be performed on input data in order to obtain a result (or the output data) and it usually does involve some loops for handling repeating sequences of such steps.

Also defining your problem as clear as possible is of the essence because we can not read minds (or if we can we like to hide this capability) and if you do not define you problem clearly then many people will be confused about what you want to obtain and will give wrong answers or no answer at all or ask for more details.

Please take the time and define your problem in clear and simple terms.

Also if you are a beginner then post in The Campus and not in The Workshop... in other words please take the time to read the description and use of each sub forum before posting  ;)
Title: Re: Array to buffer
Post by: MichaelW on March 16, 2009, 05:54:09 AM
To add to Bogdans reply, assuming that you are at least somewhat familiar with BASIC, here is the basic idea:

for i = 0 to ubound(array)
  x = array(i)
  buffer = buffer + str(x) + " "
next


The procedure that you will be using to convert each array value to its ASCII representation will terminate the result with a null byte. If you intend to display the buffer using a procedure that expects a null terminated string, you will need to overwrite all of the nulls except the last. In your example you show a space between each value, so you could replace the nulls, other than the last, with a space character and adjust the output pointer to the next byte following the space character.

BTW, to give you an idea of the amount of code required, using dwtoa to do the conversion, and szLen to get the size of the ASCII representation so I could add the space and update the output pointer, I was able to do this with only 12 lines of asm code.
Title: Re: Array to buffer
Post by: kevinr on March 16, 2009, 07:47:27 AM
Ah ok thx guys :)

I managed to do it but it took a lot more than 12 lines :(. Could you post it so i could check out what i did differently?

I just get the temp var from dwtoa, loop through it and add byte by byte to the main buffer and at the end add a space. It works but its a lot longer approach than probably what u did.

Anyways, thanks again.

(btw, whats szLen?)
Title: Re: Array to buffer
Post by: brethren on March 16, 2009, 08:31:08 AM
QuoteI managed to do it but it took a lot more than 12 lines Sad. Could you post it so i could check out what i did differently?

Post your working version first, that'll help the experts show you where to optimize your program :wink

ps a lot of assembly students come on this forum expecting the members to do their asm assignments for them (i'm not saying thats what your doing, of course) but posting your code would get you a lot more help

good look
nick
Title: Re: Array to buffer
Post by: RuiLoureiro on March 16, 2009, 12:23:24 PM
Quote from: kevinr on March 16, 2009, 07:47:27 AM
I managed to do it but it took a lot more than 12 lines 
Hi kevinr,
             you need something like this:

    .data
LENARRAY    equ 6                           ; 6 elements in somearray
somearray   dd 3, 5, 7, 44, -23, 34
stringarray   db LENARRAY * 12 dup (0)
    .code
;######################
start:
        mov     esi, offset somearray
        mov     ebx, LENARRAY             ; number of elements
        mov     edi, offset stringarray
       
@@:     invoke  dwtoa, dword ptr [esi], edi

        add     esi, 4               ; point to the next number
        add     edi, 12             ; next string buffer
        dec     ebx
        jnz     short @B

Does this help you? Have a nice work
RuiLoureiro
Title: Re: Array to buffer
Post by: Mark Jones on March 16, 2009, 12:44:46 PM
Quote from: kevinr on March 16, 2009, 07:47:27 AM
(btw, whats szLen?)

By szLen I think Michael simply means "the length of the string." Functions and data in MASM commonly use prefixes to help identify their format requirements. "Sz" stands for "string, zero-terminated" and is (or was, anyways) the most common method to declare string data. An sz-string is just a number of 8-bit bytes, terminated by a null (00h), its length being the number of bytes before the null terminator. (Unicode string data is more practical nowadays because it provides the capability to display other languages, but this imposes other, considerable complications.)
Title: Re: Array to buffer
Post by: BlackVortex on March 16, 2009, 01:55:59 PM
szlen is a include/function, check the other (legendary) thread about it

include is in last page (atm)
http://www.masm32.com/board/index.php?topic=1807.0
Title: Re: Array to buffer
Post by: herge on March 16, 2009, 02:49:09 PM
 Hi All:


XCREF
.XLIST
include \masm32\include\masm32rt.inc
.LIST
.CREF
.data
LENARRAY    equ 6                           ; 6 elements in somearray
somearray   dd 3, 5, 7, 2147483647, -23, 0
stringarray   db LENARRAY * 12 dup (0)
Space   db " ",0
    .code
;######################
start:
        mov     esi, offset somearray
        mov     ebx, LENARRAY             ; number of elements
        mov     edi, offset stringarray
       
@@:     invoke  ltoa, dword ptr [esi], edi

        add     esi, 4               ; point to the next number
        add     edi, 12            ; next string buffer
        dec     ebx
        jnz     short @B
        mov     ecx, 6
        mov     esi, offset stringarray
        public Again
Again:       
        mov     al, byte ptr [ esi]
        and     al,al
        jnz     @F
        inc     esi
        jmp      Again
@@:
        push ecx
        invoke StdOut, esi
        add   esi, eax
        invoke StdOut, offset Space
        pop   ecx
        dec   ecx
        jnz   Again
exit
end start



The resultsL

3 5 7 2147483647 -23 0


Regards herge
Title: Re: Array to buffer
Post by: MichaelW on March 16, 2009, 03:05:51 PM
Sorry, I should have made this more clear. By dwtoa I meant the MASM32 dwtoa procedure, and by szLen I meant the MASM32 szLen procedure. And the 12 lines were only for the code that extracted the dwords from the array, converted them to strings, and placed the strings in the buffer. The complete source was 29 lines.
Title: Re: Array to buffer
Post by: kevinr on March 16, 2009, 03:24:06 PM
Ah ok thx guys :)

I was taking the wrong approach - now it makes much more sense.

Title: Re: Array to buffer
Post by: herge on March 16, 2009, 04:06:21 PM
 Hi All:

The first line should be:
.XCREF


Regards herge
Title: Re: Array to buffer
Post by: askm on March 16, 2009, 05:56:54 PM
Even the most experienced programmers should know

there should not be any executable code on line

thirteen.

(insert blank line thirteen)
start:

(Just trying to point out the 'devil is in the details' ?)