flat assembler
Message board for the users of flat assembler.

flat assembler > Windows > Create your own data type

Author
Thread Post new topic Reply to topic
Mino



Joined: 14 Jan 2018
Posts: 156
Hello: )
I have a somewhat... difficult question:
How to create your own variable data type.
For example, create a type that would only accept ASCII characters from 65 to 90 (so, only letters of the upper-case alphabet).
If, for example, it were to be included '!', in this case, there would be an error, or at least a capacity overrun.
I have absolutely no idea of how to do this, even if it is possible...
So I apologize in advance.
Have a good end of the day!

_________________
The best way to predict the future is to invent it.
Post 13 Feb 2018, 12:20
View user's profile Send private message Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 1258
No there's no way to create a "data type". However, with a macro, you could probably generate the string and check it for "invalid characters", that's not a data type though.
Post 13 Feb 2018, 13:27
View user's profile Send private message Reply with quote
ProMiNick



Joined: 24 Mar 2012
Posts: 190
Location: Russian Federation, Sochi
If thou need only data definition:
Code:
macro ucaseA [val] {
   common
     match ,val \{ error \}
     match any,val \{
       virtual at 0
          db val
          repeat $
             load a byte from $-%
             if a>90
                error
             end if
             if a<65
                error
             end if
       end virtual
       db val
}

struc ucaseA [val] {
   common
     match ,val \{ error \}
     match any,val \{
        . db val
          repeat $
             load a byte from .+%
             if a>90
                error
             end if
             if a<65
                error
             end if
}    


Thou should override each instruction with macro that thou want affect thour type:
Code:
macro mov dest, src {
  match type val,src \{
     match =ucaseA,type \\{ 
        virtual at 0
          db val
          repeat $
             load a byte from $-%
             if a>90
                error
             end if
             if a<65
                error
             end if
       end virtual
      mov dest,val
       ?s equ 
    \\}
     
  \}
  match =?s,?s \{ mov dest, src \}
  
}    


I not guarantee that above code work - it is only ideas.
However, in fasm natively supported only untyped sizing of operands. All the rest throw the macros.
Post 13 Feb 2018, 13:39
View user's profile Send private message Send e-mail Reply with quote
Mino



Joined: 14 Jan 2018
Posts: 156
Thank you for the answer:)
But I confess that I don't quite understand how to use these macros. If that's not too much to ask, could you give me an example?

_________________
The best way to predict the future is to invent it.
Post 15 Feb 2018, 14:41
View user's profile Send private message Reply with quote
ProMiNick



Joined: 24 Mar 2012
Posts: 190
Location: Russian Federation, Sochi
All types are illusion that helps stupid programmer to correctly interpret data, (next line makes this stupid things in fasm ON)
Code:
  ;typesON equ 1    

;but w/o that line code stayed usable for rest of community
Code:
  macro enumns name,size,[values] {
  common local n,lst,?s
    n=0
    name#.list equ
  forward
    match a==b,values \{
      name#.list equ name#.list,a
      n=b
      a=b
      ?s equ  \}
    match =?s, ?s \{
      name#.list equ name#.list,values
      values=n \}
    n=n+1
    restore ?s
  common
   match =typesON,typesON \{
     name equ size
     ?s equ  \}
   match =?s, ?s \{
    macro name [defs] \\{
     ;\\forward
        match a,<name#.list \\\{
        if ~defs in a>
          error
        end if \\\}
      \\common
        size defs
    \\}
    struc name [defs] \\{
      \\common
        match =db,size \\\{ label .:byte \\\}
        match =dw,size \\\{ label .:word \\\}
        match =dd,size \\\{ label .:dword \\\}
        match =dq,size \\\{ label .:dqword \\\}
        name defs \\}
   \}
  }

  macro enum name,[values] {
    common local ?s
      match n:s,name \{enumns n,s,values
      ?s equ \}
      match =?s, ?s \{ enumns name,dd,values
      ?s equ \} }

  ; use all above - define our type
  enum weekday:db,monday, tuesday, wednesday, thursday, friday, saturday, sunday

  ;define data of our type
  Myday weekday monday
  Myday2 weekday 1 ; if typesON will be defined here compilation will stop (monday & 1 binary identical, but 1 is out of type weekday)    


that way you can protect data defenitions for unsigned only or signed only values, for only integer, only float, or only string values (or even with chars in lowercase or uppercase only). But all of this will be only illusion that not make affect on output code.
Post 17 Feb 2018, 23:33
View user's profile Send private message Send e-mail Reply with quote
ProMiNick



Joined: 24 Mar 2012
Posts: 190
Location: Russian Federation, Sochi
Code:
typesON equ 1
macro ucaseA [val] {
   common local ?s
     match =typesON,typesON \{ ?s equ  \}
     match =?s, ?s \{
       match ,val \\{ error \\}
       match any,val \\{
         virtual at 0
            db val
            repeat $
               load a byte from $-%
               if a>90
                  error
               end if
               if a<65
                  error
               end if
             end repeat
         end virtual \\} \}
         db val
} 

struc ucaseA [val] { 
   common
     label .:byte
     ucaseA val
}

A db "hello world!!!!"
; use macros as usual data definitions your type in place of data definition directive
B ucaseA "HERE ALL UCASE"
C ucaseA "But here is not"      
Post 17 Feb 2018, 23:52
View user's profile Send private message Send e-mail Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 419
Location: Belarus
Let me ask the topicstarter what his real problem is. The right solution to it might be in the opposite direction, and then there’s not much point in putting hundreds of lines of task-specific macros into the code.

Are you trying to implement something like Pascal/Delphi subrange types?
Post 18 Feb 2018, 09:05
View user's profile Send private message Visit poster's website Reply with quote
Mino



Joined: 14 Jan 2018
Posts: 156
Thank you for your answers Smile
Yes, if we want, I would like to try to implement custom types from the axioms. But I never programmed in Delphi or Pascal, so maybe I misunderstood.

_________________
The best way to predict the future is to invent it.
Post 18 Feb 2018, 09:28
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 419
Location: Belarus
Mino wrote:
I would like to try to implement custom types from the axioms. But I never programmed in Delphi or Pascal, so maybe I misunderstood.

Pascal/Delphi have subrange types which are defined as follows:
Code:
type
  TLatinLetter = 'A'..'Z';
  THeadCount = 1..160;

  // Or even better
  TMonth = (mnJan, mnFeb, mnMar, ..., mnDec);
  TSummerMonth = mnJun..mnAug;    

Implementing such stuff in assembly might be ineffective. In HLL compiler would do all the checks necessary. What you can do in assembly is just create a few macros to ensure you define valid data (you already have to check it yourself, so don’t gain much here). How would you prevent something like this?
Code:
        mov     eax, 'Ў'
        mov     [YourTypeVariable], eax    

Except, maybe, redefining every instruction as a macro. And injecting more and more checks into such a macro for every custom type you define.
Post 18 Feb 2018, 11:00
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 cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2018, Tomasz Grysztar.

Powered by rwasa.