Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lecture 4 Sem5A 23 Part1

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 43

Lecture 4 Part 1: Programming Tools

 Although the computer executes programs made up of


binary instructions, it would be too difficult and time
consuming to write programs in binary form.
 Programming tools give programmers the facilities they
need to prepare programs for use on the computer.
 These programming tools include:
 editors
 assemblers, interpreters, and compilers
 linking loaders
 debuggers

1
Programming Tools

 In a modern system, many of these functions


are combined into an Integrated
Development Environment (IDE).
 Visual Basic and Visual Studio, for example,
contain an editor, an interpreter, a compiler,
an object browser, a linking loader, and a
debugger, all in a single, integrated package
that allows switching among different
functions.

2
Programming Tools
 Editors
 They are used for entering and modifying a
program in text form into the computer
 e.g., Notepad
 Assemblers, Interpreters, and Compilers
 They are used for translating a program into
binary machine language which the
computer can understand and execute

3
Programming Tools

 Linkers
 They create an executable program by
linking a machine language program with
other separately translated binary
program modules and built-in function
libraries.
 Loaders
 They load an executable program into
memory for execution.

4
Programming Tools

 Linking Loaders
 They combine the functions of
the linker and loader.
 Debuggers
 They aid the programmer in
tracing and debugging a
program as it is executed

5
Programming Tools

 All these programming tools are


programs.
 Like other programs, these tools
process input data and produce
output data

6
Programming Tools

 Input data to an editor is text from keyboard


or a file, and commands from the keyboard
 The output is a data file or program (for
example in Java, or LMC assembly
language).
 This program file is called a source file.

7
Programming Tools

 The input to a compiler is a source file


 The output is a file of binary machine
code (called an object file)
 The input to a linker is one or more object
files
 Output is an executable file.

8
Program Translation
 On very early computers, programs were
written and entered in binary form
 In fact, data entry required the program to
be entered one binary word at a time,
using switches on the front panel of the
computer, one switch for each bit in the
word

9
Program Translation

 This process limited the size and


complexity of the programs
 Debugging was very difficult; errors in
bit entry were very common
 We will next look at a simple Little
Man Computer program and look at
what is involved when we need to just
add one statement.
10
Program Translation

 This LMC
program is 00 IN /input x
supposed to add 01 STO 05 /save x
two numbers 02 IN /input y
and output the 03 ADD 05 /y = y + x
result. It has one 04 OUT /output y
line missing. 05 DAT 00 /variable x

Addresses in
memory; not part of
the source code
11
Program Translation

 Adding the
HALT 00 IN /input x
instruction 01 STO 06 /save x
requires other 02 IN /input y
adjustments to 03 ADD 06 /y = y + x
be made to 04 OUT /output y
the program 05 HLT /halt
06 DAT 00 /variable x

12
Program Translation

 Imagine the effort required to modify a large


program!
 Then occurred the concept of using the
computer itself to ease the programmer’s work.
 The computer could be used to translate
programs from a more human-readable form
into executable binary code.

13
Program Translation

 This concept resulted in development of two


types of programming languages
 Machine-oriented programming languages
 These languages allowed programmers to
wirite programs that performed machine’s
instructions but could be read by humans
 The translator programs were known as
assemblers
 they assembled the individual parts of

each instruction into corresponding


machine instruction

14
Program Translation

 Function-oriented programming languages


 These attempted to free the programmer from
the details of the machine’s instructions
 These languages became known as high-level
languages; also called procedural languages
 Examples: COBOL, FORTRAN, C, Pascal,
BASIC, Modula 2, C++, Java, C#

15
Assembly Language and the
Assembler

Assembly Language is a computer


language that substitutes simple
alphanumeric names or
mnemonics for the op codes and
addresses found in machine
language instructions

16
Assembly Language and the Assembler

 Assembler is a computer utility that


translates assembly language program code
(source code) line by line, into executable
binary code (object code)
 It is the simplest form of human-
readable-to-machine-useable program
translator

17
Assembly Language
 We saw earlier the advantage of representing the
LMC op codes by short mnemonic names such as
ADD or LDA
 This made the program much easier to read and
write
 The simplest form of assembler would simply
translate the mnemonic op codes into machine
language, using a lookup table to do the translation

18
Assembly Language

 The examples in the previous slides show a


limitation of such a simple assembler
 A major concern of programmers is locating
the particular address where data is to be
found or the target address of a branching
instruction
 Changing the program forces the
programmer to find and recalculate all these
addresses

19
Assembly Language

 We can give alphanumeric names to important


addresses and the locations of data elements
 e.g., the target of jump instructions and the locations
of data elements
 Instead of writing LDA 57 to represent loading
of data from memory location 57, the
programmer could write LDA FIRST where
FIRST is equivalent to memory location 57

20
Assembly Language

 The programmer tells the assembler by putting a


label at location 57, indicating that the name
FIRST has been assigned to that location
 (57) FIRST: <instruction or data>
 In this case, FIRST is called a symbolic
address

21
Assembly Language
 Names for the
symbolic addresses START: IN
are chosen by the STO X
programmer IN
 names should have ADD X
some meaning OUT
 the simple example HLT
now becomes: X: DAT 00

22
Assembly Language

 In assembly language each line of text


represents a single instruction
 In LMC, one line of assembly language
code corresponds to exactly one memory
location, since every instruction in the
LMC occupies a single memory location

23
Assembly Language

 This is not true in real-world computers, where a


single instruction may occupy more than one
memory location
 The number of locations required for an
instruction varies from instruction to instruction
 For example, the IBM PC Assembly language
has instructions which are 2, 3 or 4 bytes long.

24
Assembly Language

 In the simple example given previously, we


see that the program can be translated into
machine code by translating the op code
mnemonics and substituting the address 06
for the symbolic address X in instructions 01
and 03.
 Program written in this form is much easier to
read than the corresponding machine code

25
Assembly Language

 When names are used to represent


addresses, the assembler can calculate the
correct memory address to substitute for the
name when it performs the translation
 This removes difficulty of locating
appropriate addresses from the programmer

26
Assembly Language

 When we use labels for various addresses,


inserting or deleting a line (or even large
scale editing) causes no problems.
 Re-translation by the assembler will
automatically produce the corrected code.

27
The Assembler
 We will translate the simple program by hand.
 Start at the top of the program, working through
each instruction, a line at time.
 For each line, determine the type of instruction from
the mnemonic, and the number of memory
locations required for that instruction.
 This information is stored in an operations table

28
The Assembler

Mnemonic op-code number


of operands
ADD 1 1 START: IN
SUB 2 1 STO X
STO 3 1 IN
LDA 5 1 ADD X
BR 6 1 OUT
BRZ 7 1 HLT
BRP 8 1 X: DAT 00
IN 901 0
OUT 902 0
HLT 0 0
LMC program
Operations table 29
The Assembler

 Decimal code for every instruction is calculated


from a single translation rule:
 code = 100 x op code + operand (if used)
 e.g., For ADD 24, code = 100 x 1 + 24 = 124

 Providing expected number of operands allows


LMC assembler to check for errors in the
source code

30
The Assembler

 The operations table will be more


complex in real computers with
 different addressing modes (more
about this later)
 different numbers of operands

31
The Assembler

 The principle is identical


 locate the correct mnemonic
 information in that data entry is then
used to check the source code format
and to allocate the required number of
memory locations for the instruction
and label names present in the source
code program

32
The Assembler

 For example, reading the first line in the


program verifies that the instruction has a
correct format, with no operands
 The label name START corresponds to
memory location 00.

33
The Assembler

 The name START is stored in a


separate table in case it is needed as an
address later
 this table is called the symbol table
 The symbol table is specific to a
particular program
 it is used during the final translation to look
up any symbols that are not located in the
operations table

34
The Assembler

 When you now move to line 01 in the


program and perform the same
process, you realise that the address START: IN
for X is not yet known STO X
IN
 line 6 has not been processed yet
ADD X
 not possible to complete the
OUT
translation of this instruction at this
time
HLT
X: DAT 00
 This situation will arise often in
assembly language programs

LMC program

35
The Assembler

 Instead of attempting to look


ahead each time, it makes sense
to continue processing each line,
in order, until the end of program is
reached

36
The Assembler

 A second pass through the program


will be used to complete the translation
process
 Use the first pass to check each
instruction for validity
 legal op code, correct number of
operands, etc

37
The Assembler
 Also during the first symbol table
pass, a symbol table
will be created
containing each label
name that appears in START 00
the program, together X 06
with the memory
address corresponding
to that label name
address
symbol

38
The Assembler

 The operations table and the symbol table


now provide everything that is required to
complete the translation of every instruction
 On the second pass, you complete the
translation by looking up the op code in the
operations table and, if necessary, the address
in the symbol table

39
The Assembler

 Any referenced address that is not


found in the symbol table has no
matching label
 to be flagged as an error condition
 The process of reading each line of
text, searching for op-codes,
operands, etc is called scanning or
parsing
40
The Assembler

START: IN 901 901


STO X 300 306
IN 901 901
ADD X 100 106
OUT 902 902
HLT 000 000
X: DAT 00 000 000

partial LMC final LMC


LMC assembly machine code after machine code after
language first pass second pass
source code
Output from the LMC two
pass assembler 41
Relocation

 Output from the assembler generally


assumes a fixed program starting point,
most commonly, memory location 0
 Most operating systems do not assign
programs to a particular location
 We assume that the program is relocatable
and assign it a location that is convenient
under the particular circumstances at the
load time
42
Relocation

 Assembler provides for relocation by


including an additional table that specifies
which locations must be adjusted at the
time that the program is actually loaded into
memory for execution.
 The table will stay with the executable code
and be used by the loader to make the
appropriate corrections at load time.

43

You might also like