flat assembler
Message board for the users of flat assembler.
 Home   FAQ   Search   Register 
 Profile   Log in to check your private messages   Log in 
flat assembler > Main > Strange behaviour when using structures.

Author
Thread Post new topic Reply to topic
The_Unknown_Member



Joined: 28 Aug 2017
Posts: 15
Strange behaviour when using structures.
Hi. I wonder what is wrong with this code:

Code:

; This is an Educational Program

format PE console
use32   ; x86_32
entry start 

include 'win32a.inc'

struct PNT 
        x dd ? 
        y dd ?
        z dd ?
        q dd ? 
ends 
; This is the data section:
; =======================================================
section '.data' data readable writeable 
        my_PNT          PNT  2,3,4,5
        ;mem            dd 1 dup(0)

; =======================================================
section '.text' code readable executable 

start:
        ; Your program begins here
        mov eax, [my_PNT; Output -> 2
        mov eax, [my_PNT + 1; Output -> 3000000
        mov eax, [my_PNT + 2; Output -> 30000
        mov eax, [my_PNT + 3; Output -> 300
        mov eax, [my_PNT + 4; Output -> 3
        ;call print_eax ; Printing the number in hexa format in the console. This comes from the fine training.inc

        ; Exit the process: 
        push 0
        call [ExitProcess]

include 'C:\Users\MrNoLife\Desktop\ASM resources\x86-asm-foundations\#asm_prog_ex-master\include\training.inc'



Look at the code that begins after the start label. In the first line I move the thing number that is in address my_PNT in eax (the number is my_PNT.x which is 2) now after that I move from the base address of my_PNT to the next byte I get this strange result "3000000". Why is this happening ? Can someone explain me ?
Post 13 Sep 2017, 11:31
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 15096
Location: The Unicomplex
Your values are defined as dd so they take four bytes each. You will need to adjust the offsets:

Code:
        mov eax, [my_PNT + 4*0;first value
        mov eax, [my_PNT + 4*1;second value
        mov eax, [my_PNT + 4*2;third value
        mov eax, [my_PNT + 4*3;fourth value
        mov eax, [my_PNT + 4*4;fifth value

But actually since you use a structure you can access the value by the symbol name:

Code:
        mov eax, [my_PNT.x]
        mov eax, [my_PNT.y]
        mov eax, [my_PNT.z]
        mov eax, [my_PNT.q]

Post 13 Sep 2017, 11:48
View user's profile Send private message Visit poster's website 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


Powered by phpBB © 2001-2005 phpBB Group.

Main index   Download   Documentation   Examples   Message board
Copyright © 2004-2016, Tomasz Grysztar.