0% found this document useful (0 votes)
46 views

SP2.1 Assembler-Basic Assembler Functions

The document discusses basic assembler functions including translating mnemonics to machine code, assigning addresses to symbols, and assembler directives. It also provides an example program and explains the two pass assembly process using symbol and operation tables to generate object code.

Uploaded by

tanjuner01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

SP2.1 Assembler-Basic Assembler Functions

The document discusses basic assembler functions including translating mnemonics to machine code, assigning addresses to symbols, and assembler directives. It also provides an example program and explains the two pass assembly process using symbol and operation tables to generate object code.

Uploaded by

tanjuner01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Chapter 2 Assemblers

-- Basic Assembler Functions

Tzong-Jye Liu
Department of Information Engineering and Computer Science
Feng Chia University
tjliu@fcu.edu.tw

Outline

n Basic assembler functions


n A simple SIC assembler
n Assembler algorithm and data structure

System Programming 2
Basic assembler functions (1/2)

n Translating mnemonic operation codes to


their machine language equivalents
n Assigning machine addresses to symbolic
labels

Source Program
• Mnemonic opcode Assembler Object code
• Symbol

System Programming 3

Basic assembler functions (2/2)


LDA FIVE
STA ALPHA
LDCH CHARZ
STCH C1
.
.
ALPHA RESW 1
FIVE WORD 5
CHARZ BYTE C'Z'
C1 RESB 1
1. Instruction format Format 3 (3 byte) op (6) n i x b p e disp (12)

2. Opcode: LDA à 00 000000


3. Addressing mode: simple addressing + PC relative n=1, i=1, p=1
4. Calculate disp disp = 12

000000 1 1 0 0 1 0 00C

03200C
Question: FIVE 為⼀個 Symbol,如何計算 FIVE 的位址?
System Programming 4
Assembler directive
n Assembler directives (組譯器指引) are pseudo
instructions
n They provide instructions to the assembler itself
n They are not translated into machine operation codes
n SIC assembler directive
n START : specify name & starting address
n END : end of source program, specify the first
execution instruction
n BYTE, WORD, RESB, RESW

n End of record : a null char (00)


n End of file : a zero-length record

System Programming 5

Example program (Figure 2.1 pp. 45) (1/4)


n Purpose of example program
n Reads records from input device (code F1)
n Copies them to output device (code 05)
n At the end of the file, writes EOF on the output device, then RSUB
to the operating system
n Data transfer (RD, WD)
n A buffer is used to store record
n Buffering is necessary for different I/O rates
n The end of each record is marked with a null character (00)16
n The end of the file is indicated by a zero-length record
n Subroutines (JSUB, RSUB)
n RDREC, WRREC
n Save link register first before nested jump

System Programming 6
Example program (Figure 2.1 pp. 45) (2/4)

Forward
reference

System Programming 7

Example program (Figure 2.1 pp. 45) (3/4)

System Programming 8
Example program (Figure 2.1 pp. 45) (4/4)

System Programming 9

A simple SIC assembler

n Assembler’s functions
n Convert mnemonic operation codes to their machine
language equivalents
v Convert symbolic operands to their equivalent machine
addresses
n Decide the proper instruction format
n Convert the data constants to internal machine
representations
n Write the object program and the assembly listing

System Programming 10
Difficult

n Convert symbolic operands to their equivalent


machine addresses
n Forward reference
n 2 passes
n First pass: scan the source program for
label definitions and assign addresses
n Second pass: perform actual translation

System Programming 11

Example program with object code


(Figure 2.2 pp. 47)

tjliu@fcu.edu.tw System Programming 12


Example program with object code
(Figure 2.2 pp. 47)

System Programming 13

Example program with object code


(Figure 2.2 pp. 47)

System Programming 14
Format of object program
(Figure 2.3 pp.49)
n Header record
Col. 1 H
Col. 2~7 Program name
Col. 8~13 Starting address of object program (hex)
Col. 14-19 Length of object program in bytes (hex)
n Text record
Col. 1 T
Col. 2~7 Starting address for object code in this record (hex)
Col. 8~9 Length of object code in this record in bytes (hex)
Col. 10~69 Object code, represented in hex (2 col. per byte)
n End record
Col.1 E
Col.2~7 Address of first executable instruction in object program (hex)

n “^” is only for separation only


System Programming 15

Format of object program


(Figure 2.3 pp.49)

Address 1033 ~ 2038: reserve storage by loader


• RETADR: 3 bytes
• LENGTH: 3 bytes
• BUFFER: 4096 bytes = (1000)16

System Programming 16
The two passes of an assembler
n Pass 1 (define symbols)
n Assign addresses to all statements in the program
n Save the addresses assigned to all labels for use in Pass 2
n Perform assembler directives, including those for address
assignment, such as BYTE and RESW
n Pass 2 (assemble instructions and generate object
program)
n Assemble instructions (generate opcode and look up addresses)
n Generate data values defined by BYTE, WORD
n Perform processing of assembler directives not done during Pass 1
n Write the object program and the assembly listing

System Programming 17

Assembler algorithm and data structures

n OPTAB: operation code table


n SYMTAB: symbol table
n LOCCTR: location counter
Assembler

LOCCTR OPTAB SYMTAB

Source Object
Pass 1 Intermediate Pass 2
Program Code
File

The intermediate file include each source statement, assigned address and error indicator
System Programming 18
OPTABLE

n Mnemonic operation codes Û Machine code


n Contain instruction format and length
n LOCCTR ¬ LOCCTR + (instruction length)
n Implementation
n It is a static table
n Array or hash table
n Usually use a hash table (mnemonic opcode
as key)

System Programming 19

LOCCTR

n Initialize to be the beginning address


specified in the “START” statement
n LOCCTR ¬ LOCCTR + (instruction length)
n The current value of LOCCTR gives the
address to the label encountered

System Programming 20
SYMTAB

n Label name Û label address, type, length, flag


n To indicate error conditions (Ex: multiple define)
n It is a dynamic table
n Insert, delete and search
n Usually use a hash table
n The hash function should perform non-random
key (Ex: LOOP1, LOOP2, X, Y, Z)

System Programming 21

Algorithm Pass 1
(Figure 2.4(a), pp.53)

System Programming 22
LABEL field
OPCODE

System Programming 23

1-token.c
2-optable.c
3-asm_pass1_u.c
SIC/XE Program Syntax
SYMBOL +INSTRUCTION [@|#]OPERAND1, OPERAND2 COMMENT ‘\n’

SYM + INST
S0 S1 S2 S3
INST
INST
(FMT1 || RSUB)
(FMT1 || RSUB) \n|EOF
SYM @|#
(comment) \n|EOF S8
(operand1)
SYM
(FMT2) \n|EOF
(operand2)
SYM SYM ,
(comment) S7 ’X’
S6 S5 SYM S4
(operand1)

SYM (comment)

System Programming 24
Algorithm Pass 2
(Figure 2.4(b), pp.54)

System Programming 25

OPERAND field

System Programming 26

You might also like