assemblers 2

Post on 10-Apr-2018

231 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 1/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

ASSEMBLY LANGUAGE

&ASSEMBLER DESIGN

Part-2

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 2/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Human-Readable Machine Language

Computers like ones and zeros…

Humans like symbols…

Assembler is a program that turns symbols intomachine instructions.

• ISA-specific:close correspondence between symbols and instruction set

mnemonics for opcodeslabels for memory locations

• additional operations for allocating storage and initializing data

ADD R6,R2,R6 ; increment index reg.

0001110010000110

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 3/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

LC-3 Assembly Language Syntax

Each line of a program is one of the following:• an instruction• an assember directive (or pseudo-op)• a comment

Whitespace (between symbols) and case are ignored.Comments (beginning with “;”) are also ignored.

An instruction has the following format:LABEL OPCODE OPERANDS ; COMMENTS

optional mandatory

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 4/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Opcodes and Operands

Opcodes• reserved symbols that correspond to LC-3 instructions.ex: ADD , AND , LD, LDR , …

Operands• registers -- specified by Rn, where n is the register number• numbers -- indicated by # (decimal) or x (hex)• label -- symbolic name of memory location• number, order, and type correspond to instruction format

ex: ADD R1,R1,R3 ADD R1,R1,#3

LD R6,NUMBER BRz LOOP

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 5/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Labels and Comments

Label• Placed at the beginning of the line.• Assigns a symbolic name to the address corresponding to line.

ex:LOOP ADD R1,R1,#-1

BRp LOOP

Comment• Anything after a semicolon is a comment ignored by assembler• Used by humans to document/understand programs

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 6/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Assembler Directives

Pseudo-operations• do not refer to operations executed by program• used by assembler• look like instruction, but “ opcode ” starts with dot

Opcode Operand Meaning .ORIG address starting address of program.END end of program.BLKW n allocate n words of storage

.FILL n allocate one word, initialize withvalue n

.STRINGZ n-characterstring

allocate n+1 locations,initialize w/characters and nullterminator

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 7/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

An Assembly Language Program;

; Program to multiply a number by the constant 6;.ORIG x3050LD R1, SIXLD R2, NUMBER

AND R3, R3, #0 ; Clear R3. It will; contain the product.

; The inner loop

AGAIN ADD R3, R3, R2 ADD R1, R1, #-1 ; R1 keeps track of

BRp AGAIN ; the iteration.

HALT NUMBER .FILL #20

SIX .FILL x0006

.END

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 8/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

; Program to multiply a number by the constant 6.ORIG x3050LD R1, SIX ; R1 is loop counterLD R2, NUMBER

AND R3, R3, #0 ; Clr R3, will hold product; The Loop

AGAIN ADD R3, R3, R2 ; Summing into R3

ADD R1, R1, #-1 ; Dec loop counterBRp AGAINHALT

; NUMBER .FILL #20

SIX .FILL x0006.END

Must have Opcode and Operands

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 9/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

; Program to multiply a number by the constant 6.ORIG x3050LD R1, SIX ; R1 is loop counterLD R2, NUMBER

AND R3, R3, #0 ; Clr R3, will hold product; The Loop

AGAIN ADD R3, R3, R2 ; Summing into R3

ADD R1, R1, #-1 ; Dec loop counterBRp AGAINHALT

; NUMBER .FILL #20

SIX .FILL x0006.END

Label and Comments optional

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 10/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

; Program to multiply a number by the constant 6.ORIG x3050LD R1, SIX ; R1 is loop counterLD R2, NUMBER

AND R3, R3, #0 ; Clr R3, will hold product; The Loop

AGAIN ADD R3, R3, R2 ; Summing into R3

ADD R1, R1, #-1BRp AGAINHALT

NUMBER .FILL #20SIX .FILL x0006

.END

Decimal #Binary bHex x

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 11/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

; Program to multiply a number by the constant 6.ORIG x3050LD R1, SIX ; R1 is loop counterLD R2, NUMBER

AND R3, R3, #0 ; Clr R3, will hold product; The Loop

AGAIN ADD R3, R3, R2 ; Summing into R3

ADD R1, R1, #-1BRp AGAINHALT

NUMBER .FILL #20SIX .FILL x0006

.END

More than just code, need to

worry about memory allocation

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 12/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Assembler

• Translating source code written in assembly language to objectcode.

• Assigning machine address to symbolic labels.

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 13/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Assembler-Design

• Assembler Design can be done in: – Single pass – Two pass

• Single Pass Assembler: – Does everything in single pass – Cannot resolve the forward referencing

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 14/43

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 15/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Assembler- Design…•

Symbol Table: – This is created during pass 1 – All the labels of the instructions are symbols – Tables has entry for symbol name, address value.

• Forward reference: – Symbols that are defined in the later part of the

program are called forward referencing. – There will not be any address value for such symbols

in the symbol table in pass 1.

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 16/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Assembly Process

Convert assembly language file (.asm)into an executable file (.obj) for the LC-3 Assembler.

First Pass:• Scan program file• Find all labels and calculate the corresponding addresses;

this is called the symbol table

Second Pass:• Convert instructions to machine language , using information from

symbol table

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 17/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

First Pass: Constructing the Symbol Table

1. Find the .ORIG statement , which tells us the addressof the first instruction.• Initialize location counter (LC), which keeps track of the

current instruction.

2. For each non-empty line in the program:a) If line contains a label, add label and LC to symbol table.b) Increment LC.

NOTE : If statement is .BLKW or .STRINGZ ,

increment LC by the number of words allocated.

3. Stop when .END statement is reached.

NOTE: A line that contains only a comment is considered an empty line.

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 18/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

PracticeConstruct the symbol table for the program

Symbol AddressSIX

NUMBER

AGAIN

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 19/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Second Pass: Generating Machine Language

•For each executable assembly language statement,generate the corresponding machine language instruction

• If operand is a label , look up the address from the symbol table .

Potential problems:

• Improper number or type of argumentsex: NOT R1,#7

ADD R1,R2ADD R3,R3,NUMBER

• Immediate argument too largeex: ADD R1,R2,#102 3

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 20/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Practice

Using the symbol table constructed earlier,translate these statements into LC-3 machine language .

Statement Machine LanguageLD R3,PTR

ADD R4,R1,#-4

LDR R1,R3,#0

BRnp GETCHAR

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 21/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Example-2 ,Program with object code

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 22/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 23/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 24/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Assembler algorithm and data structures

• OPTAB: operation code table• SYMTAB: symbol table• LOCCTR: location counter

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 25/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Algorithm Pass 1

Beginread first input lineif OPCODE = ‘START’ then begin

save #[Operand] as starting addr

initialize LOCCTR to starting addresswrite line to intermediate fileread next line

end ( if START)else

initialize LOCCTR to 0

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 26/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Algorithm Pass 1…While OPCODE != ‘END’ do

beginif this is not a comment line thenbeg

if there is a symbol in the LABEL field thenbegin

search SYMTAB for LABELif found thenset error flag (duplicate symbol)

else(if symbol)

search OPTAB for OPCODEif found then

add 3 (instr length) to LOCCTRelse if OPCODE = ‘WORD’ then

add 3 to LOCCTR

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 27/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Algorithm Pass 1…

else if OPCODE = ‘RESW’ thenadd 3 * #[OPERAND] to LOCCTR

else if OPCODE = ‘RESB’ thenadd #[OPERAND] to LOCCTR

else if OPCODE = ‘BYTE’ thenbegin

find length of constant in bytesadd length to LOCCTR

endelse

set error flag (invalid operation code)end (if not a comment)

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 28/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Algorithm Pass 1…

write line to intermediate fileread next input line

end { while not END}write last line to intermediate file

Save (LOCCTR – starting address) as program lengthEnd {pass 1}

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 29/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Pass 2 Algorithm

Beginread 1 st input line

if OPCODE = ‘START’ thenbegin

write listing lineread next input line

endwrite Header record to object programinitialize 1 st Text record

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 30/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Pass 2 Algorithm…

while OPCODE != ‘END’ dobegin

if this is not comment line thenbegin

search OPTAB for OPCODEif found then

beginif there is a symbol in OPERAND field then

begin

search SYMTAB for OPERAND field thenif found then

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 31/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Pass 2 Algorithm…begin

store symbol value as operand addresselsebegin

store 0 as operand addressset error flag (undefined symbol)

endend (if symbol)

else store 0 as operand addressassemble the object code instruction

else if OPCODE = ‘BYTE’ or ‘WORD” thenconvert constant to object code

if object code doesn’t fit into current Text record thenbegin

Write text record to object codeinitialize new Text record

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 32/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Pass 2 Algorithm…

endadd object code to Text record

end {if not comment}write listing line

read next input lineend

write listing lineread next input linewrite last listing line

End {Pass 2}

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 33/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Converting Code to Assembly

Can use a standard template approach

Typical Constructs:

• if/else• while• do/while• for

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 34/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

if/else

if(x > 0){

r2 = r3 + r4;

}else

{

r5 = r6 + r7;}

LD R1, XBRP THEN

ADD R5,R6,R7

BRNZP DONETHEN ADD R2,R3,R4

DONE ...

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 35/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

if/else

if(x > 0){

r2 = r3 + r4;

}else

{

r5 = r6 + r7;}

LD R1, XBRNZ ELSE

ADD R2,R3,R4

BRNZP DONEELSE ADD R2,R3,R4

DONE ...

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 36/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

while

i = 10;x = 0;while(i > 0)

{x = x + i;i--;

}

AND R1,R1,#0 ADD R1,R1,#10

AND R2,R2,#0

WHL BRNZ DONE ADD R2,R2,R1

ADD R1,R1,#-1

BRNZP WHL

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 37/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

LC-3 Assembler

Using “assembler” (Unix) or LC3Edit (Windows),generates several different output files.

This one gets loaded into the simulator.

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 38/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Object File Format

LC-3 object file contains• Starting address (location where program must be loaded),

followed by…• Machine instructions

Example• Beginning of “count character” object file looks like this:

00110000000000000101010010100000

00100110000100011111000000100011

.

.

.

.ORIG x3000

AND R2, R2, #0

LD R3, PTR TRAP x23

h h ll d f d d l

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 39/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Role of Loader

Source

Program TranslatorObject

Program

LoaderObject

programready forexecution

Memory

Translator – Assembler/Compiler

C i h © Th M G Hill C i I P i i i d f d i di l

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 40/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Role of Loader

Source

Program AssemblerObject

Program

LoaderObject

programready forexecution

Memory

C ight © Th M G Hill C i I P i i i d f d ti di l

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 41/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Role of Loader and Linker

Source

Program AssemblerObject

Program

Linker

ExecutableCode

Loader

Objectprogramready forexecution

Memory

Copyright © The McGraw Hill Companies Inc Permission required for reproduction or display

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 42/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Linking and Loading

Linking is the process of resolving symbols betweenindependent object files.

• suppose we define a symbol in one module,and want to use it in another

• some notation, such as.EXTERNAL

, is used to tell assembler that asymbol is defined in another module• linker will search symbol tables of other modules to resolve symbols

and complete code generation before loading

Loading is the process of copying an executable imageinto memory.

Copyright © The McGraw Hill Companies Inc Permission required for reproduction or display

8/8/2019 Assemblers 2

http://slidepdf.com/reader/full/assemblers-2 43/43

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Thank U

top related