flat assembler
Message board for the users of flat assembler.

Index > Windows > New to fasm! - Please, I need help!

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
Alexander



Joined: 22 Dec 2008
Posts: 40
Alexander 22 Dec 2008, 18:29
Hello,

I'm new to fasm. Question Please, I need help with the following topics:

Strings in FASM and Win32/Win64 API Crying or Very sad - afaik there are many possibilities to define a string, the following I've seen are

db, du, dword, tchar, ...

so what type of string/unicode string shall I use? Is there a standard in fasm.

My second question is about string manipulation or string handling. Is there a way to search for an ascii- or unicode null without lodsb, without macros or without any libs like fasmlib, like it was in TASM. So that I'm possible to get the end of the string.

Thank you for your help

Alexander Question Question
Post 22 Dec 2008, 18:29
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20451
Location: In your JS exploiting you and your system
revolution 22 Dec 2008, 23:05
db and du are the basic string definers. dword is only suitable for short strings and tchar is a structure/macro. You can use any of them you like, is all depends upon your requirement for the code.

If you want to avoid lodsb you can always directly code the two instructions: mov al,[esi]/inc esi But they are not exactly the same because lodsb does not alter the flags.
Post 22 Dec 2008, 23:05
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 23 Dec 2008, 00:29
Quote:

mov al,[esi]/inc esi But they are not exactly the same because lodsb does not alter the flags.

Lets fix that Wink mov al, [esi]/lea esi, [esi+1] Yet, this is still not exactly the same, do you see why? (not counting code difference)
Post 23 Dec 2008, 00:29
View user's profile Send private message Reply with quote
madmatt



Joined: 07 Oct 2003
Posts: 1045
Location: Michigan, USA
madmatt 23 Dec 2008, 01:29
you use 'du' for unicode strings:
Code:
ustring du "a unicode string",0.    

use 'db' for ASCII strings:
Code:
astring db "an ASCII string", 0    

dd is used rarely for 'data identifier' of 4 ASCII characters:
Code:
tag dd 'DATI'    

You usually don't put a zero at the end of the data identifier string.
Post 23 Dec 2008, 01:29
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20451
Location: In your JS exploiting you and your system
revolution 23 Dec 2008, 01:40
LocoDelAssembly wrote:
Yet, this is still not exactly the same, do you see why? (not counting code difference)
Hmm, I not sure what you are getting at here? Exception handling maybe? Timing differences? Direction flag compliance?
Post 23 Dec 2008, 01:40
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 23 Dec 2008, 02:44
Quote:

Direction flag compliance?

Very Happy
Post 23 Dec 2008, 02:44
View user's profile Send private message Reply with quote
shoorick



Joined: 25 Feb 2005
Posts: 1614
Location: Ukraine
shoorick 23 Dec 2008, 06:14
TCHAR is usefull if you wish your string will automatically depend on currently selected mode (ansi or unicode), what is determining with including or "win*a*.inc", either "win*w*.inc".
Post 23 Dec 2008, 06:14
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20451
Location: In your JS exploiting you and your system
revolution 23 Dec 2008, 06:29
LocoDelAssembly wrote:
Quote:

Direction flag compliance?

Very Happy
Code:
      mov     al,[esi]
    push    ecx
 pushfd
      pop     ecx
 shl     ecx,31-10 ;DF is bit 10
     sar     ecx,31
      or      ecx,1
       lea     esi,[esi+ecx]
       pop     ecx    
But this adds another difference, the stack is altered.
Post 23 Dec 2008, 06:29
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 23 Dec 2008, 10:35
revolution,

Memory below esp should be considered volatile, isn't it?
Post 23 Dec 2008, 10:35
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20451
Location: In your JS exploiting you and your system
revolution 23 Dec 2008, 11:05
In real mode, yes. In ring3 PM mode, no.

Although, in general, it is not good practice to rely on something that is below the stack pointer.
Post 23 Dec 2008, 11:05
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 23 Dec 2008, 19:40
revolution,

I'd meant generic case, regarding interrupt/trap through gate with DPL==CPL.

Some amendments to your code:
Code:
        mov     al, [esi]
        push    ecx
        pushfd
        bt      dword [esp], 10
        sbb     ecx, ecx
        lea     esi, [esi+1+2*ecx]
        popfd
        pop     ecx    
EFLAGS are preserved. Wink

_________________
"Don't belong. Never join. Think for yourself. Peace." – Victor Stone.
Post 23 Dec 2008, 19:40
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20451
Location: In your JS exploiting you and your system
revolution 24 Dec 2008, 06:21
Code:
  mov     al,[esi]
    pushf
       bt      word[esp],10
        sbb     esi,0
       bt      word[esp],10
        cmc
 adc     esi,0
       popf    
Only alters one word of lower stack.
Post 24 Dec 2008, 06:21
View user's profile Send private message Visit poster's website Reply with quote
Alexander



Joined: 22 Dec 2008
Posts: 40
Alexander 12 Jan 2009, 13:48
Hello,

Thank you for all your answers Very Happy!

The difference between unicode and ansi/ascii is absolutly clear, but when do I use a string, defined with du, and when do I use TCHAR? Can you please give me an example.

And here another question: How do I iterate (count a string's length) in an NASM32 Dll's exported function, which takes a "VB6" string (or any Win32/Win64 string) as an argument. Which string do I choose? Would you please be so kind to give me an example too? Embarassed

Thank you in advance
Alexander
Post 12 Jan 2009, 13:48
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20451
Location: In your JS exploiting you and your system
revolution 12 Jan 2009, 14:03
TCHAR is just a macro that is defined by the "win32[a|w].inc" file. If you include the "a" version you get bytes strings with TCHAR, if you include the "w" version you get word strings. That is all. It is a convenience for C code to be bi-textual, that is, the same source code can be either ASCII or UNICODE simply by using the TCHAR operator. In assembly the distinction is much more pronounced and making the code bi-textual is much more complicated and thus TCHAR is not as useful as with C.
Post 12 Jan 2009, 14:03
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 12 Jan 2009, 14:06
My advice would be: don't use TCHAR. If you need unicode string, use du, otherwise use db.

I am not sure which kind of string is used by VB6. Win32/64 APIs use both ASCII string (defined by "db") and unicode string (defined by "du"). Those which take ascii string end with A (like MessageBoxA), and those which take unicode string end with W (like MessageBoxW)
Post 12 Jan 2009, 14:06
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Alexander



Joined: 22 Dec 2008
Posts: 40
Alexander 12 Jan 2009, 14:13
Thank you revolution and thank you vid.

So it is better to use du instead of tchar. Okay ... my first question is now clear.

But the second one ... when I'm coming from a programming language, like VB6 or C# other than NASM, my string has wrong values.

Could you please give me an example how I'm able to iterate through a string?
Post 12 Jan 2009, 14:13
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20451
Location: In your JS exploiting you and your system
revolution 12 Jan 2009, 14:30
Alexander: To "iterate through a string" just use lodsb/lodsw, when you come to a zero value then you've reached the end. I don't know why you are against using lodsb/lodsw. For a newbie I think you shouldn't try be fussy about which instructions you don't want to use.
Post 12 Jan 2009, 14:30
View user's profile Send private message Visit poster's website Reply with quote
Alexander



Joined: 22 Dec 2008
Posts: 40
Alexander 12 Jan 2009, 14:41
I'm not against lodsb or lodsw. But I think I've problems accessing a BSTR (VB6 and .NET ie C# or VB.NET work internally with BSTRs). BSTRs are organized like the following:

Length prefix (4 Bytes) | datastring (2 Bytes * Length) | NULL (2 Bytes)

I think what I'm accidently accessing is the BSTR-Length prefix. I think that's the reason why I get "wrong values". Normally a pointer to a BSTR should point to the data, not to the length prefix itself.

I know the following question is a stupid one Embarassed: But what shall I do?

Thank you
Post 12 Jan 2009, 14:41
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20451
Location: In your JS exploiting you and your system
revolution 12 Jan 2009, 15:11
Just add 4 to your pointer before accessing the data words. You could also read the first 4 bytes and thus get the length into a register and then start iterating through your string with lodsw until your counter reaches zero.
Post 12 Jan 2009, 15:11
View user's profile Send private message Visit poster's website Reply with quote
Alexander



Joined: 22 Dec 2008
Posts: 40
Alexander 12 Jan 2009, 15:16
Thank you for all your help!
Alexander
Post 12 Jan 2009, 15:16
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< 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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.