C's macro documentation

From: Frank Kotler (fbkotler_at_comcast.net)
Date: 06/29/04


Date: Tue, 29 Jun 2004 14:49:36 GMT

I thought of sending you guys a message Thursday, saying that I was off
for the weekend partying with old friends, and I'd look at the new code
when I got back. But that sounded ominously like "Debs' Last Message"
(since which she hasn't been heard from - Where are you Debs? We love
you! Hope you're okay.), so I didn't... and it worked, I'm back! :) But
horribly behind on correspondence!

Beth wrote:
>
> Hi,
>
> Subject line says it all, I suppose; Frank said that C has
> provided documentation for the macros but I just can't find
> it...is it a separate file or did C add comments to the source
> code or what?

Just in a message to the SF list, I guess. I'll stick it in CVS
somewhere RSN, if C doesn't beat me to it.

> Unfortunately, with "MailMan" out of commission

Very nice. Not sending anything to you, and bouncing messages back to
me. I'll have a look at "settings", as you suggest, and see if I can see
anything that might fix it. It *does* appear to be some kind of horned
beast... just a head-and-shoulders picture, so we can't see if the tail
is sys_forked or not :)

------

Brief notes on macros (luxasm/_nasm/macros):
(macro syntax is loosly based on Ada language)
[limited overview of code generated]

module / end_module
  group procedures into a module (for name
  mangling)
  [no code generated]

interface / end_interface
  specify interface for another module, define
  external labels (with name mangling)
  [generate _start code if 'start' procedure
  found, else no code generated]

procedure / end_procedure
  define the call frame and setup data for access
  (via esp) for parameters & local variables
  [no code generated, end_procedure calls 'return']

locals
  define local variables (assumes size is 4bytes,
  unless number is specified following label)
  [ generates sub esp, <n> ]

return
  cleanup stack frame & return from procedure
  (return is auto called from end_procedure)
  [ generates add esp, <n> \\ ret <n> ]

if / end_if / do / loop / ...
  standard condition constructs -- you should be
  able to guess the meaning from the name
  stack _must_ be balanced else error
  [ generates cmp <p>, <p> \\ jcc <l> ]

goto
  conditional goto (issue cmp / jcc)
  [ generates cmp <p>, <p> \\ jcc <l> or jmp <l> ]

select
  generate a jump table & jump (similar to the
  C-language 'switch'), parameters are register
  for index, default option, <value / jump to
  label><pairs> -- takes ages for nasm to
  process this macro, but generates really
  fast code :-)
  [ generates cmp lt <low> \\ jcc <default>
              cmp gt <high> \\ jcc <default>
              jmp dword [ table + <reg>*4 ]
              <jump table> ]

call
  mangle name & push params depending on call mode
  note: _will_ leave junk on stack when 'ret'
  does not cleanup stack -- ie. using the C call
  convention, stack is cleaned up on end of
  block grouping (so as to clean up multiple
  calls with a single 'add esp,<n>') -- user can
  force cleanup of junk with 'drop junk', though
  as I am using the Pascal calling convention this
  is not really an issue.)
  [ generates push \\ ... \\ push \\ call <l> ]

push / pop / pushad / ...
  modify internal varaibles so 'local' access
  points to correct place on stack (via esp).
  else same as normal push/pop
  [ generates push / pop / ... ]

drop
  remove junk / data from stack (pseudo op)
  [ generates add esp, <n>*4 ]

clr
  set register to zero (pseudo op)
  [ generates xor <reg>, <reg> ]

enumerate / end_enumerate
  define group, start & stepping for enumerations
  defined with 'enum'
  [ generates nothing ]

type / end_type
  define a struct / union -- can specify alignment
  of data types
  [ generates nothing ]

variable
  define data in the ".data" or ".bss" section
  [ generates data specified, or moves bss ptr ]

constant
  define data in the ".rodata" section
  [ generates data specified ]

assert
  check a condition is true (code only generated
  when DEBUG flag is defined, else generates
  nothing) -- if condition is false 'assertion
  failed' message is printed (used a a debugging
  aid because gdb is rubbish :-) ).
  [ generates assert call, if DEBUG flag]

review
  print a review message (contents of g/p regs)
  if REVIEW flag is set
  [ generates review call, if REVIEW flag]

That should provide a good look at what is
happening, of course this is just an overview
and several features have not been covered
(including the infamous 'stack imbalance' error,
and several bugs which the library can produce
if you are not aware of them -- primarily due
to implicit stack management) but most of the
common macros are sketched here.

-------

Again, thanks for taking the time to write that up, C.

Best,
Frank



Relevant Pages

  • Re: [LuxAsm] Wheres Cs macro documentation?
    ... I should really document them in the source to the macro ... cleanup stack frame & return from procedure ... define a struct / union -- can specify alignment ... print a review message ...
    (alt.lang.asm)
  • RFC: XCall calling convention...
    ... compiler, although what I have implemented thus far differs from this spec ... Arguments are passed on the stack in right-to-left order, ... This will attempt to specify Signature strings as applied to data types. ... reference to type ...
    (alt.lang.asm)
  • Re: input from keyboard
    ... cleanup stack frame & return from procedure ... define a struct / union -- can specify alignment ... print a review message ...
    (alt.lang.asm)
  • Re: [RFC/PATCH] RLIMIT_ARG_MAX
    ... However POSIX does not specify it must be ... and older kernels won't do it. ... then it is used as the default stack ... proposal of a new resource limit. ...
    (Linux-Kernel)
  • Re: Forth and Unix -- history
    ... grammar, for example, is something like: ... "No BNF, therefore it's no language". ... Forth has formalised stack comments to some extent in the ... without having to specify it. ...
    (comp.lang.forth)