Author Topic: Macro version of printf  (Read 106 times)

Biterider

  • Moderator
  • Member
  • *****
  • Posts: 896
  • ObjAsm Developer
    • ObjAsm
Macro version of printf
« on: June 21, 2022, 04:19:24 PM »
Hi
Recently I rewrote some older code to remove a windows dependency, mainly printf. This gave me the idea to think about whether there couldn't be a better way to do the job of printf.
Since printf's format string doesn't change in most cases, it's possible to find another approach to get a more performant result.
The idea is to find a similar way, sort of like HSE did in its PrintC macro.
The macro should parse the format string and generate the code according to the result of the parser.
MASM is ideal for this task provided we have all the necessary subroutines to get the job done.

Biterider

jj2007

  • Member
  • *****
  • Posts: 12479
  • Assembler is fun ;-)
    • MasmBasic
Re: Macro version of printf
« Reply #1 on: June 21, 2022, 07:09:40 PM »
Good idea :thumbsup:

What I find strange is that C/C++ are not able to choose the format automagically, as in print MyReal8. In MASM, you can easily write a macro that checks whether the argument is an integer, a QWORD, a REAL8 etc - see attachment (pure Masm32 SDK) :cool:

PrintC is in efiutil.inc
« Last Edit: June 22, 2022, 02:42:09 AM by jj2007 »

TimoVJL

  • Member
  • *****
  • Posts: 1001
Re: Macro version of printf
« Reply #2 on: June 22, 2022, 03:53:09 AM »
What I find strange is that C/C++ are not able to choose the format automagically,
C++ have classes for that, C don't.
But some C compilers have extension __typeof
« Last Edit: June 22, 2022, 05:03:21 PM by TimoVJL »
May the source be with you

HSE

  • Member
  • *****
  • Posts: 2088
  • AMD 7-32 / i3 10-64
Re: Macro version of printf
« Reply #3 on: June 22, 2022, 08:00:31 AM »
Hi Biterider!

The idea is to find a similar way, sort of like HSE did in its PrintC macro.

 :thumbsup: That macro is a very elemental FSM wich read a character code between "%" and space. To emulate C' prints FSM must be more complex and read several characters code between [% or \ ] and [%, \,space, "," , ";" or "."] (at least).

Regards, HSE.
Equations in Assembly: SmplMath