microprocessor and assembly
microprocessor and assembly
Department of Computing
Name ID
1. Gemechu Bonsa -------------------------RU0913/15
0
Contents
1.Interrupts.................................................................................................................................................3
Interrupt Handling Process......................................................................................................................3
Assembly Language and Interrupts:........................................................................................................3
Breakdown:.............................................................................................................................................4
Real-World Application:...........................................................................................................................5
2. Types of Interrupts:.................................................................................................................................5
Hardware Interrupts................................................................................................................................5
Software Interrupts in Microprocessors..................................................................................................6
3. Interrupt Instructions:.............................................................................................................................8
INT Instruction.........................................................................................................................................8
INTO Instruction......................................................................................................................................8
INT 3 Instruction......................................................................................................................................9
IRET Instruction.....................................................................................................................................10
4. Real Mode vs. Protected Mode:............................................................................................................11
Real Mode.............................................................................................................................................11
Characteristics:......................................................................................................................................11
Limitations of Real Mode:......................................................................................................................11
Protected Mode....................................................................................................................................12
Characteristics:......................................................................................................................................12
Addressing Real Mode Limitations:.......................................................................................................12
5. Interrupt Flag Bits:.............................................................................................................................14
Interrupt Flag (IF)...................................................................................................................................14
Role of the Interrupt Flag (IF):...............................................................................................................14
Impact on Interrupt Processing:............................................................................................................14
Example Scenario:.................................................................................................................................15
6. Interrupt Vector Table:..........................................................................................................................16
Structure of the Interrupt Vector Table (IVT)........................................................................................16
Key Characteristics:...............................................................................................................................16
1
Storing an Interrupt Vector...................................................................................................................16
Example of IVT Entry:............................................................................................................................16
Using the IVT During Interrupt Processing.............................................................................................17
7. Steps in Interrupt Processing:................................................................................................................18
Basic Steps in Interrupt Processing........................................................................................................18
What Happens to the CPU State and Registers.....................................................................................19
8. Handling Hardware Interrupts:..............................................................................................................21
Mechanism of Handling Hardware Interrupts.......................................................................................21
Roles of the Interrupt Controller and the CPU.......................................................................................22
Interrupt Controller:..........................................................................................................................22
CPU:...................................................................................................................................................22
9. Nested Interrupts:.................................................................................................................................23
Nested Interrupts:.................................................................................................................................23
Effects on System Performance:............................................................................................................23
Managing Nested Interrupts:................................................................................................................24
10. Practical Application:...........................................................................................................................26
Keyboard Interrupt (IRQ1).....................................................................................................................26
Reference:.................................................................................................................................................29
2
1.Interrupts
Microprocessors rely heavily on interrupts to efficiently manage tasks and respond to
events. Interrupts are essentially a way for the processor to pause its current task, handle
a specific event, and then resume its previous activity. This mechanism is crucial for
multitasking and real-time processing.
section .data
msg db 'Interrupt Handled!', 0
section .bss
section .text
global _start
_start:
; Enable interrupts
sti
; Trigger interrupt
int 0x80
; Print message
3
mov eax, 4
mov ebx, 1
mov ecx, msg
mov edx, 17
int 0x80
; Exit
mov eax, 1
xor ebx, ebx
int 0x80
section .interrupt
; Define Interrupt Service Routine (ISR)
ISR:
; Code to handle the interrupt
iret
Breakdown:
Enable Interrupts (`sti`): The `sti` instruction enables interrupts in the x86
architecture.
Trigger Interrupt (`int 0x80`): The `int` instruction generates a software interrupt.
In this case, `0x80` is used to make a system call.
ISR (`section .interrupt`): The ISR is defined to handle the interrupt. The `iret`
instruction is used to return from the interrupt, restoring the processor's state.
Real-World Application:
In microcontroller programming, interrupts are used extensively to handle events like
button presses, sensor inputs, and communication protocols. For example, in an
embedded system controlling a robot, timer interrupts might be used to update the
robot's position at precise intervals, while external interrupts handle sensor readings and
obstacle detection.
4
2. Types of Interrupts:
Hardware Interrupts
Hardware interrupts are signals generated by external hardware devices to notify the
processor of an event that requires immediate attention. These interrupts are essential for
real-time applications and efficient hardware management.
section .data
msg db 'Timer Interrupt Handled!', 0
section .bss
section .text
global _start
_start:
; Set up timer interrupt
mov al, 0x36
out 0x43, al ; Command port of PIC
mov al, 0x9C
out 0x40, al ; Lower byte of divisor
mov al, 0x2E
out 0x40, al ; Upper byte of divisor
; Enable interrupts
sti
section .interrupt
; Define Timer Interrupt Service Routine (ISR)
timer_isr:
5
; Handle the timer interrupt
mov eax, 4
mov ebx, 1
mov ecx, msg
mov edx, 23
int 0x80
section .data
msg db 'System Call Handled!', 0
section .text
global _start
_start:
; Trigger software interrupt for system call
6
mov eax, 4 ; syscall number for write
mov ebx, 1 ; file descriptor for stdout
mov ecx, msg ; pointer to message
mov edx, 20 ; message length
int 0x80 ; software interrupt to invoke system call
; Exit
mov eax, 1 ; syscall number for exit
xor ebx, ebx ; exit code 0
int 0x80 ; software interrupt to invoke system call
Explanation:
System Call Invocation: The `mov` instructions set up the parameters for the
system call (write to console). The `int 0x80` instruction generates a software
interrupt to invoke the system call.
Handling System Call: The operating system handles the software interrupt,
performs the requested action (writing the message), and returns control to the
program.
Significance of Interrupts
o Hardware Interrupts: Enable efficient real-time processing by allowing
the processor to respond quickly to external events without constant
polling.
o Software Interrupts: Facilitate controlled execution of system calls, error
handling, and inter-process communication, ensuring orderly and
predictable program execution.
3. Interrupt Instructions:
INT Instruction
The `INT` instruction generates a software interrupt. It takes an interrupt vector number
as an operand and invokes the corresponding interrupt service routine (ISR).
Example:
section .data
msg db 'Software Interrupt Handled!', 0
section .text
7
global _start
_start:
; Trigger software interrupt with vector 0x80
int 0x80
; Print message
mov eax, 4
mov ebx, 1
mov ecx, msg
mov edx, 26
int 0x80
; Exit
mov eax, 1
xor ebx, ebx
int 0x80
INTO Instruction
The `INTO` instruction generates a software interrupt if the overflow flag (OF) is set. It
is used to handle overflow errors.
Example:
section .data
msg db 'Overflow Interrupt Handled!', 0
section .text
global _start
_start:
; Set up for overflow
mov al, 0x7F
add al, 0x01 ; This will set the overflow flag (OF)
; Print message
8
mov eax, 4
mov ebx, 1
mov ecx, msg
mov edx, 26
int 0x80
; Exit
mov eax, 1
xor ebx, ebx
int 0x80
INT 3 Instruction
The `INT 3` instruction generates a breakpoint interrupt. It is commonly used by
debuggers to set breakpoints in the code.
Example
section .data
msg db 'Breakpoint Interrupt Handled!', 0
section .text
global _start
_start:
; Trigger breakpoint interrupt
int 3
; Print message
mov eax, 4
mov ebx, 1
mov ecx, msg
mov edx, 27
int 0x80
; Exit
mov eax, 1
xor ebx, ebx
int 0x80
9
IRET Instruction
The `IRET` (Interrupt Return) instruction is used to return from an interrupt service
routine (ISR). It restores the processor's state and resumes normal execution.
Example
section .data
section .bss
section .text
global _start
_start:
; Enable interrupts
sti
section .interrupt
; Define Interrupt Service Routine (ISR)
isr_80:
; Handle the interrupt
; ...
10
4. Real Mode vs. Protected Mode:
Real Mode
Real mode is the operating mode of x86-compatible processors, which starts when the
system boots up. It provides direct access to all memory, I/O addresses, and hardware
resources.
Characteristics:
Interrupt Handling: In real mode, interrupt handling is straightforward. Interrupt
vectors are stored in the Interrupt Vector Table (IVT) located at the beginning of
memory (starting at address 0x0000:0x0000). Each interrupt vector is 4 bytes in
size, containing the address of the Interrupt Service Routine (ISR).
Privilege Levels: There are no privilege levels in real mode, meaning all code
runs with full access to system resources, posing security risks.
Protected Mode
Protected mode is an advanced operating mode of x86-compatible processors that
provides enhanced features such as extended memory access, hardware-based
multitasking, and improved security through privilege levels.
Characteristics:
Interrupt Handling: In protected mode, interrupt handling is more sophisticated.
Interrupt vectors are stored in the Interrupt Descriptor Table (IDT). Each entry in
the IDT points to an ISR and includes additional information like privilege
11
levels.
Memory Addressing: Protected mode uses a 32-bit flat memory model, allowing
access to up to 4 GB of memory. Memory is divided into segments with
descriptors stored in the Global Descriptor Table (GDT).
Example:
In protected mode, setting up an interrupt vector involves configuring the IDT, GDT,
and privilege levels, ensuring that only authorized code can handle certain interrupts.
section .data
gdt_start: dd 0x00000000, 0x00000000 ; Null descriptor
code_seg: dd 0x0000FFFF, 0x00CF9A00 ; Code segment descriptor
data_seg: dd 0x0000FFFF, 0x00CF9200 ; Data segment descriptor
gdt_end:
section .bss
section .text
global _start
_start:
; Load GDT
lgdt [gdt_start]
12
; Enable protected mode
mov eax, cr0
or eax, 1
mov cr0, eax
protected_mode_start:
; Set up data segments
mov ax, 10h
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
; Enable interrupts
sti
13
current execution to handle the interrupt by executing the corresponding
Interrupt Service Routine (ISR).
o Disabling Interrupts (IF = 0): When the IF is cleared (0), the processor
will ignore hardware interrupts, effectively preventing the processor from
being interrupted by external events. This can be useful in critical
sections of code where interrupts could cause instability or inconsistency.
14
Example Scenario:
Consider a scenario where a program needs to perform a critical section of code without
being interrupted. The interrupts are disabled using `cli` before entering the critical
section and re-enabled using `sti` after the critical section is completed:
section .data
critical_section_msg db 'Critical section in progress...', 0
section .text
global _start
_start:
; Disable interrupts to protect critical section
cli
; Exit
mov eax, 1
xor ebx, ebx
int 0x80
The Interrupt Vector Table (IVT)is a crucial data structure in a microprocessor system
that maps interrupt requests to their corresponding interrupt service routines (ISRs). It
provides a way for the processor to quickly locate and execute the correct ISR when an
interrupt occurs.
15
Structure of the Interrupt Vector Table (IVT)
The IVT is typically located at the beginning of the memory address space. In x86
architecture, it starts at address 0x0000:0x0000. The table consists of a series of entries,
each entry corresponding to a specific interrupt vector number.
Key Characteristics:
o Number of Entries: The IVT has 256 entries, each corresponding to an
interrupt vector number ranging from 0 to 255.
o Entry Size: Each entry in the IVT is 4 bytes long, divided into two 2-byte
fields:
o Offset (2 bytes): The offset of the ISR within the segment.
o Segment Selector (2 bytes): The segment selector indicating the segment
where the ISR is located.
16
Saving the State: The processor saves its current state (e.g., program counter, flags,
registers) to ensure that it can return to the interrupted task later.
Executing the ISR: The processor jumps to the ISR address obtained from the IVT and
executes the ISR to handle the interrupt.
Returning from Interrupt: Once the ISR is completed, the processor restores its saved
state and resumes execution of the interrupted task.
Example in Assembly:
section .data
msg db 'Interrupt Handled!', 0
section .bss
section .text
global _start
_start:
; Set up IVT entry for interrupt vector 0x20
mov ax, 0x0010 ; Segment selector
mov word [0x0080], ax ; Store segment selector at IVT[0x20]
mov ax, isr_offset ; Offset of ISR
mov word [0x007E], ax ; Store offset at IVT[0x20]
; Enable interrupts
sti
isr_offset:
; Define Interrupt Service Routine (ISR)
isr:
; Handle the interrupt
mov eax, 4
mov ebx, 1
mov ecx, msg
17
mov edx, 17
int 0x80
Saving the Current State: The processor saves its current state to ensure it can
resume execution after handling the interrupt. This typically involves:
o Saving the contents of the Program Counter (PC) or Instruction Pointer
(IP), which indicates the address of the next instruction to be executed.
o Saving the contents of the Flag Register (EFLAGS) to preserve the
current status of the processor.
o Saving general-purpose registers (such as AX, BX, CX, DX in x86) to
preserve the current working data.
Vectoring to the Interrupt Service Routine (ISR): The processor uses the
interrupt vector number to index into the Interrupt Vector Table (IVT) and
retrieve the address of the ISR. The IVT maps each interrupt vector number to
the corresponding ISR address.
Executing the ISR: The processor jumps to the ISR address obtained from the
IVT and executes the ISR to handle the interrupt. The ISR performs the
necessary tasks to service the interrupt, such as reading input data,
acknowledging the interrupt source, or managing error conditions.
Restoring the Previous State: After the ISR completes its tasks, the processor
restores the previously saved state. This involves:
o Restoring the contents of the Program Counter (PC) or Instruction Pointer
(IP).
o Restoring the contents of the Flag Register (EFLAGS).
o Restoring the general-purpose registers.
18
Resuming Execution: The processor resumes normal execution from where it
was interrupted, continuing the program flow as if the interrupt had never
occurred.
Program Counter (PC) or Instruction Pointer (IP): The current value of the PC or
IP is saved to ensure the processor can return to the interrupted instruction after
handling the interrupt.
Flag Register (EFLAGS): The contents of the EFLAGS register are saved to
preserve the current status of the processor, including condition codes and the
interrupt flag (IF).
Interrupt Service Routine (ISR): The processor loads the ISR address from the
IVT and executes the ISR, performing the necessary actions to handle the
interrupt.
Return from Interrupt (IRET): The `IRET` instruction is used to return from the
ISR. It restores the saved state of the processor, including the PC or IP,
EFLAGS, and general-purpose registers, allowing the processor to resume
normal execution.
Example in Assembly:
section .data
interrupt_msg db 'Interrupt Handled!', 0
section .text
global _start
_start:
; Enable interrupts
sti
19
; Trigger software interrupt with vector 0x20
int 0x20
section .interrupt
; Define Interrupt Service Routine (ISR) for vector 0x20
isr_20:
; Handle the interrupt
mov eax, 4
mov ebx, 1
mov ecx, interrupt_msg
mov edx, 17
int 0x80
CPU Interrupt Acknowledgment: Upon receiving the interrupt signal from the
interrupt controller, the CPU acknowledges the interrupt and saves its current
state. This involves saving the contents of the Program Counter (PC), Flag
Register (EFLAGS), and general-purpose registers to ensure that the CPU can
resume its previous task after handling the interrupt.
20
Interrupt Vector Lookup: The CPU uses the interrupt vector number provided by
the interrupt controller to look up the corresponding Interrupt Service Routine
(ISR) in the Interrupt Vector Table (IVT) or Interrupt Descriptor Table (IDT).
Executing the ISR: The CPU jumps to the address of the ISR and executes it. The
ISR performs the necessary tasks to handle the interrupt, such as reading data
from the hardware device, acknowledging the interrupt source, or managing error
conditions.
End of Interrupt (EOI) Signal: After the ISR completes its tasks, it sends an End
of Interrupt (EOI) signal to the interrupt controller to indicate that the interrupt
has been handled.
Restoring CPU State: The CPU restores its previously saved state, including the
Program Counter (PC), Flag Register (EFLAGS), and general-purpose registers.
Resuming Execution: The CPU resumes normal execution from where it was
interrupted.
Interrupt Controller:
o Prioritization: The interrupt controller prioritizes multiple interrupt
requests, ensuring that higher-priority interrupts are handled before
lower-priority ones.
o Interrupt Vector: The interrupt controller provides the interrupt vector
number to the CPU, indicating which ISR to execute.
o Masking and Handling: The interrupt controller can mask (disable)
certain interrupts to prevent them from being processed, allowing critical
tasks to proceed without interruption.
o EOI Handling: The interrupt controller handles the End of Interrupt (EOI)
signal from the CPU, allowing it to manage the status of active interrupts.
CPU:
o Interrupt Acknowledgment: The CPU acknowledges the interrupt signal
from the interrupt controller and saves its current state.
o ISR Execution: The CPU uses the interrupt vector number to locate and
execute the corresponding ISR.
21
o State Management: The CPU manages the saving and restoring of its
state to ensure smooth resumption of the interrupted task.
o Interrupt Flag (IF): The CPU controls the Interrupt Flag (IF) in the Flag
Register, enabling or disabling the handling of hardware interrupts as
needed.
Example in Assembly:
section .data
msg db 'Hardware Interrupt Handled!', 0
section .text
global _start
_start:
; Enable interrupts
sti
section .interrupt
; Define Interrupt Service Routine (ISR) for hardware interrupt
isr_hardware:
; Handle the hardware interrupt
mov eax, 4
mov ebx, 1
mov ecx, msg
mov edx, 26
int 0x80
22
9. Nested Interrupts:
Nested Interrupts:
Nested interrupts occur when an interrupt is allowed to interrupt another interrupt
service routine (ISR). In other words, while the CPU is handling one interrupt, another
higher-priority interrupt can preempt the current ISR, leading to a hierarchy or "nesting"
of interrupts.
Priority Levels: Each interrupt is assigned a priority level. The CPU uses these
levels to determine whether an incoming interrupt should preempt the current
ISR. Higher-priority interrupts can interrupt lower-priority ones.
Saving and Restoring State: When an interrupt occurs, the CPU saves its current
state (e.g., program counter, flags, registers) to ensure it can resume the
interrupted task later. This process is repeated for each nested interrupt, creating
a stack of saved states.
Interrupt Masking: The CPU can mask (disable) certain interrupts to prevent
them from preempting the current ISR. This is useful for protecting critical
sections of code that must not be interrupted.
23
End of Interrupt (EOI): After handling an interrupt, the ISR sends an End of
Interrupt (EOI) signal to the interrupt controller, allowing it to handle the next
interrupt. In the case of nested interrupts, the CPU manages the EOI signal for
each level of nesting.
Example in Assembly:
Here's an assembly example illustrating nested interrupts with a focus on saving and
restoring the CPU state:
section .data
msg1 db 'Outer Interrupt Handled!', 0
msg2 db 'Inner Interrupt Handled!', 0
section .text
global _start
_start:
; Enable interrupts
sti
section .interrupt
; Define Outer Interrupt Service Routine (ISR) for vector 0x20
outer_isr:
; Save state
pushad
pushfd
24
; Trigger inner interrupt with vector 0x21
int 0x21
; Restore state
popfd
popad
; Restore state
popfd
popad
25
10. Practical Application:
Assembly Program
section .data
msg db 'Key Pressed: ', 0
char db 0
section .bss
section .text
global _start
_start:
; Set up the interrupt vector for keyboard (IRQ1) to point to our ISR
cli ; Disable interrupts
mov word [0x21*4], isr_key ; Set the ISR offset (low word)
mov word [0x21*4+2], cs ; Set the ISR segment (high word)
sti ; Enable interrupts
isr_key:
; Save the state
pusha
push ds
push es
push fs
push gs
26
mov [char], al
; This instruction lets the linker know where the code segment (CS) starts
section .startup_code
cs:
jmp _start
Explanation
Interrupt Vector Setup:
o The program first disables interrupts using `cli`.
27
o It sets up the interrupt vector for IRQ1 (keyboard interrupt) to point to the
custom ISR (`isr_key`). This is done by setting the appropriate entry in
the interrupt vector table (IVT).
o It then re-enables interrupts using `sti`.
Infinite Loop:
o The main program enters an infinite loop using `hlt` to keep the program
running and waiting for interrupts.
Reference:
- Irvine, K. R. (2007). Assembly Language for x86 Processors (5th ed.). Prentice Hall.
- Tanenbaum, A. S. (2001). *Structured Computer Organization* (4th ed.). Pearson.
28