flat assembler
Message board for the users of flat assembler.
Index
> Macroinstructions > struc question |
Author |
|
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... |
|||
22 Jul 2007, 20:58 |
|
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 |
|||
22 Jul 2007, 22:35 |
|
Picnic 02 Aug 2007, 22:00
Nice information, fast and useful example, loco and kohlrak thanks.
|
|||
02 Aug 2007, 22:00 |
|
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 ) results.
I'll correct it now |
|||
02 Aug 2007, 22:13 |
|
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"... |
|||
02 Aug 2007, 22:51 |
|
Picnic 04 Aug 2007, 14:40
Great vid, this topic is a nice tutorial about struc and how to make array of struc.
|
|||
04 Aug 2007, 14:40 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.