an introduction to assembler language and subroutine linkages / save areas

21
An Introduction to Assembler Language and Subroutine Linkages / Save Areas Ch.5 - Topic 1 See Page 95 Additional information on subroutines in Topic 1 of Chapter 8 if you wish to read ahead

Upload: adeola

Post on 11-Jan-2016

39 views

Category:

Documents


0 download

DESCRIPTION

An Introduction to Assembler Language and Subroutine Linkages / Save Areas. Ch.5 - Topic 1 See Page 95 Additional information on subroutines in Topic 1 of Chapter 8 if you wish to read ahead. General Purpose Registers. 16 GRPs – live in the Processor All programs use the same 16 GPRs - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: An Introduction to Assembler Language and Subroutine Linkages / Save Areas

An Introduction to Assembler Language andSubroutine Linkages / Save Areas

Ch.5 - Topic 1

See Page 95

Additional information on subroutines in Topic 1 of Chapter 8 if you wish to read ahead

Page 2: An Introduction to Assembler Language and Subroutine Linkages / Save Areas

General Purpose Registers

• 16 GRPs – live in the Processor• All programs use the same 16 GPRs• Each GPR is 4 bytes (1 fullword)• Some GPRs should be avoided as they are often

used by the Operating System– Reg 0 & 1 used by system to pass info– Reg 13 always points to current Savearea– Reg 14 used for Return address– Reg 15 used for Forward address

• That leaves you 11 GPRs for your use

Page 3: An Introduction to Assembler Language and Subroutine Linkages / Save Areas

Subroutines

• Program within a program• Internal or External

– Internal: Assembled with the Main Program– External: Assembled separately from the Main Program – introduces some

interesting communication problems

• Requires Housekeeping– All routines must provide a

Register Save Area– All routines should adhere to linkage conventions

Page 4: An Introduction to Assembler Language and Subroutine Linkages / Save Areas

Main Program

SubRoutine

Register SaveArea

LA 13,SAVEAREABALR 14,15

SUBRTN SAVE (14,12) ST 13,SAVEA+4 LA 13,SAVEA --

Register SaveA

--L 13,SAVEA+4

RETURN (14,12)

Page 5: An Introduction to Assembler Language and Subroutine Linkages / Save Areas
Page 6: An Introduction to Assembler Language and Subroutine Linkages / Save Areas

START 0

• Does not create object code• Assembler Command• Signals beginning of source code• Establishes what should be the beginning location of the

program• If labeled, names the program

• Your program will be loaded into memory for its execution at location X’200’

Page 7: An Introduction to Assembler Language and Subroutine Linkages / Save Areas

Program Entry

Page 8: An Introduction to Assembler Language and Subroutine Linkages / Save Areas

REGS

• Creates names for the GPRs – Names appear in CROSS-REF list, numbers do not.

• Generates no Object Code

Page 9: An Introduction to Assembler Language and Subroutine Linkages / Save Areas

BEGIN BEGIN

• Performs what SAVE macro does• Names program (if not in START)• Provides a SaveArea

Page 10: An Introduction to Assembler Language and Subroutine Linkages / Save Areas

USING *,12

• Generates no object code• Establishes a Base Register• 1st Operand indicates the address -

* = address of next instruction• 2nd Operand identifies which register to use for Base Reg• Normally (not always) goes with:

• BALR example loads the address of the next instruction into R12, then would Branch to the location in the 2nd operand register – unless it is zero, then it does not Branch. Since USING establishes the Base register and BALR is first executable instruction in your program, your program is 2 bytes (length of BALR) different from load point of your program.

BALR 12,0

Page 11: An Introduction to Assembler Language and Subroutine Linkages / Save Areas

BAL & BALR

• Branch & Link (4-byte & 2-byte)• Places address of the next instruction into the 1st operand (a

Return Address?)• Then branches to the address in the 2nd operand (Entry to a

Subroutine?) unless the second operand is zero, then it doesn’t branch.

BALR 12,0USING *,12

Together BALR and USING establish addressability in your program. They need to appear in your program before the first symbolic reference used, which should be the next few instructions (not shown) which saves the calling programs registers into its savearea and makes your savearea current.

Page 12: An Introduction to Assembler Language and Subroutine Linkages / Save Areas

BAL & BALR

Calling Program

SubRoutine

LA 10,SUBRTNBALR 14,10

START 0REGS

BEGIN BEGINSAVE (14,12)BALR 12,0USING *,12

LA 14,SAVE+4RETURN (14,12)BR 14

SAVEAREA DS 18FSAVEAREA DS 18F

SAVE macro is changed into a STM 14,12,12(13) instruction. Recall, it saves the calling programs registers. The next executable instruction is BALR to establish addressability in your program.

Page 13: An Introduction to Assembler Language and Subroutine Linkages / Save Areas

SAVE

• A standard Macro Instruction• Generates multiple Assembler instructions• If used, it follows START (or CSECT)• Almost always coded as:

SAVE (14,12)

Page 14: An Introduction to Assembler Language and Subroutine Linkages / Save Areas

RETURN

• A standard Macro Instruction• Acts as the back end of SAVE - -• Restores registers that were SAVE’d and branches back to the

calling program

L 13,SAVEAREA+4RETURN (14,12)

Page 15: An Introduction to Assembler Language and Subroutine Linkages / Save Areas

LOAD (L, LR)

• 4-byte or 2-byte instruction• Takes contents of 2nd operand and places it as contents of the

1st operand• In both formats, 1st operand is a GPR

18 R1 R2

58 R1 X2

LR

L D2B2

Page 16: An Introduction to Assembler Language and Subroutine Linkages / Save Areas

LOAD (L,LR)

GPR 3 DIVISOR

BEFORE: 00 00 00 00 00 00 01 5C

AFTER: 00 00 01 5C 00 00 01 5C

L 3,DIVISOR

Load is used to move 4-bytes from memory into a register. Register instructions execute faster since they are located closer to the processor than memory. This is especially useful if the same value is to be used multiple times in a program.

Page 17: An Introduction to Assembler Language and Subroutine Linkages / Save Areas

LOAD ADDRESS (LA)

• 4-byte format only• Takes the address of the 2nd operand (rather than the contents)

and makes it the contents of the 1st operand

LA 13,SAVEAREA

The location (not the contents of the first word) is loaded into register 13

Page 18: An Introduction to Assembler Language and Subroutine Linkages / Save Areas

STORE (ST)

• 4-byte format• Takes the contents of the 1st operand and makes it the contents

of the 2nd operand• Reverse of LOAD

ST 13,SAVEAREA+4

The content of register 13 is moved to memory into SAVEAREA+4 in the program (a fullword). Remember that Load moved a fullword in the other direction – from memory into the register. There is no STORE instruction in the RR-format – A STR instruction would do the exact same thing as the LR instruction, if there was such an instruction as STR.

Page 19: An Introduction to Assembler Language and Subroutine Linkages / Save Areas

Extended Logical Branching

B

BH

BL

BE

BNH

BNL

BNE

Unconditional

Branch if 1st operand High

Branch if 1st operand Low

Branch if both operands Equal

Branch if 1st operand Not High

Branch if 1st operand Not Low

Branch of operands Not Equal

See page 114 - top

The contents of the first operand is compared to the contents of the second operand and the Branch is taken based on the results of the compare. If condition is not true, then the Branch is not taken.

Page 20: An Introduction to Assembler Language and Subroutine Linkages / Save Areas

END

• Assembler Command• Generates no instruction• Opposite of START (or CSECT)• Simply tells the Assembler there are no more source

statements to decode

Page 21: An Introduction to Assembler Language and Subroutine Linkages / Save Areas

The End