flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Work with chars of string in macro

Author
Thread Post new topic Reply to topic
demoth



Joined: 01 Dec 2012
Posts: 2
demoth
Can I work with a char of string constant in macro?
For example:
Code:
macro crypt str
{
  ...
}
someString db crypt "hello", 0    
will be
Code:
someString db 'h' xor 10, 'e' xor 11, 'l' xor 12, 'l', xor 13, 'o' xor 14, 0    
?
Post 01 Dec 2012, 14:58
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
demoth,

String literal is a token, it can't be decomposed using preprocessor.

Your crypting routine can be implemented using assembler directives:
Code:
struc crypt [def] {
common local b
  . def
  repeat $-.
    load b from .+%-1
    store b xor 10 at .+%-1
  end repeat
}
someString crypt db "hello"
  db 0    
Post 01 Dec 2012, 15:45
View user's profile Send private message Reply with quote
demoth



Joined: 01 Dec 2012
Posts: 2
demoth
baldr, thank You!

I upgraded it:
Code:
struc crypt key, [def] {
common local b
  . def
  repeat $-.
    load b from .+%-1
    store b xor key at .+%-1
  end repeat
}

macro DeclareCryptStr name, key, dat
{
  db key
  name crypt key, db dat
  db 0
}    
Maybe it will help someone else.
Post 01 Dec 2012, 16:41
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
demoth,

It can be improved more:
Code:
struc reEqu [v] {
common; probably «v» contains «,»
  match _v, v \{; save «.» value to protect it from «restore»
    restore .
    . equ _v
  \}
  match , v \{; heh, «v» is empty
    restore .
    . equ
  \}
}

struc xcrypt [def*] {
common
  !mask equ 10; establish defaults
  !def equ def; unless «key=#» is used
local b
  match =key==mask _def, !def \{
    !mask reEqu mask; extract mask
    !def reEqu _def; and definition
  \}
  match _def, !def \{ . _def \}; guess why not plain «. !def» Wink
  repeat $-.
    load b from $-%; now do it backward Wink
    store b xor !mask at $-%
  end repeat
  restore !mask; clean up
  restore !def
}

macro xcrypt [def*] {; wrapper for unusual usage
local ..label
  ..label xcrypt def
}

cryptedString xcrypt key=11 db "hello"
  db 0

cryptedUnicodeString xcrypt key=12 du "hello"
  du 0

; Now «macro» takes its turn (label «letItBe» is optional, as you may guess)
letItBe: xcrypt key=13 db "hello"
  db 0    
xcrypt (and crypt from my previous post) is kind of macros that I call "prefix macro": it stands between label and real definitions to modify their results (even if label is absent — hence additional «macro» definition).

Your approach somewhat limits their usefulness, and «DeclareCryptStr» looks strange (key is stored alongside with keyed string?)
Post 01 Dec 2012, 20:30
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.