Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Assembly Language Level Part B – The Assembly Process.

Similar presentations


Presentation on theme: "The Assembly Language Level Part B – The Assembly Process."— Presentation transcript:

1 The Assembly Language Level Part B – The Assembly Process

2 Specifying numeric values Java int i = 10; intj = 0x10; intk = 010; System.out.println( i ); System.out.println( j ); System.out.println( k ); What appears? MASM idword10d jdword10h kdword10o el=20D m=20H n=20O C/C++ inti = 10; intj = 0x10; intk = 010; #defineel20 #definem0x20 #definen020

3 Specifying numeric values Java int i = 10; intj = 0x10; intk = 010; System.out.println( i ); System.out.println( j ); System.out.println( k ); What appears? MASM idword10d jdword10h kdword10o el=20D m=20H n=20O C/C++ inti = 10; intj = 0x10; intk = 010; #defineel20 #definem0x20 #definen020 10 16 8

4 FORWARD REFERENCES (when a symbol is used before it is defined)

5 Two-pass assembler (translator) class test { public static void main ( String args[] ) { System.out.println( "k=" + k ); f(); } static int k = 72; static void f ( ) { System.out.println( "in f()" ); } valid forward references

6 Two-pass assembler (translator) class test { public static void main ( String args[] ) { System.out.println( "k=" + k ); int k = 72; } invalid forward reference

7 Two-pass assembler (translator) Forward reference – symbol is used before it is defined Solutions: 1.(One-pass translators make only one pass and don’t allow any forward references at all.) 2.Make two passes (read the source file twice). The first pass collects symbol definitions and label locations. The second pass uses the table built in the first pass. 3.Make one pass over the source and produce an intermediate form. A second pass is made over this intermediate data.

8 PASS ONE

9 Pass one Purpose: build the symbol table – a table containing: the name of the symbol and its value or the name of the label and its location – ILC = Instruction Location Counter – Variable set to 0 and incremented by instruction (or data) length for each line of code. – Note: code or data may both be labeled.

10 Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education, Inc. All rights reserved. 0-13-148521-0 Pass One: Two-Pass Assemblers 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.

11 Pass one Symbol types: 1.Symbol and corresponding value Assigned by a pseudoinstruction Ex. bufsize equ 8192 2.Label and corresponding location ;CF flag testebx, CF;carry set? jzcf0;jump if 0 printSADD(" CF:1");flag is on jmpnxt;skip over else part cf0: printSADD(" CF:0");flag is off nxt:

12 Pass one Employs 3 (or sometimes 4) tables: 1.Symbol table 2.Pseudoinstruction table 3.Opcode table 4.Literal table (optional)

13 Pass one Employs 3 tables: 1.Symbol table Symbol name Value (ILC for labels; defn/value for equ) Length of data field (especially for strings) Relocation bits (relocatable?) Scope of symbol 2.Pseudoinstruction table 3.Opcode table 4.Literal table (optional)

14 Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education, Inc. All rights reserved. 0-13-148521-0 Pass One: Two-Pass Assemblers A symbol table for the program of Fig. 7-7.

15 Pass one Employs 3 tables: 1.Symbol table 2.Pseudoinstruction table db1 dw2 dd4 3.Opcode table 4.Literal table (optional)

16 Pass one Employs 3 tables: 1.Symbol table 2.Pseudoinstruction table 3.Opcode table 4.Literal table (optional)

17 Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education, Inc. All rights reserved. 0-13-148521-0 Pass one: Two-Pass Assemblers A few excerpts from the opcode table for a Pentium 4 assembler.

18 Pass one Employs 3 tables: 1.Symbol table 2.Pseudoinstruction table 3.Opcode table 4.Literal table (optional) for pseudoimmediate instructions – when there is not any support for immediate instructions – only loads from memory are allowed

19 Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education, Inc. All rights reserved. 0-13-148521-0 Pass One (part 1) Pass one of a simple assembler....

20 Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education, Inc. All rights reserved. 0-13-148521-0 Pass One (part 2) Pass one of a simple assembler.... (Of course, a line might contain more than one literal!)

21 Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education, Inc. All rights reserved. 0-13-148521-0 Pass One (part 3) Pass one of a simple assembler....

22 PASS TWO

23 Pass two Purpose: – to generate object code – to optionally generate assembly listing Reads in each line, 1 line at a time (from original or intermediate code). Processes each line. – writes out binary object code

24 MASM listing (.lst) file 00000000.data;insert variables below = " "promptequ" ";prompt string … 00000000.code;insert executable instructions below 00000000mainPROC;program execution begins here 00000000 B8 00000001moveax, 1;set regs values 00000005 BB 00000002movebx, 2 0000000A B9 00000003movecx, 3 0000000F BA 00000004movedx, 4 00000014 BE 00000005movesi, 5 00000019 BF 00000006movedi, 6 0000001E E8 00000036calldump;show contents of regs 00000000 1.data 00000000 3C 68 69 74 20 1??0019db prompt, 0 72 65 74 75 72 6E 3E 00 00000000 1.data? 00000000 1??001Adb 132 dup (?) 00000023 1.code 0000003C C6 80 00000000 R 1mov BYTE PTR [??001A+eax], 0 00 0000004D B8 00000000 Rmoveax, input(prompt);prompt the user exit;end of program 00000059mainENDP

25 00000000.code;insert executable instructions below 00000000mainPROC;program execution begins here 00000000 B8 00000001moveax, 1;set regs values 00000005 BB 00000002movebx, 2 0000000A B9 00000003movecx, 3 0000000F BA 00000004movedx, 4 00000014 BE 00000005movesi, 5 00000019 BF 00000006movedi, 6

26 00000000.code;insert executable instructions below 00000000mainPROC;program execution begins here 00000000 B8 00000001moveax, 1;set regs values 00000005 BB 00000002movebx, 2 0000000A B9 00000003movecx, 3 0000000F BA 00000004movedx, 4 00000014 BE 00000005movesi, 5 00000019 BF 00000006movedi, 6

27 00000000.data;insert variables below = " "promptequ" ";prompt string 00000000 1.data 00000000 3C 68 69 74 20 1??0019db prompt, 0 72 65 74 75 72 6E 3E 00 ASCII & MASM listing file

28 Pass two Possible errors: 1.Symbol used but not defined 2.Multiply defined symbol 3.Unrecognized opcode 4.Too few/many operands 5.Bad (binary/octal/decimal/hex) # 6.Illegal register use (ex. Branch to reg) 7.END missing

29 Pass Two (part 1) Pass two of a simple assembler. Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education, Inc. All rights reserved. 0-13-148521-0... Read one entry from the intermediate file. (Questionable limitations (16 bytes).)

30 Pass two of a simple assembler. Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education, Inc. All rights reserved. 0-13-148521-0 Pass Two (part 2)...

31 SYMBOL TABLE ORGANIZATION

32 Symbol table Stores pairs. How do we find the value associated with a particular symbol? If we store the symbol table in an unordered array, then linear search requires: – best case:1 – worst case:n – average:n/2 search effort on average – O(n)

33 Symbol table Stores pairs. How do we find the value associated with a particular symbol? If we store the symbol table in a sorted array, then binary search requires: – best case:1 – worst case:log 2 (n) – O(log 2 n)

34 Symbol table Stores pairs. How do we find the value associated with a particular symbol? If we store the symbol table in a hash table, then search requires: – best case = worst case = average case = O(1) iff we have a perfect hash function H:S-->I(H maps Strings onto the Ints)

35 Symbol table Hash function H:S-->I – H maps Strings onto the Ints – We store our symbols in a table of size k. – Possible hash functions:

36 Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education, Inc. All rights reserved. 0-13-148521-0 The Symbol Table (1) Hash coding. (a) Symbols, values, and the hash codes derived from the symbols. We need a method to cope with the situation when the hash scheme is less than perfect (i.e., when a collision occurs).

37 Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education, Inc. All rights reserved. 0-13-148521-0 The Symbol Table (1) Hash coding. (a) Symbols, values, and the hash codes derived from the symbols. We need a method to cope with the situation when the hash scheme is less than perfect (i.e., when a collision occurs). Chaining: One solution is to make a linked list of symbols that hash to the same value.

38 Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education, Inc. All rights reserved. 0-13-148521-0 The Symbol Table (2) Hash coding. (b) Eight-entry hash table with linked lists of symbols and values.

39 Analysis of hashing with chaining If all symbols hash to the same position (i.e., to only one position), what happens? Load factor – Given a hash table T with m slots that stores n elements, we define a load factor f for T as f = n / m. » Average number of elements in a chain.

40 Java and hash tables http://download.oracle.com/javase/6/docs/api /java/util/Hashtable.html http://download.oracle.com/javase/6/docs/api /java/util/Hashtable.html This example creates a hashtable of names & numbers. It uses names as keys: Hashtable tbl = new Hashtable (); tbl.put( "fred", 1 ); tbl.put( "ethel", 29 ); tbl.put( "barney", 3 ); To retrieve a number, use the following code: Integer v = tbl.get( "ethel" ); if (v != null) { System.out.println( "ethel = " + v ); } Note: What’s going on with 1, 29, and 3, and Integer?

41 C++ and hash tables See http://en.wikipedia.org/wiki/Hash_map_%28C%2B%2B%29 #include struct eqstr { bool operator() ( const char* s1, const char* s2 ) const { return strcmp(s1,s2) == 0; //true when eq } }; … hash_map, eqstr > tbl; … tbl["fred"] = 1; tbl["ethel"] = 29; tbl["barney"] = 3; … int v = tbl["ethel"]; Or one may use iterator find ( const key_type& k ) or size_type count ( const key_type& k ) const to check.

42 Comparison (Java vs. C++) import java.util.Hashtable; … Hashtable tbl = new Hashtable (); … tbl.put( "fred", 1 ); tbl.put( "ethel", 29 ); tbl.put( "barney", 3 ); … Integer v = tbl.get( "ethel" ); if (v != null) { System.out.println( "ethel = " + v ); } #include struct eqstr { bool operator() ( const char* s1, const char* s2 ) const { return strcmp(s1,s2)==0; } }; … hash_map, eqstr > tbl; … tbl["fred"] = 1; tbl["ethel"] = 29; tbl["barney"] = 3; … int v = tbl["ethel"]; //can/should use find or count first to check for // existence Why? Nicer syntax. Objects only! Objects or primitive types.


Download ppt "The Assembly Language Level Part B – The Assembly Process."

Similar presentations


Ads by Google