flat assembler
Message board for the users of flat assembler.

Index > Main > A basic question ("db" and strings, colon, size)

Author
Thread Post new topic Reply to topic
Standard User



Joined: 26 Jul 2013
Posts: 6
Standard User
I didn't find a section for complete beginners on this forum, so I dare to ask it here. I started to learn asm just 30 minutes ago, so if my question sounds stupid, please take it easy Smile Let's say, I declare one byte length variable, e.g.
Code:
Var db 255       

This is OK. But, let's say:
Code:
Var db "qwerty"     

How can one-byte variable contain a 6 byte length string?

----

EDIT by DOS386 2013-Aug-21 : enhanced subject (PS: welcome to the FASM forum ... this is the right place to ask ... please use descriptive subjects)
Post 30 Jul 2013, 20:13
View user's profile Send private message Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 619
cod3b453
Welcome Cool

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'    
This has the obvious advantage of being easier to write and nicer to look at than the above.
Post 30 Jul 2013, 21:31
View user's profile Send private message Reply with quote
Standard User



Joined: 26 Jul 2013
Posts: 6
Standard User
cod3b453, thanks, slightly more clear )
Post 30 Jul 2013, 21:44
View user's profile Send private message Reply with quote
uart777



Joined: 17 Jan 2012
Posts: 369
uart777
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    
Post 31 Jul 2013, 03:39
View user's profile Send private message Reply with quote
Standard User



Joined: 26 Jul 2013
Posts: 6
Standard User
uart777, many thanks, this will come in handy.
Post 31 Jul 2013, 08:06
View user's profile Send private message Reply with quote
dogman



Joined: 18 Jul 2013
Posts: 114
dogman
Standard User wrote:
I started to learn asm just 30 minutes ago, so if my question sounds stupid, please take it easy Smile Let's say, I declare one byte length variable, e.g.
Code:
Var db 255       

This is OK. But, let's say:
Code:
Var db "qwerty"     

How can one-byte variable contain a 6 byte length string?


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! Smile

_________________
Sources? Ahahaha! We don't need no stinkin' sources!
Post 31 Jul 2013, 09:48
View user's profile Send private message Reply with quote
uart777



Joined: 17 Jan 2012
Posts: 369
uart777
Standard User wrote:
Quote:
uart777, many thanks, this will come in handy.
Anytime Wink Hope my information helps. Your question was not "stupid" at all even to a master ASM programmer with decades of experience. It indicates you are inquisitive and have interest/desire to learn; meaning, receptive to knowledge.

Any questions? Please don't hesitate to ask. Have a good day! Smile

PS: My favorite program: CodeVu (Click here)
Post 31 Jul 2013, 12:16
View user's profile Send private message Reply with quote
Standard User



Joined: 26 Jul 2013
Posts: 6
Standard User
Quote:
Any questions? Please don't hesitate to ask.

Hm, if you please, one more question Smile 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
So, I define a label:
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?
Post 31 Jul 2013, 14:51
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3500
Location: Bulgaria
JohnFound
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]    
Post 31 Jul 2013, 15:25
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
Standard User



Joined: 26 Jul 2013
Posts: 6
Standard User
JohnFound, thanks. Seems I understood the label size thing, plus or minus. But still:
Quote:
data1 db 0
data2: db 0

Data1 contains an address of the data in memory, right? But what does data2 contain?
Post 31 Jul 2013, 15:50
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3500
Location: Bulgaria
JohnFound
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]    
Post 31 Jul 2013, 16:02
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
Standard User



Joined: 26 Jul 2013
Posts: 6
Standard User
Thanks again!
Post 31 Jul 2013, 16:18
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
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]    
Post 31 Jul 2013, 16:35
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3500
Location: Bulgaria
JohnFound
baldr, yes, but it is somehow hard for a beginner. Smile
Post 31 Jul 2013, 16:46
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 595
Roman
baldr
Давай покажи новобранцу хардкор Smile
Post 19 Aug 2013, 20:29
View user's profile Send private message Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 518
Location: Munich
shutdownall
Standard User wrote:

Code:
Var db "qwerty"     

How can one-byte variable contain a 6 byte length string?


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. Razz
Post 20 Aug 2013, 11:28
View user's profile Send private message Send e-mail Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc
baldr wrote:
Code:
label a at eax*2+ecx; sizeless, but different Wink    

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
Post 23 Aug 2013, 13:12
View user's profile Send private message 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.

Powered by rwasa.