LC3 Assembler, Loader, RR Instructions

Review

Von Neumann architecture

·         Processing Unit

·         ALU

·         Registers

 

R0

0x058D

R1

0x0000

R2

0xXXXX

R3

0xXXXX

R4

0xXXXX

R5

0xXXXX

R6

0xXXXX

R7

0xXXXX

·         Control Unit

·         Program Counter

0x3000

·         Instruction Register

0x1702

 

·         Memory

0x0000

 

 

 

 

0x0001

 

 

 

 

0x0002

 

 

 

 

0x0003

 

 

 

 

0x0004

3

7

5

1

0x0005

 

 

 

 

 

 

 

 

 

 

 

 

 

0xFFFD

 

 

 

 

0xFFFE

 

 

 

 

0xFFFF

 

 

 

 

·         Input/Output Devices

Register to Register (RR) Instructions

Read and write directly from registers

In LC3, only 3…

ADD

o   Assembler:

o   OpCode: ADD

o   Operands: Destination Register, Source Register 1, Source Register 2 or Immediate Value

o   Object:

o   OpCode: 0001

o   Operands: DR SR1, SR2 or IMM5

o   Adds source register 1 to either source register 2 or immediate value and stores the  result in the destination register

AND

o   Assembler:

o   OpCode: AND

o   Operands: Destination Register, Source Register 1, Source Register 2 or Immediate Value

o   Object:

o   OpCode: 0101

o   Operands: DR SR1, SR2 or IMM5

o   Bitwise “and”s source register 1 to either source register 2 or immediate value and stores the  result in the destination register

NOT

o   Assembler:

o   OpCode: NOT

o   Operands: Destination Register, Source Register

o   Object:

o   OpCode: 1001

o   Operands: DR SR

o   Inverts source register and stores the  result in the destination register

So, to set a register R3 to zero…

            NOT R2,R3  set r2 = ~r3

            AND R3,R2,R3 r3 = r3 & ~r3 = 0

Immediate Values

·         Immediate (or Literal) Values – Replace second source register with number

·         Immediate Flag Bit in instruction differentiates between immediate values and second source registers

·         Number in object (machine) code is 5 bit two’s complement! (imm5) (-16->15!)

·         In Assembler Language, literals are preceded by:

·         “#” For a decimal number, e.g. #3 or #-8

·         “x” For a hexadecimal number e.g. x1F

·         “o” For an octal number e.g. o37

For example…

            AND R3,R3,x0 ; zero r3

ADD R3,R3,#10  ; r3=r3+10=0+10=10
            AND R2,R2,#0 ; zero r2
            ADD R2,R2,#15 ; r2=15
            ADD R4,R3,R2 ; r4=25

 

What does the following example do???

            AND R0,R1,#0
            ADD R0,R0,#13
            ADD R0,R0,R0
            NOT R1,R0
            ADD R1,R1,#1
            AND R2,R1,#0
            ADD R2,R2,#14
            ADD R2,R2,R2
            ADD R6,R1,R2

How do I add 100 to a number in a register?

How many instructions do I need?

Can I do it in fewer instructions?

RR Instruction Cycle

o   Fetch Instruction: Read word @PC into IR, Increment PC

o   Decode: Read OpCode, parse operands

o   Evaluate Address: No operation (No op)

o   Fetch Operands: Retreive SR1, SR2, and/or sign-extend IMM5 and send them to ALU

o   Execute: Perform required ALU operation on operands

o   Store Results: Move ALU output to DR

The Assembly Process

Converting Assembly language instructions into “object” (machine) code

For example…

Assembler

 

OPC

DR

SR1

 

IMM5/SR2

 

 

 

 

 

 

Object

            .ORIG x3000

            AND R0,R1,#0
            ADD R0,R0,#13
            ADD R0,R0,R0
            NOT R1,R0
            ADD R1,R1,#1
            AND R2,R1,#0
            ADD R2,R2,#14
            ADD R2,R2,R2
            ADD R6,R1,R2

            TRAP x25

            .END

     

 

0101

0001

0001

1001

0001

0101

0001

0001

0001

1111

 

000

000

000

001

001

010

010

010

110

-

 

001

000

000

000

001

001

010

010

001

-

 

1

1

0 00

-

1

1

1

0 00

0 00

0000

 

00000

01101

000

111111

00001

00000

01110

010

010

0010 0101

 

0101

0001

0001

1001

0001

0101

0001

0001

0001

1111

 

0000

0000

0000

0010

0010

0100

0100

0100

1100

0000

 

0110

0010

0000

0011

0110

0110

1010

1000

0100

0010

 

0000

1101

0000

1111

0001

0000

1110

0010

0010

0101

 

0x5060

0x102D

0x1000

0x923F

0x1261

0x5460

0x14AE

0x1482
0x1A42

0xF025

 See TextBook  Appendix A

More about labels and addresses later!

Pseudo-Instructions

·         Parameters to the Assembly (and/or Load) process

·         Have pseudo OpCodes and pseudo operands

.ORIG <address>

·         Tells assembler where in memory the machine code will go

·         Usually
    .ORIG x3000

.END

·         Tells assembler to stop reading for more instructions

·         Does NOT stop the instruction processing loop!

·         Use: “TRAP x25” … to stop the instruction processing loop.

The Loader Process

1.      Copy object (machine) code into memory (from .orig)

2.      Set the PC to the address of the first instruction (from .orig)

0x0000

 

 

 

 

 0x0001

 

 

 

 

0x0002

 

 

 

 

 

 

 

 

0x3000

5

0

6

0

0x3001

1

0

2

D

0x3002

1

0

0

0

0x3003

9

2

3

F

 

 

 

 

 

 

 

 

 

 

PC

0x3000

Getting from Memory to Registers and Back

PC Offset Addressing

·         Special field in instruction – PCOFFSET9

o   9 bit two’s complement number (-256 -> 255)

·         Value added to current PC value

o   Remember, after Instruction Fetch, PC points to the next instruction!

For example…

Assembler

MEM@

Object Code

.ORIG x3000

 

 

LD R3,#1

0x3000

0010 011 000000001

TRAP x25

0x3001

1111 0000 0010 0101

ADD R0,R0,R0

0x3002

0001 000 000 0 00 000

.END

 

 

Assembler Data Literals

Pseudo Instruction .FILL <value>

o   Fills in memory location with specified value

o   Operand specifies value using “x”, “o”, and “#”

o   Fills in 16 bit two’s complement word in memory

For example…

Assembler

MEM@

Object Code

.ORIG x3000

 

 

LD R3,#2

0x3000

0010 011 000000010

TRAP x25

0x3001

1111 0000 0010 0101

.FILL #320

0x3002

0000 0001 0100 0000

.FILL #321

0x3003

0000 0001 0100 0001

.FILL xFFFF

0x3004

1111 1111 1111 1111

.END

 

 

PC Offset Load and Store

LOAD

o   Assembler:

o   OpCode: LD

o   Operands: – Destination Register, PC Offset

o   Object:

o   OpCode: 0010

o   Operands: DR PCOFFSET9

o   Reads memory at effective address (PC + PCOFFSET), and writes that value into Register

MAR and MDR

o   MAR – Memory Address Register – holds calculated addresses (e.g. PC + PCOFFSET)

o   MDR – Holds data being read from or written to memory

Load Instruction Cycle

For example – LD R3,#2 = 0010 011 000000010

·         Fetch Instruction: Read word @PC into IR

·         Decode: opcode=LD, DR=3 PCOFFSET9=000000010 PC=PC+1=0x3001

·         Evaluate Address: PC->ALU operand 1, PCOFFSET9 sign extended ->ALU operand 2, ADD, write result (x3003) to MAR

·         Fetch Operands: Read memory @MAR into MDR (x0141)

·         Execute: No operation (No op)

·         Store Results: Copy MDR to Dest Reg (R3=x0141=321)

STORE

o   Assembler:

o   OpCode: ST

o   Operands: SR, PC Offset

o   EXCEPTION WARNING: Reads Left to Right!

o   Object

o   OpCode: 0011

o   Operands : SR PCOFFSET9

o   Reads value in SR, and writes that value to memory at effective address (PC+PCOFFSET)

Assembler

MEM@

Object Code

.ORIG x3000

 

 

AND R3,R3,#0

0x3000

0101 011 011 1 00000

ADD R3,R3,#14

0x3001

0001 011 011 1 01110

ST R3,#1

0x3002

0011 011 000000001

TRAP x25

0x3003

1111 0000 0010 0101

.FILL #0

0x3004

0000 0000 0000 0000

.END

 

 

Store Instruction Cycle

Example: ST R3,#1 = 0011 011 000000001 @ PC=0x3002

·         Fetch Instruction: Read word @PC into IR, Increment PC 0x3002+1=0x3003

·         Decode: opcode=ST, SR=3 PCOFFSET9=000000001

·         Evaluate Address: PC->ALU operand 1, PCOFFSET9 sign extended ->ALU operand 2, ADD, write result (x3004) to MAR

·         Fetch Operands: Read value of R3 (0x000E) – write into MDR

·         Execute: No operation (No op)

·         Store Results: Write MDR to memory at address in MAR

Assembler Symbol Table

o   Assembler calculates PC offset automatically

o   Allows labels in place of PC offset values

For Example

Assembler

 

MEM@

Object Code

 

.ORIG x3000

 

 

 

AND R3,R3,#0

0x3000

0101 011 011 1 00000

 

ADD R3,R3,#14

0x3001

0001 011 011 1 01110

 

ST R3,mydata

0x3002

0011 011 000000001

 

TRAP x25

0x3003

1111 0000 0010 0101

mydata

.FILL #0

0x3004

0000 0000 0000 0000

 

.END

 

 

o    First pass of assembler… build symbol table

o   Symbol Table – for each label, what is the memory address

Example Symbol Table…

Symbol

Location

mydata

0x3004

o   Second Pass… Convert label references to PCOFFSET

For example,

mydata location = 0x3004

ST R3,mydata, PC=0x3002,

incremented PC=0x3003

mydata location – incremented PC = 0x3004 – 0x3003

= 0x0001