39
System z Assembler z/OS Lehrgang 2008/09 Prof. Dr.-Ing. Wilhelm G. Spruth Teil 2 Linkage Convention

System z Assembler - cedix.de

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: System z Assembler - cedix.de

System z Assembler

z/OS Lehrgang 2008/09

Prof. Dr.-Ing. Wilhelm G. Spruth

Teil 2 Linkage Convention

Page 2: System z Assembler - cedix.de

Branch and Link Register Precision, Power PC, MIPS und S/390 verzichten auf Stack (Implementierung in Software) und verwenden “Branch and Link” Mechanismus, z.B. S/390. BALR R14, R15 (R15 enthält Unterprogramm-Adresse) R14 PC (Program Counter) PC R15 Rückkehr erfolgt mit BR R14 PC R14 Bei verschachtelten (nested) Unterprogramm-Aufrufen muß das Unterprogramm den Inhalt von R14 zwischen-speichern. Eine “SAVE” Area im Hauptspeicher wird an Stelle eines Stacks für die Zwischenspeicherung von Rücksprung-adressen verwendet. Jedes Unterprogramm verwaltet seine eigene SAVE Area. PC = Befehlszähler =Program Counter

Page 3: System z Assembler - cedix.de

Status Register Befehlszähler (PC)

Programm Status Wort

1 2 R0 R1

BALR R14, R15 (R15 enthält Unterprogramm-Adresse)

R14 Befehlszähler (PC) PC R15

R2 R3 R4 R5 R6 R7 T8 R9 R10 R11 R12 R13 R14 R15

Branch and Link Register Befehl BALR

Page 4: System z Assembler - cedix.de

Das aufgerufene Programm ist

verantwortlich, dass der Inhalt aller Register

unverändert zurückgegeben wird.

Page 5: System z Assembler - cedix.de

Wird Sub1 mit BAL R14,.. oder mit BALR R14,.. aufgerufen. so steht die Return Adresse für den späteren Rücksprung nach Main in R14. Sub1 darf R14 nicht überschreiben. Ruft Sub1 irgendwann Sub2 auf, dann muß vorher der Inhalt von R14 in der Sub1 SAVE Area gerettet werden.

Main Sub1 Sub2 BAL BAL BR BR Save Save Save Main Sub1 Sub2

S/390 Linkage Convntion

Page 6: System z Assembler - cedix.de

Mehrzweck Register calling 0 Program BALR 14,15 1 Parameter List Pointer called call Program 2

return 13 Save Area Pointer 14 Return Addresse 15 Calling Adresse Save Area

S/390 Unterprogramm

Aufruf

Page 7: System z Assembler - cedix.de

Load Multiple Instruction

LM has three operands:

• The first operand is the first in a group of consecutive target registers. • The second operand specifies the last register in the group • The third operand specifies the source main storage address

Page 8: System z Assembler - cedix.de

Load Multiple Instruction This operation could be performed by a number of load instructions, each loading a single fullword into a register one at a time. However the Load Multiple (LM) instruction is provided to facilitate this fairly common requirement by a single instruction. Using the LM instruction, a number of consecutive fullwords from main storage are loaded into a range of GPRs, from the GPR specified as the first operand to the GPR specified as the second Operand, both inclusive. One interesting feature of Load Multiple (LM) is that if the second operand specifies a lower numbered register than the first operand, this indicates a range of registers from R14 to R15and from R0 to R12. Thus LM R14,R12,SAVE loads 15 registers; R14, R15, R0, R1….R12 in that order, from the 15 consecutive fullwordsbeginning at SAVE

Page 9: System z Assembler - cedix.de

Load Multiple (LM) Instruction For example: LM R5,R7,FWDS When the instruction executes, it loads a fullword from main storage at location address “FWDS” to the GPRs beginning with R5, the next fullword in storage (i.e. at location FWDS+4) would be loaded into R6 and the next (at location FWDS+8) into R7.

Page 10: System z Assembler - cedix.de

Store Multiple Instruction Store Multiple (STM) is a RS format (register to Storage) instruction. The STM instruction performs the opposite operation of the LM instruction.

Store Multiple Instruction STM has three operands:

Page 11: System z Assembler - cedix.de

•The first operand specifies the starting register number•The second operand specifies the last register in the group•The third operand specifies the target main storage address The contents of several GPRs, from the GPR specified as the first operand to the GPR specified as the second operand, are stored into consecutive fullwords in main storage. For example: STM R14,R12,SAVE This STM instruction stores 15 registers, R14, R15, R0, R1....,R12 in that order to the 15 consecutive fullwords beginning at SAVE.

Page 12: System z Assembler - cedix.de

SAVE Area des SAVE Area des aufrufenden aufgerufenen Programm Programm 68

64 60 56 52 48 44 40 36 32 28 24 20 16 12 08 04 00 SAVE Adresse steht in Reg. 13

Linkage Convention Es ist die Aufgabe der aufgerufenen Routine, die Mehrzweckregister der aufrufenden Routine in deren SAVE Area abzuspeichern. Die Adresse dieser SAVE Area steht in Register 13.

Page 13: System z Assembler - cedix.de

Macro Macro instructions are useful because of the following reasons:

• You can create your own macro instructions to decrease the tedium of coding common instruction sequences multiple times.

• It provides a common method of performing a certain action.

• The macro facility in Assembler Language is a powerful one. It allows you to create new language statements by defining them in terms of existing statements.

Page 14: System z Assembler - cedix.de

LINKAGE CONVENTIONS Dies sind Folgen von Befehlen, die eine Verzweigung in Unterprogramme bzw. die Rückkehr aus einem Unterprogramm bewirken. Linkage Conventions sind nicht zwingend vorgeschrieben. Es sind Vereinbarungen, die von allen Systemprogrammen des Herstellers, von Sprachübersetzern, sowie - bei einem Minimum von Disziplin - von allen Assembler - Anwendungsprogrammierern eingehalten werden.

wgs 12-87

Page 15: System z Assembler - cedix.de

Adresse FF ... FF Sub 2 Adresse ZZZZ Sub 1 Adresse YYYY main Adresse XXXX Überwacher Adresse 00 ... 00

Basis Register Das Assembler Directive using *,R12 spezifiziert das Basis Register. Jedes Load Module enthält eine Tabelle mit externen Referenzen, die vom Loader aufgelöst werden.

Page 16: System z Assembler - cedix.de

Assembler Directive What is the USING Instruction?

In order for the Assembler to convert addresses to base-displacement format, you must tell the Assembler what register to use as a base register, and what value the register will contain at execution time. This information is provided with the USING instruction. You must also include machine instructions in your program, to load the proper value into the base register at execution time. A standard way of accomplishing these two actions is with the following instructions, included as part of the program initiation:

BALR R12,R0 USING *, R12

Page 17: System z Assembler - cedix.de

Converting Addresses to Base-Displacement Format

The first of these instructions, a Machine instruction, places the absolute address of the next instruction after BALR into register 12. The * symbol is used to represent the current value of the location counter in an Assembler program. The USING instruction tells the Assembler that R12 is to be used as the base register, and that R12 will contain the address of the current instruction. Since BALR has in fact loaded R12 with the address of the instruction following BALR (which is now the current instruction) this correctly establishes addressability in the

Page 18: System z Assembler - cedix.de

X CSECT ESTABLISH NAME OF PROGRAM

STM 14,12,12(13) SAVE REGISTERS 14,15,0,1,2,...,12 BALR 12,0 ESTABLISH BASE ADDRESS IN REG 12 USING *,12 ST 13,SAVE+4 SAVE CALLER'S SAVE AREA ADDRESS LA 13,SAVE LOAD OWN SAVE AREA ADDRESS

L 13,SAVE+4 RESTORE CALLER'S SAVE AREA ADDRESS

Hierhin kommt der Code unseres aufgerufenen Programms. Wenn wir fertig sind, dann :

LM 14,12,12(13) RESTORE CALLER'S REGS 14,15,0,...,12 BR 14 BRANCH TO ADDRESS GIVEN IN REG 14

In Ergänzung zu diesen Befehlen kommt die Definition der SAVE Area:

Save DS 18F RESERVE 18 CONSECUTIVE FULL WORDS

Standard S/390 Program Linkage

Page 19: System z Assembler - cedix.de

SAMPLE CSECT * * THIS PROGRAM ADDS TWO INTEGERS X AND Y. THE SUM IS Z. * STM 14,12,12(13) STANDARD ENTRY BALR 12,0 USING *,12 Beispiel Programm

Standard Linkage Convention

ST 13,SAVE+4 LA 13,SAVE * * COMPUTE Z = X + Y * L 3,X GET x A 3,Y ADD Y TO x ST 3,Z LET Z be THE SUM * *EXIT FROM PROGRAM * L 13,SAVE+4 STANDARD EXIT LM 14,12,12(13) BR 14 * * DATA * X DC F'3' X IS ADDRESS OF +3 Y DC F'4' Y IS ADDRESS OF +4 Z DS F Z IS AnDRESS OF SUM * SAVE DS 18F END

Page 20: System z Assembler - cedix.de
Page 21: System z Assembler - cedix.de

Linkage Convention für den Aufruf eines Unterprogramms

Page 22: System z Assembler - cedix.de

Die ersten Statements in einem Assembler Quelltext

Die letzten Statements in einem

Assembler Quelltext

Page 23: System z Assembler - cedix.de

Der Programmierer muss die Basis Adresse und des entsprechenden Basis Registers mit Hilfe eines Assembler Directives spezifizieren. Diese Spezifikation hat die Form Op Code Operand USING A, R Hierbei ist A die Basis Adresse und R das entsprechende Basis Register. Beispiel: Die Standard Form des Using Directives ist: Using *,12 Dies informiert den Assembler, dass alle folgenden RX, RS, and SS Maschinenbefehle das Register 12 als Basis Register benutzen.

Page 24: System z Assembler - cedix.de
Page 25: System z Assembler - cedix.de
Page 26: System z Assembler - cedix.de
Page 27: System z Assembler - cedix.de

Nehmen wir an, der mit SAVE bezeichnete Speicherbereich (häufig als Save Area bezeichnet) ist 72 Byte oder 18 Worte (full Words) groß. Am Ende des Assembler Programms würde der Speicherbereich mit einem Assembler Directive Save DS 18F deklariert und reseviert werden. Nehmen wir an, der Assembler plaziert den mit Save bezeichneten Bereich auf 18 aufeinanderfolgende Worte beginnend mit der hexadezimalen Adresse 500, also auf die Adressen 0x500 – 0x512 0x bedeutet: dies ist eine hexadezimale Zahl. Der Befehl ST 13,Save+4 ist ein 32 Bit langer RX Format Befehl

8 4 4 4 12 Bit

50 D 0 C 4 FE Op Code für ST Displacement Register 13 Register 0 Register 12 (Base Register) Der ST Befehl speichert den Inhalt von Register 13 auf der Adresse 0x504 ab (Save + 4). Wird Register 0 für die Berechnung der effektiven adresse verwendet, gibt es immer den wert 0 wieder.

Page 28: System z Assembler - cedix.de

0x508 0x504 Save+4 0x500 Save 0x4FC 0x4F8 Die Adresse von Save + 4 wird bebildet durch die Summe des Inhalts von Register 12 plus den Inhalt des Displacement Feldes. Der Inhalt von Register 12 ist gleich 0x006. Der Assembler lädt in das Displacement Feld des ST Befehls den Wert 0x4FE. Denn: 0c4FE + 0x006 = 0x504

Page 29: System z Assembler - cedix.de

Einfaches S/390 Assembler Programm * Formalismus fuer Assembler-Code: * Angaben in den Stellen 1 - 71, Fortsetzungszeichen '*' in Stelle 72, * Optional: Nummerierung in Stellen 73 - 80 * Kommentar-Zeile: '*' in Stelle 1 gefolgt vom Kommentar bis Stelle 71 * Assemblercode-Zeile: Optional: Referenzname in 1 - 8 * Zwingend: Operation ab 10 * Optional: Parameter mit mind. 1 Blank hinter * Optional: Kommentar mit mind. 1 Blank hinter * Parametern bis 71 see Tutorial 2A

Page 30: System z Assembler - cedix.de

SAVEREG Makro: Einstieg und Retten Register OPEN (PRINTOUT,(OUTPUT)) Makro: Open DCB PRINTOUT PUT PRINTOUT,PRINTREC Makro: Schreibe Text CLOSE PRINTOUT Makro: Close DCB PRINTOUT * Bei Ende des Programms wird vom System der Inhalt von Register 15 * als Return-Code verwendet, der in der JCL als Condition-Code * abgefragt werden kann. SR R15,R15 Subtract Register --> R15 = 0 * ---> Fehlerfreies Ende B ENDE Verzweige unbedingt nach ENDE ENDE EQU * Bezeichnet die Programmstelle fuer den Assembler * mit Namen EXITREG Makro: Laden der Register mit dem Inhalt zur * Zeit des Programmbeginns und Ruecksprung * zum System ***** Definitionen DC = Declare Constant PRINTREC DC Cl80'Hallo Welt, unser erstes TSO-Programm in ASSEMBLER' PRINTOUT DCB DDNAME=SYSPRINT, Makro: Data Control Block * DSORG=PS, * MACRF=(PM), * LRECL=80 END

Page 31: System z Assembler - cedix.de

S/390 Assembler Programm mit Ein- und Ausgabe

SAVEREG Makro: Einstieg und Retten Register OPEN (READIN,(INPUT),PRINTOUT,(OUTPUT)) PUT PRINTOUT,PRINTQST GET READIN,WORKAREA MVC ANSWRBUF(6),=CL5'Hallo ' MVC ANSWRBUF+6(30),WORKAREA kopiere erste 30 Zeichen PUT PRINTOUT,ANSWRBUF CLOSE (READIN,,PRINTOUT) * Bei Ende des Programms wird vom System der Inhalt von Register 15 * als Return-Code verwendet, der in der JCL als Condition-Code * abgefragt werden kann. SR R15,R15 Subtract Register --> R15 = 0 * ---> Fehlerfreies Ende B ENDE Verzweige unbedingt nach ENDE ENDE EQU * Bezeichnet die Programmstelle fuer den Assembler * mit Namen EXITREG Makro: Laden der Register mit dem Inhalt zur * Zeit des Programmbeginns und Ruecksprung * zum System

Page 32: System z Assembler - cedix.de

***** Definitionen DC = Declare Constant PRINTQST DC Cl80'Hallo, wie ist dein Name?' * DS = Declare Storage WORKAREA DS CL80 ANSWRBUF DS CL80 * Output DCB PRINTOUT DCB DDNAME=SYSPRINT, Makro: Data Control Block * DSORG=PS, * MACRF=(PM), * LRECL=80 * Input DCB READIN DCB DDNAME=SYSIN, * DSORG=PS, * MACRF=(GM), * LRECL=80 END

Page 33: System z Assembler - cedix.de

Struktur eines Adressenraums, in den ein ausführungsfähiges Programm geladen wurde.

Page 34: System z Assembler - cedix.de

R13 EQU 13 R14 EQU 14 R15 EQU 15 STM R14,R12,12(R13) LR R12,R15 USING FIRST,R12 ST R13,SAVE+4 LR R15,R13 LA R13,SAVE ST R13,8(R15) LR R2,R1 B ANFANG DC CL16'SAVEAREA' SAVE DC 18F'0' ANFANG EQU * MEND

MACRO SAVEREG FIRST EQU * R0 EQU 0 R1 EQU 1 R2 EQU 2 R3 EQU 3 R4 EQU 4 R5 EQU 5 R6 EQU 6 R7 EQU 7 R8 EQU 8 R9 EQU 9 R10 EQU 10 R11 EQU 11 R12 EQU 12

Makro SAVEREG

Page 35: System z Assembler - cedix.de

MACRO EXITREG L R13,SAVE+4 L R14,12(R13) LM R0,R12,20(R13) BR R14 MEND

Makro EXITREG

Page 36: System z Assembler - cedix.de

What is a DCB ?What is a DCB ?A file definition, called a Data Control Block (DCB) must be created for each file the Assembler programmer wishes to access in a program. The DCB is a special kind of data area, which maintains information about an external file. The DCB is created using a system macro instruction. The DCB contains the following:

• Name used to refer to the data

Page 37: System z Assembler - cedix.de

• The format and size of the records and blocks within the dataset • Types of instructions used to access the dataset and the current status of the dataset The DCB

macro instruction generates the various data areas and constants necessary to maintain information about an external file. The Assembler program only has to know how to handle I/O at a high level. The expansion of the macro instruction into multiple low level instructions generates the detailed level code.

Page 38: System z Assembler - cedix.de

Mehrfache Base Register

Page 39: System z Assembler - cedix.de

Masch. RR RX RX RX SI SS RS Befehl

R ← R R ← C R ← H R ← F S ←IMM S ← S LOAD LR IC LH L MVI MVC LM STORE STC STH ST STM ADD AR AH A SUB SR SH S MPY MR MH M DIV DR DH D AND NR N NI NC OR OR O OI OC XOR XR X XI XC SHIFT L SLL SHIFT R SRL COMPARE CR CH C CLI CLC TEST TM TRT CLM

Maschinenbefehle Übersicht