Author Topic: Use of TCHAR in UNICODE build  (Read 14142 times)

dedndave

  • Member
  • *****
  • Posts: 8829
  • Still using Abacus 2.0
    • DednDave
Re: Use of TCHAR in UNICODE build
« Reply #15 on: January 04, 2014, 05:35:21 AM »
here is a little test program
build it as a console app
try it with/without the __UNICODE__ equate
Code: [Select]
;###############################################################################################

        __UNICODE__ EQU 1

        .XCREF
        .NoList
        INCLUDE    \Masm32\Include\Masm32rt.inc
        .List

;###############################################################################################

;TCHR macro by qWord

TCHR    MACRO lbl,args:VARARG

    IFNDEF __UNICODE__
        lbl db args
    ELSE
        UCSTR lbl,args
    ENDIF

        ENDM

;###############################################################################################

        .DATA

    TCHR    szTest,"Test",0

;###############################################################################################

        .CODE

;***********************************************************************************************

_main   PROC

    print   offset szTest,13,10
    mov     eax,lengthof szTest
    print   str$(eax),13,10
    mov     eax,sizeof szTest
    print   str$(eax),13,10
    inkey
    exit

_main   ENDP

;###############################################################################################

        END     _main

MichaelK

  • Regular Member
  • *
  • Posts: 11
    • M. J. Kwayisi
Re: Use of TCHAR in UNICODE build
« Reply #16 on: January 04, 2014, 05:39:08 AM »
Thanks. But just wondering, why did you post
spot the difference ?   :biggrin:
earlier in this thread. You spotted an error somewhere?
So [or eax, 0FFh] == [mov eax, -1]? Not again, asm!

dedndave

  • Member
  • *****
  • Posts: 8829
  • Still using Abacus 2.0
    • DednDave
Re: Use of TCHAR in UNICODE build
« Reply #17 on: January 04, 2014, 05:46:14 AM »
i'm not sure - lol
i am too old to remember what i did yesterday, let alone last week   :biggrin:

ragdog

  • Member
  • ****
  • Posts: 610
Re: Use of TCHAR in UNICODE build
« Reply #18 on: January 04, 2014, 11:45:54 PM »
Hi and a happy new year:biggrin:

This is a good question i use for unicode WSTR

WSTR  wszNAME,"Masm32"   

What is now better ;TCHR macro by qWord or WSTR ?

Greets,

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 7777
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Use of TCHAR in UNICODE build
« Reply #19 on: January 05, 2014, 12:15:46 AM »
It is not a case of better or worse, read the reference material and use them for what they do.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

dedndave

  • Member
  • *****
  • Posts: 8829
  • Still using Abacus 2.0
    • DednDave
Re: Use of TCHAR in UNICODE build
« Reply #20 on: January 05, 2014, 04:02:21 AM »
WSTR is fine if you always want a UNICODE string
well - it doesn't always return correct results if you use SIZEOF/LENGTHOF operators with a string created with WSTR

Code: [Select]
WSTR szTest,"Test",0
the correct "LENGTHOF szTest" should be 5 - with WSTR, it will be 4
the correct "SIZEOF szTest" should be 10 - with WSTR, it will be 8
WSTR always defines a UNICODE string, whether or not the symbol __UNICODE__ is defined

in the latest version of the masm32 package, Hutch added a number of UNICODE macros written by qWord
of particular interest are qWord's UCSTR and UCCSTR macros
UCCSTR is like UCSTR, except it adds support for many C-style escape sequences (\n, \t, etc)
these macros always create UNICODE strings, but allow more complex definitions
also, the LENGTHOF and SIZEOF operators seem to work well with these macros

Hutch has a STRING macro that defines a UNICODE string if __UNICODE__ is defined and an ANSI string if it isn't
however, the LENGTHOF and SIZEOF operators do not always return proper values

so, qWord wrote the TCHR macro, which is not in the masm32 library
it's a pretty simple macro, because it relies on UCSTR (a complex macro) to do all the UNICODE work

Code: [Select]
;TCHR macro by qWord

TCHR    MACRO lbl,args:VARARG

    IFNDEF __UNICODE__
        lbl db args
    ELSE
        UCSTR lbl,args
    ENDIF

        ENDM

you can read a previous discussion, here...

http://masm32.com/board/index.php?topic=1305

and an older topic by qWord, here...

http://masm32.com/board/index.php?topic=680.0

ragdog

  • Member
  • ****
  • Posts: 610
Re: Use of TCHAR in UNICODE build
« Reply #21 on: January 05, 2014, 05:36:26 AM »
Thanks Dave

Code: [Select]
in the latest version of the masm32 package,I use Masm32v10 and for a long time there was problems with the last package with the kernel.inc
for unicode or ascii builds is this all fixed?

What use your?

Greets,

dedndave

  • Member
  • *****
  • Posts: 8829
  • Still using Abacus 2.0
    • DednDave
Re: Use of TCHAR in UNICODE build
« Reply #22 on: January 05, 2014, 06:45:37 AM »
i have both versions - i can switch back and forth by renaming a few folders   :P

but - the version 11 issues can be overcome by creating a new kernel32.inc and lib

qWord's UCSTR macro is probably in the forum, someplace
if you like, i don't think qWord would mind me posting it

qWord

  • Member
  • *****
  • Posts: 1476
  • The base type of a type is the type itself
    • SmplMath macros
Re: Use of TCHAR in UNICODE build
« Reply #23 on: January 05, 2014, 08:02:09 AM »
When Hutch request help for the beta release of MASM32v11, my focus was on removing the bug in WSTR (IIRC) and improving the fn/rv macros (-->fnx/rvx). In context of this I proposed the UC* macros, which are indented to have the same behavior as MASM's DB/BYTE directive (can be used in any segment; SIZEOF/LENGTHOF works). At that time I didn't recognize that there is also the need for a macro like TCHR. Remarks that for the C-style version (UCCSTR) there is the issue that "\n" emits {10,13} instead of {13,10}, which should be fixed.
qWord's UCSTR macro is probably in the forum, someplace
if you like, i don't think qWord would mind me posting it
The UC* macros are independent and can be copied from MASM32v11 to v10 without any problems, if needed. Here the fixed C-style version:
Code: [Select]
UCCSTR MACRO lbl,args:VARARG

    IFNDEF uccstr_lbl_cntr
        uccstr_lbl_cntr = 0
    ENDIF
    IFB <&lbl>
        uccstr_lbl TEXTEQU <anonym_WSTR_>,%uccstr_lbl_cntr
        uccstr_lbl_cntr = uccstr_lbl_cntr + 1
    ELSE
        uccstr_lbl TEXTEQU <&lbl>
    ENDIF

    uccstr_size = 0
    uccstr_flg = 0
    uccstr_iarg = 1
    uccstr_esc = 0
    FOR _arg,<args>
        uccstr_flg = 0
        FORC char,<&_arg>
            IF uccstr_flg NE 0
                uccstr_pos INSTR 1,<"'\>,<&char>
                IF uccstr_pos EQ uccstr_flg
                    EXITM
                ELSEIF uccstr_pos EQ 3 AND uccstr_esc EQ 0
                uccstr_esc = 1
                ELSE
                    IF uccstr_esc
                        uccstr_pos INSTR 1,<\0ablrxqtpAn>,<&char>
                       
                        IFE uccstr_pos
                            uccstr_flg=0
                            EXITM
                        ENDIF
                        uccstr_size = uccstr_size + uccstr_pos/12
                        uccstr_esc = 0
                    ENDIF
                    uccstr_size = uccstr_size + 1
                ENDIF
            ELSE
                uccstr_flg INSTR 1,<"'>,<&char>
                IFE uccstr_flg
                    IF (OPATTR _arg) AND 100y
                        uccstr_flg = 3
                    ENDIF
                    EXITM
                ENDIF
            ENDIF
        ENDM
        IF uccstr_flg EQ 0 OR uccstr_esc NE 0
            EXITM
        ELSEIF uccstr_flg EQ 3
            uccstr_size = uccstr_size + 1
        ENDIF       
        uccstr_iarg = uccstr_iarg + 1
    ENDM
    IF uccstr_flg EQ 0 OR uccstr_esc NE 0
        IF uccstr_esc
        %   .err <invalid escape sequence : argument : @CatStr(%uccstr_iarg)>
        ELSE
        %   .err <invalid string specifier : argument : @CatStr(%uccstr_iarg)>
        ENDIF
        EXITM
    ENDIF
   
    align 2
    uccstr_lbl WORD uccstr_size dup (?)
    org $-uccstr_size*2
   
    uccstr_esc = 0
    FOR _arg,<&args>
        uccstr_flg = 0
        FORC char,<&_arg>
            IF uccstr_flg NE 0
                uccstr_pos INSTR 1,<"'\>,<&char>
                IF uccstr_pos EQ uccstr_flg
                    EXITM
                ELSEIF uccstr_pos EQ 3 AND uccstr_esc EQ 0
                    uccstr_esc = 1
                ELSE
                    IFE uccstr_esc
                        uccstr_char CATSTR <dw >,uccstr_quote,<&char>,uccstr_quote
                        uccstr_char
                    ELSE
                        uccstr_pos INSTR 1,<\0ablrxqtpAn>,<&char>
                        IFE uccstr_pos
                            uccstr_flg=0
                            EXITM
                        ENDIF
                        uccstr_char SUBSTR <  5ch00h28h29h3ch3eh21h22h09h25h26h0dh,0ah>,uccstr_pos*3,3+4*(uccstr_pos/12)
                        uccstr_esc = 0
                        dw uccstr_char
                    ENDIF
                ENDIF
            ELSE
                uccstr_flg INSTR 1,<"'>,<&char>
                IFE uccstr_flg
                    IF (OPATTR _arg) AND 100y
                        uccstr_flg = 3
                    ENDIF
                    EXITM
                ENDIF
                uccstr_quote TEXTEQU <&char>
            ENDIF
        ENDM
        IF uccstr_flg EQ 3
            dw _arg
        ENDIF       
    ENDM

endm

MREAL macros - when you need floating point arithmetic while assembling!

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 7777
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Use of TCHAR in UNICODE build
« Reply #24 on: January 05, 2014, 09:55:15 AM »
Arrrrgh,

> however, the LENGTHOF and SIZEOF operators do not always return proper values

The macros were not designed to do that. None the less this is the test result.

Code: [Select]
IF 0  ; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
                      Build this template with "CONSOLE ASSEMBLE AND LINK"
ENDIF ; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    __UNICODE__ equ 1

    include \masm32\include\masm32rt.inc

    .code

start:
   
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    call main
    inkey
    exit

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

main proc

    WSTR mytext,"1234567890"                ; 10 characters

    print ADDR mytext," original string",13,10

    print ustr$(len(ADDR mytext))," len macro",13,10

    print ustr$(LENGTHOF mytext)," character count",13,10

    print ustr$(SIZEOF mytext)," byte count",13,10

    ret

main endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

end start

; results

    1234567890
    10 len macro
    10 character count
    20 byte count
    Press any key to continue ...
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

jj2007

  • Member
  • *****
  • Posts: 10856
  • Assembler is fun ;-)
    • MasmBasic
Re: Use of TCHAR in UNICODE build
« Reply #25 on: January 05, 2014, 03:08:38 PM »
Folks, we started that debate over five years ago :badgrin:

include \masm32\MasmBasic\MasmBasic.inc        ; download
  Init
  wData MyWide$, 32, "Test", 32, 0
  wPrint wStr$("Size of MyWide$: \t%i bytes\n", SIZEOF MyWide$), wStr$("Length of MyWide$: \t%i chars\n", LENGTHOF MyWide$)
  wInkey "And there it is:", wTb$, "[", offset MyWide$, "]"
  Exit
end start

Output:
Size of MyWide$:        14 bytes
Length of MyWide$:      7 chars
And there it is:        [ Test ]


P.S. Just switched on my notebook after a long trip - no sign of life, the screen was dead. Some hours on the radiator helped wake it up, though.

Happy New Year :biggrin:

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 7777
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Use of TCHAR in UNICODE build
« Reply #26 on: January 06, 2014, 08:56:45 AM »
JJ,

Sounds like its really cold over there at the moment. We have the opposite problem although not the panic stations some would have you believe. I build computers with 6 inch fans front and back and monster coolers and on a hot day run a pedestal fan behind me.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy: