Tài liệu THE ASSEMBLY LANGUAGE LEVEL-7 pdf

20 426 0
Tài liệu THE ASSEMBLY LANGUAGE LEVEL-7 pdf

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

7 THE ASSEMBLY LANGUAGE LEVEL 2222222222222222222222222222222222222222222222222222222222222222222 1 Programmer-years to Program execution 12222222222222222222222222222222222222222222222222222222222222222222 produce the program time in seconds 1 1 Assembly language 50 33 1 1 10 100 1 High-level language 1 1 1 Mixed approach before tuning 1 1 Critical 10% 1 90 1 1 Other 90% 10 1 1 33 33 1 Total 10 100 1 1 1 1 Mixed approach after tuning 1 1 1 Critical 10% 30 1 Other 90% 10 1 1 33 33 1 1 Total 15 40 12222222222222222222222222222222222222222222222222222222222222222222 Figure 7-1 Comparison of assembly language and high-level language programming, with and without tuning Opcode Operands Comments 2Label 22222222222222222222222222222222222222222222222222222222222222222222 FORMULA: MOV EAX,I ; register EAX = I ADD EAX,J ; register EAX = I + J MOV N,EAX ;N=I+J I J N DW DW DW ; reserve bytes initialized to ; reserve bytes initialized to ; reserve bytes initialized to (a) Label Opcode Operands Comments 2222222222222222222222222222222222222222222222222222222222222222222222 FORMULA MOVE.L I, D0 ; register D0 = I ADD.L J, D0 ; register D0 = I + J MOVE.L D0, N ;N=I+J I J N DC.L DC.L DC.L ; reserve bytes initialized to ; reserve bytes initialized to ; reserve bytes initialized to (b) Opcode Operands Comments 2Label 22222222222222222222222222222222222222222222222222222222222222222222222222222222222 FORMULA: SETHI %HI(I),%R1 ! R1 = high-order bits of the address of I LD [%R1+%LO(I)],%R1 ! R1 = I SETHI %HI(J),%R2 ! R2 = high-order bits of the address of J LD [%R2+%LO(J)],%R2 ! R2 = J NOP ! wait for J to arrive from memory ADD %R1,%R2,%R2 ! R2 = R1 + R2 SETHI %HI(N),%R1 ! R1 = high-order bits of the address of N ST %R2,[%R1+%LO(N)] I: J: N: WORD WORD WORD ! reserve bytes initialized to ! reserve bytes initialized to ! reserve bytes initialized to (c) Figure 7-2 Computation of N = I + J (a) Pentium II (b) Motorola 680x0 (c) SPARC 222222222222222222222222222222222222222222222222222222222222222222222222222 1222222222222222222222222222222222222222222222222222222222222222222222222222 Pseudoinstr Meaning 1 SEGMENT Start a new segment (text, data, etc.) with certain attributes 222222222222222222222222222222222222222222222222222222222222222222222222222 1 1222222222222222222222222222222222222222222222222222222222222222222222222222 End the current segment ENDS 1 ALIGN 1222222222222222222222222222222222222222222222222222222222222222222222222222 Control the alignment of the next instruction or data 1222222222222222222222222222222222222222222222222222222222222222222222222222 Define a new symbol equal to a given expression EQU 1 222222222222222222222222222222222222222222222222222222222222222222222222222 DB Allocate storage for one or more (initialized) bytes DD 1222222222222222222222222222222222222222222222222222222222222222222222222222 Allocate storage for one or more (initialized) 16-bit halfwords 1 DW Allocate storage for one or more (initialized) 32-bit words 222222222222222222222222222222222222222222222222222222222222222222222222222 1 DQ 1222222222222222222222222222222222222222222222222222222222222222222222222222 Allocate storage for one or more (initialized) 64-bit double words 1 PROC Start a procedure 222222222222222222222222222222222222222222222222222222222222222222222222222 1 ENDP 1222222222222222222222222222222222222222222222222222222222222222222222222222 End a procedure 1 MACRO Start a macro definition 222222222222222222222222222222222222222222222222222222222222222222222222222 1 ENDM End a macro definition 1222222222222222222222222222222222222222222222222222222222222222222222222222 1 1222222222222222222222222222222222222222222222222222222222222222222222222222 1 PUBLIC Export a name defined in this module 1 EXTERN 1222222222222222222222222222222222222222222222222222222222222222222222222222 Import a name from another module 1222222222222222222222222222222222222222222222222222222222222222222222222222 Fetch and include another file INCLUDE 1 IF 1222222222222222222222222222222222222222222222222222222222222222222222222222 Start conditional assembly based on a given expression 1222222222222222222222222222222222222222222222222222222222222222222222222222 1 ELSE Start conditional assembly if the IF condition above was false 1 222222222222222222222222222222222222222222222222222222222222222222222222222 ENDIF End conditional assembly COMMENT Define a new start-of-comment character 1222222222222222222222222222222222222222222222222222222222222222222222222222 1 PAGE Generate a page break in the listing 222222222222222222222222222222222222222222222222222222222222222222222222222 1 1 END 222222222222222222222222222222222222222222222222222222222222222222222222222 Terminate the assembly program Figure 7-3 Some of the pseudoinstructions available in the Pentium II assembler (MASM) MOV MOV MOV MOV EAX,P EBX,Q Q,EAX P,EBX MOV MOV MOV MOV EAX,P EBX,Q Q,EAX P,EBX SWAP MACRO MOV EAX,P MOV EBX,Q MOV Q,EAX MOV P,EBX ENDM SWAP SWAP (a) (b) Figure 7-4 Assembly language code for interchanging P and Q twice (a) Without a macro (b) With a macro 2222222222222222222222222222222222222222222222222222222222222222222222222222 1 Item Macro call Procedure call 21 222222222222222222222222222222222222222222222222222222222222222222222222222 1 1 When is the call made? 21 222222222222222222222222222222222222222222222222222222222222222222222222222 During assembly During execution 1 Is the body inserted into the object Yes No 1 program every place the call is 1 1 1 made? 21 222222222222222222222222222222222222222222222222222222222222222222222222222 1 1 Is a procedure call instruction No Yes 1 inserted into the object program 1 1 1 and later executed? 21 222222222222222222222222222222222222222222222222222222222222222222222222222 1 1 Must a return instruction be used No Yes 12222222222222222222222222222222222222222222222222222222222222222222222222222 1 after the call is done? 1 1 How many copies of the body ap1 One per macro call 1 pear in the object program? 12222222222222222222222222222222222222222222222222222222222222222222222222222 1 Figure 7-5 Comparison of macro calls with procedure calls MOV MOV MOV MOV EAX,P EBX,Q Q,EAX P,EBX MOV MOV MOV MOV EAX,R EBX,S S,EAX R,EBX CHANGE MACRO P1, P2 MOV EAX,P1 MOV EBX,P2 MOV P2,EAX MOV P1,EBX ENDM CHANGE P, Q CHANGE R, S (a) (b) Figure 7-6 Nearly identical sequences of statements (a) Without a macro (b) With a macro Label Opcode Operands Comments Length ILC 222222222222222222222222222222222222222222222222222222222222222222222222222 MARIA: MOV EAX,I EAX = I 100 MOV EBX, J EBX = J 105 ROBERTA: MOV ECX, K ECX = K 111 117 IMUL EAX, EAX EAX = I * I 119 IMUL EBX, EBX EBX = J * J 122 IMUL ECX, ECX ECX = K * K 125 MARILYN: ADD EAX, EBX EAX = I * I + J * J 127 ADD EAX, ECX EAX = I * I + J * J + K * K STEPHANY: JMP DONE branch to DONE 129 Figure 7-7 The instruction location counter (ILC) keeps track of the address where the instructions will be loaded in memory In this example, the statements prior to MARIA occupy 100 bytes 222222222222222222222222222222222222222222222222 1222222222222222222222222222222222222222222222222 Value 1 Symbol Other information 1 1 MARIA 1222222222222222222222222222222222222222222222222 100 1 1222222222222222222222222222222222222222222222222 ROBERTA 111 1 MARILYN 125 1 222222222222222222222222222222222222222222222222 1 1 STEPHANY 129 1222222222222222222222222222222222222222222222222 Figure 7-8 A symbol table for the program of Fig 7-7 2222222222222222222222222222222222222222222222222222222222222222222222 1 First Second Hexadecimal Instruc- Instruc- 1 Opcode operand operand 1 tion tion opcode 1 1 1 length class 21 222222222222222222222222222222222222222222222222222222222222222222222 1 1 1 12222222222222222222222222222222222222222222222222222222222222222222222 1 1 1 AAA — — 37 ADD EAX immed32 1 1 05 21 222222222222222222222222222222222222222222222222222222222222222222222 1 1 1 ADD 01 19 reg reg 1 1 21 222222222222222222222222222222222222222222222222222222222222222222222 AND EAX immed32 1 1 25 21 222222222222222222222222222222222222222222222222222222222222222222222 1 1 1 AND 21 19 12222222222222222222222222222222222222222222222222222222222222222222222 reg reg 1 1 Figure 7-9 A few excerpts from the opcode table for a Pentium II assembler public static void pass3one( ) { // This procedure is an outline of pass one of a simple assembler boolean more3input = true; // flag that stops pass one String line, symbol, literal, opcode; // fields of the instruction int location 3counter, length, value, type; // misc variables final int END3STATEMENT = −2; // signals end of input location 3counter = 0; initialize 3tables( ); // assemble first instruction at // general initialization while (more 3input) { line = read3next3line( ); length = 0; type = 0; // more3input set to false by END // get a line of input // # bytes in the instruction // which type (format) is the instruction if (line 3is3not3comment(line)) { symbol = check3for3symbol(line); // is this line labeled? if (symbol != null) // if it is, record symbol and value enter3new3symbol(symbol, location 3counter); literal = check3for3literal(line); // does line contain a literal? if (literal != null) // if it does, enter it in table enter3new3literal(literal); // Now determine the opcode type −1 means illegal opcode opcode = extract3opcode(line); // locate opcode mnemonic type = search3opcode3table(opcode); // find format, e.g OP REG1,REG2 if (type < 0) // if not an opcode, is it a pseudoinstruction? type = search3pseudo3table(opcode); switch(type) { // determine the length of this instruction case 1: length = get3length3of3type1(line); break; case 2: length = get3length3of3type2(line); break; // other cases here } } write 3temp3file(type, opcode, length, line);// useful info for pass two location 3counter = location 3counter + length;// update loc3ctr if (type == END3STATEMENT) { // are we done with input? more3input = false; // if so, perform housekeeping tasks rewind 3temp3for3pass3two( ); // like rewinding the temp file sort3literal 3table( ); // and sorting the literal table remove3redundant3literals( ); // and removing duplicates from it } } } Figure 7-10 Pass one of a simple assembler public static void pass3two( ) { // This procedure is an outline of pass two of a simple assembler boolean more 3input = true; // flag that stops pass one String line, opcode; // fields of the instruction int location3counter, length, type; // misc variables final int END3STATEMENT = −2; // signals end of input final int MAX3CODE = 16; // max bytes of code per instruction byte code[ ] = new byte[MAX 3CODE]; // holds generated code per instruction location3counter = 0; // assemble first instruction at while (more3input) { // more3input set to false by END type = read3type( ); // get type field of next line opcode = read3opcode( ); // get opcode field of next line length = read3length( ); // get length field of next line line = read3line( ); // get the actual line of input if (type != 0) { // type is for comment lines switch(type) { // generate the output code case 1: eval3type1(opcode, length, line, code); break; case 2: eval3type2(opcode, length, line, code); break; // other cases here } } write3output(code); // write the binary code write3listing(code, line); // print one line on the listing location3counter = location3counter + length;// update loc3ctr if (type == END3STATEMENT) {// are we done with input? more3input = false; // if so, perform housekeeping tasks finish3up( ); // odds and ends } } } Figure 7-11 Pass two of a simple assembler Andy Anton Cathy Dick Erik Frances Frank Gerrit Hans Henri Jan Jaco Maarten Reind Roel Willem Wiebren 14025 31253 65254 54185 47357 56445 14332 32334 44546 75544 17097 64533 23267 63453 76764 34544 34344 3 4 (a) Hash table Linked table Andy 14025 Maarten 23267 Reind 63453 Wiebren 34344 Henri 75544 Frances 56445 Frank 14332 Hans 44546 Gerrit 32334 Jan 17097 Cathy 65254 Jaco 64533 Willem 34544 Roel 76764 Dick 54185 Anton 31253 Erik 47357 (b) Figure 7-12 Hash coding (a) Symbols, values, and the hash codes derived from the symbols (b) Eight-entry hash table with linked lists of symbols and values Source procedure Source procedure Source procedure Object module Translator Object module Linker Executable binary program Object module Figure 7-13 Generation of an executable binary program from a collection of independently translated source procedures requires using a linker Object module B 600 500 CALL C Object module A 400 400 300 CALL B 300 200 MOVE P TO X 200 100 100 MOVE Q TO X BRANCH TO 200 BRANCH TO 300 Object module C 500 400 CALL D Object module D 300 300 200 MOVE R TO X MOVE S TO X 100 100 200 BRANCH TO 200 BRANCH TO 200 Figure 7-14 Each module has its own address space, starting at 1900 1800 1900 MOVE S TO X 1700 1600 1500 Object module D BRANCH TO 200 1500 CALL D 1000 MOVE R TO X 1300 BRANCH TO 200 1100 1000 CALL C MOVE Q TO X Object module B 800 700 600 600 BRANCH TO 300 400 CALL B 300 MOVE P TO X 200 100 CALL 1600 MOVE R TO X Object module C BRANCH TO 1300 CALL 1100 900 700 500 BRANCH TO 1800 1200 900 800 Object module D 1400 Object module C 1200 1100 MOVE S TO X 1700 1600 1400 1300 1800 500 Object module A MOVE Q TO X Object module B BRANCH TO 800 400 CALL 500 300 MOVE P TO X Object module A 200 BRANCH TO 200 100 BRANCH TO 300 Figure 7-15 (a) The object modules of Fig 7-14 after being positioned in the binary image but before being relocated and linked (b) The same object modules after linking and after relocation has been performed Together they form an executable binary program, ready to run End of module Relocation dictionary Machine instructions and constants External reference table Entry point table Identification Figure 7-16 The internal structure of an object module produced by a translator 2200 2100 MOVE S TO X 2000 1900 1800 Object module D BRANCH TO 1800 CALL 1600 1700 1600 MOVE R TO X Object module C 1500 1400 1300 BRANCH TO 1300 CALL 1100 1200 1100 MOVE Q TO X Object module B 1000 900 800 BRANCH TO 800 700 CALL 500 600 MOVE P TO X Object module A 500 400 BRANCH TO 300 Figure 7-17 The relocated binary program of Fig 7-15(b) moved up 300 addresses Many instructions now refer to an incorrect memory address  , ,, A procedure segment CALL EARTH The linkage segment rect Indi ssing e Invalid address r add E A R T H CALL FIRE Invalid address A I R Linkage information for the procedure of AIR Invalid address F I R E Name of the procedure is stored as a character string CALL AIR CALL WATER CALL EARTH Indirect word w Invalid address A T E R CALL WATER (a) A procedure segment CALL EARTH The linkage segment rect Indi ssing Address of earth re add E A R T H To earth Invalid address A I R CALL FIRE CALL AIR F CALL WATER Invalid address I R E Invalid address W A T E R CALL EARTH CALL WATER (b) Figure 7-18 Dynamic linking (a) Before EARTH is called (b) After EARTH has been called and linked User process User process DLL Header A B C D Figure 7-19 Use of a DLL file by two processes ... 12222222222222222222222222222222222222222222222222222222222222222222 produce the program time in seconds 1 1 Assembly language 50 33 1 1 10 100 1 High-level language 1 1 1 Mixed approach before tuning 1 1 Critical 10% 1 90 1 1 Other 90% 10 1 1 33 33... Critical 10% 30 1 Other 90% 10 1 1 33 33 1 1 Total 15 40 12222222222222222222222222222222222222222222222222222222222222222222 Figure 7-1 Comparison of assembly language and high-level language programming,... 222222222222222222222222222222222222222222222222222222222222222222222222222 Terminate the assembly program Figure 7-3 Some of the pseudoinstructions available in the Pentium II assembler (MASM) MOV MOV MOV MOV EAX,P EBX,Q

Ngày đăng: 12/12/2013, 09:15

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan