1. Trang chủ
  2. » Giáo án - Bài giảng

kiến trúc máy tính dạng thanh tin figs 7 the assignmentignmentignmentignmentembly language level sinhvienzone com

20 35 0

Đ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

Thông tin cơ bản

Định dạng
Số trang 20
Dung lượng 58,09 KB

Nội dung

7 THE ASSEMBLY LANGUAGE LEVEL CuuDuongThanCong.com https://fb.com/tailieudientucntt Assembly language High-level language Mixed approach before tuning Critical 10% Other 90% Total Mixed approach after tuning Critical 10% Other 90% Total Programmer-years to produce the program Program execution time in seconds 50 10 33 100 90 10 10 100 30 10 15 40 Figure 7-1 Comparison of assembly language and high-level language programming, with and without tuning CuuDuongThanCong.com https://fb.com/tailieudientucntt Label FORMULA: Opcode MOV ADD MOV Operands EAX,I EAX,J N,EAX Comments ; register EAX = I ; register EAX = I + J ;N=I+J I J N DW DW DW ; reserve bytes initialized to ; reserve bytes initialized to ; reserve bytes initialized to (a) Label FORMULA Opcode MOVE.L ADD.L MOVE.L Operands I, D0 J, D0 D0, N Comments ; register D0 = I ; register D0 = I + J ;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) Label FORMULA: I: J: N: Opcode SETHI LD SETHI LD NOP ADD SETHI ST Operands %HI(I),%R1 [%R1+%LO(I)],%R1 %HI(J),%R2 [%R2+%LO(J)],%R2 %R1,%R2,%R2 %HI(N),%R1 %R2,[%R1+%LO(N)] WORD WORD WORD Comments ! R1 = high-order bits of the address of I ! R1 = I ! R2 = high-order bits of the address of J ! R2 = J ! wait for J to arrive from memory ! R2 = R1 + R2 ! R1 = high-order bits of the address of N ! 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 CuuDuongThanCong.com https://fb.com/tailieudientucntt Pseudoinstr SEGMENT ENDS ALIGN EQU DB DD DW DQ PROC ENDP MACRO ENDM PUBLIC EXTERN INCLUDE IF ELSE ENDIF COMMENT PAGE END Meaning Start a new segment (text, data, etc.) with certain attributes End the current segment Control the alignment of the next instruction or data Define a new symbol equal to a given expression Allocate storage for one or more (initialized) bytes Allocate storage for one or more (initialized) 16-bit halfwords Allocate storage for one or more (initialized) 32-bit words Allocate storage for one or more (initialized) 64-bit double words Start a procedure End a procedure Start a macro definition End a macro definition Export a name defined in this module Import a name from another module Fetch and include another file Start conditional assembly based on a given expression Start conditional assembly if the IF condition above was false End conditional assembly Define a new start-of-comment character Generate a page break in the listing Terminate the assembly program Figure 7-3 Some of the pseudoinstructions available in the Pentium II assembler (MASM) CuuDuongThanCong.com https://fb.com/tailieudientucntt 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 CuuDuongThanCong.com https://fb.com/tailieudientucntt Item When is the call made? Is the body inserted into the object program every place the call is made? Is a procedure call instruction inserted into the object program and later executed? Must a return instruction be used after the call is done? How many copies of the body appear in the object program? Macro call During assembly Yes Procedure call During execution No No Yes No Yes One per macro call Figure 7-5 Comparison of macro calls with procedure calls CuuDuongThanCong.com https://fb.com/tailieudientucntt 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 CuuDuongThanCong.com https://fb.com/tailieudientucntt Label MARIA: ROBERTA: MARILYN: STEPHANY: Opcode Operands MOV EAX,I MOV EBX, J MOV ECX, K IMUL EAX, EAX IMUL EBX, EBX IMUL ECX, ECX ADD EAX, EBX ADD EAX, ECX JMP DONE Comments EAX = I EBX = J ECX = K EAX = I * I EBX = J * J ECX = K * K EAX = I * I + J * J EAX = I * I + J * J + K * K branch to DONE 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 CuuDuongThanCong.com https://fb.com/tailieudientucntt Length 6 3 2 ILC 100 105 111 117 119 122 125 127 129 Symbol MARIA ROBERTA MARILYN STEPHANY Value 100 111 125 129 Other information Figure 7-8 A symbol table for the program of Fig 7-7 CuuDuongThanCong.com https://fb.com/tailieudientucntt Opcode AAA ADD ADD AND AND First operand Second operand Hexadecimal opcode — EAX reg EAX reg — immed32 reg immed32 reg 37 05 01 25 21 Instruction length 5 Figure 7-9 A few excerpts from the opcode table for a Pentium II assembler CuuDuongThanCong.com https://fb.com/tailieudientucntt Instruction class 19 19 public static void pass one( ) { // This procedure is an outline of pass one of a simple assembler boolean more input = true; // flag that stops pass one String line, symbol, literal, opcode; // fields of the instruction int location counter, length, value, type; // misc variables final int END STATEMENT = −2; // signals end of input location counter = 0; initialize tables( ); // assemble first instruction at // general initialization while (more input) { line = read next line( ); length = 0; type = 0; // more input set to false by END // get a line of input // # bytes in the instruction // which type (format) is the instruction if (line is not comment(line)) { symbol = check for symbol(line); // is this line labeled? if (symbol != null) // if it is, record symbol and value enter new symbol(symbol, location counter); literal = check for literal(line); // does line contain a literal? if (literal != null) // if it does, enter it in table enter new literal(literal); // Now determine the opcode type −1 means illegal opcode opcode = extract opcode(line); // locate opcode mnemonic type = search opcode table(opcode); // find format, e.g OP REG1,REG2 if (type < 0) // if not an opcode, is it a pseudoinstruction? type = search pseudo table(opcode); switch(type) { // determine the length of this instruction case 1: length = get length of type1(line); break; case 2: length = get length of type2(line); break; // other cases here } } write temp file(type, opcode, length, line);// useful info for pass two location counter = location counter + length;// update loc ctr if (type == END STATEMENT) { // are we done with input? more input = false; // if so, perform housekeeping tasks rewind temp for pass two( ); // like rewinding the temp file sort literal table( ); // and sorting the literal table remove redundant literals( ); // and removing duplicates from it } } } Figure 7-10 Pass one of a simple assembler CuuDuongThanCong.com https://fb.com/tailieudientucntt public static void pass two( ) { // This procedure is an outline of pass two of a simple assembler boolean more input = true; // flag that stops pass one String line, opcode; // fields of the instruction int location counter, length, type; // misc variables final int END STATEMENT = −2; // signals end of input final int MAX CODE = 16; // max bytes of code per instruction byte code[ ] = new byte[MAX CODE]; // holds generated code per instruction location counter = 0; // assemble first instruction at while (more input) { // more input set to false by END type = read type( ); // get type field of next line opcode = read opcode( ); // get opcode field of next line length = read length( ); // get length field of next line line = read line( ); // get the actual line of input if (type != 0) { // type is for comment lines switch(type) { // generate the output code case 1: eval type1(opcode, length, line, code); break; case 2: eval type2(opcode, length, line, code); break; // other cases here } } write output(code); // write the binary code write listing(code, line); // print one line on the listing location counter = location counter + length;// update loc ctr if (type == END STATEMENT) {// are we done with input? more input = false; // if so, perform housekeeping tasks finish up( ); // odds and ends } } } Figure 7-11 Pass two of a simple assembler CuuDuongThanCong.com https://fb.com/tailieudientucntt 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 CuuDuongThanCong.com https://fb.com/tailieudientucntt 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 CuuDuongThanCong.com https://fb.com/tailieudientucntt 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 CuuDuongThanCong.com https://fb.com/tailieudientucntt 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 CuuDuongThanCong.com https://fb.com/tailieudientucntt 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 CuuDuongThanCong.com https://fb.com/tailieudientucntt 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 CuuDuongThanCong.com https://fb.com/tailieudientucntt  , ,, 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 CuuDuongThanCong.com https://fb.com/tailieudientucntt User process User process DLL Header A B C D Figure 7-19 Use of a DLL file by two processes CuuDuongThanCong.com https://fb.com/tailieudientucntt ... 65254 54185 473 57 56445 14332 32334 44546 75 544 170 97 64533 232 67 63453 76 764 34544 34344 3 4 (a) Hash table Linked table Andy 14025 Maarten 232 67 Reind 63453 Wiebren 34344 Henri 75 544 Frances... 111 1 17 119 122 125 1 27 129 Symbol MARIA ROBERTA MARILYN STEPHANY Value 100 111 125 129 Other information Figure 7- 8 A symbol table for the program of Fig 7- 7 CuuDuongThanCong .com https://fb .com/ tailieudientucntt... Gerrit 32334 Jan 170 97 Cathy 65254 Jaco 64533 Willem 34544 Roel 76 764 Dick 54185 Anton 31253 Erik 473 57 (b) Figure 7- 12 Hash coding (a) Symbols, values, and the hash codes derived from the symbols

Ngày đăng: 28/01/2020, 23:01

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN