assemblymacrossparc

How to expand the macro definitions and identify the unnecessary instructions in ARC Assembly?


This is such a tidiest topic so far while learning this assembly. The book doesn't give good examples, so I'm totally lost as to how am I supposed to expand this macro, perhaps within a macro? Here's what it asks: Given two macros push and pop as defined below, unnecessary instructions can be inserted into a program if a push immediately follows a pop. Need to expand the macro definitions shown below and identify the unnecessary instructions. I will be SO grateful for good explanations, I want to understand how this should be expanded.

.begin 
.macro push arg1 
addcc %r14, -4, %r14 
st arg1, [%r14] 
.endmacro 
.macro pop arg1 
ld [%r14], arg1 
addcc %r14, 4, %r14 
.endmacro 
! Start of program 
.org 2048 
pop %r1 
push %r2 
. 
. 
. 
.end

Solution

  • Macros are usually "text-replacements".

    That is:

    pop %r1 
    

    expands as (is replaced with):

    ld [%r14], arg1 
    addcc %r14, 4, %r14 
    

    Where arg1 (macro parameter) is replaced with the parameter given in the 'call': arg1 -> %r1

    So it finally becomes:

    ld [%r14], %r1
    addcc %r14, 4, %r14
    

    "The rest is left to the reader as an excercise." ;-)

    I'm not sure what is meant by the "excess" instructions. Maybe the stack space handling? Or NOPs?