Download presentation
Presentation is loading. Please wait.
Published byJoshua Mathews Modified over 8 years ago
1
1 CE 454 Computer Architecture Lecture 11 Ahmed Ezzat The Assembly Language Level, Ch-(7.1 – 7.4)
2
CE 454Ahmed Ezzat 2 Outline Introduction to Assembly Language Macros The Assembly Process Linking and Loading
3
CE 454Ahmed Ezzat 3 Introduction to Assembly Language Assembly language level differ significantly from microarchitecture, ISA, and OS levels – it is implemented by translation rather than by interpretation Translation converts source language target language. Target language is either executed by the hardware or by an interpreter Executing target language is equivalent to executing the source language if a processor for the source language is available
4
CE 454Ahmed Ezzat 4 Introduction to Assembly Language In translation, we have two steps: generating an equivalent program in the target language executing the target program on available hardware or using an interpreter In interpretation, we have one step: executing the source program, and no target program is generated While program is being executed, only three levels are involved: the microarchitecture level, the ISA level, and the OS machine level
5
CE 454Ahmed Ezzat 5 Introduction to Assembly Language: What is an Assembly Language Translators can be either: – Assembler: SRC is symbolic representation and target is machine language representation – Compiler: SRC is HLL and target is either a binary machine language or a symbolic representation Assembly stmt produces exactly one machine instruction (binary representation) Assembly programmers can give symbolic names to memory locations / addresses
6
CE 454Ahmed Ezzat 6 Introduction to Assembly Language: What is an Assembly Language Assembly programmers have access to all machine features. HLL programmers do not! – For example: If target machine has an overflow-bit, an assembly language program can test it, but a C / Java program cannot directly test it! An assembly language program can only run on one family of machines, whereas an HLL program can potentially run on many machines
7
CE 454Ahmed Ezzat 7 Introduction to Assembly Language: Why Use an Assembly Language Given that assembly language programs are difficult to write/debug, when it is appropriate to use? – Performance: more efficient than HLL – Access to the machine hardware
8
CE 454Ahmed Ezzat 8 Introduction to Assembly Language: Format of an Assembly Language Statement Assembly language statements for different machines have sufficient resemblance to one another
9
CE 454Ahmed Ezzat 9 Introduction to Assembly Language: Format of an Assembly Language Statement We will use Microsoft Assembler syntax (MASM) for the Intel family Conventions: Opcode and Registers are Upper case Assembly statements consist of 4 parts: – Label field: symbolic name to memory address, typically 6-8 characters followed by a colon – Opcode: symbolic abbreviation of the Opcode or a command to the assembler – Operand field: specify the address & registers used as operands. Operands can be registers, constants, memory locations, etc. – Comments field: explain how the program works? Has no effect on the assembly process or on the generated program
10
CE 454Ahmed Ezzat 10 Introduction to Assembly Language: Pseudo-instruction Pseudoinstructions or assembler directives are commands to the assembler itself, e.g., allocate some storage, eject to a new page on the listing
11
CE 454Ahmed Ezzat 11 Macros: Problem Definition Repeating sequence of assembly instructions: – Can be achieved through repeat writing – tedious and hard to maintain! – Can be achieved through procedure call. We would need a procedure call and return instructions. If the procedure is short, the overhead of procedure call can be relatively high – Macros solve this problem
12
CE 454Ahmed Ezzat 12 Macros: Macros Definition, Call, and Expansion Given a name to a sequence of instructions, the program can write the macro name instead of the sequence of instructions SWAP is a macro abbreviating the 4 instructions: – Macro header defining the name – Sequence of instructions – Pseudoinstruction marking the end of the macro definition
13
CE 454Ahmed Ezzat 13 Macros: Macros Definition, Call, and Expansion When assembler encounters a macro definition, it saves it in a macro definition table for subsequent use Whenever the assembler encounters a macro ( SWAP ) as an Opcode (macro call), the assembler replaces it by the macro body ( macro expansion ) Macro expansion happens during the assembly process and not during program execution Macro call is an instruction to the assembler, while procedure call is a machine instruction that is part of the object program and will be executed at runtime
14
CE 454Ahmed Ezzat 14 Macros: Macros Definition, Call, and Expansion Assembly process is 2-passes: – Pass-1: all macro definitions are saved and macro calls are expanded – Pass-2: the expanded program is translated into machine instructions
15
CE 454Ahmed Ezzat 15 Macros: Macros with Parameters Macros to exchange two values: Macro definition support “ formal parameters ”, and macro calls would supply the “ actual parameters ” When macro is expanded, each formal parameter is replaced by the corresponding actual parameter
16
CE 454Ahmed Ezzat 16 Macros: Advanced Features Macro body contains a Label ! On expansion, we will have duplicate labels – this is an error! MASM allows a label to be declared LOCAL. This allows the assembler to generate automatically different label on each expansion
17
CE 454Ahmed Ezzat 17 Macros: Advanced Features Macro body includes other macro definition. Either way, the Macro M2 will be defined, but the definition will depend on whether the program is being assembled on a 16- or 32-bit machine Macro can call another macro, including themselves. This requires passing different actual parameters on each expansion, and must test the parameters and terminate the recursion M1MACRO IF WORDSIZE GT 16 M2MACRO … ENDM ELSE M2MACRO … ENDM ENDIF ENDM
18
CE 454Ahmed Ezzat 18 Macros: Implementation of Macro Facility in Assembly The assembler must perform two functions: – Save macro definition in a “ macro definition ” table:. During Pass-1, Opcode is looked up and macros are expanded – Macro expansion : replaces the macro name with the corresponding macro definition & replace the formal parameters with the actual parameters
19
CE 454Ahmed Ezzat 19 The Assembly Process: Two-Pass Assembler Forward reference problem: branch to L, where L is being used before it is defined ! Approach I: – On Pass-1, definition of symbols, statement labels are collected and stored into a symbol table – On Pass-2, values of all symbols are known, thus no forward reference problem, and each statement can be read, assembled/translated into a machine instruction and output Approach II: – On Pass-1, read the assembly program once, convert it into an intermediate form, store it into a table in memory – On Pass-2, pass is made on the table to produce the machine instructions. IN principal this approach saves I/O time Either way, in Pass-1, we need to save all macro definitions and expand the calls as they are encountered
20
CE 454Ahmed Ezzat 20 The Assembly Process: Pass One Main task is to build up a table called symbol table, containing the values of all symbols. A symbol is either a label or a value assigned a symbolic name by means of a pseudoinstruction BUFSIZE EQU 8192 Assembly must know what address the instruction will have during execution! Assembler maintains a variable known as Instruction Location Counter ( ILC ).
21
CE 454Ahmed Ezzat 21 The Assembly Process: Pass One ILC is se to 0 at the beginning of Pass-1 and is incremented by the instruction length for each instruction processed
22
CE 454Ahmed Ezzat 22 The Assembly Process: Pass One Pass-I typically uses at least 3 tables: – Symbol table: labels – Pseudoinstruction table: explicit definitions (EQU) – Opcode table: entry per symbolic Opcode (mnemonic) – Optionally literal table: these are constants that assembler automatically reserves memory for
23
CE 454Ahmed Ezzat 23 The Assembly Process: Pass Two Generate the object program and possibly print the assembly listing Output certain information needed by linker for linking up multiple assembly modules into a single executable file Pas-2 reads lines one at a time and process them one at a time using the information generated from Pass-1 Assembler print an error message and try to continue assembly
24
CE 454Ahmed Ezzat 24 The Assembly Process: Pass Two Common errors handled in Pass-2 include: – A symbol has been used but not defined – A symbol has been defined more than once – Opcode is not a legal Opcode – Opcode is not supplied with enough Operands or too many Operands – Octal number contains an 8 or 9 – Illegal register use (e.g., branch to a register) – Missing END statement
25
CE 454Ahmed Ezzat 25 The Assembly Process: The Symbol Table Symbol table information is built in Pass-1. Several ways to organize it: – Simulate associative memory: pairs. Given a symbol returns the value – Array of pairs, and a lookup method. Simple but slow – Sort symbols, and use binary search – Hash coding (simulate associative memory): requires hashing function that maps any symbol into an integer in the range of ( 0 k-1 ). Symbols will be stored into a table of k buckets numbered ( 0 k-1 ). All symbols that hash into bucket i in the hash table are stored on a linked list pointed to by slot i in the hash table. With n symbols and k buckets, the average bucket list length is ( n / k ). If n = k then one lookup on the average is needed
26
CE 454Ahmed Ezzat 26 The Assembly Process: The Symbol Table
27
CE 454Ahmed Ezzat 27 Linking and Loading Most programs consist of more than one module/procedure Compiler/Assembler translates each module into a separate file Before running the application, all modules must be linked together properly by a “Linker / Linking Loader / Linking Editor” into one executable binary program Why 2 steps: Assembly Linking? – If all is done in one step, then changing one statement in one module would require all modules to be retranslated! – With 2 steps process, changing one statement in one module would require retranslating only that module still needs to link all modules in a new executable
28
CE 454Ahmed Ezzat 28 Linking and Loading
29
CE 454Ahmed Ezzat 29 Linking and Loading: Tasks Performed by the Linker Setting ILC initially to zero is equivalent to assuming that the object module will be loaded a virtual address zero Linking all object modules into main memory to form the image of the executable binary program Perform relocation and external reference resolution as follows: – Construct a table of all object modules and their length – Assign a starting address to each object module – Find all instructions that reference memory address, add to each a relocation constant equal to the starting address of its module – Find all instructions that reference other procedures and insert the address of that procedure in place
30
CE 454Ahmed Ezzat 30 Linking and Loading: Structure of an Object Module Typically object modules consists of six parts: – Name of the module – List of symbols used in the module but defined in other module + which instruction uses them – Assembled code and constants. Only part loaded in memory for execution – Relocation dictionary – End-of-module indication, may be checksum, and start address to begin execution
31
CE 454Ahmed Ezzat 31 Linking and Loading: Structure of an Object Module Most linkers require 2 passes: – 1 st Pass: read all object modules and build a table of module names and their length, global symbol table consisting of entry points and external references – 2 nd Pass: object modules are read, relocated, and linked one module at a time
32
CE 454Ahmed Ezzat 32 Linking and Loading: Binding Time and Dynamic Relocation
33
CE 454Ahmed Ezzat 33 Linking and Loading: Binding Time and Dynamic Relocation In multiprogramming, a program can be paged out then paged back in to memory. It is impossible to guarantee that the program will be read back into the same location every time! – Figure 7-15 (a) shows the 4 modules of one application laid consecutively in memory, each starting from address 0. – Figure 7-15 (b) shows the same executable but after linking and relocation are done. – Figure 7-17 shows the same executable of Figure 7-15 (b) after being moved up 300 bytes (reloaded at address 400 rather than 100). Instructions are referring to incorrect addresses. Furthermore, relocation information has been long discarded
34
CE 454Ahmed Ezzat 34 Linking and Loading: Binding Time and Dynamic Relocation Moving programs in memory is related to the time where symbols are bound to memory addresses. Six possibilities for binding time are possible: – When the program is written – When the program is translated – When the program is linked but before it is loaded – When the program is loaded – When a base register used for addressing is loaded – When the instruction containing the address is executed If instruction is moved after binding, it will be incorrect
35
CE 454Ahmed Ezzat 35 Linking and Loading: Binding Time and Dynamic Relocation Binding can be divided into 2 steps: – Symbolic names are bound to virtual addresses – Virtual addresses are bound to physical addresses An executable binary program is ONLY the first step in binding, i.e., symbolic names are bound into virtual addresses Mechanisms to facilitate moving programs in memory include: – Paging: only a page table entry needs to be changed – Runtime relocation register: when program is moved, the OS needs to update the relocation register – Referencing memory relative to the Program Counter (PIC): when a program is moved in memory only the PC needs to be updated. PIC code can be placed anywhere in the virtual address space without need for relocation
36
CE 454Ahmed Ezzat 36 Linking and Loading: Dynamic Linking Linking strategy discussed so far assumes that all modules are linked before the program can begin execution A more flexible model is to link each module at the time it is first called. This is known as dynamic linking Unix supports shared libraries which are similar to Windows DLLs. It is an achieve file containing multiple procedures or data modules that are present in memory at runtime and can be bound to multiple processes at the same time Standard C library and much of the networking code is shared libraries Unix supports implicit linking. A shared library consists of 2 parts: – Host library, which is statically linked with the executable file – Target library, which is called at run time.
37
CE 454Ahmed Ezzat 37
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.