π Lecture 27 β Interrupt Driven I/O
π Overview: This lecture covers the fundamental concepts of interrupt-driven I/O, contrasting it with programmed I/O polling. It explains how interrupts enable efficient CPU utilization by allowing the processor to respond to events from peripherals only when necessary. The lecture details interrupt types, handling mechanisms, priority schemes, and the system layers involved in I/O software.
ποΈ Topics Covered
The lecture begins with a review of the programmed I/O driver for the SRC processor. It then introduces the concept of interrupts and their advantages over polling. The main discussion categorizes interrupts into internal, external, hardware, and software types. The lecture explains the structure of the I/O software system layers, the Interrupt Service Routine (ISR), and methods for determining the branch address (vectored vs. non-vectored). It covers interrupt handling in the Intel 8086/8088, including the interrupt vector table and branch address calculation. The lecture concludes with concepts of interrupt latency, response deadlines, and methods for handling simultaneous interrupt requests through daisy-chaining and parallel priority.
π Lecture Summary
Programmed I/O Driver for SRC
The lecture references Figure 8.10 of the text and its explanation for the programmed I/O driver for the SRC processor. This method involves the CPU continuously polling the I/O device to check its status, which is inefficient for devices with slow data rates.
Interrupt Driven I/O: Introduction
An interrupt is a request to the CPU to suspend normal processing and temporarily divert the flow of control through a new program. This new program to which control is transferred is called an Interrupt Service Routine (ISR) or Interrupt Handler. Interrupts are used to demand attention from the CPU. They are asynchronous breaks in program flow that occur as a result of events outside the running program, usually hardware related, stemming from events such as a key or button press, timer expiration, or completion of a data transfer.
The basic purpose of interrupts is to divert CPU processing only when it is required. For example, in a word-processing program on a multi-tasking OS, polling the keyboard wastes processor cycles because user input is much slower than the CPU. With interrupts, a peripheral device signals the processor only when it has data to exchange. The processor saves its state, executes the interrupt handler, and then resumes its task. A modern processor like the Pentium can execute up to 300,000,000 instructions in the 500 ms it takes for an average user to press consecutive keys.
Advantages of interrupts:
- Useful for interfacing I/O devices with low data transfer rates.
- CPU is not tied up in a tight loop for polling the I/O device.
Types of Interrupts
The general categories of interrupts are: Internal Interrupts, External Interrupts, Hardware Interrupts, and Software Interrupts.
Internal Interrupts:
- Internal interrupts are generated by the processor.
- These are used by the processor to handle the exceptions generated during instruction execution.
- Internal interrupts are used to handle conditions such as stack overflow or a divide-by-zero exception. They are also referred to as traps and are mostly used for exception handling.
External Interrupts: External interrupts are generated by devices other than the processor. They are of two types:
- Hardware interrupts are generated by the external hardware.
- Software interrupts are generated by the software using some interrupt instruction.
As the name implies, external interrupts are generated by devices external to the CPU, such as the click of a mouse or pressing a key on a keyboard. These events require quick service by the software.
Hardware interrupts: Hardware interrupts are generated by external events specific to peripheral devices. Most processors have at least one line dedicated to interrupt requests. When a device signals on this specific line, the processor halts its activity and executes an interrupt service routine. Such interrupts are always asynchronous with respect to instruction execution and are not associated with any particular instruction. They do not prevent instruction completion as exceptions like arithmetic overflow does. Thus, the control unit only needs to check for such interrupts at the start of every new instruction.
There are two types of hardware interrupt: Maskable Interrupts and Non-maskable Interrupts.
Maskable Interrupts:
- These interrupts are applied to the INTR pin of the processor.
- These can be blocked by resetting the flag bit for the interrupts.
Non-maskable Interrupts:
- These interrupts are detected using the NMI pin of the processor.
- These can not be blocked or masked.
- Reserved for catastrophic event in the system.
Software interrupts: Software interrupts are usually associated with the software. A simple output operation in a multitasking system requires software interrupts to be generated so that the processor may temporarily halt its activity and place the data on its data bus for the peripheral device. Software interrupts are also used with system calls. When the operating system switches from user mode to supervisor mode it does so through software interrupts. For example, when a user program makes a system call to delete a file, a software interrupt is generated, causing the processor to halt its current activity and switch to supervisor mode.
I/O Software System Layers
At the bottom lies the actual hardware itself, i.e., the peripheral device. The peripheral device uses hardware interrupts to communicate with the processor. The processor responds by executing the interrupt handler for that particular device. The device drivers form the bridge between the hardware and the software. The operating system uses the device drivers to communicate with the device in a hardware independent fashion. The user programs run at top of the operating system.
Interrupt Service Routine (ISR)
- It is a routine which is executed when an interrupt occurs.
- Also known as an Interrupt Handler.
- Deals with low-level events in the hardware of a computer system, like a tick of a real-time clock.
Branch Address of the ISR
There are two ways used to choose the branch address of an Interrupt Service Routine: Non-vectored Interrupts and Vectored Interrupts.
Non-vectored Interrupts: In non-vectored interrupts, the branch address of the interrupt service routine is fixed. The code for the ISR is loaded at a fixed memory location. Non-vectored interrupts are very easy to implement but not flexible at all. In this case, the number of peripheral devices is fixed. Once the interrupt is generated, the processor queries each peripheral device to find out which device generated the interrupt.
Vectored Interrupts: Interrupt vectors are used to specify the address of the interrupt service routine. The code for ISR can be loaded anywhere in the memory. This approach is much more flexible as the programmer may easily locate the interrupt vector and change its addresses to use custom interrupt servicing routines. Using vectored interrupts, multiple devices may share the same interrupt input line to the processor. A process called daisy chaining is then used to locate the interrupting device.
Interrupt Vector: An interrupt vector is a fixed size structure that stores the address of the first instruction of the ISR.
Interrupt Vector Table:
- All of the interrupt vectors are stored in the memory in a special table called Interrupt Vector Table.
- Interrupt Vector Table is loaded at the memory location 0 for the 8086/8088.
Interrupts in Intel 8086/8088
- Interrupts in 8086/8088 are vector interrupts.
- Interrupt vector is of 4 bytes to store IP and CS.
- Interrupt vector table is loaded at address 0 of main memory.
- There is provision of 256 interrupts.
Branch Address Calculation:
- The number of the interrupt is the number of the interrupt vector in the interrupt vector table.
- Since the size of each vector is 4 bytes and the interrupt vector table starts from address 0, the address of the interrupt vector can be calculated by simply multiplying the number by 4.
π Definition β Interrupt Vector Address Calculation: The address of the interrupt vector = Interrupt Number Γ 4. π Formula: Address = Interrupt Number Γ 4 β This gives the memory location where the 4-byte pointer (IP and CS) for the ISR is stored. π Example: For interrupt number 5, the interrupt vector address = 5 Γ 4 = 20 (0x14). The CPU reads the 4 bytes starting at memory address 20 to get the IP and CS of the ISR.
Returning from the ISR
Every ISA should have an instruction, like the IRET instruction, which should be executed when the ISR terminates. This means that the IRET instruction should be the last instruction of every ISR. This is, in effect, a FAR RETURN in that it restores a number of registers, and flags to their value before the ISR was called. Thus the previous environment is restored after the servicing of the interrupt is completed.
Interrupt Handling
The CPU responds to the interrupt request by completing the current instruction, and then storing the return address from PC into a memory stack. Then the CPU branches to the ISR that processes the requested operation. In general, the following sequence takes place.
Hardware Interrupt Handling:
- Hardware issues interrupt signal to the CPU.
- CPU completes the execution of current instruction. CPU acknowledges interrupt.
- Hardware places the interrupt number on the data bus.
- CPU determines the address of ISR from the interrupt number available on the data bus.
- CPU pushes the program status word (flags) on the stack along with the current value of program counter.
- The CPU starts executing the ISR.
- After completion of the ISR, the environment is restored; control is transferred back to the main program.
Interrupt Latency
Interrupt Latency is the time needed by the CPU to recognize (not service) an interrupt request. It consists of the time to perform the following:
- Finish executing the current instruction.
- Perform interrupt-acknowledge bus cycles.
- Temporarily save the current environment.
- Calculate the IVT address and transfer control to the ISR.
If wait states are inserted by either some memory module or the device supplying the interrupt type number, the interrupt latency will increase accordingly. Interrupt Latency for external interrupts depends on how many clock periods remain in the execution of the current instruction. On the average, the longest latency occurs when a multiplication, division or a variable-bit shift or rotate instruction is executing when the interrupt request arrives.
Response Deadline
It is the maximum time that an interrupt handler can take between the time when interrupt was requested and when the device must be serviced.
Expanding Interrupt Structure
When there is more than one device that can interrupt the CPU, an Interrupt Controller is used to handle the priority of requests generated by the devices simultaneously.
Interrupt Precedence
Interrupts occurring at the same time (i.e., within the same instruction) are serviced according to a pre-defined priority.
- In general, all internal interrupts have priority over all external interrupts; the single-step interrupt is an exception.
- NMI has priority over INTR if both occur simultaneously.
- The above priority structure is applicable for the recognition of (simultaneous) interrupts. For servicing, the single-step interrupt gets the highest priority, then the NMI, and finally those interrupts that occur last.
Simultaneous Hardware Interrupt Requests
The priority of the devices requesting service at the same time is resolved by using two ways: Daisy-Chained Interrupt and Parallel Priority Interrupt.
Daisy-Chaining Priority:
- The daisy-chaining method to resolve the priority consists of a series connection of the devices in order of their priority.
- Device with maximum priority is placed first and device with least priority is placed at the end.
- The devices interrupt the CPU. The CPU sends acknowledgement to the maximum priority device. If the interrupt was generated by that device, it is serviced. Otherwise, the acknowledgement is passed to the next device.
- If the higher priority devices interrupt continuously, the device with the lower priority is not serviced. So some additional circuitry is also needed to introduce fairness.
Parallel Priority:
- Parallel priority method uses individual bits of a priority encoder.
- The priority of the device is determined by the position of the input of the encoder used for the interrupt.
β Key Takeaways
The central concept of this lecture is that interrupt-driven I/O is far more efficient than polling for interacting with slow peripheral devices, freeing the CPU for other work. A student must understand the different categories of interrupts (internal/external, hardware/software, maskable/non-maskable) and their specific roles. The distinction between vectored and non-vectored interrupts is crucial, especially the use of the Interrupt Vector Table (IVT) and the formula for calculating the IVT address (Interrupt Number Γ 4) in the 8086/8088. The hardware and software sequence for handling an interrupt, from the signal to execution of the ISR and final restoration via IRET, must be memorized. Finally, the concepts of interrupt latency and response deadline, along with the two priority resolution methods (daisy-chaining and parallel priority), are essential for understanding system performance.
π§ Quick Revision Questions
- What is the fundamental difference between how a CPU handles I/O in polling vs. interrupt-driven I/O, and which one is more efficient for keyboards?
- List the four general categories of interrupts and provide an example of a specific event that would generate each type.
- How does a vectored interrupt differ from a non-vectored interrupt in determining the starting address of the Interrupt Service Routine?
- In the Intel 8086/8088, if interrupt number 0x10 (16) occurs, what is the memory address where the CPU finds the pointer to the ISR?
- Explain the sequence of steps a CPU performs in hardware interrupt handling, starting from the device issuing the interrupt signal to the CPU resuming the main program.
π Lecture 28 β Interrupt Hardware and Software
π Overview: This lecture covers the hardware and software aspects of interrupt-driven I/O, comparing it with polling and exploring design issues like device identification and priority mechanisms. It provides detailed examples using the SRC and FALCON-A processors to illustrate interrupt handling, including the implementation of interrupt service routines (ISRs) and the hardware logic for interrupt requests and acknowledges.
ποΈ Topics Covered
The lecture begins with a comparison of interrupt-driven I/O and polling, then moves into two key design issues: device identification and priority mechanisms. Device identification is explored through multiple interrupt lines, software poll, and daisy chain techniques, with hardware implications for each. The second half focuses on interrupt handler software, using a detailed FALCON-A example covering both the interrupt hardware and the software components: a dummy calling program, a printer driver, and an interrupt service routine (ISR), including memory maps and RTL for int/iret instructions.
π Lecture Summary
Comparison of Interrupt driven I/O and Polling
Interrupt-driven I/O is superior to polling because it saves CPU time that would otherwise be wasted polling the peripheral device for readiness. In polling, the CPU repeatedly questions the device, consuming valuable processing cycles. With interrupts, the device signals the CPU only when it needs service, allowing the CPU to perform other tasks in the meantime.
Design Issues
There are two primary design issues in implementing interrupts: device identification and priority mechanism. Device identification addresses how the CPU knows which device initiated an interrupt. Priority mechanism resolves which interrupt to service first when multiple interrupts occur simultaneously.
Device Identification
Three different mechanisms can be used for device identification:
-
Multiple Interrupt Lines: This is the most straightforward approach, using several dedicated interrupt lines between the CPU and I/O modules. However, it is impractical to have more than a few bus lines or CPU pins for interrupts, so each line often still needs to handle multiple I/O modules, requiring additional techniques to identify the specific device on that line.
-
Software Poll: In this method, the CPU polls to identify the interrupting module and then branches to an interrupt service routine (ISR) once detected. Identification can be done using special commands like a test I/O (where the CPU places the address of a specific I/O module on the address line and checks for a positive response) or by reading the device's status register. Once the correct module is found, the CPU branches to its specific device service routine.
-
Daisy Chain: The wired-OR interrupt signal allows multiple devices to request an interrupt simultaneously, but only one device must receive the acknowledge signal to avoid data bus contention. In a daisy chain, the acknowledge signal passes from one device to the next. For a jth device to receive an acknowledge, the previous device (j-1) must not have an enabled interrupt request. The logic is represented by the equation: π Formula:
iack_j = iack_(j-1) ^ Β¬(req_(j-1) β§ enb_(j-1))β The acknowledge signal passes to the next device only if the previous device did not generate an interrupt (i.e., its interrupt was not enabled). π Definition β Daisy Chain: A method for handling multiple interrupt requests where the acknowledge signal propagates serially from one device to the next, giving highest priority to the device physically closest to the CPU.
Disadvantages of Software Poll and Daisy Chain
Software poll has the disadvantage of consuming a lot of time because the CPU must individually check each device. Daisy chain is more efficient but has the disadvantage that the device nearest the CPU has the highest priority. To provide fair access, mechanisms like cyclic priority (starting the chain from the device where the CPU finished its last interrupt) can be used.
Interrupt Handler Software
The lecture provides examples using SRC and FALCON-A processors. The FALCON-A example is detailed, with several assumptions: only one interrupt pin is available, so no NMI, nesting, priority, arbitration, or vectored interrupts are possible. The address of the ISR is stored at absolute memory address 2. The printer activates ACKNLG# only when not BUSY.
Interrupt Hardware (FALCON-A Example)
The interrupt request is synchronized using handshaking signals IREQ and IACK. The printer asserts IREQ when ACKNLG# goes low (printer ready for new data) and IRQEN=1. The processor completes the current instruction and then executes the ISR. An inverting tri-state buffer at the D flip-flop's clock input, enabled by IRQEN, ensures additional requests are disabled after the current print job is complete. The CPU's IACK line is connected to the flip-flop's asynchronous reset (R) to prevent the same interrupt request from being presented again. The flip-flop's asynchronous set (S) is permanently connected to logic 1, and its D input is also connected to logic 1, so it is always set synchronously in response to ACKNLG# when IRQEN=1. π‘ Why this matters: This hardware design ensures that interrupts are properly synchronized and that the same interrupt request is not serviced multiple times, preventing spurious or duplicate events.
Interrupt Software (FALCON-A Example)
The software consists of three parts: a Dummy Calling Program, a Printer Driver, and an ISR. The calling program (main program) passes parameters (number of bytes and buffer start address) to the printer driver. The printer driver initializes the printer, sets up the ISR, and ensures no previous print job is in progress. The ISR is invoked to print the first character and subsequent characters, saving and restoring the CPU's environment (registers) using memory locations starting at temp. The int instruction saves the PC into the invisible IPC register and loads the ISR address from memory location 2, setting the interrupt flag (IF) to 0. The iret instruction restores the PC from IPC and sets IF back to 1.
π Definition β Interrupt Service Routine (ISR): A special subroutine invoked by a hardware or software interrupt to service the requesting device. It must save and restore the CPU's environment (registers) and end with an iret instruction.
π Formula: int IPC β PC, PC β M[2], IF β 0 β The int instruction stores the current program counter, loads the ISR address from memory location 2, and disables further interrupts.
π Formula: iret PC β IPC, IF β 1 β The iret instruction restores the program counter and re-enables interrupts.
π Example: In the FALCON-A interrupt-driven printer example, the ISR is invoked when the printer is ready for the next character. The ISR tests the BUSY flag once, outputs a character, and updates the buffer pointer and byte count. The memory map shows the ISR can be loaded anywhere, but its address must be stored at memory location 2.
β Key Takeaways
Interrupt-driven I/O is more efficient than polling because it eliminates wasted CPU polling cycles. The two critical design issues are identifying the interrupting device and managing priority among simultaneous interrupts, with techniques like multiple lines, software poll, and daisy chain offering trade-offs in speed and complexity. Hardware handshaking using IREQ/IACK signals is essential for synchronization, and the interrupt hardware must prevent spurious re-triggering using mechanisms like the D flip-flop and reset logic. The interrupt software structure requires a calling program, a driver, and an ISR, with the ISR saving/restoring the CPU environment and using int/iret instructions for context switching. The FALCON-A example demonstrates a practical, simplified interrupt system where only one interrupt is possible, highlighting the essential steps of initialization, first character printing, and subsequent byte-by-byte transfer.
π§ Quick Revision Questions
- What is the primary advantage of interrupt-driven I/O over polling?
- Describe the three methods for device identification discussed in the lecture.
- What is the formula for the daisy chain acknowledge signal, and what does it represent?
- What are the two RTL operations of the
intandiretinstructions in the FALCON-A example? - In the FALCON-A printer driver example, what is the purpose of the PB flag and the
IRQENsignal?
π Lecture 29 β FALSIM
π Overview: This lecture introduces FALSIM, the software application combining the FALCON-A assembler and simulator. It covers how to prepare source files for FALSIM, how to use the assembler and simulator features, and essential FALCON-A assembly language programming techniques to overcome addressing limitations.
ποΈ Topics Covered
The lecture covers the introduction to FALSIM including the FALCON-A Assembler and Simulator with their graphical user interfaces and features, preparing source files with proper directives and structure, step-by-step usage of FALSIM for assembling and simulating programs, and FALCON-A assembly language techniques for handling large values, register operations, and bit manipulation.
π Lecture Summary
Introduction to FALSIM:
FALSIM is the name of the software application which consists of the FALCON-A assembler and the FALCON-A simulator. It runs under Windows XP.
FALCON-A Assembler:
This tool loads a FALCON-A assembly file with a (.asmfa) extension and parses it. It shows the parsed results in an error log, lets the user view the assembled fileβs contents in the file listing and also provides the features of printing the machine code, an Instruction Table and a Symbol Table to a FALCON-A listing file. It also allows the user to run the FALCON-A Simulator.
The FALCON-A Assembler source code has two main modules, the 1st-pass module and the 2nd-pass module. The 1st-pass module takes an assembly file with a (.asmfa) extension and processes the file contents. It then generates a Symbol Table which corresponds to the storage of all program variables, labels and data values in a data structure at the implementation level. The Symbol Table is used by the 2nd-pass module. Failures of the 1st-pass are handled by the assembler using its exception handling mechanism.
The 2nd-pass module sequentially processes the .asmfa file to interpret the instruction op-codes, register op-codes and constants using the Symbol Table. It then produces a list file with a .lstfa extension independent of successful or failed pass. If the pass is successful a binary file with a .binfa extension is produced which contains the machine code for the program contained in the assembly file.
FALCON-A Simulator:
This tool loads a FALCON-A binary file with a (.binfa) extension and presents its contents into different areas of the simulator. It allows the user to execute the program to a specific point within a time frame or just executes it, line by line. It also allows the user to view the registers, I/O port values and memory contents as the instructions execute.
FALSIM Features:
The FALCON-A Assembler provides its user with the following features:
Select Assembly File: Labeled as β1β in Figure 1, this feature enables the user to choose a FALCON-A assembly file and open it for processing by the assembler.
Assembler Options: Labeled as β2β in Figure 1.
- Print Symbol Table: If selected, writes the Symbol Table (produced after the execution of the 1st-pass of the assembler) to a FALCON-A list file with an extension of (.lstfa). The Symbol Table includes variables, addresses and labels with their respective values.
- Print Instruction Table: If selected, writes the FALCON-A instructions along with their op-codes at the end of the list file.
List File: Labeled as β3β in Figure 1, gives a detailed insight of the FALCON-A listing file, produced as a result of the execution of the 1st and 2nd-pass. It shows the Program Counter value in hexadecimal and decimal formats along with the machine code generated for every line of assembly code. These values are printed when the 2nd-pass is completed.
Error Log: Labeled as β4β in Figure 1. It informs the user about the errors and their respective details, which occurs in any of the two passes of the assembler. The size of this window can be changed by dragging the boundary line up or down.
Highlight: Labeled as β5β in Figure 1, helps the user to search for a certain input with the options of searching with βmatch wholeβ and βmatch anyβ parts of the string. The search also has the option of checking with/without considering βcase-sensitivityβ. It searches the List File area and highlights the search results using the yellow color. It also indicates the total number of matches found.
Start Simulator: Labeled as β6β in Figure 1. The FALCON-A Simulator is run using the FALCON-A Assemblerβs βStart Simulatorβ option. Its features are detailed as follows:
Load Binary File: The button labeled as β11β in Figure 6, allows the user to choose and open a FALCON-A binary file with a (.binfa) extension. When a file is being loaded into the simulator all the register, constants (if any) and memory values are set.
Registers: The area labeled as β12β in Figure 6 enables the user to see values present in different registers before, during and after execution.
Instruction: This area is labeled as β13β in Figure 6 and contains the value of PC, address of an instruction, its representation in Assembly, the Register Transfer Language, the op-code and the instruction type.
I/O Ports: I/O ports are labeled as β14β in Figure 6. These ports are available for the user to enter input operation values and visualize output operation values whenever an I/O operation takes place in the program. The input value for an input operation is given by the user before an instruction executes. The output values are visible in the I/O port area once the instruction has successfully executed.
Memory: The memory is divided into two areas and is labeled as β15β in Figure 6, to facilitate the view of data stored at different memory locations before, during and after program execution.
Processorβs State: Labeled as β16β in Figure 6, this area shows the current values of the Instruction Register and the Program Counter while the program executes.
Highlight: The highlight option for the FALCON-A simulator is labeled as β17β in Figure 6. It offers to highlight the search string which is entered as an input, with the βAllβ and βPartβ option. The results of the search are highlighted using the yellow color. It also indicates the total number of matches.
The following is a description of the options available on the button panel labeled as β18β in Figure 6:
Single Step: Lets the user execute the program, one instruction at a time. The next instruction is not executed unless the user does a βsingle stepβ again. By default, the instruction to be executed will be the one next in the sequence. It changes if the user specifies a different PC value using the Change PC option.
Change PC: This option lets the user change the value of PC (Program Counter). By changing the PC the user can execute the instruction to which the specified PC points. The value in the PC must be an even address.
Execute: By choosing this button, the user is able to execute the loaded program with the options of execution with/without breakpoint insertion. In case of breakpoint insertion, the user has the option to choose from a list of valid breakpoint values. It also has the option to set a limit on the time for execution. This βMax Execution Timeβ option restricts the program execution to a time frame specified by the user.
Change Register: Using the Change Register feature, the user can change the value present in a particular register.
Change Memory Word: This feature enables the user to change values present at a particular memory location.
Display Memory: Shows an updated memory area, after a particular memory location other than the pre-existing ones is specified by the user.
Change I/O: Allows the user to give an I/O port value if the instruction to be executed requires an I/O operation. Giving in the input in any one of the I/O ports areas before instruction execution, indicates that a particular I/O operation will be a part of the program and it will have an input from some source. The value given by the user indicates the input type and source.
Display I/O: Works in a manner similar to Display Memory. Here the user specifies the starting index of an I/O port. This features displays the I/O ports stating from the index specified.
2. Preparing Source Files for FALSIM:
In order to use the FALCON-A assembler and simulator, FALSIM, the source file containing assembly language statements and directives should be prepared according to the following guidelines:
-
The source file should contain ASCII text only. Each line should be terminated by a carriage return. The extension
.asmfashould be used with each file name. After assembly, a list file with the original filename and an extension.lstfa, and a binary file with an extension.binfawill be generated by FALSIM. -
Comments are indicated by a semicolon (;) and can be placed anywhere in the source file. The FALSIM assembler ignores any text after the semicolon.
-
Names in the source file can be of one of the following types:
- Variables: These are defined using the
.equdirective. A value must also be assigned to variables when they are defined. - Addresses in the βdata and pointer areaβ within the memory: These can be defined using the
.dwor the.swdirective. The difference between these two directives is that when.dwis used, it is not possible to store any value in the memory. The integer after.dwidentifies the number of memory words to be reserved starting at the current address. (The directive.dbcan be used to reserve bytes in memory.) Using the.swdirective, it is possible to store a constant or the value of a name in the memory. It is also possible to use pointers with this directive to specify addresses larger than 127. Data tables and jump tables can also be set up in the memory using this directive. - Labels: An assembly language statement can have a unique label associated with it. Two assembly language statements cannot have the same name. Every label should have a colon (:) after it.
- Variables: These are defined using the
-
Use the
.org 0directive as the first line in the program. Although the use of this line is optional, its use will make sure that FALSIM will start simulation by picking up the first instruction stored at address 0 of the memory. (Address 0 is called the reset address of the processor). Ajump [first]instruction can be placed at address 0, so that control is transferred to the first executable statement of the main program. Thus, the labelfirstserves as the identifier of the βentry pointβ in the source file. The.orgdirective can also be used anywhere in the source file to force code at a particular address in the memory. -
Address 2 in the memory is reserved for the pointer to the Interrupt Service Routine (ISR). The
.swdirective can be used to store the address of the first instruction in the ISR at this location. -
Address 4 to 125 can be used for addresses of data and pointers. However, the main program must start at address 126 or less, otherwise FALSIM will generate an error at the
jump [first]instruction. -
The main program should be followed by any subprograms or procedures. Each procedure should be terminated with a
retinstruction. The ISR, if any, should be placed after the procedures and should be terminated with theiretinstruction. -
The last line in the source file should be the
.enddirective. -
The
.equdirective can be used anywhere in the source file to assign values to variables. -
It is the responsibility of the programmer to make sure that code does not overwrite data when the assembly process is performed, or vice versa. As an example, this can happen if care is not exercised during the use of the
.orgdirective in the source file.
3. Using FALSIM:
- To start FALSIM (the FALCON-A assembler and simulator), double click on the FALSIM icon. This will display the assembler window, as shown in Figure 1.
- Select one or both assembler options shown on the top right corner of the assembler window labeled as β2β. If no option is selected, the symbol table and the instruction table will not be generated in the list (.lstfa) file.
- Click on the select assembly file button labeled as β1β. This will open the dialog box as shown in Figure 2.
- Select the path and file containing the source program that is to be assembled.
- Click on the open button. FALSIM will assemble the program and generate two files with the same filename, but with different extensions. A list file will be generated with an extension
.lstfa, and a binary (executable) file will be generated with an extension.binfa. FALSIM will also display the list file and any error messages in two separate panes, as shown in Figure 3. - Double clicking on any error message highlights and displays the corresponding erroneous line in the program listing window pane for the user. This is shown in Figure 4. The highlight feature can also be used to display any text string, including statements with errors in them. If the assembler reported any errors in the source file, then these errors should be corrected and the program should be assembled again before simulation can be done. Additionally, if the source file had been assembled correctly at an earlier occasion, and a correct binary (.binfa) file exists, the simulator can be started directly without performing the assembly process.
- To start the simulator, click on the start simulation button labeled as β6β. This will open the dialog box shown in Figure 6.
- Select the binary file to be simulated, and click Open as shown in Figure 7. (It is also possible to open the file by double clicking on the file name in the βOpenβ window).
- This will open the simulation window with the executable program loaded in it as shown in Figure 8. Notice that the first instruction at address 0 is ready for execution. All registers are initialized to 0. The memory contains the address of the ISR (i.e., 64h which is 100 decimal) at location 2 and the address of the printer driver at location 4. These two addresses are determined at assembly time in our case. In a real situation, these addresses will be determined at execution time by the operating system, and thus the ISR and the printer driver will be located in the memory by the operating system (called re-locatable code). Subsequent memory locations contain constants defined in the program.
- Click single step button labeled as β19β. FALSIM will execute the
jump [main]instruction at address 0 and the PC will change to 20h (32 decimal), which is the address of the first instruction in the main program (i.e., the value ofmain). - Clicking on the single step button twice, executes the next two instructions.
- The execution of the
callinstruction simulates the event of a print request by the user. This transfers control to the printer driver. Thus, when thecall r4, r6instruction is single stepped, the PC changes to 32h (50 decimal) for executing the first instruction in the printer driver. - Double click on memory location 000A, which is being used for holding the PB (printer busy) flag. Enter a 1 and click the change memory button. This will store a 0001 in this location, indicating that a previous print job is in progress. Now click single step and note that this value is brought from memory location 000E into register r1. Clicking single step again will cause the
jnz r1, [message]instruction to execute, and control will transfer to the message routine at address 0046h. - Click again on the single step button. Note that when the
ret r4instruction executes, the value in r4 (i.e., 28h) is brought into the PC. The blue highlight bar is placed on the next instruction after thecall r4, r6instruction in the main program. In case of the dummy calling program, this is thehaltinstruction. - Double click on the value of the PC labeled as β20β. This will open a dialog box. Enter a value of the PC (i.e. 26h) corresponding to the
call r4, r6instruction, so that it can be executed again. A βlistβ of possible PC values can also be pulled down using, and 0026h can be selected from there as well. - Click single step again to enter the printer driver again.
- Change memory location 000A to a 0, and then single step the first instruction in the printer driver. This will bring a 0 in r1, so that when the next
jnz r1, [message]instruction is executed, the branch will not be taken and control will transfer to the next instruction after this instruction. This ismovi r1, 1at address 0036h. - Continue single stepping.
- Notice that a 1 has been stored in memory location 000A, and r1 contains 11h, which is then transferred to the output port at address 3Ch (60 decimal) when the
out r1, controlpinstruction executes. This can be verified by double clicking on the top left corner of the I/O port pane, and changing the address to 3Ch. Another way to display the value of an I/O port is to scroll the I/O window pane to the desired position. - Continue single stepping till the
intinstruction and note the changes in different panes of the simulation window at each step. - When the
intinstruction executes, the PC changes to 64h, which is the address of the first instruction in the ISR. Clicking single step executes this instruction, and loads the address oftemp(i.e., 0010h) which is a temporary memory area for storing the environment. The five store instructions in the ISR save the CPU environment (working registers) before the ISR change them. - Single step through the ISR while noting the effects on various registers, memory locations, and I/O ports till the
iretinstruction executes. This will pass control back to the printer driver by changing the PC to the address of thejump [finish]instruction, which is the next instruction after theintinstruction. - Double click on the value of the PC. Change it to point to the
intinstruction and click single step to execute it again. Continue to single step till thein r1, statuspinstruction is ready for execution. - Change the I/O port at address 3Ah (which represents the status port at address 58) to 80 and then single step the
in r1, statuspinstruction. The value in r1 should be 0080. - Single step twice and notice that control is transferred to the
movi r7, FFFFinstruction, which stores an error code of β1 in r1. (Note: The instruction was originallymovi r7, -1. Since it was converted to machine language by the assembler, and then reverse assembled by the simulator, it becamemovi r7, FFFF. This is because the machine code stores the number in 16-bits after sign-extension. The result will be the same in both cases.)
4. FALCON-A assembly language programming techniques:
- If a signed value, x, cannot fit in 5 bits (i.e., it is outside the range -16 to +15), FALSIM will report an error with a
load r1, [x]or astore r1, [x]instruction. To overcome this problem, usemovi r2, xfollowed byload r1, [r2].
π Definition β Memory-register-indirect addressing: A technique using two loads where the address is first stored in memory using .sw, then loaded into a register, then used as a pointer for the final load.
- If a signed value, x, cannot fit in 8 bits (i.e., it is outside the range -128 to +127), even the previous scheme will not work. FALSIM will report an error with the
movi r2, xinstruction. The following instruction sequence should be used to overcome this limitation of the FALCON-A. First store the 16-bit address in the memory using the.swdirective. Then use two load instructions as shown below:
This is essentially a βmemory-register-indirectβ addressing. It has been made possible by thea: .sw x load r2, [a] load r1, [r2].swdirective. The value of a should be less than 15. - A similar technique can be used with immediate ALU instructions for large values of the immediate data, and with the transfer of control (
callandjump) instructions for large values of the target address.
π Formula: Technique for large 16-bit values β Use multiplication and addition to construct the value in a register.
π Example: To bring a 201 in register r1:
movi r2, 10
movi r3, 20
mul r1, r2, r3 ; r1 contains 200 after this instruction
addi r1, r1, 1 ; r1 now contains 201
- Moving from one register to another can be done by using the instruction
addi r2, r1, 0. - Bit setting and clearing can be done using the logical (
and,or,not, etc) instructions. - Using shift instructions (
shiftl,asr, etc.) is faster thanmulanddiv, if the multiplier or divisor is a power of 2.
β Key Takeaways
The FALSIM tool consists of a two-pass assembler and a simulator for FALCON-A architecture. Students must remember the proper file structure: source files use .asmfa extension, produce .lstfa list files and .binfa binary executables. The memory map is critical β address 0 is the reset address, address 2 holds the ISR pointer, addresses 4-125 are for data/pointers, and the main program must start at address 126 or less. For handling large values that exceed the 5-bit or 8-bit immediate fields, programmers must use memory-indirect addressing with .sw directives or construct values using multiplication and addition. The simulator provides single-step execution, register/memory/I/O viewing, breakpoints, and PC modification for debugging.
π§ Quick Revision Questions
- What are the two main modules of the FALCON-A Assembler, and what does each produce?
- What is the difference between the
.dwand.swdirectives when preparing a FALSIM source file? - Why must the main program start at address 126 or less in FALCON-A?
- How would you load a 16-bit value (e.g., 5000) into a register when
movicannot accommodate it? - What is the purpose of address 2 in the FALCON-A memory map?