the assembly language levelthe assembly language...

51
The Assembly Language Level The Assembly Language Level Chapter 7 1

Upload: others

Post on 16-Mar-2020

42 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

The Assembly Language LevelThe Assembly Language Level

Chapter 7

1

Page 2: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

Contemporary Multilevel Machines

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

Page 3: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

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

Page 4: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

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

Page 5: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

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

Page 6: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

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

Page 7: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

Why Use Assembly Language?

a) Performance

b) Access to the machine

7

Page 8: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

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

Page 9: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

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

Page 10: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

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

Page 11: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

Format of an Assembly Language Statement (3)

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

Page 12: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

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

Page 13: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

Pseudoinstructions (1)

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

Page 14: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

Pseudoinstructions (2)

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

Page 15: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

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

Page 16: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

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

Page 17: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

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

Page 18: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

Macro Definition, Call, Expansion (2)

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

Page 19: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

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

Page 20: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

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

Page 21: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

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

Page 22: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

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

Page 23: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

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

Page 24: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

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

Page 25: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

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

Page 26: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

Pass One (1)

P f i l bl

. . .

Pass one of a simple assembler.26

Page 27: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

Pass One (2). . .

P f i l bl. . .

Pass one of a simple assembler.27

Page 28: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

Pass One (3)

. . .

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

Page 29: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

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

Page 30: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

Pass Two (1)

P t f i l bl

. . .Pass two of a simple assembler.

30

Page 31: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

Pass Two (2). . .

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

Page 32: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

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

Page 33: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

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

Page 34: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

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

Page 35: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

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

Page 36: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

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

Page 37: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

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

Page 38: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

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

Page 39: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

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

Page 40: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

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

Page 41: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

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

Page 42: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

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

Page 43: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

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

Page 44: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

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

Page 45: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

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

Page 46: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

Structure of an Object Module

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

Page 47: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

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

Page 48: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

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

Page 49: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

Dynamic Linking in MULTICS (1)

B f EARTH i ll dBefore EARTH is called.49

Page 50: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

Dynamic Linking in MULTICS (2)

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

Page 51: The Assembly Language LevelThe Assembly Language Levelabdnoor80.weebly.com/uploads/1/2/2/8/12282674/ch7_coa.pdf · Assembly Language a) A pure assembly language is a language in which

Dynamic Linking in Windows

Use of a DLL file by two processes.51