The Assembly Language LevelThe Assembly Language...

Post on 16-Mar-2020

42 views 6 download

Transcript of The Assembly Language LevelThe Assembly Language...

The Assembly Language LevelThe Assembly Language Level

Chapter 7

1

Contemporary Multilevel Machines

A i l l tA six-level computer. The support method for each level is indicated below it .2

Assembly Language Level

a) It is implemented by translation rather than interpretation.a) It is implemented by translation rather than interpretation.

b) Programs that convert a user’s program written in some language to anther language are called translator.

) Th l i hi h th i i l i itt i ll dc) The language in which the original program is written is called the source language and the language to which it is converted is called the target language.

d) Translation is used when a processor is available for the t t l b t t f th ltarget language but not for the source language.

3

Assembly Language Level

a) In translation, the original program is converted to an equivalent program called an object program whose q p g j p gexecution is carried out only after the translation has been completed.

b) In interpretation, there is only one step: executing the original source program. No equivalent program needs to besource program. No equivalent program needs to be generated first.

4

Translator

a) Translator can be roughly divided into two groups.

b) When the source language is essentially a symbolic representation for a numerical machine language therepresentation for a numerical machine language, the translator is called an assembler and the source language is called an assembly language.

c) When the source language is a high-level language and the t t l i ith i l hi ltarget language is either a numerical machine language or a symbolic one, the translator is called a compiler.

5

Assembly Language

a) A pure assembly language is a language in which each statement produces exactly one machine instruction.p y

b) It is much easy to program in symbolic forms.

c) The assembly programmer has access to all the features and instructions available on the target machineinstructions available on the target machine.

d) An assembly language program can run only on one family ofd) An assembly language program can run only on one family of machines, whereas a program written in a high-level language can potentially run on many machines.

6

Why Use Assembly Language?

a) Performance

b) Access to the machine

7

Why Use Assembly Language?

Comparison of assembly language and high level languageComparison of assembly language and high-level language programming, with and without tuning. 8

Format of an Assembly Language Statement (1)

C t ti f N I J ( ) P ti 4Computation of N = I + J. (a) Pentium 4.9

Format of an Assembly Language Statement (2)

C t ti f N I J (b) M t l 680 0Computation of N = I + J. (b) Motorola 680x0.10

Format of an Assembly Language Statement (3)

C t ti f N I J ( ) SPARCComputation of N = I + J. (c) SPARC.11

Pseudoinstructions

a) In addition to specifying which machine instructions to execute, an assembly language program can also contain , y g g p gcommands to the assembler itself.

b) Commends to the assembler itself are called pseudoinstructions or sometimes assembler directives.

12

Pseudoinstructions (1)

Some of the pseudoinstructions available in theSome of the pseudoinstructions available in the Pentium 4 assembler (MASM). 13

Pseudoinstructions (2)

Some of the pseudoinstructions available in theSome of the pseudoinstructions available in the Pentium 4 assembler (MASM). 14

Macros

a) Assembly language programmers frequently need to repeat sequences of instructions several times within a program.q p g

b) A way is to make the sequence into a procedure and call it wherever it is needed. But it requires a procedure call instruction and a return instruction.

c) A macro definition is a way to give a name to a piece of text. After a macro has been defined, the programmer can write the macro name instead of the piece of program.

d) A macro is an abbreviation for a piece of textd) A macro is an abbreviation for a piece of text.15

Macro Definition, Call, Expansion (1)

Assembly language code for interchanging P and Q twiceAssembly language code for interchanging P and Q twice. (a) Without a macro. (b) With a macro. 16

Macrosa) A macro header gives the name of the macro.

b) The text comprises the body of the macro.

c) A pseudoinstruction marks the end of the definition.

d) When the assembler encounters a macro definition it saves itd) When the assembler encounters a macro definition, it saves it in a macro definition table for subsequent use.

e) The use of a macro name as an opcode is known as a macro call and its replacement by the macro body is called macro expansionexpansion.

17

Macro Definition, Call, Expansion (2)

C i f ll ith d llComparison of macro calls with procedure calls.18

Macros with ParametersFormal parameters

A t l t

Nearly identical sequences of statements

Actual parameters

Nearly identical sequences of statements.(a) Without a macro. (b) With a macro. 19

Advanced Features

a) Avoid label duplication – to allow a label to be declared local, with the assembler automatically generating a different labelwith the assembler automatically generating a different label on each expansion of the macro.

20

The Assembly Process

a) It might seen natural to have an assembler that reads one statement then translates it to machine language and finallystatement, then translates it to machine language, and finally outputs the generated machine language onto a file. Unfortunately, this strategy does not work.

b) A branch to L, can not be assembled until the address of statement L is known Forward reference problem – astatement L is known. Forward reference problem – a reference has been made to a symbol whose definition will only occur later.

21

The Assembly Processa) The assembler may in fact read the source program twice.

Two pass. The translator that reads the input program twice i ll d t t l tis called a two-pass translator.

b) On pass one, the definitions of symbols, including statementb) On pass one, the definitions of symbols, including statement labels, are collected and stored in a table. By the time the second pass begins, the values of all symbols are known; thus no forward reference remains and each statement canthus no forward reference remains and each statement can be read, assembled, and output.

) A th h i t f di th blc) Another approach consists of reading the assembly program once, converting it to an intermediate form, and storing this form in a table in memory. Then a second pass is made over th t bl i t d f th ( d hthe table instead of over the source program. (need enough memory) 22

Two Pass Assemblers (1)

The instruction location counter (ILC) keeps track of the addresswhere the instructions will be loaded in memory. In this example, the y p

statements prior to MARIA occupy 100 bytes.23

Two Pass Assemblers (2)

A b l t bl f th f Fi 7 7A symbol table for the program of Fig. 7-7.24

Two Pass Assemblers (3)

A f t f th d t bl f P ti 4 blA few excerpts from the opcode table for a Pentium 4 assembler.25

Pass One (1)

P f i l bl

. . .

Pass one of a simple assembler.26

Pass One (2). . .

P f i l bl. . .

Pass one of a simple assembler.27

Pass One (3)

. . .

P f i l blPass one of a simple assembler.28

Pass Two

a) The function of pass two is to generate the object program and possibly print the assembly listingand possibly print the assembly listing.

b) In addition, pass two must output certain information needed ) p pby the linker for linking up procedures assembled at different times into a single executable file.

29

Pass Two (1)

P t f i l bl

. . .Pass two of a simple assembler.

30

Pass Two (2). . .

P t f i l blPass two of a simple assembler.31

The Symbol Tablea) During pass one, the assembler accumulates information

about symbols and their values that must be stored in the symbol table for lookup during pass two.

b) All the organizing methods attempt to simulate an associativeb) All the organizing methods attempt to simulate an associative memory, which conceptually is a set of pairs (symbol, value).

c) The simplest implementation is to implement the symbol table as an array of pairs. On average, half of the table will have to be searched on each lookupbe searched on each lookup.

d) Binary search – A table of size n entries can be searched ind) Binary search A table of size n entries can be searched in about log2n attempts.

32

The Symbol Table (1)

Hash coding (a) Symbols values and the hash codes derivedHash coding. (a) Symbols, values, and the hash codes derived from the symbols. 33

The Symbol Table (2)

Hash coding (b) Eight entry hash table with linked lists ofHash coding. (b) Eight-entry hash table with linked lists of symbols and values. 34

a) With n symbols and k slots in the hash table, the average list ill h l h /kwill have length n/k.

b) By choosing k approximately equal to n symbols can beb) By choosing k approximately equal to n, symbols can be located with only about one lookup on the average.

c) By adjusting k we can reduce table size at the expense of slower lookups.

35

Linking and Loading

a) Most program consists of more than on e procedure.

b) Compilers and assemblers generally translate one procedure at a time and put the translated output on disk.

c) Before the program can be run, all the translated procedures t b f d d li k d t th lmust be found and linked together properly.

d) Programs that perform these functions are called linkersd) Programs that perform these functions are called linkers.

36

Linking and Loading

Generation of an executable binary program from a collection ofGeneration of an executable binary program from a collection ofindependently translated source procedures requires using a linker.37

Linker

a) The linker’s function is to collect procedures translated separately and link them together to be run as an executableseparately and link them together to be run as an executable binary program.

b) The translation from source procedure to object module represents a change of level. The linking process, however, does not represent a change of level, since both the linker’sdoes not represent a change of level, since both the linker s input and the linker’s output are programs for the same virtual machine.

c) The two-step process of translating and linking can save a great deal of time during the development of a programgreat deal of time during the development of a program.

38

Tasks Performed by the Linker (1)

E h d l h it dd t ti t 0Each module has its own address space, starting at 0.39

Tasks Performed by the Linker (2)

E h d l h it dd t ti t 0Each module has its own address space, starting at 0.40

Tasks Performed by the Linker (3)

E h d l h it dd t ti t 0Each module has its own address space, starting at 0.41

Tasks Performed by the Linker (4)

E h d l h it dd t ti t 0Each module has its own address space, starting at 0.42

Tasks Performed by the Linker (5)

The object modules of Fig. 7-14 after being positioned in the binary image but before being relocated and linked.

43

Tasks Performed by the Linker (6)

The same object modules after linking and after relocation has been performed Together they form an executable binarybeen performed. Together they form an executable binary program, ready to run 44

Linking Steps

a) It constructs a table of all the object modules and their l thlengths.

b) Based on this table it assigns a starting address to eachb) Based on this table, it assigns a starting address to each object module.

c) It finds all the instructions that reference memory and adds to each a relocation constant equal to the starting address of its modulemodule.

d) It finds all the instructions that reference other procedures d) ds a e s uc o s a e e e ce o e p ocedu esand inserts the address of these procedures in place.

45

Structure of an Object Module

The internal structure of an object module produced by a translator.46

Dynamic Linking

a) A more flexible way to link separately compiled procedures isa) A more flexible way to link separately compiled procedures is to link each procedure at the time it is first called.

b) This process is known as dynamic linking.

47

Binding Time and Dynamic Relocation

The relocated binary program of Fig 7 15(b) moved up 300 addressesThe relocated binary program of Fig. 7-15(b) moved up 300 addresses. Many instructions now refer to an incorrect memory address.

48

Dynamic Linking in MULTICS (1)

B f EARTH i ll dBefore EARTH is called.49

Dynamic Linking in MULTICS (2)

Aft EARTH h b ll d d li k dAfter EARTH has been called and linked.50

Dynamic Linking in Windows

Use of a DLL file by two processes.51