News:

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

Array to buffer

Started by kevinr, March 16, 2009, 01:19:09 AM

Previous topic - Next topic

kevinr

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!

PBrennick

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
The GeneSys Project is available from:
The Repository or My crappy website

kevinr

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)

ChrisLeslie

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?

kevinr

Yes :)

I'm new and have noooo idea how to even start this. (i managed to go in reverse direction but dont know this)

askm

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 ..."

BogdanOntanu

#6
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  ;)
Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

MichaelW

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.
eschew obfuscation

kevinr

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?)

brethren

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

RuiLoureiro

#10
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

Mark Jones

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.)
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

BlackVortex

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

herge

 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
// Herge born  Brussels, Belgium May 22, 1907
// Died March 3, 1983
// Cartoonist of Tintin and Snowy

MichaelW

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.
eschew obfuscation