flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > struc question

Author
Thread Post new topic Reply to topic
tneo77



Joined: 21 Jul 2007
Posts: 1
tneo77 21 Jul 2007, 13:45
hi im total newbie please help me understund struc
i make 10 records 2 bytes each
is this legal can you show me other beter way?
thank you !


org $100

struc pos {
.x db ?
.y db ?
}
foo pos

mov bx,mypos
sub bx,2
mov cx,10
@@:
add bx,2
mov [bx+foo.x],somevalue
mov [bx+foo.y],somevalue
loop @b

mypos rb 10*2
int $20
Post 21 Jul 2007, 13:45
View user's profile Send private message Reply with quote
kohlrak



Joined: 21 Jul 2006
Posts: 1421
Location: Uncle Sam's Pad
kohlrak 22 Jul 2007, 20:58
Here is the structure...

Code:
struc pos { 
.x db ? 
.y db ? 
}     


Now you have the structure defined.

Code:
foo pos     


You just declared one of that structure in memory, taking two bytes. So you now have a variable called foo that is two bytes, and you can access byte one by either "foo" or "foo.x" (since the .x part is the first byte) and the other you can access as "foo+1" (since foo is the part y is one byte after the first byte) or "foo.y" If you want ten of them, just keep making them. Personally, when i do something like ths, i just do it depending on situation. Chances are, if i wanted to make each one have a name, i would do it like this...

Code:
foo pos
anotherfoo pos
yetanotherfoo pos    


And so on, only comming up with better names. Anyway, if i wanted to just make un-named ones, i would have just made the structure (the code block), then addressed them as i go... for example...

Code:
mypos 10*2 rb (0) ;i didn't check this for syntax...    


Then, if i wanted to store the first part into al, and the second part into bl, i could do the following.

Code:
mov al, [mypos+pos.x]
mov bl, [mypos+pos.y]    


Then the next part...

Code:
mov al, [(mypos+2)+pos.x] ;We add the +2 since the next one should be 2 more than the first one since it's 2 bytes.
mov al, [(mypos+2)+pos.y]    


Note that i didn't check syntax for that either, but i think that explains what i think you're asking. Also, the newline before the + shoudln't be there...
Post 22 Jul 2007, 20:58
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 22 Jul 2007, 22:35
Code:
struc pos {
.x db ? 
.y db ? 
} 
virtual at 0
  pos pos        ; Now we have defined a zero based label fields
  sizeof.pos = $ ; And its size calculated as well
end virtual

somevalue = 0

; CODE AREA
org $100
  mov     bx, mypos
  mov     cx, 10

@@:
  mov     [bx+pos.x], somevalue
  mov     [bx+pos.y], somevalue
  add     bx, 2
  loop    @b

  int $20

; DATA AREA
  mypos   rb 10*sizeof.pos    


[edit]A fix in the code, check my post below [/edit]


Last edited by LocoDelAssembly on 02 Aug 2007, 22:14; edited 1 time in total
Post 22 Jul 2007, 22:35
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1392
Location: Piraeus, Greece
Picnic 02 Aug 2007, 22:00
Nice information, fast and useful example, loco and kohlrak thanks.
Post 02 Aug 2007, 22:00
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 02 Aug 2007, 22:13
mmmm, thanks thimis for remember me this post, I'm looking a bad practice at it. The "mypos rb 10*2" should be "mypos rb 10*sizeof.pos". It is equivalent anyway but still incorrect because any future change to the structure will led into unpredictible (a very predictible buffer overflow actually Laughing) results.

I'll correct it now
Post 02 Aug 2007, 22:13
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 02 Aug 2007, 22:51
Quote:
You just declared one of that structure in memory, taking two bytes. So you now have a variable called foo that is two bytes, and you can access byte one by either "foo" or "foo.x" (since the .x part is the first byte) and the other you can access as "foo+1" (since foo is the part y is one byte after the first byte) or "foo.y" If you want ten of them, just keep making them. Personally, when i do something like ths, i just do it depending on situation. Chances are, if i wanted to make each one have a name, i would do it like this...

slight mistake:
"foo" itself isn't two bytes. "foo" is label without any size assigned.

Here is most simple explaination of what struct does:

This code
Code:
struct point
{
.x db 0
.y db 0
}
point1 point
point2 point
    

is exactly same as
Code:
point1:
.x db 0
.y db 0

point2:
.x db 0
.y db 0
    


labels whose name start with "." character are called "local", and their name is prepended by name of last non-local label. So, following code is also exactly same as previous two:

Code:
point1:
point1.x db 0
point1.y db 0
point2:
point2.x db 0
point2.y db 0
    


This could also explain "virtual at 0" trick:
Code:
struct point {
  .x db 0
  .y db 0
}
virtual at 0
  point point
  sizeof.point = $
end virtual
    

"virtual at 0" is similar to "org 0", it causes labels to be generated as if code was on address 0. Difference from "org 0" is that inside "virtual" no real code is generated, just labels are defined as if code was there.

So, that construct becomes:
Code:
virtual at 0
  point:            
  point.x db 0   ;this is at offset 0, so label "point.x" has value 0
  point.y db 0   ;this is at offset 1, so label "point.y" has value 1
  sizeof.point = $  ;"$" is current offset, so "sizeof.point" is set to 2
end virtual
    


This way you declare relative offsets of members within structure (point.x, point.y). You use them this way:

Code:
;ebx = pointer to "point" structure
mov al, [ebx + point.x]
mov ah, [ebx + point.y]
    


That should be all you need to know about "struc"...
Post 02 Aug 2007, 22:51
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1392
Location: Piraeus, Greece
Picnic 04 Aug 2007, 14:40
Great vid, this topic is a nice tutorial about struc and how to make array of struc.

_________________
Hobby BASIC Interpreter
Post 04 Aug 2007, 14:40
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


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

Website powered by rwasa.