System Programming Notes
System Programming Notes
System Programming
SYSTEM SOFTWARE
OBJECTIVES
• To understand the relationship between system software and machine architecture.
• To know the design and implementation of assemblers
• To know the design and implementation of linkers and loaders.
• To have an understanding of macroprocessors.
• To have an understanding of system software tools.
UNIT I INTRODUCTION 8
System software and machine architecture – The Simplified Instructional Computer (SIC) - Machine
architecture - Data and instruction formats - addressing modes - instruction sets - I/O and programming.
UNIT II ASSEMBLERS 10
Basic assembler functions - A simple SIC assembler – Assembler algorithm and data structures - Machine
dependent assembler features - Instruction formats and addressing modes – Program relocation - Machine
independent assembler features - Literals – Symbol-defining statements – Expressions - One pass assemblers
and Multi pass assemblers - Implementation example - MASM assembler.
1
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
UNIT I
INTRODUCTION TO SYSTEM SOFTWARE AND MACHINE STRUCTURE
• System software consists of a variety of programs that support the operation of a computer.
• It is a set of programs to perform a variety of system functions as file editing, resource management, I/O
management and storage management.
• The characteristic in which system software differs from application software is machine dependency.
• An application program is primarily concerned with the solution of some problem, using the computer
as a tool.
• System programs on the other hand are intended to support the operation and use of the computer itself,
rather than any particular application.
• For this reason, they are usually related to the architecture of the machine on which they are run.
• For example, assemblers translate mnemonic instructions into machine code. The instruction formats,
addressing modes are of direct concern in assembler design.
• There are some aspects of system software that do not directly depend upon the type of computing
system being supported. These are known as machine-independent features.
• For example, the general design and logic of an assembler is basically the same on most computers.
OPERATING SYSTEM
• It is the most important system program that act as an interface between the users and the system. It
makes the computer easier to use.
• It provides an interface that is more user-friendly than the underlying hardware.
• The functions of OS are:
1. Process management
2. Memory management
3. Resource management
4. I/O operations
5. Data management
6. Providing security to user’s job.
LANGUAGE TRANSLATORS
It is the program that takes an input program in one language and produces an output in another language.
Language
Translator
2
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
Compilers
• A compiler is a language program that translates programs written in any high-level language into its
equivalent machine language program.
• It bridges the semantic gap between a programming language domain and the execution domain.
• Two aspects of compilation are:
o Generate code to increment meaning of a source program in the execution domain.
o Provide diagnostics for violation of programming language, semantics in a source program.
• The program instructions are taken as a whole.
Compiler
High level language Machine language program
Interpreters:
• It is a translator program that translates a statement of high-level language to machine language and
executes it immediately. The program instructions are taken line by line.
• The interpreter reads the source program and stores it in memory.
• During interpretation, it takes a source statement, determines its meaning and performs actions which
increments it. This includes computational and I/O actions.
• Program counter (PC) indicates which statement of the source program is to be interpreted next. This
statement would be subjected to the interpretation cycle.
• The interpretation cycle consists of the following steps:
o Fetch the statement.
o Analyze the statement and determine its meaning.
o Execute the meaning of the statement.
• The following are the characteristics of interpretation:
o The source program is retained in the source form itself, no target program exists.
o A statement is analyzed during the interpretation.
Interpreter Memory
Source
Program Program
counter
Assemblers:
• Programmers found it difficult to write or red programs in machine language. In a quest for a convenient
language, they began to use a mnemonic (symbol) for each machine instructions which would
subsequently be translated into machine language.
• Such a mnemonic language is called Assembly language.
3
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
• Programs known as Assemblers are written to automate the translation of assembly language into
machine language.
Assembler
Assembly language program Machine language program
• Fundamental functions:
1. Translating mnemonic operation codes to their machine language equivalents.
2. Assigning machine addresses to symbolic tables used by the programmers.
• It consists of bytes(8 bits) ,words (24 bits which are consecutive 3 bytes) addressed by the location of
their lowest numbered byte.
• There are totally 32,768 bytes in memory.
Registers:
There are 5 registers namely
1. Accumulator (A)
2. Index Register(X)
3. Linkage Register(L)
4. Program Counter(PC)
5. Status Word(SW).
• Accumulator is a special purpose register used for arithmetic operations.
• Index register is used for addressing.
• Linkage register stores the return address of the jump of subroutine instructions (JSUB).
• Program counter contains the address of the current instructions being executed.
• Status word contains a variety of information including the condition code.
Data formats:
• Integers are stored as 24-bit binary numbers: 2’s complement representation is used for negative values
characters are stored using their 8 bit ASCII codes.
• They do not support floating – point data items.
Instruction formats:
All machine instructions are of 24-bits wide
Opcode (8) X (1) Address (15)
• X-flag bit that is used to indicate indexed-addressing mode.
Addressing modes:
4
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
3. Branching instructions
Ex: JLT, JEQ, TGT.
Registers:
• 10 x 24 bit registers
System Programming
S 4 General register
T 5 General register
F 6 Floating Point Accumulator (48 bits)
PC 8 Program Counter (PC)
SW 9 Status Word (includes Condition Code, CC)
Data Format:
• Integers are stored in 24 bit, 2's complement format
• Characters are stored in 8-bit ASCII format
• Floating point is stored in 48 bit signed-exponent-fraction format:
Instruction Format:
• There are 4 different instruction formats available:
Format 1 (1 byte):
op {8}
Format 2 (2 bytes):
Format 3 (3 bytes):
Format 4 (4 bytes):
System Programming
Flag x:
x=1 Indexed addressing add contents of X register to TA calculation
Flag e:
e=0 use Format 3
e=1 use Format 4
Instructions:
• 28 (256) I/O devices may be attached, each has its own unique 8-bit address
• 1 byte of data will be transferred to/from the rightmost 8 bits of register A
7
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
SIC/XE Has capability for programmed I/O (I/O device may input/output data while CPU does other work) - 3
additional instructions are provided:
• SIO Start I/O
• HIO Halt I/O
• TIO Test I/O
System Programming
base-relative or program-
counter relative mode
Assembler selects either
1 0 0 1 0 0 op @m (B) + disp ((TA)) base-relative or program-
counter relative mode
Direct-addressing
Immediate 0 1 0 0 0 0 op #c disp TA
Instruction
Format 4 & Direct-
0 1 0 0 0 1 op #m addr TA
addressing Instruction
Assembler selects either
0 1 0 0 1 0 op #m (PC) + disp TA base-relative or program-
counter relative mode
Assembler selects either
0 1 0 1 0 0 op #m (B) + disp TA base-relative or program-
counter relative mode
UNIT II
ASSEMBLERS
9
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
1. Convert mnemonic operation codes to their machine language equivalents. Eg: Translate STL to 14 (line
10).
2. Convert symbolic operands to their equivalent machine addresses. Eg:Translate RETADR to 1033 (line
10).
3. Build the machine instructions in the proper format.
4. Convert the data constants specified in the source program into their internal machine representations.
Eg: Translate EOF to 454F46(line 80).
5. Write the object program and the assembly listing.
All fuctions except function 2 can be established by sequential processing of source program one line at a time.
This instruction contains a forward reference (i.e.) a reference to a label (RETADR) that is defined later in the
program. It is unable to process this line because the address that will be assigned to RETADR is not known.
Hence most assemblers make two passes over the source program where the second pass does the actual
translation.
The assembler must also process statements called assembler directives or pseudo instructions which are not
translated into machine instructions. Instead they provide instructions to the assembler itself.
10
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
Examples: RESB and RESW instruct the assembler to reserve memory locations without generating data
values.
The assembler must write the generated object code onto some output device. This object program will later be
loaded into memory for execution.
• Header record: Contains the program name, starting address and length.
• Text record: Contains the machine code and data of the program.
• End record: Marks the end of the object program and specifies the address in the program where
execution is to begin.
Header record:
Col. 1 H
Col.2-7 Program name
Col.8-13 Starting address of object program
Col.14-19 Length of object program in bytes
Text record:
Col.1 T
Col.2-7 Starting address for object code in this record
Col.8-9 Length of object code in this record in bytes
Col 10-69 Object code, represented in hexadecimal (2 columns per byte of object code)
End record:
Col.1 E
Col.2-7 Address of first executable instruction in object program.
System Programming
• Includes the name and value for each label in the source program and flags to indicate error conditions.
• During Pass 1 of the assembler, labels are entered into SYMTAB as they are encountered in the source
program along with their assigned addresses.
• During Pass 2, symbols used as operands are looked up in SYMTAB to obtain the addresses to be
inserted in the assembled instructions.
Pass 1 usually writes an intermediate file that contains each source statement together with its assigned address,
error indicators. This file is used as the input to Pass 2. This copy of the source program can also be used to
retain the results of certain operations that may be performed during Pass 1 such as scanning the operand field
for symbols and addressing flags, so these need not be performed again during Pass 2.
2.2. MACHINE DEPENDENT ASSEMBLER FEATURES
12
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
13
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
Indirect addressing is indicated by adding the prefix @ to the operand (line70). Immediate operands are denoted
with the prefix # (lines 25, 55,133). Instructions that refer to memory are normally assembled using either the
program counter relative or base counter relative mode.
The assembler directive BASE (line 13) is used in conjunction with base relative addressing. The four byte
extended instruction format is specified with the prefix + added to the operation code in the source statement.
Register-to-register instructions are used wherever possible. For example the statement on line 150 is changed
from COMP ZERO to COMPR A,S. Immediate and indirect addressing have also been used as much as
possible.
Register-to-register instructions are faster than the corresponding register-to-memory operations because they
are shorter and do not require another memory reference.
While using immediate addressing, the operand is already present as part of the instruction and need not be
fetched from anywhere. The use of indirect addressing often avoids the need for another instruction.
• SIC/XE
o PC-relative or Base-relative addressing: op m
o Indirect addressing: op @m
o Immediate addressing: op #c
o Extended format: +op m
o Index addressing: op m,x
o register-to-register instructions
o larger memory -> multi-programming (program allocation)
Translation
• Register translation
o register name (A, X, L, B, S, T, F, PC, SW) and their values (0,1, 2, 3, 4, 5, 6, 8, 9)
o preloaded in SYMTAB
• Address translation
o Most register-memory instructions use program counter relative or base relative addressing
o Format 3: 12-bit address field
▪ base-relative: 0~4095
▪ pc-relative: -2048~2047
o Format 4: 20-bit address field
Absolute Program
14
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
• The only parts of the program that require modification at load time are those that specify direct
addresses.
• The rest of the instructions need not be modified.
o Not a memory address (immediate addressing)
o PC-relative, Base-relative
• From the object program, it is not possible to distinguish the address and constant.
o The assembler must keep some information to tell the loader.
o The object program that contains the modification record is called a relocatable program.
Modification record
System Programming
2.3.1 Literals
• The programmer writes the value of a constant operand as a part of the instruction that uses it. This
avoids having to define the constant elsewhere in the program and make a label for it.
• Such an operand is called a Literal because the value is literally in the instruction.
• A literal is identified with the prefix =, followed by a specification of the literal value.
16
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
• Example:
• Literals
The assembler generates the specified value as a constant at some other memory location.
• Immediate Operands
• We can have literals in SIC, but immediate operand is only valid in SIC/XE.
Literal Pools
• Normally literals are placed into a pool at the end of the program
• In some cases, it is desirable to place literals into a pool at some other location in the object program
• Assembler directive LTORG
o When the assembler encounters a LTORG statement, it generates a literal pool (containing all
literal operands used since previous LTORG)
• Reason: keep the literal operand close to the instruction
o Otherwise PC-relative addressing may not be allowed
Duplicate literals
System Programming
▪ e.g. =X’05’
o Compare the generated data value
▪ Better, but will increase the complexity of the
▪ assembler
▪ e.g. =C’EOF’ and =X’454F46’
Implementation of Literals
Pass 1
• Build LITTAB with literal name, operand value and length, leaving the address unassigned
• When LTORG or END statement is encountered, assign an address to each literal not yet assigned an
address
o updated to reflect the number of bytes occupied by each literal
Pass 2
• Search LITTAB for each literal operand encountered
• Generate data values using BYTE or WORD statements
• Generate Modification record for literals that represent an address in the program
18
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
• Most assemblers provide an assembler directive that allows the programmer to define symbols and
specify their values.
19
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
Forward-Reference Problem
2.3.3 Expressions
System Programming
▪ * (value of LOCCTR)
• Expressions can be
• Absolute
o Only absolute terms.
o MAXLEN EQU 1000
• Relative terms in pairs with opposite signs for each pair.
MAXLEN EQU BUFEND-BUFFER
• Relative
All the relative terms except one can be paired as described in “absolute”. The remaining
unpaired relative term must have a positive sign.
• To determine the type of an expression, we must keep track of the types of all symbols defined in the
program.
• We need a “flag” in the SYMTAB for indication.
• Assembler rearranges these segments to gather together the pieces of each block and assign address.
21
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
• USE [blockname]
• At the beginning, statements are assumed to be part of the unnamed (default) block
• If no USE statements are included, the entire program belongs to this single block
• Each program block may actually contain several separate segments of the source program
Example
22
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
Pass 1
• A separate location counter for each program block
o Save and restore LOCCTR when switching between blocks
o At the beginning of a block, LOCCTR is set to 0.
• Assign each label an address relative to the start of the block
• Store the block name or number in the SYMTAB along with the assigned relative address of the label
• Indicate the block length as the latest value of LOCCTR for each block at the end of Pass1
• Assign to each block a starting address in the object program by concatenating the program blocks in a
particular order
Pass 2
23
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
• Calculate the address for each symbol relative to the start of the object program by adding
o The location of the symbol relative to the start of its block
o The starting address of this block
Object Program
• It is not necessary to physically rearrange the generated code in the object program
• The assembler just simply inserts the proper load address in each Text record.
• The loader will load these codes into correct place
24
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
• Instructions in one control section may need to refer to instructions or data located in another section
• External definition
o EXTDEF name [, name]
o EXTDEF names symbols that are defined in this control section and may be used by other
sections
o Ex: EXTDEF BUFFER, BUFEND, LENGTH
• External reference
o EXTREF name [,name]
o EXTREF names symbols that are used in this control section and are defined elsewhere
o Ex: EXTREF RDREC, WRREC
• To reference an external symbol, extended format instruction is needed.
25
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
Case 1
• 15 0003 CLOOP +JSUB RDREC 4B100000
• The operand RDREC is an external reference.
• The assembler
o Has no idea where RDREC is
o Inserts an address of zero
o Can only use extended format to provide enough room (that is, relative addressing for external
reference is invalid)
• The assembler generates information for each external reference that will allow the loader to perform the
required linking.
26
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
Case 2
Case 3
• On line 107, BUFEND and BUFFER are defined in the same control section and the expression can be
calculated immediately.
• 107 1000 MAXLEN EQU BUFEND-BUFFER
• The assembler must include information in the object program that will cause the loader to insert proper
values where they are required.
• Modification record
Col. 1 M
Col. 2-7 Starting address of the field to be modified (hexiadecimal)
Col. 8-9 Length of the field to be modified, in half-bytes (hexadeccimal)
Col.11-16 External symbol whose value is to be added to or subtracted from the indicated field
• Control section name is automatically an external symbol, i.e. it is available for use in Modification
records.
Object Program
27
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
• Extended restriction
o Both terms in each pair of an expression must be within the same control section
o Legal: BUFEND-BUFFER
o Illegal: RDREC-COPY
• How to enforce this restriction
o When an expression involves external references, the assembler cannot determine whether or not
the expression is legal.
o The assembler evaluates all of the terms it can, combines these to form an initial expression
value, and generates Modification records.
o The loader checks the expression for errors and finishes the evaluation.
System Programming
Load-and-Go Assembler
• Load-and-go assembler generates their object code in memory for immediate execution.
• No object program is written out, no loader is needed.
• It is useful in a system with frequent program development and testing
• The efficiency of the assembly process is an important consideration.
• Programs are re-assembled nearly every time they are run; efficiency of the assembly process is an
important consideration.
One-Pass Assemblers
System Programming
• Omits the operand address if the symbol has not yet been defined.
• Enters this undefined symbol into SYMTAB and indicates that it is undefined.
• Adds the address of this operand address to a list of forward references associated with the SYMTAB
entry.
• When the definition for the symbol is encountered, scans the reference list and inserts the address.
• At the end of the program, reports the error if there are still SYMTAB entries indicated undefined
symbols.
• For Load-and-Go assembler
o Search SYMTAB for the symbol named in the END statement and jumps to this location to
begin execution if there is no error.
30
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
• If the operand contains an undefined symbol, use 0 as the address and write the Text record to the object
program.
• Forward references are entered into lists as in the load-and-go assembler.
• When the definition of a symbol is encountered, the assembler generates another Text record with the
correct operand address of each entry in the reference list.
• When loaded, the incorrect address 0 will be updated by the latter Text record containing the symbol
definition.
Object code generated by one-pass assembler
31
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
• Most assemblers divide the processing of the source program into two passes.
• The internal tables and subroutines that are used only during Pass 1 are no longer needed after the first
pass is completed.
• The routines and tables for Pass 1 and Pass 2 are never required at the same time.
• There are certain tables (SYMTAB) and certain processing subroutines (searching SYMTAB) that are
used by both passes.
• Since Pass 1 and Pass 2 segments are never needed at the same time, they can occupy the same locations
in memory during execution of the assembler.
• Initially the Root and Pass 1 segments are loaded into memory.
• The assembler then makes the first pass over the program being assembled.
• At the end of the Pass1, the Pass 2 segment is loaded, replacing the Pass 1 segment.
• The assembler then makes its second pass of the source program and terminates.
• The assembler needs much less memory to run in this way than it would be if both Pass 1 and Pass 2
were loaded at the same time.
• A program that is designed to execute in this way is called an Overlay program because some of its
segments overlay others during execution.
• The symbol BETA cannot be assigned a value when it is encountered during Pass 1 because DELTA has
not yet been defined.
• Hence ALPHA cannot be evaluated during Pass 2.
• Symbol definition must be completed in pass 1.
• Prohibiting forward references in symbol definition is not a serious inconvenience.
• Forward references tend to create difficulty for a person reading the program.
• The general solution for forward references is a multi-pass assembler that can make as many passes as
are needed to process the definitions of symbols.
• It is not necessary for such an assembler to make more than 2 passes over the entire program.
• The portions of the program that involve forward references in symbol definition are saved during Pass
1.
• Additional passes through these stored definitions are made as the assembly progresses.
32
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
Implementation
Consider the symbol table entries from Pass 1 processing of the statement.
• Since MAXLEN has not yet been defined, no value for HALFS2 can be computed.
• The defining expression for HALFS2 is stored in the symbol table in place of its value.
• The entry &1 indicates that 1 symbol in the defining expression undefined.
• SYMTAB simply contain a pointer to the defining expression.
• The symbol MAXLEN is also entered in the symbol table, with the flag * identifying it as undefined.
• Associated with this entry is a list of the symbols whose values depend on MAXLEN.
33
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
UNIT III
LOADERS AND LINKERS
INTRODUCTION
For a simple absolute loader, all functions are accomplished in a single pass as follows:
1) The Header record of object programs is checked to verify that the correct program has been presented for
loading.
2) As each Text record is read, the object code it contains is moved to the indicated address in memory.
3) When the End record is encountered, the loader jumps to the specified address to begin execution of the
loaded program.
34
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
Fig (b) shows a representation of the program from Fig (a) after loading.
35
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
• It is very important to realize that in Fig (a), each printed character represents one byte of the object
program record.
• In Fig (b), on the other hand, each printed character represents one hexadecimal digit in memory (a half-
byte).
• Therefore, to save space and execution time of loaders, most machines store object programs in a binary
form, with each byte of object code stored as a single byte in the object program.
• In this type of representation a byte may contain any binary value.
System Programming
• The absolute loader has several potential disadvantages. One of the most obvious is the need for the
programmer to specify the actual address at which it will be loaded into memory.
• On a simple computer with a small memory the actual address at which the program will be loaded can
be specified easily.
• On a larger and more advanced machine, we often like to run several independent programs together,
sharing memory between them. We do not know in advance where a program will be loaded. Hence we
write relocatable programs instead of absolute ones.
37
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
• Writing absolute programs also makes it difficult to use subroutine libraries efficiently. This could not
be done effectively if all of the subroutines had pre-assigned absolute addresses.
• The need for program relocation is an indirect consequence of the change to larger and more powerful
computers. The way relocation is implemented in a loader is also dependent upon machine
characteristics.
• Loaders that allow for program relocation are called relocating loaders or relative loaders.
3.2.1 Relocation
38
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
39
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
Object program
• Each Modification record specifies the starting address and length of the field whose value is to be
altered.
• It then describes the modification to be performed.
• In this example, all modifications add the value of the symbol COPY, which represents the starting
address of the program.
.
.
40
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
• The Modification record is not well suited for use with all machine architectures.Consider, for example,
the program in Fig (2) .This is a relocatable program written for standard version for SIC.
• The important difference between this example and the one in Fig (1) is that the standard SIC machine
does not use relative addressing.
• In this program the addresses in all the instructions except RSUB must modified when the program is
relocated. This would require 31 Modification records, which results in an object program more than
twice as large as the one in Fig (1).
• The relocation bits are gathered together into a bit mask following the length indicator in each Text
record. In Fig (3) this mask is represented (in character form) as three hexadecimal digits.
• If the relocation bit corresponding to a word of object code is set to 1, the program’s starting address is
to be added to this word when the program is relocated. A bit value of 0 indicates that no modification is
necessary.
• If a Text record contains fewer than 12 words of object code, the bits corresponding to unused words are
set to 0.
• For example, the bit mask FFC (representing the bit string 111111111100) in the first Text record
specifies that all 10 words of object code are to be modified during relocation.
41
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
• Example: Note that the LDX instruction on line 210 (Fig (2)) begins a new Text record. If it were
placed in the preceding Text record, it would not be properly aligned to correspond to a relocation bit
because of the 1-byte data value generated from line 185.
Consider the three (separately assembled) programs in the figure, each of which consists of a single control
section.
Program 1 (PROGA):
Program 2 (PROGB):
Program 3 (PROGC):
42
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
PROGA:
43
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
PROGB:
PROGC:
44
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
• REF3 is an immediate operand whose value is to be the difference between ENDA and LISTA (that is,
the length of the list in bytes).
• In PROGA, the assembler has all of the information necessary to compute this value. During the
assembly of PROGB (and PROGC), the values of the labels are unknown.
• In these programs, the expression must be assembled as an external reference (with two Modification
records) even though the final result will be an absolute value independent of the locations at which the
programs are loaded.
• Consider REF4.
• The assembler for PROGA can evaluate all of the expression in REF4 except for the value of LISTC.
This results in an initial value of ‘000014’H and one Modification record.
• The same expression in PROGB contains no terms that can be evaluated by the assembler. The object
code therefore contains an initial value of 000000 and three Modification records.
• For PROGC, the assembler can supply the value of LISTC relative to the beginning of the program (but
not the actual address, which is not known until the program is loaded).
• The initial value of this data word contains the relative address of LISTC (‘000030’H). Modification
records instruct the loader to add the beginning address of the program (i.e., the value of PROGC), to
add the value of ENDA, and to subtract the value of LISTA.
Fig (4): The three programs as they might appear in memory after loading and linking.
PROGA has been loaded starting at address 4000, with PROGB and PROGC immediately following.
For example, the value for reference REF4 in PROGA is located at address 4054 (the beginning address of
PROGA plus 0054).
45
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
The initial value (from the Text record) is 000014. To this is added the address assigned to LISTC, which 4112
(the beginning address of PROGC plus 30).
• The algorithm for a linking loader is considerably more complicated than the absolute loader algorithm.
• A linking loader usually makes two passes over its input, just as an assembler does. In terms of general
function, the two passes of a linking loader are quite similar to the two passes of an assembler:
• Pass 1 assigns addresses to all external symbols.
• Pass 2 performs the actual loading, relocation, and linking.
• The main data structure needed for our linking loader is an external symbol table ESTAB.
(1) This table, which is analogous to SYMTAB in our assembler algorithm, is used to store the name
and address of each external symbol in the set of control sections being loaded.
(2) A hashed organization is typically used for this table.
• Two other important variables are PROGADDR (program load address) and CSADDR (control
section address).
(1) PROGADDR is the beginning address in memory where the linked program is to be loaded. Its
value is supplied to the loader by the OS.
(2) CSADDR contains the starting address assigned to the control section currently being scanned by
the loader. This value is added to all relative addresses within the control section to convert them to
actual addresses.
3.2.3.1 PASS 1
• During Pass 1, the loader is concerned only with Header and Define record types in the control sections.
46
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
1) The beginning load address for the linked program (PROGADDR) is obtained from the OS. This becomes
the starting address (CSADDR) for the first control section in the input sequence.
2) The control section name from Header record is entered into ESTAB, with value given by CSADDR. All
external symbols appearing in the Define record for the control section are also entered into ESTAB. Their
addresses are obtained by adding the value specified in the Define record to CSADDR.
3) When the End record is read, the control section length CSLTH (which was saved from the End record) is
added to CSADDR. This calculation gives the starting address for the next control section in sequence.
• At the end of Pass 1, ESTAB contains all external symbols defined in the set of control sections together
with the address assigned to each.
• Many loaders include as an option the ability to print a load map that shows these symbols and their
addresses.
3.2.3.2 PASS 2
• Pass 2 performs the actual loading, relocation, and linking of the program.
1) As each Text record is read, the object code is moved to the specified address (plus the current value of
CSADDR).
2) When a Modification record is encountered, the symbol whose value is to be used for modification is looked
up in ESTAB.
3) This value is then added to or subtracted from the indicated location in memory.
4) The last step performed by the loader is usually the transferring of control to the loaded program to begin
execution.
47
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
• The End record for each control section may contain the address of the first instruction in that control
section to be executed. Our loader takes this as the transfer point to begin execution. If more than one
control section specifies a transfer address, the loader arbitrarily uses the last one encountered.
• If no control section contains a transfer address, the loader uses the beginning of the linked program
(i.e., PROGADDR) as the transfer point.
• Normally, a transfer address would be placed in the End record for a main program, but not for a
subroutine.
This algorithm can be made more efficient. Assign a reference number, which is used (instead of the symbol
name) in Modification records, to each external symbol referred to in a control section. Suppose we always
assign the reference number 01 to the control section name.
Fig (6): Object programs using reference numbers for code modification
48
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
System Programming
• Loading and linking are often thought of as OS service functions. Therefore, most loaders include fewer
different features than are found in a typical assembler.
• They include the use of an automatic library search process for handling external reference and some
common options that can be selected at the time of loading and linking.
• Many linking loaders can automatically incorporate routines from a subprogram library into the program
being loaded.
• Linking loaders that support automatic library search must keep track of external symbols that are
referred to, but not defined, in the primary input to the loader.
• At the end of Pass 1, the symbols in ESTAB that remain undefined represent unresolved external
references.
• The loader searches the library or libraries specified for routines that contain the definitions of these
symbols, and processes the subroutines found by this search exactly as if they had been part of the
primary input stream.
• The subroutines fetched from a library in this way may themselves contain external references. It is
therefore necessary to repeat the library search process until all references are resolved.
• If unresolved external references remain after the library search is completed, these must be treated as
errors.
50
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
• Precondition: The source program is first assembled or compiled, producing an object program.
• A linking loader performs all linking and relocation operations, including automatic library search if
specified, and loads the linked program directly into memory for execution.
• A linkage editor produces a linked version of the program (load module or executable image), which is
written to a file or library for later execution.
• The linkage editor performs relocation of all control sections relative to the start of the linked program.
Thus, all items that need to be modified at load time have values that are relative to the start of the
linked program.
• This means that the loading can be accomplished in one pass with no external symbol table required.
• If a program is to be executed many times without being reassembled, the use of a linkage editor
substantially reduces the overhead required.
• Linkage editors can perform many useful functions besides simply preparing an object program for
execution. Ex., a typical sequence of linkage editor commands used:
INCLUDE PLANNER (PROGLIB)
DELETE PROJECT {delete from existing PLANNER}
INCLUDE PROJECT (NEWLIB) {include new version}
REPLACE PLANNER (PROGLIB)
• Linkage editors can also be used to build packages of subroutines or other control sections that are
generally used together. This can be useful when dealing with subroutine libraries that support high-
level programming languages.
• Linkage editors often include a variety of other options and commands like those discussed for linking
loaders. Compared to linking loaders, linkage editors in general tend to offer more flexibility and
control.
Fig (7): Processing of an object program using (a) Linking loader and (b) Linkage
editor
51
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
• Linkage editors perform linking operations before the program is loaded for execution.
• Linking loaders perform these same operations at load time.
• Dynamic linking, dynamic loading, or load on call postpones the linking function until execution time: a
subroutine is loaded and linked to the rest of the program when it is first called.
• Dynamic linking is often used to allow several executing programs to share one copy of a subroutine or
library, ex. run-time support routines for a high-level language like C.
• With a program that allows its user to interactively call any of the subroutines of a large mathematical
and statistical library, all of the library subroutines could potentially be needed, but only a few will
actually be used in any one execution.
• Dynamic linking can avoid the necessity of loading the entire library for each execution except those
necessary subroutines.
52
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
Fig (a): Instead of executing a JSUB instruction referring to an external symbol, the program makes a load-and-
call service request to OS. The parameter of this request is the symbolic name of the routine to be called.
Fig (b): OS examines its internal tables to determine whether or not the routine is already loaded. If necessary,
the routine is loaded from the specified user or system libraries.
53
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
Fig (c): Control is then passed from OS to the routine being called
Fig (d): When the called subroutine completes it processing, it returns to its caller (i.e., OS). OS then returns
control to the program that issued the request.
Fig (e): If a subroutine is still in memory, a second call to it may not require another load operation. Control
may simply be passed from the dynamic loader to the called routine.
• With the machine empty and idle there is no need for program relocation.
• We can specify the absolute address for whatever program is first loaded and this will be the OS, which
occupies a predefined location in memory.
• We need some means of accomplishing the functions of an absolute loader.
1. To have the operator enter into memory the object code for an absolute loader, using switches on the
computer console.
2. To have the absolute loader program permanently resident in a ROM.
3. To have a built –in hardware function that reads a fixed –length record from some device into
memory at a fixed location.
• When some hardware signal occurs, the machine begins to execute this ROM program.
• On some computers, the program is executed directly in the ROM: on others, the program is copied
from ROM to main memory and executed there.
• The particular device to be used can often be selected via console switches.
• After the read operation is complete, control is automatically transferred to the address in memory where
the record was stored, which contains machine where the record was stored, which contains machine
instructions that load the absolute program that follow.
• If the loading process requires more instructions that can be read in a single record, this first record
causes the reading of others, and these in turn can cause the reading of still more records – boots trap.
• The first record is generally referred to as bootstrap loader:
• Such a loader is added to the beginning of all object programs that are to be loaded into an empty and
idle system.
• This includes the OS itself and all stand-alone programs that are to be run without an OS.
UNIT IV
MACROPROCESSORS
INTRODUCTION
54
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
Macro Instructions
• A macro instruction (macro)
– It is simply a notational convenience for the programmer to write a shorthand version of a
program.
– It represents a commonly used group of statements in the source program.
– It is replaced by the macro processor with the corresponding group of source language
statements. This operation is called “expanding the macro”
• For example:
– Suppose it is necessary to save the contents of all registers before calling a subroutine.
– This requires a sequence of instructions.
– We can define and use a macro, SAVEREGS, to represent this sequence of instructions.
Macro Processor
• A macro processor
– Its functions essentially involve the substitution of one group of characters or lines for another.
– Normally, it performs no analysis of the text it handles.
– It doesn’t concern the meaning of the involved statements during macro expansion.
• Therefore, the design of a macro processor generally is machine independent.
• Macro processors are used in
– assembly language
– high-level programming languages, e.g., C or C++
– OS command languages
– general purpose
Example:
MACRO
SUM X,Y
LDA X
MOV BX,X
LDA Y
ADD BX
MEND
55
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
56
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
Macro Invocation
• A macro invocation statement (a macro call) gives the name of the macro instruction being invoked and
the arguments in expanding the macro.
• The processes of macro invocation and subroutine call are quite different.
o Statements of the macro body are expanded each time the macro is invoked.
o Statements of the subroutine appear only one; regardless of how many times the subroutine is
called.
57
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
• The macro invocation statements treated as comments and the statements generated from macro
expansion will be assembled as though they had been written by the programmer.
Macro Expansion
• Each macro invocation statement will be expanded into the statements that form the body of the macro.
• Arguments from the macro invocation are substituted for the parameters in the macro prototype.
o The arguments and parameters are associated with one another according to their positions.
▪ The first argument in the macro invocation corresponds to the first parameter in the
macro prototype, etc.
• Comment lines within the macro body have been deleted, but comments on individual statements have
been retained.
• Macro invocation statement itself has been included as a comment line.
58
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
• In expanding the macro invocation on line 190, the argument F1 is substituted for the parameter and
INDEV wherever it occurs in the body of the macro.
• Similarly BUFFER is substituted for BUFADR and LENGTH is substituted for RECLTH.
• Lines 190a through 190m show the complete expansion of the macro invocation on line 190.
• The label on the macro invocation statement CLOOP has been retained as a label on the first statement
generated in the macro expansion.
• This allows the programmer to use a macro instruction in exactly the same way as an assembler
language mnemonic.
• After macro processing the expanded file can be used as input to assembler.
• The macro invocation statement will be treated as comments and the statements generated from the
macro expansions will be assembled exactly as though they had been written directly by the
programmer.
• It is easy to design a two-pass macro processor in which all macro definitions are processed during the
first pass ,and all macro invocation statements are expanded during second pass
• Such a two pass macro processor would not allow the body of one macro instruction to contain
definitions of other macros.
Example 1:
59
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
Example 2:
• Defining MACROS or MACROX does not define RDBUFF and the other macro instructions. These
definitions are processed only when an invocation of MACROS or MACROX is expanded.
• A one pass macroprocessor that can alternate between macro definition and macro expansion is able to
handle macros like these.
• There are 3 main data structures involved in our macro processor.
System Programming
1. The third Data Structure in an argument table (ARGTAB), which is used during expansion of macro
invocations.
2. When macro invocation statements are recognized, the arguments are stored in ARGTAB according to
their position in argument list.
3. As the macro is expanded, arguments from ARGTAB are substituted for the corresponding parameters
in the macro body.
• The position notation is used for the parameters. The parameter &INDEV has been converted to ?1,
&BUFADR has been converted to ?2.
• When the ?n notation is recognized in a line from DEFTAB, a simple indexing operation supplies the
property argument from ARGTAB.
Algorithm:
• The procedure DEFINE, which is called when the beginning of a macro definition is recognized, makes
the appropriate entries in DEFTAB and NAMTAB.
• EXPAND is called to set up the argument values in ARGTAB and expand a macro invocation
statement.
• The procedure GETLINE gets the next line to be processed
• This line may come from DEFTAB or from the input file, depending upon whether the Boolean variable
EXPANDING is set to TRUE or FALSE.
61
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
System Programming
Machine independent macro processor features are extended features that are not directly related to architecture
of computer for which the macro processor is written.
• Most Macro Processor allows parameters to be concatenated with other character strings.
• A program contains a set of series of variables:
▪ XA1, XA2, XA3,…
▪ XB1, XB2, XB3,…
• If similar processing is to be performed on each series of variables, the programmer might want to
incorporate this processing into a macro instructuion.
• The parameter to such a macro instruction could specify the series of variables to be operated on (A, B,
C …).
• The macro processor constructs the symbols by concatenating X, (A, B, …), and (1,2,3,…) in the macro
expansion.
• Suppose such parameter is named &ID, the macro body may contain a statement:
LDA X&ID1, in which &ID is concatenated after the string “X” and before the string “1”.
LDA XA1 (&ID=A)
LDA XB1 (&ID=B)
• Ambiguity problem:
E.g., X&ID1 may mean
“X” + &ID + “1”
“X” + &ID1
This problem occurs because the end of the parameter is not marked.
• Solution to this ambiguity problem:
Use a special concatenation operator “→” to specify the end of the parameter
LDA X&ID →1
So that the end of parameter &ID is clearly identified.
Macro definition
63
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
• The macroprocessor deletes all occurrences of the concatenation operator immediately after performing
parameter substitution, so the character → will not appear in the macro expansion.
• Labels in the macro body may cause “duplicate labels” problem if the macro is invocated and expanded
multiple times.
• Use of relative addressing at the source statement level is very inconvenient, error-prone, and difficult to
read.
• It is highly desirable to
1. Let the programmer use label in the macro body
• Labels used within the macro body begin with $.
2. Let the macro processor generate unique labels for each macro invocation and expansion.
• During macro expansion, the $ will be replaced with $xx, where xx is a two-character
alphanumeric counter of the number of macro instructions expanded.
• XX=AA, AB, AC …….
5 COPY START 0
:
:
135 TD =X ‘&OUTDEV’
:
140 JEQ *-3
:
155 JLT *-14
:
255 END FIRST
• If a label was placed on the TD instruction on line 135, this label would be defined twice, once for each
invocation of WRBUFF.
• This duplicate definition would prevent correct assembly of the resulting expanded program.
• The jump instructions on line 140 and 155 are written using the re4lative operands *-3 and *-14,
because it is not possible to place a label on line 135 of the macro definition.
• This relative addressing may be acceptable for short jumps such as “ JEQ *-3”
64
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
• For longer jumps spanning several instructions, such notation is very inconvenient, error-prone and
difficult to read.
• Many macroprocessors avoid these problems by allowing the creation of special types of labels within
macro instructions.
RDBUFF definition
• Labels within the macro body begin with the special character $.
Macro expansion
65
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
Macro
Time
variable
Boolean Expression
System Programming
• When an WHILE statement is encountered during the expansion of a macro, the specified Boolean
expression is evaluated. If value is
o TRUE
▪ The macro processor continues to process lines from DEFTAB until it encounters the
next ENDW statement.
▪ When ENDW is encountered, the macro processor returns to the preceding WHILE, re-
evaluates the Boolean expression, and takes action again.
o FALSE
▪ The macro processor skips ahead in DEFTAB until it finds the next ENDW statement
and then resumes normal macro expansion.
• Positional parameters
o Parameters and arguments are associated according to their positions in the macro prototype and
invocation. The programmer must specify the arguments in proper order.
o If an argument is to be omitted, a null argument should be used to maintain the proper order in
macro invocation statement.
o For example: Suppose a macro instruction GENER has 10 possible parameters, but in a
particular invocation of the macro only the 3rd and 9th parameters are to be specified.
67
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
• Keyword parameters
o Each argument value is written with a keyword that names the corresponding parameter.
o Arguments may appear in any order.
o Null arguments no longer need to be used.
o If the 3rd parameter is named &TYPE and 9th parameter is named &CHANNEL, the macro
invocation would be
GENER TYPE=DIRECT,CHANNEL=3.
o It is easier to read and much less error-prone than the positional method.
• Here each parameter name is followed by equal sign, which identifies a keyword parameter and a
default value is specified for some of the parameters.
68
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
Here the value if &INDEV is specified as F3 and the value of &EOR is specified as null.
69
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
• RDCHAR:
o read one character from a specified device into register A
o should be defined beforehand (i.e., before RDBUFF)
• Previous macro processor design cannot handle such kind of recursive macro invocation and expansion,
e.g., RDBUFF BUFFER, LENGTH, F1
• Reasons:
1) The procedure EXPAND would be called recursively, thus the invocation arguments in the
ARGTAB will be overwritten.
2) The Boolean variable EXPANDING would be set to FALSE when the “inner” macro expansion
is finished, that is, the macro process would forget that it had been in the middle of expanding an
“outer” macro.
3) A similar problem would occur with PROCESSLINE since this procedure too would be called
recursively.
• Solutions:
1) Write the macro processor in a programming language that allows recursive calls, thus local
variables will be retained.
2) Use a stack to take care of pushing and popping local variables and return addresses.
• Another problem: can a macro invoke itself recursively?
70
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
o To solve this problem, a counter LEVEL is used to keep track of the level of macro definitions.
▪ Increase LEVEL by 1 each time a MACRO directive is read.
▪ Decrease LEVEL by 1 each time a MEND directive is read.
▪ A MEND can terminate the whole macro definition process only when LEVEL reaches 0.
▪ This process is very much like matching left and right parentheses when scanning an
arithmetic expression.
71
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
Advantages
• Programmers do not need to learn many macro languages.
• Although its development costs are somewhat greater than those for a language-specific macro
processor, this expense does not need to be repeated for each language, thus save substantial overall
cost.
Disadvantages
• Large number of details must be dealt with in a real programming language
• Situations in which normal macro parameter substitution should not occur, e.g., comments.
• Facilities for grouping together terms, expressions, or statements
• Tokens, e.g., identifiers, constants, operators, keywords
• Syntax
1) Preprocessors
o Process macro definitions.
o Expand macro invocations.
o Produce an expanded version of the source program, which is then used as input to an assembler or
compiler.
2) Line-by-line macro processor
o Used as a sort of input routine for the assembler or compiler.
o Read source program.
72
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
Benefits
• It avoids making an extra pass over the source program.
• Data structures required by the macro processor and the language translator can be combined (e.g.,
OPTAB and NAMTAB)
• Utility subroutines can be used by both macro processor and the language translator.
o Scanning input lines
o Searching tables
o Data format conversion
• It is easier to give diagnostic messages related to the source statements.
• They must be specially designed and written to work with a particular implementation of an assembler
or compiler.
• The cost of macro processor development is added to the costs of the language translator, which results
in a more expensive software.
• The assembler or compiler will be considerably larger and more complex.
UNIT V
DEBUGGER
5.1 INTRODUCTION
73
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
• Debuggers are the magnifying glass, the microscope, the logic analyzer, the profiler, and the browser
with which a program can be examined.
• Debuggers are quite complex pieces of software some background information and terminology of
debugger.
74
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
Kernel Debugger: Used for dealing with problems with an OS Kernel on its own or for interactions between
heavily OS dependent applications and the OS.
Machine level Debugger: Used for debugging the actual running code as they arte processed by the CPU.
User Interface
• This section describes the state-of-the-art of graphical debugger user interface.
• The user interface of the graphical debugger consists of a series of views.
• Each view represents a different picture of the program being debugged.
Source View
• The source view is the most critical focal point for the programmer during debugging.
• This is the view that gives the user the illusion that the debugger is actually executing the originally
typed source code directly.
• The user is presented with information about which statements are executable.
• Executable statements show a small red dot indicating where breakpoints may be set.
• Active breakpoints show larger red stop signs.
Disassembly view
• Breakpoints are the debugger’s responsibility to map the underlying machine execution back to this
source view.
• When this is not possible, an alternative view must be shown that represents the machine-level execution
directly.
• This is the CPU or Disassembly view.
Stack view
• The stack view gives a representation of the function or procedure stack trace.
• This view is thread-specific as there is a stack maintained for each active thread in a process.
Breakpoints view
• The breakpoints view gives an overview of all the breakpoints set by the user anywhere in the currently
active process.
• Breakpoint displayed in this view show the source location of each breakpoint as well as its status.
CPU view
• This view is critical to most debugging situations because while the source view gives an excellent
mapping of the machine representation back to the source code, it is rarely enough to completely
understand how the program is behaving.
Variables view
• This view is closely tied to the browser, which in turn is closely tied to the compiler generated symbol
tables.
75
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
• Examining variables is critical in understanding program behaviour. The values of the variables used in
the program are critical for understanding the root causes of program defects.
Kernel
• The second outer layer is the Debugger kernel.
• This is the part of the debugger that services all the views.
• The application we wish to debug is a process, to the OS.
• It has the following functions:
o Symbol table management
o Expression evaluation
The Debugger’s ability to control the execution of the debuggee depends on the hardware support built into the
processor and specialized debugging mechanisms built into the OS.
The minimal basic requirements a debugger places on the underlying hardware are:
1. A way to specify a breakpoint – a specific location in the executing code such that when the processor
reaches this location, execution will stop.
2. A notification system, also called an interrupt or a trap, that will notify the OS that an important event
has occurred with respect to the running process.
3. The ability to read and write directly out of and into the hardware registers when the interrupt occurs;
this includes the PC register.
76
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
Breakpointing
• Breakpoints are usually implemented as a special instruction that causes a trap to the OS, which then can
notify a special program that has registered itself as a debugger.
• On architectures with varying length instructions, it is normal for the trap – or breakpoint instructions to
be length of the shortest possible instruction.
• This makes it much simpler for the debugger to guarantee breakpoints are placed on instruction
boundaries.
• The procedure of replacing the breakpoints is basic to debugger operation.
INPUT: Previously inserted breakpoint instruction just trapped to the OS. Original instruction at the breakpoint
address was saved when the breakpoint initially was inserted.
Debugger must re-insert the saved instruction to procedd and still replace breakpoint instruction as well so it
can be hit again.
After breakpoint processing has completed and the debugger is instructed to continue execution of the
debuggee, it must replace the original instruction and then put the breakpoint back before letting the debuggee
proceed at full speed.
Loc=current_address
Address[loc] = saved_instruction
Initiate single step in debuggee
When trap occurs {
If(trap was for single-step completion)
{
Address[loc]=bkpt_instructipon
Run debuggee full speed
}
Else
{
Standard handling for debuggee exceptions.
When re-starting debuggee, re-start same thread that stopped.
}
}
77
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
Multiprocessor Support
• Two types of parallel architectures employed
o Multiple Instruction Multiple Data (MIMD)
o Single Instruction Multiple Data (SIMD)
• In both types stall detection is important to debugger.
• To provide a controlled debugging environment, debuggers controlling asynchronous multithreaded
programs may need to operate only on thread at a time so that no thread can “run away” in a
uncontrolled fashion.
78
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
On a simple breakpoint, where the debuggee is running and the debugger is waiting for some notification, there
are six significant transitions between debugger and debugee before the debugger is completely stopped and the
debugger is ready to deal with the user’s next request.
1. Debugee running hits breakpoint. Transition #1 to OS to deal with trap
2. Transition #2 to debugger waiting for event. Debugger now running.
3. Debugger requests state information on debuggee. Transition #3 to OS to deal with request.
4. Transition #4 to debuggee context to get information. Debuggee context now active.
5. Transition #5 to OS to return information.
6. Transition #6 to debugger to handle information. Debugger active.
• The first job of a debugger with respect to controlling execution is the creation of the debugger from
scratch or via attachment to an existing process.
• Next step is to get the debugger ready to run under the debugger’s control.
• The first thing the debugger must do, once the user has specified what executable is to be debugged, is
to initiate the calls to the OS that create the debugger.
• The OS needs either to create the debuggee process for the debugger explicitly through a special call or
to attach the debugger to an existing process.
• The debugger gets notifications from the OS whenever a material event occurs with the debuggee.
• All notifications result in the debuggee being stopped for examination.
• The debugger gets its notification about a new debuggee once all the setup is done and the debuggee is
ready for debugger control.
• Attaching to a running program is a very important facility for multiprocess, post-mortem or just-in-time
debugging.
79
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
• Multiprocess debugging
o The debugger needs to able to debug any and all processes that interact with the debuggee.
o All interacting processes are under the control of the same debugger to fully understand what is
going wrong.
• Post-mortem debugging
o Process of examining the memory image of a process that has faulted and was terminated.
o Its faulted state is saved to disk for examination.
• Just-in-time debugging
o Related to post-mortem debugging
o When the OS issues a fatal exception but before the process is flushed from memory the OS
allows a debugger to attach to the process to examine its memory image.
Setting a Breakpoint
• When breakpopints are executed by the debugger
o It causes to halt its execution immediately
o And to trap to the OS
• These special instructions are defined by CPU architecture.
• Sometimes they are just a special value of general “interrupt” instruction or a special dedicated
breakpoint instruction.
• In either case, it is an instruction designed to trap to a special OS breakpoint handler.
System Programming
Breakpoint Validation
• When a breakpoint is set by a user there may not be an address at which to physically place the
breakpoint instruction yet.
• This can occur because there may not yet be a valid mapping from the source code the user can see and
manipulate and the executable code, which executes on the processor.
• Validation of breakpoints must get triggered at the earliest possible moment.
• When the process is first created no instructions are executed before the debugger has a chance to
process all the invalidated breakpoints and get physical breakpoints inserted.
Temporary Breakpoints
• Temporary breakpoints are used to implement features such as “run-to-main” or “run-to-here”.
• Run-to-main is used at debuggee startup to quickly execute past all startup code and to stop on a
program’s main routine.
• Run-to-here allows the user to point to source code where the PC is desired to be and quickly have the
debuggee execute upto that point.
Side effects
• Breakpoints can be used for much more than just a way to stop the debuggee program and give control
to the user.
• We can program the debugger to do anything useful when a breakpoint activates by associating actions
to be performed when a breakpoint evaluates its condition.
• Logging is a simple action that can be performed at a breakpoint.
• The breakpoint history approach via a debugger has the advantage pf not requiring the program to be
recompiled.
• Pass counts are another frequently used side effect. They are simple expressions to be evaluated by the
debugger upon breakpoints.
Single – step
• Single-step is important because users need to be able to “watch” execution proceed.
• To fully support breakpoints and single step in its various forms, the debugger needs to have a very
sophisticated execution control mechanism.
81
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
Evaluating Expressions
• A symbolic debugger can display data by evaluating an expression that uses the same identifiers and
syntax that appear in the source program.
• This requires the debugger to implement an interpreter for the expression syntax and semantics of one
source language.
• The debugger’s interpreter does not allocate its own storage for variables but instead accesses them
within the debugger’s child process at the addresses specified by the debugging tables emitted by the
compiler.
Scope Resolution
• If a debugger is meant to work with more than one language, it must avoid the temptation to build into
its symbol – access methods any assumptions about identifier scoping because they differ among
languages.
• The syntax of each language can be extended while in the debugger’s evaluator subsystem to let the user
specify variables that exist but that are currently not in scope.
• Some debuggers will automatically re-evaluate an expression each time the child process stops.
• The evaluator must separate the mapping of identifiers onto symbols from the rest of the evaluation
work.
• It must provide a list of the scopes required so that the debugger avoids re-evaluating an expression
unless the stack contains a frame for each of those scopes.
• The ability to invoke a function during expression evaluation is important in languages like C++ because
the user may use an operator in an expression that has been overloaded with a function.
• The debugger does not interpret the function invocation.
• Instead it
o Builds an argument list using the stack within the child process.
o Sets a breakpoint at the function return address
o Starts the child process running at the beginning of the function.
• A symbolic debugger depends on the compiler and linker to emit debugging table (also called symbol
table).
• A debugging symbol table must deal with a number of issues:
o Does the symbol table cater to the compiler or to the debugger?
o How does it divide the work among compiler,linker and debugger?
o Does it permit incremental processing and caching of information?
o Can it support a variety of target machines?
o Can it support a variety of compilers?
System Programming
• Most debuggers have a certain set of queries to access the symbol table depending on the service it
provides.
• List of queries:
o Map instruction address onto the enclosing scope.
o Map scope onto statically enclosing parent scope.
o Map scope plus identifier onto type and location.
o Map instruction address onto source statement.
o Map code or data address onto statically allocated variable or procedure.
o Map source statement onto instruction address range.
TEXT BOOK
1. Leland L. Beck, “System Software – An Introduction to Systems Programming”, 3rd Edition, Pearson
Education Asia, 2000.
REFERENCES
1. D. M. Dhamdhere, “Systems Programming and Operating Systems”, Second Revised Edition, Tata
McGraw-Hill, 1999.
2. John J. Donovan “Systems Programming”, Tata McGraw-Hill Edition, 1972.
System Programming
4) What are the important machine structures used in the design of system software?
➢ Memory structure
➢ Registers
➢ Data formats
➢ Instruction formats
➢ Addressing modes
➢ Instruction set
84
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
➢ Addressing Modes
➢ Instruction Set
o integer arithmetic operations: ADD, SUB, MUL, DIV, etc.
▪ All arithmetic operations involve register A and a word in memory, with the result being
left in the register
o comparison: COMP
▪ COMP compares the value in register A with a word in memory, this instruction sets a
condition code CC to indicate the result
o conditional jump instructions: JLT, JEQ, JGT
▪ these instructions test the setting of CC and jump accordingly
o subroutine linkage: JSUB, RSUB
▪ JSUB jumps to the subroutine, placing the return address in register L
▪ RSUB returns by jumping to the address contained in register L
8)
9)
10)
11) Explain SIC/XE architecture
SIC/XE Machine Architecture
➢ Memory
85
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
➢ More Registers
Mnemonic Number Special use
B 3 Base register; used for addressing
S 4 General working register
T 5 General working register
F 6 Floating-point acumulator (48bits)
➢ Data Formats
o Floating-point data type: frac*2(exp-1024)
▪ frac: 0~1
▪ exp: 0~2047
➢ Instruction Formats
Format 1
op(8)
Format 2
op(8) r1(4) r2(4)
Format 3 e=0
op(6) n I xb p e disp(12)
Format 4 e=1
op(6) n I x b p e address (20)
➢ Instruction Set
o new registers: LDB, STB, etc.
o floating-point arithmetic: ADDF, SUBF, MULF, DIVF
o register move: RMO
o register-register arithmetic: ADDR, SUBR, MULR, DIVR
o supervisor call: SVC
▪ generates an interrupt for OS
➢ Input/Output
SIO, TIO, HIO: start, test, halt the operation of I/O device
86
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
◼ Addressing modes
➢ Base relative (n=1, i=1, b=1, p=0)
➢ Program-counter relative (n=1, i=1, b=0, p=1)
➢ Direct (n=1, i=1, b=0, p=0)
➢ Immediate (n=0, i=1, x=0)
➢ Indirect (n=1, i=0, x=0)
➢ Indexing (both n & i = 0 or 1, x=1)
➢ Extended (e=1 for format 4, e=0 for format 3)
87
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
System Programming
(a)
89
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
90
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)
lOMoARcPSD|9133798
System Programming
91
k. Adisesha
Downloaded by Navaraj Pandey (mtopandey@gmail.com)