0

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
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189

1 Answers1

1

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?

turboscrew
  • 676
  • 4
  • 13
  • @turboscew thank you for explanation! So basically anywhere there's `pop %r1` I expand it then? For example there's in the start of the program scope, after `.org 2048`? Also i'm not sure where you see it's asking for "excess" instructions – pointing_Stack Oct 29 '14 at 15:42
  • 1
    Yes, everywhere you see 'pop %r1' mark it and paste the definition in place to replace it. Then chenge all 'arg1's with '%r1's. And sorry, not 'excess' but 'unnecessary'. – turboscrew Oct 29 '14 at 19:16
  • thank you! Just to make sure we don't need to bother expanding the `push`, right? – pointing_Stack Oct 29 '14 at 20:20
  • 1
    You can do it. Then you could look at the two expansions one written afret another and figure out the unnecessary instructions. the progran is: .org 2048 pop %r1 push %r2 Replace for pop %r1 then for push %r2 and see what the program becomes like. – turboscrew Oct 29 '14 at 21:57