Presentation is loading. Please wait.

Presentation is loading. Please wait.

Assembler When a source program is a assembly language and the target program is a numerical machine language then the translator is called as a assembler.

Similar presentations


Presentation on theme: "Assembler When a source program is a assembly language and the target program is a numerical machine language then the translator is called as a assembler."— Presentation transcript:

1

2

3 Assembler When a source program is a assembly language and the target program is a numerical machine language then the translator is called as a assembler. Assembly language is basically a symbolic representation for a numerical machine language

4 Assembly language is a convenient language using mnemonics (symbolic names and symbolic addresses) for coding machine instructions The assembly programmer has access to all the features and instructions available on the target machine and thus can execute every instruction in the instruction set of the target machine

5 Problems with ALP Assembly language programming is difficult. Takes longer time. Takes longer time to debug. Difficult to maintain.

6 Why go for assembly language programming? Performance issues – For some applications, speed and size of the code are critical. An expert assembly language programmer can often produce code that is much smaller and much faster than a high level programmer can. Example – embedded applications such as the code on a smart card, the code in a cellular telephone, BIOS routines, inner loops of performance critical applications etc. 2. Access to the machine – Some procedures need complete access to the hardware, something which is impossible in high level languages. Example – low level interrupt and trap handlers in an operating system

7 Skeleton of ALP Header……………..start of ALP Body ……………..ALP Statements Footer……………..End of ALP The body of ALP consists of Assembler Directives and Mnemonics [label:]mnemonic[operands][;comments]

8 Assembly language format The format of a typical assembly language program consists of Label field – provides symbolic names for memory addresses which is needed on executable statements so that the statements can be jumped to. It also permits the data stored there to be accessible by the symbolic name. Example: TEMP,FORMUL etc Operation field – contains a symbolic abbreviation for the opcode or a pseudo-instruction. Example: MOVE, ADD etc. Operands field – specifies addresses or registers used by operands of the machine instruction. Example: D0,R1,R2 etc. Comment field – is used for documentation purposes.

9 Types of ALP Statements ALP statements can be broadly categorized into following 3 types: –Assembler Directive statements Ex: START 1000 –Declarative Statements Ex ABC DB 1 –Imperative Statements Ex: MOV A,B

10 Features of ALP Use of Mnemonics to specify Opcodes makes the assembly language program much more readable and debugging is also easier. Use of Symbols to specify Operands means that program can be modified with no overhead. That is, if definition address of a symbol changes, the change in ALP is done only at the place where the symbol is declared; all the places where the symbol has been used need not be updated. Separation of Code and Data Segments allows the programmer to keep aside some portion of memory for the data to be used by the program.

11 Basic data Structures of an ALP Assembler needs following basic data structures (also called as databases) as input: ALP Source Code – START 4000; Start storing the program from location 4000 LOAD 1000; Load data at location 1000 into register A MOV B,A; Copy contents of Register A into register B LOAD 2000; Load data at location 2000 into register A ADD B ; Add content of register A with that of B, store the sum in A STORE 3000 ; Store the sum from register A at location 3000. END; End of Program

12 Assembler design for Hypothetical machine MOT( Machine op-code table) and POT( pseudo op-code table) for Hypothetical machine( Supporting tables for assembler design) ALP ( input for design of assembler)

13 MOT MnemonicsopcodeNo. of OperandsLOI ADD112 SUB212 MULT312 JMP412 JNEG512 JPOS612 JZ712 LOAD812 STORE912 READ1012 WRITE1112 STOP1201

14 POT Pseudo opcodeNo. of Operands DB2 DW2 EQU2 CONST2 START1 ORG1 LTORG1 ENDP0 END0

15 ALP Line No.LabelPseudo code/ MnemonicOperand START2000 1READN 2LOADZERO 3STORECOUNT 4SRORESUM 5LOOP:READX 6LOADX 7ADDSUM 8STORESUM 9LOADCOUNT 10ADDONE 11STORECOUNT 12SUBN 13JZOUTER

16 Line No.LabelPseudo code/ Mnemonic Operand 14JMPLOOP 15OUTER:WRITESUM 16STOP 17ENDP; END OF CODE SEG 18ZEROCONST0 19ONECONST1 20SUMDB? 21COUNTDB? 22NDB? 23XDB? 24END; END OF PROGRAM

17 forward reference problem In the above example one important point is worth noting. How does the assembler know what is stored in a location N. such a reference which is used even before it is defined is called a forward reference.

18 The Forward Reference: is a reference which is used even before it is defined. Can be handled in two ways: – Two pass assembler – One pass assembler Each reading of the source program is called a pass. Any translator which reads the input program once is called a one-pass assembler and if it reads twice is called a two-pass assembler.

19 Two - pass assembler: 1. In pass-one of a two-pass assembler, the definitions of symbols, statement labels etc are collected and stored in a table known as the symbol table. 2. In pass-two, each statement can be read, assembled and output as the values of all symbols are known. This approach is thus quite simple though it requires an additional pass.

20

21 One - pass assembler: The assembly program is read once and converted to an intermediate form and thereafter stored in a table in memory. Uses the data structure FRT( Forward reference table)

22 Symbol Table (ST) SymbolTypeAddress of Operands

23 OBJECT CODE LOCATION COUNTEROUTPUT

24 Data structures – pass 1 Mot Pot St Potptr, motptr, stptr Input_file, inter_file,location counter Code flag

25 Data structures – pass 2 Mot. Pot. St. Potptr, motptr, stptr. Input_file, inter_file,location counter. Code flag. Output file.

26 Functions –pass 1 Readline(inputfile) Writeline(interfile) SearchMot(symbol) SearchPot(symbol) Searchst(symbol) InsertSymST(symbol) InsertattrSymST(symbol) Initlc(loi) GetattrfromST(symbol)

27 Functions –pass 2 Readline(inputfile) Writeline(outfile) SearchMot(symbol) SearchPot(symbol) Searchst(symbol) InsertSymST(symbol) InsertattrSymST(symbol) Initlc(loi) GetattrfromST(symbol)

28 Selection of Data structure for Assembler design File Array of Structure Structures Class

29 Algorithm: 2 Pass Assembler Design Refer to algo.doc

30

31

32 Example 2: Translate the ALP to M/C code and generate Symbol Table ORG 1000 LOAD A ADD ONE STORE A STOP ENDP A DB ? ONE CONST 1 END

33 Example 3: Translate the ALP to M/C code and generate Symbol Table LOAD A BACK : ADD ONE JNZ A STORE A JMP BACK A : SUB ONE STOP ENDP A DB ? ONE CONST 1 END

34 Literal Handling by Assembler The literal is an operand with syntax : Literal name = ‘ ’ Ex : ADD =‘4’ Differs from constant Becoz its location can not be specified in ALP, which ensures that its value can not be changed during execution of program Its nothing but “ PURE CONSTANT”

35 Example : Literal Handling LOAD A ADD =‘4’ STORE A SUB =‘4’ STORE B ADD =‘5’ STOP ENDP A DB ? B DB ? END Generate the object code for above Pgm along with ST and LT.

36 Symbol Table( After First Pass) NameTypeAddress of symbol AVar0012 BVar0013

37 Literal Table Name of the Literal Value of the Literal Address of Usage of Symbol Address of Defination of Symbol ‘4’40003,00070014 ‘5’500110015

38 OBJECT CODE LOCATION COUNTEROUTPUT 000008 ----- 000212 ----

39 Procedure Handling PROC ADD2 ; PROC HEADER ORG 1000 LOAD A ADD B; PROC BODY RET ENDP; PROC FOOTER LOAD A CALL ADD2: CALL TO A PROCEDURE STORE B ENDP A DB ? B DB ? END

40 FEASIBILITY OF ONE PASS ASSEMBLER Find object code and symbol table for the following ALP DATA SEGMENT X DB ? Y DB ? CODE SEGMENT ORG 0000 LOAD X BACK : ADD Y JNEG FORWARD ; INSTANCE OF FORWARD REFERENCE SUB X JZ FORWARD ; INSTANCE OF FORWARD REFERENCE ADD X JMP BACK FORWARD : STORE Y STOP END

41 Forward Reference Table / TII Name of the Symbol AttributeAddress of Usage of Symbol Address of Defination of Symbol ForwardLabel0005,00090014

42 OBJECT CODE IN ARRAY ( AT THE END OF FIRST PASS) LOCATION COUNTEROUTPUT 000008 0000 000201 0001 000405 --------------- 000602 0000 000807 ---------------- 001001 0000 001204 0002 001409 0001 001612

43 FINAL OBJECT CODE( WITH THE HELP OF FRT) LOCATION COUNTEROUTPUT 000008 0000 000201 0001 000405 0014 000602 0000 000807 0014 001001 0000 001204 0002 001409 0001 001612

44 Sample Program: To multiply N1 * N2 by successive addition method Input ALP Code: (memory location) START 1000 LOAD N2; Load value of N2 into register A STORE COUNT; Store value of N2 into COUNT variable LOAD ‘=0’; Load literal value 0 into register A repeat: ADD N1; Add value of N1 to register A DEC COUNT; Decrement COUNT variable by 1 JNZ repeat; Jump if-not-zero to’ repeat’ STORE SUM; Store sum value into SUM variable JNC down; Jump if-not-carry to ‘down’ INC SUM; Increment SUM if there is carry down:STOP; program execution ends ENDP; code segment ends N1 DB 07; Define a Byte for variable ‘N1’ with value 07 N2DB 05; Define a Byte for variable ‘N2’ with value 05 COUNT DB ?; Define a Byte for variable ‘COUNT’ with null value SUMDW ??; Define a word for variable ‘SUM’ with null value END; End of program

45 Symbol NameTypeUsage AddressDefinition Address N2VAR10021022 COUNTVAR1004,10111023 N1VAR10091021 repeatLABEL1013 1008 SUMVAR1015, 10191024 downLABEL10171020 Literal NotationValueUsage AddressDefinition Address ‘=0’010061026 Forward Reference Table (FRT) Literal Table (LT)

46 AddressOpcodeOperand 1000031022 1002041023 1004031026 1006011021 1008061023 1010081008 101204 101409 101605 101810- Output File:

47

48 Error Handling Mechanism of Assembler 1)“ Unexpected END of FILE” 2)“ END of COMMENT not Found” 3)“UNDEFINED Symbol” 4)“UNDEFINED Label” 5)“Duplicate Label” 6)““Duplicate Symbol” 7)“Missing EOF” 8)“ END of PROC not Found” 9)“ RESERVED word used as a SYMBOL” 10)“ Number of OPERANDS mismatch” 11)“INCORRECT Instruction”

49 SYMBOLS WHICH CAN NOT BE RESOLVED BY ASSEMBLER External symbols External Procedures

50


Download ppt "Assembler When a source program is a assembly language and the target program is a numerical machine language then the translator is called as a assembler."

Similar presentations


Ads by Google