




















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





















This lecture will focus on writing macros , and use stack handling as an example of macro use. Macros differ from standard subroutines and functions. Functions and subroutines represent separate blocks of code to which control can be transferred. Linkage is achieved by management of a return address , which is managed in various ways. A macro represents code that is automatically generated by the assembler and inserted into the source code. Macros are less efficient in terms of code space; each invocation of the macro will generate a copy of the code. Macros are more efficient in terms of run time; they lack the overhead associated with subroutine call and return. Before discussing macros, let’s discuss an application.
The first thing we note is that the IBM 370 supports neither in native mode. A stack is a LIFO (Last–In / First–Out) data structure with three basic operations: PUSH places an item onto the stack, POP removes an item from the stack INIT initializes the stack. A heap is a dynamic structure used by a RTS (Run–Time System) to allocate memory in response to object creators, such as New. A modern RTS will allocate an area of memory for use by both the stack and the heap. By convention in system design: The stack starts at high memory addresses and moves toward lower addresses. The heap starts at low memory addresses and moves toward higher addresses. NOTE: IBM has macros called “PUSH” and “POP”, associated with handling print output. We must pick other names for our stack macros.
Our goal in this lecture is to examine the basic stack structure, and its implementation using macros. Our implementation will use a fixed–size array to hold the stack. The stack will grow towards higher addresses. The stack pointer will point to the location into which the next item will be pushed. PUSH STACK[SP] = ITEM SP = SP + 1 // Moves toward higher addresses POP SP = SP – 1 ITEM = STACK[SP] This non–standard approach is easier for me to code.
Here we push four integers, one after the other. We then pop the values. Push onto the stack Pop from the stack: note the order is reversed.
The MACRO definitions should occur very early in the program text. Only comments and assembler control directives may precede a MACRO definition. This commonly includes the PRINT directive. A MACRO begins with the key word MACRO, includes a prototype and a macro body, and ends with the trailer keyword MEND. Parameters to a MACRO are prefixed by the ampersand “&”. Here is an example. Header MACRO Prototype DIVID ",&DIVIDEND,&DIVISOR Model Statements ZAP &QOUT,&DIVIDEND DP ",&DIVISOR Trailer MEND Note that the header and trailer must appear as is. Each of the terms “MACRO” and “MEND” begin in column 10. Nothing else is allowed on either line.
In assembly language, a macro is a single statement that causes the assembler to emit a sequence of other statements specified by the macro definition. Consider the above example, with prototype DIVID ",&DIVIDEND,&DIVISOR. The macro body is ZAP &QOUT,&DIVIDEND DP ",&DIVISOR Here is an example of the macro expansion. We assume that the labels used as “parameters” have been properly defined by DS or DC statements. DIVID MPG,MILES,GALS MACRO INSTRUCTION
The macro prototype contains a list of symbolic parameters. Each symbolic parameter is written as follows:
It might appear that a macro invocation cannot be the target of a branch instruction. Here is some of my early code. I had defined a macro, STKPOP, in the proper place. It was used by a routine, called DOFACT, to be discussed later. I tried the following code: B DOFACT CALL THE FACTORIAL CODE Here is the branch target. DOFACT STKPOP 4 POP THE ARGUMENT INTO R STKPOP 8 POP THE RETURN ADDRESS BR 8 BRANCH TO RETURN ADDRESS That did not assemble. The complaint was that the symbol DOFACT was not defined. What happened? The label was clearly there in the source code.
In order to solve the above problem, we need to focus on a more precise statement of the form of a macro definition. We must focus on the prototype and body. The general form of a prototype statement is as follows. Symbolic Name Name of macro Zero or more symbolic parameters If the symbolic name is to be used, it has the form of a symbolic parameter. If the symbolic name is to be used, it must be duplicated on the first line of the body. Here is an example, using the DIVID macro. MACRO &LABEL DIVID ",&DIVIDEND,&DIVISOR &LABEL ZAP &QOUT,&DIVIDEND DP ",&DIVISOR MEND Note that the symbolic parameter “&LABEL” is treated as any other such parameter.
Note that each of the labels B10DIV and B20DIV now appears in the expanded code and can be used as a branch target address.
The stack is implemented as an array of full words, with two auxiliary counters. There is a halfword that counts the number of items on the stack. There is a halfword constant that gives the maximum stack capacity. This is not changed by the code. There is the fixed–size array that holds the stack elements. Here is the declaration of the stack. STKCOUNT DC H’0’ NUMBER OF ITEMS STORED ON STACK STKSIZE DC H’64’ MAXIMUM STACK CAPACITY THESTACK DC 64F’0’ THE STACK HOLDS 64 FULLWORDS Note that the elements are full–words while the addresses are byte addresses. The elements of the stack will be stored at the following addresses. THESTACK, THESTACK + 4, THESTACK + 8, THESTACK + 12 up to a full word starting at THESTACK + 252.
Here is the macro that initializes the stack. *STKINIT MACRO &L1 STKINIT &L1 SR 4,4 CLEAR R4 – SUBTRACT FROM SELF STH 4,STKCOUNT STORE AS THE STACK COUNT MEND
Note the standard trick of clearing a register by subtracting it from itself. The register exists only for the purpose of placing a 0 into the stack count. Following standard practice, the contents of the stack are not changed, because the elements of interest will be overwritten before they are used. Note that this macro does not have any symbolic parameters.
Here is the macro STKPOP *STKPOP MACRO &L3 STKPOP &R &L3 LH 3,STKCOUNT GET THE STACK COUNT SH 3,=H’1’ SUBTRACT 1 WORD OFFSET OF TOP STH 3,STKCOUNT STORE AS NEW SIZE SLA 3,2 BYTE OFFSET OF STACK TOP LA 2,THESTACK ADDRESS OF STACK BASE L &R,0(3,2) LOAD ITEM INTO THE REGISTER MEND
Again, this macro has one symbolic parameter: &R. Again, a register number. When called as STKPOP 6, this is assembled with the last statement as L 6,0(3,2). NOTE: When invoked as STKPOP MYDOG, this will assemble as L MYDOG,0(3,2); the assembler takes anything.
Here is the part of the unexpanded source code that uses the macros. STARTUP OPEN (FILEIN,(INPUT)) OPEN THE STANDARD INPUT OPEN (PRINTER,(OUTPUT)) OPEN THE STANDARD OUTPUT PUT PRINTER,PRHEAD PRINT HEADER STKINIT INITIALIZE THE STACK GET FILEIN,RECORDIN GET THE FIRST RECORD, IF THERE
A10LOOP MVC DATAPR,RECORDIN MOVE INPUT RECORD PUT PRINTER,PRINT PRINT THE RECORD PACK PACKIN,FIELD01 CONVERT DIGITS INPUT TO PACKED CVB R4,PACKIN CONVERT THE NUMBER TO BINARY STKPUSH 4 PUSH THE NUMBER ONTO THE STACK GET FILEIN,RECORDIN GET THE NEXT RECORD B A10LOOP GO BACK AND PROCESS
Here, it is obvious that I have retained register R4 for communicating results with macros and subroutines. That is an arbitrary choice.