Search code examples
assemblymacrosgnu-assemblerpreprocessor-directive

GNU Assembler .print expression instead of string


According this https://sourceware.org/binutils/docs/as/Print.html#Print, I only can print string instead of expression.

I tried this one line assembler

.print "Hello" //No Error
.print 1+2 //Error: missing string

How do I make assembler recognize that expression 1+2 is a string? I expect it will print:

Hello
3

UPDATE: I found something strange after using .altmacro https://sourceware.org/binutils/docs/as/Altmacro.html#Altmacro

.altmacro

.macro myMacro
  LOCAL myLabel
  .set myLabel, 1+2
  .print "myLabel"
.endm

myMacro

It said I can use %expr: You can write ‘%expr’ to evaluate the expression expr and use the result as a string.

But my console shown:

.LL0001

Solution

  • According your both refference:

    • Enable .altmacro which has more feature than legacy as such as LOCAL labeling.

    • Define macro with name .printPlusPlus, name inspired from C++ that upgraded version of C but this context is version of .print

    • Since .print only accept double quote string then put the parameter a to the double-quoted argument.

    • Use pair of angle bracket <my string> for print string, use %(expr) to print string expression. Note, you must use escaped character hexadecimal to use hell character like double-quoted. \x22 means Character number 22 in hexadecimal form in ascii table which it's a ".

    Usage

    .altmacro
    
    .macro .printPlusPlus a
      .print "\a"
    .endm
    
    .printPlusPlus <\x22\Hello World\x22>
    .printPlusPlus %(1 +2)
    

    Output

    $ as temp.m
    "Hello World"
    3
    

    EDIT

    .altmacro
    
    .macro .printPlusPlus a
      .print "\a"
    .endm
    
    .macro summation a,b
      .printPlusPlus %(\a + \b)
    .endm
    
    summation 3,4 //will show 7, i just tested it works fine