chapter 2d assembler- single pass assembler

48

Click here to load reader

Upload: sunita-aher

Post on 07-Jan-2017

384 views

Category:

Engineering


26 download

TRANSCRIPT

Page 1: Chapter 2d Assembler- Single Pass Assembler

Chapter 2: Assembler

Mrs. Sunita M Dol (Aher),Assistant Professor,

Computer Science and Engineering Department,Walchand Institute of Technology, Solapur, Maharashtra

Page 2: Chapter 2d Assembler- Single Pass Assembler

05/02/2023 Mrs. Sunita M Dol, CSE Dept 2

• Elements of Assembly Language Programming• A Simple Assembly Scheme• Pass Structure of Assemblers• Design of a Two Pass Assembler• A Single Pass Assembler for IBM PC

2. Assemblers

Page 3: Chapter 2d Assembler- Single Pass Assembler

05/02/2023 Mrs. Sunita M Dol, CSE Dept 3

Architecture of Intel 8088

CPU contains following features:– Data Registers AX, BX, CX and DX– Index Registers SI and DI– Stack Pointers Registers SP and BP– Segment registers Code Stack, Data and Extra

Page 4: Chapter 2d Assembler- Single Pass Assembler

05/02/2023 Mrs. Sunita M Dol, CSE Dept 4

AH AL

BH BL

CH CL

DH DL

SP

BP

SI

DI

CODE

STACK

DATA

EXTRA

Data Register

Base Register

Index Register

Segment Register

Page 5: Chapter 2d Assembler- Single Pass Assembler

05/02/2023 Mrs. Sunita M Dol, CSE Dept 5

Addressing ModesAddressing

ModesExamples Remarks

Immediate MOV SUM, 1234H Data = 1234H

Register MOV SUM, AX AX contains data

Direct MOV SUM, [1234H] Data Displacement = 1234H

Indirect MOV SUM, [BX] Data Displacement = (BX)

Register Indirect

MOV SUM, CS: [BX] Segment Override: Segment Base = (CS)Data Displacement (BX)

Based MOV SUM, 12H[BX] Data Displacement = 12H + (BX)

Indexed MOV SUM, 34H[SI] Data Displacement = 34H + (SI)

Based and Indexed

MOV SUM, 56H[SI][BX] Data Displacement = 56H + (SI) + (BX)

Page 6: Chapter 2d Assembler- Single Pass Assembler

05/02/2023 Mrs. Sunita M Dol, CSE Dept 6

Assembly Language of Intel 8088

Statement Format:[Label:] opcode operand(S) ; comment string

Page 7: Chapter 2d Assembler- Single Pass Assembler

05/02/2023 Mrs. Sunita M Dol, CSE Dept 7

• Assembler Directives– ORG– EQU– END

Assembly Language of Intel 8088

Page 8: Chapter 2d Assembler- Single Pass Assembler

05/02/2023 Mrs. Sunita M Dol, CSE Dept 8

• Declarations:– DB (e.g. A DB 25 ; Reserve byte and initialize)– DW (e.g. B DW ? ; Reserve byte and no initialization)– DD (e.g. A DD 6DUP(0) ; 6 Double words, all 0’s)– DQ– DT

Assembly Language of Intel 8088

Page 9: Chapter 2d Assembler- Single Pass Assembler

05/02/2023 Mrs. Sunita M Dol, CSE Dept 9

• EQU and PURGE e.g. XYZ DB ? ABC EQU XYZ ; ABC represent name

XYZ PURGE ABC ; ABC no longer XYZ ABC EQU 25 ; ABC stands for ’25’

Assembly Language of Intel 8088

Page 10: Chapter 2d Assembler- Single Pass Assembler

05/02/2023 Mrs. Sunita M Dol, CSE Dept 10

• SEGMENT, ENDS and ASSUME– SEGMENT and ENDS directives demarcate the

segments in assembly language.– ASSUME tells the assembler that it can assume the

address of indicated segment to be present in <register>

ASSUME <register> : <segment name>

Assembly Language of Intel 8088

Page 11: Chapter 2d Assembler- Single Pass Assembler

05/02/2023 Mrs. Sunita M Dol, CSE Dept 11

SAMPLE_DATA SEGMENTARRAY DW 100 DUP ?SUM DW 0SAMPLE_DATA ENDSSAMPLE_CODE SEGMENT ASSUME DS: SAMPLE_DATAHERE MOV AX, SAMPLE_DATA MOV DS, AX MOV AX, SUM -------SAMPLE_CODE ENDS END HERE

Assembly Language of Intel 8088

Page 12: Chapter 2d Assembler- Single Pass Assembler

05/02/2023 Mrs. Sunita M Dol, CSE Dept 12

• PROC, ENDP, NEAR and FAR e.g. SAMPLE_CODE SEGMENT CALCULATE PROC FAR --------- RET CALCULATE ENDP SAMPLE_CODE ENDS PGM SEGMENT -------- CALL -------- ENDS END

Assembly Language of Intel 8088

Page 13: Chapter 2d Assembler- Single Pass Assembler

05/02/2023 Mrs. Sunita M Dol, CSE Dept 13

• PUBLIC and EXTRN– PUBLIC : when a symbolic name declared in one

assembly module is to be accessible in other module, it is specified in a PUBLIC statement

– EXTRN : Another module wishing to use this name must specify in an EXTRN statement

Assembly Language of Intel 8088

Page 14: Chapter 2d Assembler- Single Pass Assembler

05/02/2023 Mrs. Sunita M Dol, CSE Dept 14

• Analytic operator– SEG– OFFSET– TYPE– SIZE– LENGTH

Assembly Language of Intel 8088

Page 15: Chapter 2d Assembler- Single Pass Assembler

05/02/2023 Mrs. Sunita M Dol, CSE Dept 15

• Synthetic operator– PTR creates new memory operand with the

same segment and offset addresses as an existing operand but having a different type.

– THIS performs the special function of creating a new memory operand with the same address as the next memory byte available for allocation

Assembly Language of Intel 8088

Page 16: Chapter 2d Assembler- Single Pass Assembler

05/02/2023 Mrs. Sunita M Dol, CSE Dept 16

Example

XYZ DW 312 NEW_NAME EQU BYTE PTR XYZLOOP: CMP AX, 234 JMP LOOPFAR_LOOP EQU FAR PTR LOOP JMP FAR_LOOP

Assembly Language of Intel 8088

Page 17: Chapter 2d Assembler- Single Pass Assembler

05/02/2023 Mrs. Sunita M Dol, CSE Dept 17

Example

DW NEW_NAME EQU THIS BYTEXYZ DW 312FAR_LOOP EQU THIS FARLOOP CMP AX, 234 JMP LOOP ------------------------------ JMP FAR_LOOP

Assembly Language of Intel 8088

Page 18: Chapter 2d Assembler- Single Pass Assembler

05/02/2023 Mrs. Sunita M Dol, CSE Dept 18

Sr.No Statement Offset1 CODE SEGMENT2 ASSUME CS:CODE,DS:DATA3 MOV AX,DATA 04 MOV DS,AX 35 MOV CX,LENGTH STRING 56 MOV COUNT,0000 87 MOV SI,OFFSET STRING 11

8 ASSUME ES:DATA,DS:NOTHING9 MOV AX,DATA 1410 MOV ES,AX 1711 COMP: SEGMENT [SI],'A' 1912 JNE NEXT 2213 MOV COUNT,1 2414 NEXT: INC SI 2715 DEC CX 2916 JNE COMP 3017 CODE ENDS

18 DATA SEGMENT19 ORG 120 COUNT DB ? 121 STRING DW 50 Dup(?) 222 DATA ENDS23 END

Page 19: Chapter 2d Assembler- Single Pass Assembler

05/02/2023 Mrs. Sunita M Dol, CSE Dept 19

MnemonicOpcode

(6)

Machine Opcode

(2)

Alignment/Format info

(1)

RoutineId (4)

JNE 75H 00H R2

Mnemonics table(MOT)

Page 20: Chapter 2d Assembler- Single Pass Assembler

05/02/2023 Mrs. Sunita M Dol, CSE Dept 20

(8) (1) (2) (2) (2) (2) (2) (2) (2) (2)

Symbol Table (SYMTAB)

Symbol

Type/Defined?/Segment Name?/EQU?

Offset in segmentOwner Segment (SYMTAB entry #)

SizeLength

Source Statement # FRT Pointer

Pointer to first CRT entry

Pointer to last CRT entry

Page 21: Chapter 2d Assembler- Single Pass Assembler

05/02/2023 Mrs. Sunita M Dol, CSE Dept 21

Segment register

(1)

Segment name

(2)00(ES) 23

:

Segment register table array (SRTAB_ARRAY)

SRTAB#1

SRTAB#2

Page 22: Chapter 2d Assembler- Single Pass Assembler

05/02/2023 Mrs. Sunita M Dol, CSE Dept 22

Pointer (2)

SRTAB # (1)

InstructionAddress

(2)

UsageCode (1)

SourceStmt# (2)

Pointer (2)

Source Stmt#(2)

Forward reference table(FRT)

Cross Reference Table (CRT)

Page 23: Chapter 2d Assembler- Single Pass Assembler

05/02/2023 Mrs. Sunita M Dol, CSE Dept 23

symbol D?

S?

T Offset Owner segment

Length & size

FTRPointer

CRT pointerFirst Last

CODE Y YDATA N YCOMP Y N -1 19 1 - . .NEXT Y N -1 27 1 - . .COUNT N N . .STRING N N . .

PTR SRTAB ADDR CODE STMT# #1 0008 D 0006

. #2 0024 D 0013

#1 0005 L 0005

. #1 0011 F 0007

16

STMT# PTR

12

CRT

01(CS)11(DS)

12

00 (ES)01(CS)

21

SRTAB#1

SRTAB#2

Page 24: Chapter 2d Assembler- Single Pass Assembler

05/02/2023 Mrs. Sunita M Dol, CSE Dept 24

Single Pass Assembler1. code_area_address := address of code_area; srtab_no := 1; LC := 0; stmt_no := 1; SYMTAB_segment_entry := 0; Clear ERRTAB , SRTAB_ARRAY2. While next statement is not an END statement a) Clear machine_code_buffer. b) If label is present then this_label := symbol in the label field;

Page 25: Chapter 2d Assembler- Single Pass Assembler

05/02/2023 Mrs. Sunita M Dol, CSE Dept 25

c) If an EQU statement i) this_address := value of operand expression; ii) Make an entry for this_label in SYMTAB with offset := this_address; defined := ‘yes’; owner_segment :=m owner_segment of operand symbol; source_stmt# := stmt_no; iii) Enter stmt_no in the CRT list of the label in the oprand field. iv) Process forward references to this_label; v) size := 0;

Page 26: Chapter 2d Assembler- Single Pass Assembler

d) If an ASSUME statement i) Copy the SRTAB in SRTAB_ARRAY[srtab_no] into SRTAB_ARRAY[srtab_no + 1]; ii) srtab_no := srtab_no + 1; iii) this_register := register mentioned in the statement. iv) this_segment := entry number of SYMTAB entry of the segment appearing in the operand field. v) Make the entry (this_register ; this_segment) in the SRTAB_ARRAY[srtab_no]. (This overwrites an existing entry for this register.) vi) size := 0;

Page 27: Chapter 2d Assembler- Single Pass Assembler

05/02/2023 Mrs. Sunita M Dol, CSE Dept 27

e) If a SEGMENT statement i) Make an entry for this_label in SYMTAB. ii) Set segment name ? := true; iii) SYMTAB_segment_entry := entry no in SYMTAB; iv) LC := 0; v) size := 0;

f) If an ENDS statement then SYMTAB_segment_entry := 0; g) If a declaration statement

i) Align LC according to the specification in the operand field. ii) Assemble the constant(s), if any, in the machine_code_buffer iii) size := size of memory area required;

Page 28: Chapter 2d Assembler- Single Pass Assembler

05/02/2023 Mrs. Sunita M Dol, CSE Dept 28

h) If an imperative statement i) If operand is a symbol symb then enter stmt_no in the CRT list of symb. ii) If operand symbol is already defined then Check its alignment & addressability. Generate the address specification (segment register, offset) for the symbol using its SYMTAB entry and SRTAB_ARRAY[srtab_no]. else Make an entry for symbol in SYMTAB. Defined := ‘no’; Enter (srtab_no, LC, usage_code, stmt_no) in FRT. iii)Assemble the instruction in machine_code_buffer. iv) size := size of the instruction;

Page 29: Chapter 2d Assembler- Single Pass Assembler

05/02/2023 Mrs. Sunita M Dol, CSE Dept 29

i) If size <> 0 then i) If label is present then

Make an entry for this_label in SYMTAB. owner_segment := SYMTAB_segment_entry; Defined := ‘yes’ ; offset := LC; source_stmt# := stmt_no;

ii) Move contents of machine_code_buffer to the address code_area_address; iii) code_area_address := code_area_address + size; iv) Process forward references to the symbol. Check for alignment & addressability errors. Enter errors in ERRTAB. v) List the statement with errors contained in ERRTAB. vi) Clear ERRTAB.

Page 30: Chapter 2d Assembler- Single Pass Assembler

05/02/2023 Mrs. Sunita M Dol, CSE Dept 30

3) (Processing of END statement) a) Report undefined symbol from SYMTAB. b) Produce cross reference listing. c) Write code_area into output file.

Page 31: Chapter 2d Assembler- Single Pass Assembler

Code_Area_Address = address of code_area;srtab_no = 1; LC = 0;

Statement_Number = 1;SYMTAB_segment_entry = 0;

Clear ERRTAB , SRTAB_ARRAY

While next statement is not end statement

Label ?

EQU?

ASSUME?

SEGMENT?

b c d e

ENDS?

Clear Machine_Code_Buffer

fa

m

Declaration Statement?

Imperative statement?

g h

Size <> 0 ?

i

START

Page 32: Chapter 2d Assembler- Single Pass Assembler

05/02/2023 Mrs. Sunita M Dol, CSE Dept 32

b

This_Label = Symbol in label field

m

Label?

Page 33: Chapter 2d Assembler- Single Pass Assembler

Code_Area_Address = address of code_area;srtab_no = 1; LC = 0;

Statement_Number = 1;SYMTAB_segment_entry = 0;

Clear ERRTAB , SRTAB_ARRAY

While next statement is not end statement

Label ?

EQU?

ASSUME?

SEGMENT?

b c d e

ENDS?

Clear Machine_Code_Buffer

fa

m

Declaration Statement?

Imperative statement?

g h

Size <> 0 ?

i

START

Page 34: Chapter 2d Assembler- Single Pass Assembler

05/02/2023 Mrs. Sunita M Dol, CSE Dept 34

c

This_Address = Value of operand specification

Make an entry for This_Label in Symbol Table Entry with Offset = This_Address; Defined = ‘yes’; Owner_Segment =m Owner_Segment of operand symbol; Source_Statement# = Statement_Number;

m

EQU?

Enter Statement_Number in the CRT list of the label in the operand field.

Process forward references to This_Label

size = 0

Page 35: Chapter 2d Assembler- Single Pass Assembler

Code_Area_Address = address of code_area;srtab_no = 1; LC = 0;

Statement_Number = 1;SYMTAB_segment_entry = 0;

Clear ERRTAB , SRTAB_ARRAY

While next statement is not end statement

Label ?

EQU?

ASSUME?

SEGMENT?

b c d e

ENDS?

Clear Machine_Code_Buffer

fa

m

Declaration Statement?

Imperative statement?

g h

Size <> 0 ?

i

START

Page 36: Chapter 2d Assembler- Single Pass Assembler

05/02/2023 Mrs. Sunita M Dol, CSE Dept 36

d

Copy the SRTAB in SRTAB_ARRAY[srtab_no] into SRTAB_ARRAY[srtab_no + 1]

srtab_no = srtab_no + 1

m

ASSUME?

This_Register = Register mentioned in the statement

This_Segment = entry number of SYMTAB entry of the segment appearing in the operand field

size := 0

Make the entry (This_Register ; This_Segment) in the SRTAB_ARRAY[srtab_no].

(This overwrites an existing entry for this register.)

Page 37: Chapter 2d Assembler- Single Pass Assembler

Code_Area_Address = address of code_area;srtab_no = 1; LC = 0;

Statement_Number = 1;SYMTAB_segment_entry = 0;

Clear ERRTAB , SRTAB_ARRAY

While next statement is not end statement

Label ?

EQU?

ASSUME?

SEGMENT?

b c d e

ENDS?

Clear Machine_Code_Buffer

fa

m

Declaration Statement?

Imperative statement?

g h

Size <> 0 ?

i

START

Page 38: Chapter 2d Assembler- Single Pass Assembler

05/02/2023 Mrs. Sunita M Dol, CSE Dept 38

e

Make an entry for This_Label in SYMTAB

Set segment name ? = true

m

SEGMENT?

SYMTAB_segment_entry = entry no in SYMTAB

LC = 0

Size = 0

Page 39: Chapter 2d Assembler- Single Pass Assembler

Code_Area_Address = address of code_area;srtab_no = 1; LC = 0;

Statement_Number = 1;SYMTAB_segment_entry = 0;

Clear ERRTAB , SRTAB_ARRAY

While next statement is not end statement

Label ?

EQU?

ASSUME?

SEGMENT?

b c d e

ENDS?

Clear Machine_Code_Buffer

fa

m

Declaration Statement?

Imperative statement?

g h

Size <> 0 ?

i

START

Page 40: Chapter 2d Assembler- Single Pass Assembler

05/02/2023 Mrs. Sunita M Dol, CSE Dept 40

f

SYMTAB_segment_entry = 0

m

ENDS?

Page 41: Chapter 2d Assembler- Single Pass Assembler

Code_Area_Address = address of code_area;srtab_no = 1; LC = 0;

Statement_Number = 1;SYMTAB_segment_entry = 0;

Clear ERRTAB , SRTAB_ARRAY

While next statement is not end statement

Label ?

EQU?

ASSUME?

SEGMENT?

b c d e

ENDS?

Clear Machine_Code_Buffer

fa

m

Declaration Statement?

Imperative statement?

g h

Size <> 0 ?

i

START

Page 42: Chapter 2d Assembler- Single Pass Assembler

05/02/2023 Mrs. Sunita M Dol, CSE Dept 42

g

Align LC according to the specification in the operand

field

Assemble the constant(s), if any, in the Machine_Code_Buffer

m

Declaration Statement?

size := size of memory area required

Page 43: Chapter 2d Assembler- Single Pass Assembler

Code_Area_Address = address of code_area;srtab_no = 1; LC = 0;

Statement_Number = 1;SYMTAB_segment_entry = 0;

Clear ERRTAB , SRTAB_ARRAY

While next statement is not end statement

Label ?

EQU?

ASSUME?

SEGMENT?

b c d e

ENDS?

Clear Machine_Code_Buffer

fa

m

Declaration Statement?

Imperative statement?

g h

Size <> 0 ?

i

START

Page 44: Chapter 2d Assembler- Single Pass Assembler

05/02/2023 Mrs. Sunita M Dol, CSE Dept 44

h

If operand is a symbol symb then enter Statement_Number in the CRT list of symb

m

Imperative Statement?

Operand Symbol already defined?

Check its alignment & addressability

Generate the address specification (segment register, offset) for the symbol using its

SYMTAB entry and SRTAB_ARRAY[srtab_no]

Make an entry for symbol in SYMTAB

Defined = ‘No’

Enter (srtab_no, LC, usage_code, Statement_Number) in FRT

Assemble the instruction in Machine_Code_Buffer

size := size of the instruction

Yes No

Page 45: Chapter 2d Assembler- Single Pass Assembler

Code_Area_Address = address of code_area;srtab_no = 1; LC = 0;

Statement_Number = 1;SYMTAB_segment_entry = 0;

Clear ERRTAB , SRTAB_ARRAY

While next statement is not end statement

Label ?

EQU?

ASSUME?

SEGMENT?

b c d e

ENDS?

Clear Machine_Code_Buffer

fa

m

Declaration Statement?

Imperative statement?

g h

Size <> 0 ?

i

START

Page 46: Chapter 2d Assembler- Single Pass Assembler

05/02/2023 Mrs. Sunita M Dol, CSE Dept 46

i

m

Size <> 0 ?

Label present?

Make an entry for This_Label in SYMTAB

Move contents of Machine_Code_Buffer to the address Code_Area_Address

Code_Area_Address := Code_Area_Address + size

Owner_Segment = SYMTAB_segment_entry; Defined = ‘yes’ ; Offset = LC;Source_Statement# = Statement_Number

Process forward references to the symbol. Check for alignment & addressability errors. Enter errors in ERRTAB

List the statement with errors contained in ERRTAB

List the statement with errors contained in ERRTAB

Clear ERRTAB

Yes

No

Page 47: Chapter 2d Assembler- Single Pass Assembler

Code_Area_Address = address of code_area;srtab_no = 1; LC = 0;

Statement_Number = 1;SYMTAB_segment_entry = 0;

Clear ERRTAB , SRTAB_ARRAY

While next statement is not end statement

Label ?

EQU?

ASSUME?

SEGMENT?

b c d e

ENDS?

Clear Machine_Code_Buffer

fa

m

Declaration Statement?

Imperative statement?

g h

Size <> 0 ?

i

START

Page 48: Chapter 2d Assembler- Single Pass Assembler

05/02/2023 Mrs. Sunita M Dol, CSE Dept 48

b c d e fa g h i

Report undefined symbol from SYMTAB

Produce cross reference listing

Write code_area into output file

END