News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Need help with strings in structures

Started by valium123, December 07, 2014, 12:56:17 AM

Previous topic - Next topic

valium123


I am using irvine32.inc and want to display a string using the writeString procedure.
Suppose there is a structure defined as:



Person struct
     name byte 10 dup(?)
Person ends
personArray Person 3 dup(?)


I have tried this:
mov ecx, 3
mov esi, type Person
L1:
     mov edx, offset personArray[esi].name
      mov writestring
      add esi, type Person
loop L1

But it doesnt work. Please help.

dedndave

you're really close  :biggrin:

Person struct
  name byte 10 dup(?)
Person ends

    .DATA?

personArray Person 3 dup(<>)


        mov     ecx, 3
        mov     esi, offset personArray
L1:
        mov     edx, [esi].Person.name
        call    WriteString
        add     esi, sizeof Person
        loop    L1

valium123

Thanks for replying. I am using visual studio right now and this error comes up when try your solution:
1>main.asm(103): error A2022: instruction operands must be the same size



jj2007

Quote from: valium123 on December 07, 2014, 05:25:25 AM
Thanks for replying. I am using visual studio right now and this error comes up when try your solution:
1>main.asm(103): error A2022: instruction operands must be the same size

Please send the IP and password of your computer, so that those who want to help you can launch a Remote Desktop Connection that allows them to see what you have coded in line 103.

valium123

Dude I mentioned above that when I try the solution posted, the error appears. Its obvious that the line containing "mov edx" is causing problems.

jj2007

Dude it's not obvious but I'll be kind because you sound desperate:

Person struct
  xname byte 10 dup(?)   ; "name" is a bad choice
Person ends
...
        lea edx, [esi].Person.xname   ; unless you want the first 4 bytes only, of course

valium123

thanks but the problem is we have not studied lea yet. I need to use something like


mov edx, offset msg
call writestring


:(

And the code is much longer than the example I wrote above. Will you have look at my code if I send it to you?

dedndave

my mistake - please change this line
L1:
        lea     edx, [esi].Person.name


LEA stands for "load effective address"
it calculates the address of [esi].Person.name and places it in edx

because the member "name" is at the beginning of the structure,
you could just use
        mov     edx,esi
but, that's not a good solution if Person is updated

dedndave

another solution - not as pretty, but workable...

        mov     ecx, 3
        mov     esi, offset personArray.Person.name
L1:
        mov     edx, esi
        call    WriteString
        add     esi, sizeof Person
        loop    L1

dedndave

i actually tried this - the member name "name" is not good - leads to errors

this code assembles OK...

;###############################################################################################

        INCLUDE    \Masm32\Include\Masm32rt.inc

;###############################################################################################

Person STRUCT
  pname db 10 dup(?)
Person ENDS

;###############################################################################################

        .DATA?

personArray Person 3 dup(<>)

;###############################################################################################

        .CODE

;***********************************************************************************************

main    PROC

        mov     esi,offset personArray.Person.pname

        inkey
        INVOKE  ExitProcess,0

main    ENDP

;###############################################################################################

        END     main

valium123

There is another problem


input_details PROC uses ecx esi edi
           mov ecx, 6
           mov esi, type flightInfo
           L1:
        inc edi
mov edx, offset msg1
call writestring
mov edx, offset flights
add edx, esi
mov ecx, SIZEOF (flightInfo ptr [edx]).F_airline
call readstring

mov edx, offset msg2
call writestring
mov edx, offset flights
add edx, esi
mov ecx, SIZEOF (flightInfo ptr [edx]).F_from
call readstring

mov edx, offset msg3
call writestring
mov edx, offset flights
add edx, esi
mov ecx, SIZEOF (flightInfo ptr [edx]).F_to
call readstring
                add esi, type flightInfo
            LOOP L1
                ret
input_details ENDP           


Is this correct coz I feel that it is overwriting previously entered strings.

flightInfo STRUCT
F_airline byte 20 dup(0)
F_from byte 10 dup(?)
F_to byte 10 dup(?)
F_departure_time byte 7 dup(?)
F_arrival_time byte 7 dup(?)
F_Duration dword ?
F_seats dword ?
F_ticket dword ?
F_flight_ID dword ?
flightInfo ENDS




dedndave

flightInfo STRUCT
F_airline byte 20 dup(0)


while it is allowed to use initialized variables inside structure definitions, we rarely do so
it makes a very different animal than an uninitialized structure
mainly, any use of flightInfo must be in the .DATA section - not the .DATA? section

i would probably use
F_airline byte 20 dup(?)

the uninitialized data section is set to all 0's at program startup

dedndave

i see that Kip's ReadString funtion uses ReadConsole
i seem to recall that ReadConsole always wants a 256-character buffer, regardless of the documentation

http://masm32.com/board/index.php?topic=3572.msg37552#msg37552

if that's the case, i would use a common 256-character buffer for all user input
then, move the desired limited string length into the destination buffer each time

dedndave

you might do this....
write a PROC that handles keyboard input - pass the address and size of the buffer
use a LOCAL buffer that allows at least 256 characters
if the user input exceeds the buffer size parameter, display a "string too long" message and try again

qWord

According to the MASM 6.1 reference manual the NAME directive is ignored =>it is perfectly OK to remove that keyword at the begin of your program:
option nokeyword:<name>

MREAL macros - when you need floating point arithmetic while assembling!