Download presentation
Presentation is loading. Please wait.
Published byGeorge Penniman Modified over 9 years ago
1
The LC-3 – Chapter 7 COMP 2620 Dr. James Money COMP 2620 1
2
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 ;.ORIGx3000 ANDR2, R2, #0; R2 is counter, initially 0 LDR3, PTR; R3 is pointer to characters GETC; R0 gets character input LDRR1, R3, #0; R1 gets first character ; ; Test character for end of file ; TESTADDR4, R1, #-4; Test for EOT (ASCII x04) BRzOUTPUT; If done, prepare the output
3
Assembly Program ; ; Test character for match. If a match, increment count. ; NOTR1, R1 ADDR1, R1, R0; If match, R1 = xFFFF NOTR1, R1; If match, R1 = x0000 BRnpGETCHAR; If no match, do not increment ADDR2, R2, #1 ; ; Get next character from file. ; GETCHAR ADD R3, R3, #1 ; Point to next char LDRR1, R3, #0; R1 gets next char to test BRnzpTEST ; ; Output the count. ; OUTPUTLDR0, ASCII; Load the ASCII template ADDR0, R0, R2; Covert binary count to ASCII OUT; ASCII code in R0 is displayed. HALT; Halt machine
4
Assembly Program ; ; Storage for pointer and ASCII template ; ASCII.FILLx0030 PTR.FILLx4000.END
5
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
6
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
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
8
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
9
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
10
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
11
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
12
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
13
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
14
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
15
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: SymbolAddress TESTx3004
16
First Pass The second label is at line 20 The LC is now increments to x300B Thus, the table is updated to SymbolAddress TESTx3004 GETCHARx300B
17
First Pass After you reach.END, the symbol table is SymbolAddress TESTx3004 GETCHARx300B OUTPUTx300E ASCIIX3012 PTRx3013
18
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
19
Second Pass The instruction is LD R3, PTR So, the opcode is 0010 The DR is 011 How do we compute the PC offset?
20
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
21
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
22
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.