1 / 22

The LC-3 – Chapter 7

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 ;

Download Presentation

The LC-3 – Chapter 7

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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 ; .ORIG x3000 AND R2, R2, #0 ; R2 is counter, initially 0 LD R3, PTR ; R3 is pointer to characters GETC ; R0 gets character input LDR 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

  3. Assembly Program ; ; Test character for match. If a match, increment count. ; NOT R1, R1 ADD R1, R1, R0 ; If match, R1 = xFFFF NOT R1, R1 ; If match, R1 = x0000 BRnp GETCHAR ; If no match, do not increment ADD 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 test BRnzp TEST ; ; Output the count. ; OUTPUT LD R0, ASCII ; Load the ASCII template ADD R0, 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 .FILL x0030 PTR .FILL x4000 .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:

  16. First Pass • The second label is at line 20 • The LC is now increments to x300B • Thus, the table is updated to

  17. First Pass • After you reach .END, the symbol table is

  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

More Related