home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / jsage / znode3 / info / mac-chal.z80 < prev    next >
Encoding:
Text File  |  1994-09-02  |  3.8 KB  |  106 lines

  1. ; MAC-CHAL.Z80
  2. ; A solution to the Jay Sage/Ben Grey "Macro Challenge"
  3. ; R. A. Freed, 26 May 1989
  4. ;_______________________________________________________________________
  5. ;
  6. ; This is an INCLUDE file for use with SLR Systems' Z80ASM+ macro
  7. ; assembler.  It has not been tested with SLR's Z80ASM or Microsoft's
  8. ; M80, but it should work with those also.
  9. ;
  10. ; The author submits this bit of fluff for its intellectual curiosity
  11. ; value only.  He certainly would not advocate use of such an obscure
  12. ; macro parameter convention.  Mainly, he can't resist a good
  13. ; programming puzzle.  (The original "challenge" message from Jay
  14. ; Sage's Newton Centre Z-Node, 617/965-7259, is reproduced below.)
  15. ;_______________________________________________________________________
  16. ;
  17. ; Preliminaries
  18. ;
  19. ; The macro MSTR builds a string argument for an assembler pseudo-op.
  20. ; The macro MCHR adds the next character to the string (if OP is MSTR),
  21. ;  or it generates the final pseudo-op (specified by OP).
  22. ; The major "trick" here is that MCHR is defined by MSTR.  Hence, MCHR
  23. ;  causes its own redefinition when invoked with OP=MSTR.
  24.  
  25. MSTR    MACRO    STR
  26. MCHR    MACRO    OP,CHR
  27.     OP    STR&&CHR    ;; see note below
  28.     ENDM
  29.     ENDM
  30.  
  31. ; Note the double ampersands above:  The first is stripped during the
  32. ; expansion of MSTR (definition of MCHR); the second is stripped
  33. ; during the expansion of MCHR.
  34. ;_______________________________________________________________________
  35. ;
  36. ; The "Solution"
  37. ;
  38. ; The macro DWB generates either a DW or DB, depending upon the first
  39. ; character of its single arbitrary parameter string.  If the leading
  40. ; character of STR is a pound sign (#), it is stripped and a DW is
  41. ; generated using the remaining characters of STR.  Otherwise, a DB is
  42. ; generated.
  43.  
  44. DWB    MACRO    STR
  45. FIRST    DEFL    NOT 0
  46.   IRPC CHR,<STR>
  47.     IF FIRST
  48.       IFDIF <CHR>,<#>
  49.     DB    STR
  50.     EXITM
  51.       ELSE
  52. FIRST    DEFL    0
  53.     MSTR
  54.       ENDIF
  55.     ELSE
  56.     MCHR    MSTR,<CHR>
  57.     ENDIF
  58.   ENDM
  59.   IF NOT FIRST
  60.     MCHR    DW
  61.   ENDIF
  62. ENDM
  63. ;_______________________________________________________________________
  64. ;
  65. ; The "Challenge"
  66. ;
  67.     .COMMENT    |
  68.  
  69. Msg 382 is 18 line(s) on 05/24/89 from JAY SAGE
  70. to ALL about MACRO CHALLENGE
  71.  
  72. Here is a challenge for macro assembler programmers.  It was posed to me
  73. by Ben Grey.  So far I have not come up with a solution.
  74.  
  75. A macro is to be passed a parameter which is a number with or without a
  76. leading "#" character.  If the "#" is present, one thing is to be done
  77. with the remaining numerical part (e.g., make it the argument of a DB).
  78. If the "#" is absent, then something else will be done with the number,
  79. such as making it the argument of a DW.
  80.  
  81. This is all part of a more general problem of coming up with a macro that
  82. can be passed a string that the macro splits into two parts.
  83.  
  84. Any ideas?
  85.  
  86. P.S.  I think I can solve the problem if I know that the numerical part
  87. of the parameter is a DECIMAL number, but I cannot figure out how to
  88. handle the general case, where it might be hex or binary or even a general
  89. symbolic token.
  90.  
  91.  
  92. Msg 388 is 08 line(s) on 05/25/89 from BOB FREED
  93. to JAY SAGE about MACRO CHALLENGE
  94.  
  95. Unfortunately, most Z80 assemblers (notably M80 and Z80ASM) lack the
  96. necessary assembly-time string processing operations that would make this
  97. sort of thing easy (if not at all possible) to do.  SLR's MS-DOS
  98. assembler OPTASM allows text equates via certain forms of the EQU
  99. pseudo-op, that makes the solution a snap.  Lacking such a feature, I
  100. think I may have a somewhat complex (and certainly inelegant) way to
  101. solve the challenge for the general case you described.  (I can't resist
  102. a puzzle.)  I'll let you know after I have a chance to test it out.....
  103.  
  104. |
  105. ;_______________________________________________________________________
  106.