flat assembler
Message board for the users of flat assembler.

Index > Main > [SOLVED] Pointing to the wrong address

Author
Thread Post new topic Reply to topic
Apos



Joined: 11 Jan 2012
Posts: 17
Location: Paris, France (I'm not from France though.)
Apos
I have played with the 'dup' command in order to create arrays.

When I do something like this:

Code:
int_var         db      2 dup(0)

...

     mov ecx, int_var        ;ecx points to int_var
      mov byte [ecx], 50      ;int_var[0] = 50
    inc ecx                 ;ecx now points to int_var[1]
       mov byte [ecx], 30      ;int_var[1] = 30
    dec ecx                 ;ecx now points to int_var[0] again (But it doesn't.)
    


ecx ends up pointing to something else than int_var[0] which is 50. I thought that doing it that way would work but it doesn't.

However, when I do it without putting the 30, it works.

Code:
int_var             db      2 dup(0)

...

     mov ecx, int_var        ;ecx points to int_var
      mov byte [ecx], 50      ;int_var[0] = 50
    inc ecx                 ;ecx now points to int_var[1]

   dec ecx                 ;ecx now points to int_var[0] again
    

_________________
"A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away." - Antoine de Saint-Exupéry


Last edited by Apos on 14 Jan 2012, 23:16; edited 1 time in total
Post 14 Jan 2012, 19:14
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
You must have something wrong in how you check that int_var[0] is 50 (or the code is not an exact enough representation of your real compilable code). One mistake could be that you are pretending int_var[0] to be 4 bytes long (dword, int), so of course, when you write 30 in the second array slot you change the dword interpretation of int_var[0] to something different than 50 (50 + 30*2^8 + x*2^16 + y*2^24 = 7730 with x = y = 0 based on your experience when you don't write 30, but technically both x and y are unknown here).


Last edited by LocoDelAssembly on 14 Jan 2012, 19:34; edited 1 time in total
Post 14 Jan 2012, 19:29
View user's profile Send private message Reply with quote
Apos



Joined: 11 Jan 2012
Posts: 17
Location: Paris, France (I'm not from France though.)
Apos
Well... The 50 does end up being 7730... What do I have to do in order to only modify int_var[1]?
Post 14 Jan 2012, 19:34
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Then your number to string routine is working with the wrong size, you have byte sized numbers here, not dword. If you want int as per C definition then your code should be like this:
Code:
int_var         dd      2 dup(0)

...

        mov ecx, int_var        ;ecx points to int_var
        mov dword [ecx], 50      ;int_var[0] = 50
        add ecx, 4                 ;ecx now points to int_var[1]
        mov dword [ecx], 30      ;int_var[1] = 30
        sub ecx, 4                 ;ecx now points to int_var[0] again    
Post 14 Jan 2012, 19:37
View user's profile Send private message Reply with quote
Apos



Joined: 11 Jan 2012
Posts: 17
Location: Paris, France (I'm not from France though.)
Apos
That works. Thanks!

I think I used to do something like that in C when I was going to the next address. Can't remember correctly though... Skipping 4 rings a bell.

_________________
"A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away." - Antoine de Saint-Exupéry
Post 14 Jan 2012, 19:42
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1419
Location: Toronto, Canada
AsmGuru62
I use a simple macro to write values into arrays:
Code:
macro vSet32 vect,index,value
{
     mov     [vect + index*4], value
}
...
array dd 32 dup(0)
...
mov    esi, array
vSet32    esi, 7, eax     ; array[7]=eax
vSet32        esi, 11, 627    ; array[11]=627
    

It is better to use a register as an index, because this instruction gets 3 bytes longer once an index exceeds 32. However, you also need to load that index into a register, which also takes room.

You can make a macro for each data type, so you can't make that same mistake.
Post 15 Jan 2012, 15:27
View user's profile Send private message Send e-mail Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.