Princeton University
COS 217:  Introduction to Programming Systems

Precept 19:  Assembler Assignment Pass 2

Purpose

Describe pass 2 of the assembler

Stage 2:  Pass 2 Data Section

See the Assembler Assignment Development Stages summary sheet

Must allocate a block of memory to store the data section

How much?  Determined by data section location counter from pass 1

Must fill that block of memory with an appropriate sequence of bytes

Suggestion:  Use calloc, not malloc

Example: 03directive.s

Tests pass 2 processing of directives

Handouts showing testing program 03directive.s and the data section that it generates

Implementation hints

See the Suggestions for Creating the Data Section handout

Testing

Use objcmp and objcmp_detail commands to compare .o file produced by myas vs. .o file produced by as

../myas 04opcode.s
as -o 04opcodeb.o 04opcode.s
objcmp -d 04opcode.o 04opcodeb.o
objcmp_detail -d 04opcode.o 04opcodeb.o

Stage 3:  Pass 2 Text Section

See the Assembler Assignment Development Stages summary sheet

Must dynamically allocate a block of memory to store the text section

How much?  Determined by text section location counter from pass 1

Must fill that block of memory with an appropriate sequence of bytes

Suggestion:  Use calloc, not malloc

To do that, must know SPARC mnemonic formats:

See SPARC Instruction Formats handout

Must know the fields for each mnemonic

For add mnemonic:

Show handout of page 108 of the SPARC Architecture Manual, showing the format of the add mnemonic

Etc, for each mnemonic

Example:  04opcode.s

Describe showing testing program 04opcode.s and the text section that it generates

Give handout of page 108 of the SPARC Architecture Manual, showing the format of the add instructions

For call instructions:  in an expression, a label evaluates to its offset

Implementation suggestion:

See the Suggestions for Creating the Text Section handout

Example:  05addrformat.s

Tests memory address formats

ld [%r1 + %r2], %r2

Format 3a:  (see previous testing program)

ld [%r1 + 2], %r2

Format 3b:  11 00010 000000 00001 1 0000000000010

ld [%r1 - 2], %r2

Format 3b:  11 00010 000000 00001 1 1111111111110

ld [%r1], %r2

Format 3a:  11 00010 000000 00001 0 00000000 00000

rs2 == g0

Example:  06instrformat.s

Tests instruction formats

(More of the same)

Example:  07register.s

Tests register specifications

Implementation suggestion:

Create function regToU

Parameter:  struct register_info*

Return value:   unsigned int

switch (psReg->reg_type)
{
   case R:
   case G:
     return psReg->reg_number;
   case O:
     return psReg->reg_number + 8;
   case L:
     return psReg->reg_number + 16;
   case I:
     return psReg->reg_number + 24;
   case SP:
     return 14;
   case FP:
     return 30;
}

Testing

Use objcmp and objcmp_detail commands to compare .o file produced by myas vs. .o file produced by as

../myas 04opcode.s
as -o 04opcodeb.o 04opcode.s
objcmp -t 04opcode.o 04opcodeb.o
objcmp_detail -t 04opcode.o 04opcodeb.o

Copyright © 2002 by Robert M. Dondero, Jr.