flat assembler
Message board for the users of flat assembler.

Index > Heap > Can we program without DB/DW/DQ?

Author
Thread Post new topic Reply to topic
system error



Joined: 01 Sep 2013
Posts: 671
system error
Ok this one bugs me for a while. Since 'db/dw/dq etc' are not standard x86 instructions, can we program in assembly without them? We could say it is possible by using all-procs program but then how about string? Where and how do we keep string constants like 'hello world'? Shocked
Post 30 Nov 2014, 04:18
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2913
Location: 0x77760000
typedef
system error wrote:
Since 'db/dw/dq etc' are not standard x86 instructions


They're not a standard part of anything. They simply define logical data size/width. i.e, they are markers in a bit stream.

And no! you cannot program without them. How else will you be able to manipulate data?
Post 30 Nov 2014, 06:54
View user's profile Send private message Reply with quote
neville



Joined: 13 Jul 2008
Posts: 507
Location: New Zealand
neville
typedef wrote:
And no! you cannot program without them. How else will you be able to manipulate data?
Actually yes you can. This will produce a double-zero-terminated 'hello world' string using X86 instructions and no data definition directives Wink
Code:
DATA:
PUSH 6C65H
INSB
OUTSW
AND [BX+6FH],DH
JB DATA+6CH
ADD [FS:BX+SI],AL
    

_________________
FAMOS - the first memory operating system
Post 30 Nov 2014, 07:29
View user's profile Send private message Visit poster's website Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc
system error
Quote:
Since 'db/dw/dq etc' are not standard x86 instructions

The motivation is fallacious, but the actual question might be reasonable. The data definition directives are nothing but an indication for the compiler to directly put the specified bytes at the specified location. These bytes may or may not be valid standard x86 instructions. In a way you may consider the instruction mnemonics as data definition directives with well-known encodings (that's what neville did).

Now to the reasonableness of your question. If the question is about the computational completeness of a programming style without static data definition (meaning that whatever data is used by the program it is dynamically created at runtime), then yes it is computationally complete. This kind of programming style is useful for some kinds of polymorphic code. Note however that this has nothing to do with the formal use of the data definition directives that can be legitimately used to put valid x86 instructions into the code.

Quote:
Where and how do we keep string constants like 'hello world'?

Lots of ways. E.g.:
Code:
push 'rld'
push 'o wo'
push 'hell'    

This will put 'hello world' onto the stack and then you'll be able to use esp as a pointer to it. I even have a macro for this use case.

_________________
Faith is a superposition of knowledge and fallacy
Post 30 Nov 2014, 13:24
View user's profile Send private message Reply with quote
system error



Joined: 01 Sep 2013
Posts: 671
system error
neville wrote:
typedef wrote:
And no! you cannot program without them. How else will you be able to manipulate data?
Actually yes you can. This will produce a double-zero-terminated 'hello world' string using X86 instructions and no data definition directives Wink
Code:
DATA:
PUSH 6C65H
INSB
OUTSW
AND [BX+6FH],DH
JB DATA+6CH
ADD [FS:BX+SI],AL
    
hi neville. nice code. what port is that? mem or vid?
Post 30 Nov 2014, 15:06
View user's profile Send private message Reply with quote
system error



Joined: 01 Sep 2013
Posts: 671
system error
l_inc wrote:

Lots of ways. E.g.:
Code:
push 'rld'
push 'o wo'
push 'hell'    



combining that with neville's idea, I came up with this. No push though.

Code:
format elf64 executable

mov rdi,data_area
mov eax,'Hell'
stosd
mov eax,'o Wo'
stosd
mov eax,'rld!'
stosd          ;could be zero-ended as well

mov rsi,data_area
mov edx,12
mov edi,1
mov eax,1
syscall

call EXIT

data_area:    
The problem is bound-checking as the string could expand dynamically at funtime. I don't want to use malloc (and avoiding stack if possible). Is there any way to create a safer 'data_area' somewhere in memory (assuming that I have only one data string and nothing else except code).
Post 30 Nov 2014, 15:18
View user's profile Send private message Reply with quote
system error



Joined: 01 Sep 2013
Posts: 671
system error
typedef wrote:
They're not a standard part of anything. They simply define logical data size/width. i.e, they are markers in a bit stream.
And no! you cannot program without them. How else will you be able to manipulate data?


I thought it was impossible as well. But after browsing though fasm source, well,... i rarely found any data directives used by tomasz. Its either tomasz was crazy or something he ate. hehehe Laughing And yes, i think one can come up with a great assembler without using any data directives. string goes back and forth the registers.
Post 30 Nov 2014, 15:47
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2913
Location: 0x77760000
typedef
Ok. Maybe I was thinking too much. I was thinking of instructions like MOVSx, LODSx, PUSHx, etc because they also use the same construct of "get/put this x amount of bytes from/to this location", just like the DB, DD, DW, DQ directives only that the latter are used at compile time.
Post 30 Nov 2014, 17:24
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2913
Location: 0x77760000
typedef
neville wrote:
typedef wrote:
And no! you cannot program without them. How else will you be able to manipulate data?
Actually yes you can. This will produce a double-zero-terminated 'hello world' string using X86 instructions and no data definition directives Wink
Code:
DATA:
PUSH 6C65H
INSB
OUTSW
AND [BX+6FH],DH
JB DATA+6CH
ADD [FS:BX+SI],AL
    


But what about a large static lookup table? Would you need initialization code as well? Even so, that initialization code would also need some static parameters/variables which would have to be defined by one of the directives. Also, when using initialization code, one has to consider the fact that memory allocation is not guaranteed during runtime. Surprised
Post 30 Nov 2014, 17:28
View user's profile Send private message Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc
typedef
What neville provided has nothing to do with the initialization code. It's just a sequence of nonsense mnemonics, that would be compiled into data, i.e. using mnemonics as data definition directives with no semantical difference to db/dw/...

Quote:
that initialization code would also need some static parameters/variables which would have to be defined by one of the directives

No. All the data can be a part of the code as I've shown that with the push instructions. It's static data definition vs. dynamic data generation.

Quote:
Also, when using initialization code, one has to consider the fact that memory allocation is not guaranteed during runtime

There's some sort of guarantee for the stack space.

_________________
Faith is a superposition of knowledge and fallacy
Post 30 Nov 2014, 17:37
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2913
Location: 0x77760000
typedef
I_inc wrote:

No. All the data can be a part of the code as I've shown that with the push instructions. It's static data definition vs. dynamic data generation.


But what about a large static lookup table? Would you need just a sequence of nonsense mnemonics as well?

I_inc wrote:
There's some sort of guarantee for the stack space.

For a large lookup table?
Post 30 Nov 2014, 17:48
View user's profile Send private message Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc
typedef
Once again. I wasn't suggesting a sequence of nonsense mnemonics. neville did. But his suggestion has no semantical difference to db/dw/... and hence makes no sense.

Quote:
For a lookup table?

Sure, why not? The static memory overhead for 32-bit code is about 25%.

_________________
Faith is a superposition of knowledge and fallacy
Post 30 Nov 2014, 17:53
View user's profile Send private message Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1171
Location: Overflow
Matrix
system error wrote:
l_inc wrote:

Lots of ways. E.g.:
Code:
push 'rld'
push 'o wo'
push 'hell'    



combining that with neville's idea, I came up with this. No push though.

Code:
format elf64 executable

mov rdi,data_area
mov eax,'Hell'
stosd
mov eax,'o Wo'
stosd
mov eax,'rld!'
stosd          ;could be zero-ended as well

mov rsi,data_area
mov edx,12
mov edi,1
mov eax,1
syscall

call EXIT

data_area:    
The problem is bound-checking as the string could expand dynamically at funtime. I don't want to use malloc (and avoiding stack if possible). Is there any way to create a safer 'data_area' somewhere in memory (assuming that I have only one data string and nothing else except code).


Smile
gcc actually does exactly that when it loads your constants
Post 01 Dec 2014, 11: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 can attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar.

Powered by rwasa.