Princeton University
COS 217:  Introduction to Programming System

Precept 17:  Assembler Assignment Overview

Purpose

Introduce the "assembler" assignment

Specifically, introduce the high-level structure of the program that you will create

Describe the stages of program development

Program Structure

Handwritten diagram showing use of "as" assembler and "gcc" linker

Note Executable and Linking Format (ELF)

Handwritten diagram showing use of "myas" assembler and "gcc" linker

Diagram showing structure within the myas assembler:  input(), your code, and output()

See file main.c

Note (unfortunate) reliance on global variables

The Assembly Language

Subset of SPARC assembly language

No preprocessor directives; file suffix is .s, not .S

In particular, referring to the "SPARC Assembly Language Summary" handout...

Sections

data
bss
text

No rodata section, programmer-defined sections

Directives (alias pseudo-ops)

.section
.skip
.align
.byte
.half
.word
.ascii
.asciz
.global

No .common, .empty (rarely used anyway) (see Paul p. 255 if interested)

Mnemonics (alias executable instructions)

Load and store:  all

Shift:  all

Arithmetic:  all

Logical:  all non-synthetic, mov, tst, not

No clr, btst, bset, bclr, btog

Integer branch:  almost all

No bz (same as be) and bnz (same as bne)

No annul bit

Control:  jmpl, call, ret, save, restore, nop, sethi

No retl, set

Trap:  ta, rett

Expressions

Constants

.skip  4
.word  18
.word  18, 3
save   %sp, -96, %sp

Constants and operators

.skip  4 * 100
.word  (18 + 5) * 3
save   %sp, (-92 - 8) & -8, %sp

Labels

call  label1
bg    label2
sethi %hi(label3), %l1
or    %l1, %lo(label3), %l1

But not:

.word label5
call  label6 + 16
bg    label7 - 12
... %hi(label8) + 24 ...
... %hi(label8 + 24) ...

Most of which are unusual anyway

Example:  app_01fibonacci.s

[Give handout]

[Give quick demo]

Note:  uses only the specified assembly language subset

Lines 57-58:  no set instruction

Line 125:  Label in ble instruction

Line 116, 138:  Label in call instruction

Line 57:  Label as operand of hi operator

Line 58:   Label as operand of lo operator

The In-Memory Assembly Language Program

Interface defined in input.h

See The In-Memory Assembly Language Program handout

Handwritten example instructions to illustrate each structure

The In-Memory Object Program

Interface defined in output.h

Four data structures:  symbol table, data section, bss section, text section

Symbol table

A table containing bindings

Contains 1 binding for each label

key = label name (a string)

value = pointer to Elf32_Sym structure

(1) Section (data, bss, text)
(2) Offset into section
(3) Local vs. global
(4) Sequence number (unique across all sections)

Implemented using Hanson's Table ADT

Data section

A slab of memory containing an appropriate sequence of bytes

Bss section

A slab of memory containing zeros

Text section

A slab of memory containing an appropriate sequence of bytes -- 4 bytes for each instruction

Relocation information

Label within an expression must be converted into an address

Some labels can be converted by assembler

Example:  line 125:  ble LL12

Some labels must be resolved by linker

Example:  line 138:  call printf

Assembler must mark such labels by creating a relocation structure

The Object Program

See lecture notes

See ELF manual

Need not know thoroughly to write your portion of the assembler

Assembler Assignment Stages

See the Assembler Assignment:  Development Stages summary sheet

Stage 0:  The Development Environment

See the summary sheet

Copyright © 2002 by Robert M. Dondero, Jr.