Docsity

Dynamic Memory: Stacks and Heaps - Notes | CPSC 3121, Study notes of Programming Languages

Toggle sidebar
bg1
Writing Macros
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.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c

Partial preview of the text

Download Dynamic Memory: Stacks and Heaps - Notes | CPSC 3121 and more Study notes Programming Languages in PDF only on Docsity!

Writing Macros

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.

Dynamic Memory: Stacks and Heaps

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 Stack Implementation

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.

A Stack Example

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.

Mechanics of Writing Macros

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 &QUOT,&DIVIDEND,&DIVISOR Model Statements ZAP &QOUT,&DIVIDEND DP &QUOT,&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.

Example of Macro Expansion

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 &QUOT,&DIVIDEND,&DIVISOR. The macro body is ZAP &QOUT,&DIVIDEND DP &QUOT,&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

  • ZAP MPG,MILES ITS EXPANSION
  • DP MPG,GALS

Symbolic Parameters

The macro prototype contains a list of symbolic parameters. Each symbolic parameter is written as follows:

  1. The name begins with an ampersand (&).
  2. The ampersand is followed by one to seven alphanumeric characters, the first of which must be a letter.
  3. Put another way, the first character of the name must be a “&” and the second character of the name must be a letter. Note that this “seven character” rule limits the total length of the symbolic parameter to eight characters: the “&” and the 1 – 7 others. According to the IBM HLASM reference manual, “Symbolic parameters have a local scope; that is, the name and value they are assigned only applies to the macro definition in which they have been declared”. Page 251, High Level Assembler for z/OS & z/VM & z/VSE Language Reference Manual, Release 6 (July 2008), SC26–4940–

A Potential Problem with Macros.

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.

The Solution to the Branch Target Problem

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 &QUOT,&DIVIDEND,&DIVISOR &LABEL ZAP &QOUT,&DIVIDEND DP &QUOT,&DIVISOR MEND Note that the symbolic parameter “&LABEL” is treated as any other such parameter.

Code Example to Illustrate the Solution

MACRO

&LABEL DIVID &QUOT,&DIVIDEND,&DIVISOR

&LABEL ZAP &QOUT,&DIVIDEND

DP &QUOT,&DIVISOR

MEND

B10DIV DIVID X,Y,Z

+B10DIV ZAP X,Y

DP X,Z

B20DIV DIVID A,B,C

+B20DIV ZAP A,B

DP A,C

Note that each of the labels B10DIV and B20DIV now appears in the expanded code and can be used as a branch target address.

Our Stack Data Structure

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.

Initialize the Stack

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.

POP: Removing Items From the Stack

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.

Using the Macros

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

  • READ AND PROCESS LOOP

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.