22
The LC-3 – Chapter 7 COMP 2620 Dr. James Money COMP 2620 1

The LC-3 – Chapter 7

Embed Size (px)

DESCRIPTION

The LC-3 – Chapter 7. COMP 2620. Assembly Program. ; ; Program to count occurrences of a character in a file. ; Character to be input from the keyboard. ; Result to be displayed on the monitor. ; Program only works if no more than 9 occurrences are found. ; ; ; Initialization ; - PowerPoint PPT Presentation

Citation preview

Page 1: The LC-3 – Chapter 7

The LC-3 – Chapter 7COMP 2620

Dr. James Money COMP 2620

1

Page 2: The LC-3 – Chapter 7

Assembly Program

;; Program to count occurrences of a character in a file.; Character to be input from the keyboard.; Result to be displayed on the monitor.; Program only works if no more than 9 occurrences are found.; ;; Initialization;

.ORIG x3000AND R2, R2, #0 ; R2 is counter, initially 0LD R3, PTR ; R3 is pointer to charactersGETC ; R0 gets character inputLDR R1, R3, #0 ; R1 gets first character

;; Test character for end of file;TEST ADD R4, R1, #-4 ; Test for EOT (ASCII x04)

BRz OUTPUT ; If done, prepare the output

Page 3: The LC-3 – Chapter 7

Assembly Program

;; Test character for match. If a match, increment count.;

NOT R1, R1ADD R1, R1, R0 ; If match, R1 = xFFFFNOT R1, R1 ; If match, R1 = x0000BRnp GETCHAR ; If no match, do not incrementADD R2, R2, #1

;; Get next character from file.;GETCHAR ADD R3, R3, #1 ; Point to next char

LDR R1, R3, #0 ; R1 gets next char to testBRnzp TEST

;; Output the count.;OUTPUT LD R0, ASCII ; Load the ASCII template

ADD R0, R0, R2 ; Covert binary count to ASCIIOUT ; ASCII code in R0 is displayed.HALT ; Halt machine

Page 4: The LC-3 – Chapter 7

Assembly Program

;; Storage for pointer and ASCII template;ASCII .FILL x0030PTR .FILL x4000

.END

Page 5: The LC-3 – Chapter 7

Assembly Process

Before you can execute an LC-3 assembly program, it has to converted to machine code for the LC-3 ISA

The job of the assembler is to perform this task

Essentially, it is a translation from one assembly command to a corresponding LC-3 instruction

Page 6: The LC-3 – Chapter 7

Two-Pass Process

We consider how the assembler performs this translation to machine code

Recall there is a one-to-one correspondence between assembly instructions and machine instructions

One could do this in one pass The first 9 lines are comments are ignored

Page 7: The LC-3 – Chapter 7

Two-Pass Process

At line 0A, we have a pseudo-op which sets the initial PC to 0x3000

Line 0B is an AND instruction which readily translates to

0x3000: 0101010010100000

Page 8: The LC-3 – Chapter 7

Two-Pass Process

However, at line 0C, there is no knowledge yet of the memory location that PTR refers to

At this point, the assembler fails and exits if it only uses one pass of the assembly file input

To get around this, we have to use two passes of the assembly file

Page 9: The LC-3 – Chapter 7

Two-Pass Process

Pass 1:– Create list of addresses corresponding to labels– This is called a symbol table

Pass 2:– We translate the assembly instructions– We use the symbol table to clarify references

Page 10: The LC-3 – Chapter 7

Two-Pass Process

Now, at line 0C, we have to translate

LD R3,PTR But we know from the first pass, PTR refers

to memory location 0x3013 Thus, this translates to the instruction

0x3001: 0010011000010001

Page 11: The LC-3 – Chapter 7

First Pass

From our perspective, the symbol table is only list of symbolic names with 16 bit memory locations

We obtain this table by going through the file completely and see what lines and appropriate memory locations applies to each symbol

Page 12: The LC-3 – Chapter 7

First Pass

If we have made all our labels in the assembly program, then we have no unfound symbols in our table for the second pass

For now, we only consider single file programs with only one .ORIG and .END pseudo-ops

Page 13: The LC-3 – Chapter 7

First Pass

The pseudo-op at line 0A makes the current location 0x3000

This is called the location counter or the LC The LC is initialized to whatever value is

provided by the .ORIG operand

Page 14: The LC-3 – Chapter 7

First Pass

Then, for the rest of the file, the LC increments one value for each line that is not a comment

If there is a label, an entry is put in the symbol table

The first pass ends when .END is encountered

Page 15: The LC-3 – Chapter 7

First Pass

The first label is at line 13, which is TEST This is the fifth instruction, so LC is 0x3004 Thus the table looks like:

Symbol Address

TEST x3004

Page 16: The LC-3 – Chapter 7

First Pass

The second label is at line 20 The LC is now increments to x300B Thus, the table is updated to

Symbol Address

TEST x3004

GETCHAR x300B

Page 17: The LC-3 – Chapter 7

First Pass

After you reach .END, the symbol table isSymbol Address

TEST x3004

GETCHAR x300B

OUTPUT x300E

ASCII X3012

PTR x3013

Page 18: The LC-3 – Chapter 7

Second Pass

In the second pass, we generate the machine code for each instruction

This time, though, we use the symbol table to assist in memory references

When we reach line 0C, we can use the address 0x3013 which corresponds to the label PTR

Page 19: The LC-3 – Chapter 7

Second Pass

The instruction is

LD R3, PTR So, the opcode is 0010 The DR is 011 How do we compute the PC offset?

Page 20: The LC-3 – Chapter 7

Second Pass

Recall the incremented PC is LC+1 here Thus, PC = x3002 And PTR is x3013 Subtracting we have 0x3013-0x3002 = 0x11 Thus, PC offset = 000010001 Hence, our instruction is

0010011000010001

Page 21: The LC-3 – Chapter 7

Second Pass

Note, if the address of PTR more than +256 or less that -255, an error is issued by the assembler

This is because we only have 9 bits to encode the offset

If this is the case, you will have to use another load instruction

Page 22: The LC-3 – Chapter 7

Second Pass

Second pass continues, and finishes, incrementing LC as it goes

It uses the symbol table as needed Resulting code is on the page 188