flat assembler
Message board for the users of flat assembler.
Index
> Main > A basic question ("db" and strings, colon, size) |
Author |
|
cod3b453 30 Jul 2013, 21:31
Welcome
db has a special case for (ASCII) strings, which is equivalent to: Code: Var: db 'q','w','e','r','t','y' ; or Var: db 'q' db 'w' db 'e' db 'r' db 't' db 'y' |
|||
30 Jul 2013, 21:31 |
|
Standard User 30 Jul 2013, 21:44
cod3b453, thanks, slightly more clear )
|
|||
30 Jul 2013, 21:44 |
|
uart777 31 Jul 2013, 03:39
Hi, Standard User. Welcome to the FASM community. db can define byte variables (db 123) as well as arrays of bytes (db 'Hello, evil world!', 0Dh, 0Ah). Please allow me to share some macros that declare HL variables:
Code: ; create 32BIT integer variable. example: ; variable v, 123 ; output: v dd 123 macro variable name, value { name dd value } ; supports: variable v=123. [va] creates ; a variable list of arguments. this ; version just replaces , with = ; variable v=123 ; output: v dd 123 (same) macro variable [p] { ; p = va list common ; do this once match a==b, p \{ ; extract a=b from p a dd b ; name dd value \} } ; supports 2+ names/values on same line: ; variable a=1, b=2, c=3 ; forward iterates/loops through each ; argument/parameter separated with commas: ; a=1, b=2, c=3 macro variable [p] { forward ; for i=0 to va.n { match a==b, p \{ ; name=value (p=va[i]) a dd b \} ; ... ; next va[i++] } } ; supports variables with no initial value ; and error checking. default=0 if no value ; is specified ; in the following, name=value must be ; matched before name by itself (with no ; =value) or there would be a partial match. ; in the second match, =0 ?s ensures there ; was no previous match: "if ?s=0 then get ; name/a from p". in effect, "=0 ?s" ; creates a chain of "else-if match"s macro variable [p] { forward ; for i=0 to va.n define ?s 0 match a==b, p \{ ; name=value a dd b define ?s 1 ; success \} match =0 a, ?s p \{ ; name by itself a dd 0 ; name=0 define ?s 1 ; success \} if ?s eq 0 ; neither match 'Syntax error' ; was successful end if } ; example variables... variable a=123, b=456, c, d=789 ; output: 4 lines... ; a dd 123 ; b dd 456 ; c dd 0 ; c=0 ; d dd 789 |
|||
31 Jul 2013, 03:39 |
|
Standard User 31 Jul 2013, 08:06
uart777, many thanks, this will come in handy.
|
|||
31 Jul 2013, 08:06 |
|
dogman 31 Jul 2013, 09:48
Standard User wrote: I started to learn asm just 30 minutes ago, so if my question sounds stupid, please take it easy Let's say, I declare one byte length variable, e.g. If more people with 30 minutes of experience knew enough to ask how a one-byte variable could contain a 6 byte string the world would be a much brighter place! _________________ Sources? Ahahaha! We don't need no stinkin' sources! |
|||
31 Jul 2013, 09:48 |
|
uart777 31 Jul 2013, 12:16
Standard User wrote:
Quote: uart777, many thanks, this will come in handy. Any questions? Please don't hesitate to ask. Have a good day! PS: My favorite program: CodeVu (Click here) |
|||
31 Jul 2013, 12:16 |
|
Standard User 31 Jul 2013, 14:51
Quote: Any questions? Please don't hesitate to ask. Hm, if you please, one more question Now I'm slightly confused about the labels. Fasm manual says: Quote: There are different ways to define labels. The simplest is to follow the name of label by the colon. It defines the label whose value is equal to offset of the point where it’s defined. ... The other way is to follow the name of label (without a colon) by some data directive. It defines the label with value equal to offset of the beginning of defined data Code: Var1 db 255 and, to my understanding, Var1 contains an address in memory where the data begins, right? But, if to define it like this: Code: Var1: db 255 What does Var1 contain now? In other words, could you elaborate on "It defines the label whose value is equal to offset of the point where it’s defined" or, in particular - what is "the offset of the point(...)"? Also, is there any difference between "label" and "variable" in Fasm? |
|||
31 Jul 2013, 14:51 |
|
JohnFound 31 Jul 2013, 15:25
The label in FASM, besides from its offset (address, value) has also other parameter associated - the size of the data pointed by this label.
Now, labels defined by colon have a data size of 0, because they are "code labels". The label defined by data directive has the size, according to the data directive used. "DB" has 1, "DW", 2 and so on. The size of the data labels is used when you want to use this label in a instruction. If the size of the source and destination does not match, FASM will end with "operand sizes do not match" error. For the labels with size of 0 such checks are not provided. See the following code. The first "mov" will generate error. All the next are OK. Code: data1 db 0 data2: db 0 mov eax, [data1] mov al, [data1] mov eax, dword [data1] mov eax, [data2] |
|||
31 Jul 2013, 15:25 |
|
Standard User 31 Jul 2013, 15:50
JohnFound, thanks. Seems I understood the label size thing, plus or minus. But still:
Quote: data1 db 0 Data1 contains an address of the data in memory, right? But what does data2 contain? |
|||
31 Jul 2013, 15:50 |
|
JohnFound 31 Jul 2013, 16:02
Also the address of the data in memory (the value of data1=0 and data2=1 in the above example). Note also, that the addresses are simply numbers.
In FASM there is no big difference between labels and constants. In fact the only difference is that the data labels have size information associated, that allows data size check during the compilation and thus protects from some errors. For example following code will be compiled OK and data3 will point to data2 actually: Code: data1 db 0 data2: db 0 data3 = 1 mov eax, [data3] |
|||
31 Jul 2013, 16:02 |
|
Standard User 31 Jul 2013, 16:18
Thanks again!
|
|||
31 Jul 2013, 16:18 |
|
baldr 31 Jul 2013, 16:35
JohnFound,
Actually, having size information associated is not the only difference between labels and constants. Consider the following: Code: use32 label a at eax*2+ecx; sizeless, but different ;-) mov eax, [a+1000-eax+3*ecx] |
|||
31 Jul 2013, 16:35 |
|
JohnFound 31 Jul 2013, 16:46
baldr, yes, but it is somehow hard for a beginner.
|
|||
31 Jul 2013, 16:46 |
|
Roman 19 Aug 2013, 20:29
baldr
Давай покажи новобранцу хардкор |
|||
19 Aug 2013, 20:29 |
|
shutdownall 20 Aug 2013, 11:28
Standard User wrote:
db is a directive for defining data bytes. Can be one, can be more bytes. Look at this: Code: varx db 100h dup(0) vary db 100h dup('blablabla') varx defines 256 bytes of value 0 and points to the first element. Same is for vary which defines 2304 bytes of the text string 'blablabla' (256 times). Cool - eh ? There are more creative data definitions and initialization possible. Just read the documentation. You don't have to read all at one day, just read one chapter about data definitions and you can learn a lot and save much time in writing code. |
|||
20 Aug 2013, 11:28 |
|
l_inc 23 Aug 2013, 13:12
baldr wrote:
Was it always sizeless by default? I clearly remember, that when declared using the label directive the size of a label defaulted to byte, but I can't reproduce this behaviour anymore. _________________ Faith is a superposition of knowledge and fallacy |
|||
23 Aug 2013, 13:12 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.