340 likes | 351 Views
Assembly 08. Interrupts. Introduction. Interrupts are similar to procedures They are used to alter a program’s control flow The interrupt service is also preformed using a routine. They are different The are not invoked by a call instruction. Software interrupts.
E N D
Assembly 08 Interrupts
Introduction • Interrupts are similar to procedures • They are used to alter a program’s control flow • The interrupt service is also preformed using a routine. • They are different • The are not invoked by a call instruction. • Software interrupts. • Software invoked interrupts are caused by executing the int instruction. • Hardware interrupts • These interrupts handle an un-anticipated event, which causes are external to the program. • User caused interrupts (Ctrl-C) • Hardware failure or hardware caused event. • Exceptions, such as divide by zero
Interrupt Service • The raise of an interrupt, usually called for a service/help. • This service is performed using what is calls an interrupt service routine (ISR) or handler. • When the ISR is completed, • The interrupted program resumes execution as if it were not interrupted. • The interrupted routine my decide to terminate the program and not to return.
Software interrupts • Invoking software interrupts is performed by the int instruction. • Software interrupts are mainly used to access I/O devices such as a keyboard, printer, display screen, disk drive, etc. • Software interrupts are also classified into • system-defined • user-defined.
Hardware interrupts • Hardware interrupts are generated by hardware devices to get the attention of the processor. • Nonmaskable interrupts is always handled by processor • Example: the RAM parity error indicating memory malfunction. • Maskable interrupts can be delayed until execution reaches a convenient point. • Example: while running a ISR.
Protected Mode’s Interrupts • Interrupts are identified by a type number, called a vector. • Pentium supports 256 different interrupt types (0..255). • The interrupt type number/vector, is used as an index into a table that stores the addresses of ISRs. • This table is called the interrupt descriptor table (IDT). • Each descriptor is a pointer to an ISR and requires eight bytes. • The interrupt type number is scaled by 8 to form an index into the IDT. • The IDT may reside anywhere in physical memory. • The location of the IDT is maintained in an IDT register IDTR. • The IDTR is a 48-bit register that stores the 32-bit IDT base address • and a 16-bit IDT limit value.
IDT continue • The IDT can have three types of descriptors: • Interrupt gate • Trap gate • Task gate. • Interrupt and Task gates include • 16-bit segment selector, • 32-bit offset, • Descriptor privilege level (DPL), • P bit to indicate whether the segment is present or not.
Interrupt Handling • When an interrupt occurs • The segment selector is used to select a segment descriptor that is in either the GDT or the current LDT (based on the TI bit). • The segment descriptor provides the base address of segment that contains the interrupt service routine and the offset part comes from the interrupt gate. • Start the ISR by • Push the EFLAGS register onto the stack; • Clear the interrupt and trap flags; • Push CS and EIP registers onto the stack; • Load CS with the 16-bit segment selector from the interrupt gate; • Load EIP with the 32-bit offset values from the interrupt gate.
Interrupt and Trap gates • Processing a trap gate is similar interrupt gate interrupt gate except it does not modify the (interrupt flag) IF flag. • Some types of exceptions also push an error code onto the stack. • The exception handler can use this error code in identifying the cause for the exception.
Exceptions • Exceptions could be faults, traps, or aborts depending on the way they are reported and handled • Examples • Divide by zero • Segment not present • Breakpoint interrupt (debugger)
The special, CPU-dedicated interrupts are shown below 0 - Division by zero exception 1 - Debug exception 2 - Non maskable interrupt 3 - Breakpoint exception 4 - 'Into detected overflow' 5 - Out of bounds exception 6 - Invalid opcode exception 7 - No coprocessor exception 8 - Double fault (pushes an error code) 9 - Coprocessor segment overrun 10 - Bad TSS (pushes an error code) 11 - Segment not present (pushes an error code) 12 - Stack fault (pushes an error code) 13 - General protection fault (pushes an error code) 14 - Page fault (pushes an error code) 15 - Unknown interrupt exception 16 - Coprocessor fault 17 - Alignment check exception 18 - Machine check exception 19-31 - Reserved
Interrupt Service Routine isr_common_stub:pusha; Pushes edi,esi,ebp,esp,ebx,edx,ecx,eax mov ax, ds ; Lower 16-bits of eax = ds. push eax ; save the data segment descriptor mov ax, 0x10 ; load the kernel data segment descriptor mov ds, axcall isr_handler pop eax ; reload the original data segment descriptor mov ds, ax popa; Pops edi,esi,ebp... add esp, 8 ; Cleans up the pushed error code and pushed ISR number stiiret ; pops 5 things at once: CS, EIP, EFLAGS, SS, and ESP
File System Calls-Software Interrupt • System call 8 — Create and open a file Inputs: EAX = 8 EBX = file name ECX = file permissions Returns: EAX = file descriptor Error: EAX = error code • System call 5—Open a file Inputs: EAX = 5 EBX = file name ECX = file access mode EDX = file permissions Returns: EAX = file descriptor Error: EAX = error code
File System Calls-Software Interrupt • System call 3 — Read from a file Inputs: EAX = 3 EBX = file descriptor ECX = pointer to input buffer EDX = buffer size (maximum number of bytes to read) Returns: EAX = number of bytes read Error: EAX = error code • System call 4 — Write to a file Inputs: EAX = 4 EBX = file descriptor ECX = pointer to output buffer EDX = buffer size (number bytes to write) Returns: EAX = number of bytes written Error: EAX = error code
File System Calls-Software Interrupt • System call 6 — Close a file Inputs: EAX = 6 EBX = file descriptor Returns: EAX = — Error: EAX = error code • System call 19— lseek (Updates file pointer) Inputs: EAX = 19 EBX = file descriptor ECX = offset EDX = whence Returns: EAX = byte offset from the beginning of file Error: EAX = error code
Examples ;------------------------------------------------------------ ; Put character procedure receives the character in AL. ;------------------------------------------------------------ putch: pusha mov [temp_char],AL mov EAX,4 ; 4 = write mov EBX,1 ; 1 = std output (display) mov ECX,temp_char ; pointer to char buffer mov EDX,1 ; # bytes = 1 int 0x80 popa ret
Examples ;------------------------------------------------------------ ; Get string procedure receives input buffer pointer in EDI ; and the buffer size in ESI. ;------------------------------------------------------------ getstr: pusha pushf mov EAX,3 ; file read service mov EBX,0 ; 0 = std input (keyboard) mov ECX,EDI ; pointer to input buffer mov EDX,ESI ; input buffer size int 0x80 dec EAX done_getstr: mov byte[EDI+EAX],0 ; append NULL character popf popa ret
Example-A file copy program %include "io.mac" %define BUF_SIZE 256 .DATA in_fn_prompt db ’Please enter the input file name: ’,0 out_fn_prompt db ’Please enter the output file name: ’,0 in_file_err_msg db ’Input file open error.’,0 out_file_err_msg db ’Cannot create output file.’,0 .UDATA in_file_name resb 30 out_file_name resb 30 fd_in resd 1 fd_out resd 1 in_buf resb BUF_SIZE .CODE .STARTUP PutStr in_fn_prompt ; request input file name GetStr in_file_name,30 ; read input file name PutStr out_fn_prompt ; request output file name GetStr out_file_name,30 ; read output file name
Example-A file copy program ;open the input file mov EAX,5 ; file open mov EBX,in_file_name ; pointer to input file name mov ECX,0 ; file access bits (0 = read only) mov EDX,0700 ; file permissions int 0x80 mov [fd_in],EAX ; store fd for use in read routine cmp EAX,0 ; open error if fd < 0 jge create_file PutStr in_file_err_msg newline jmp done create_file: ;create output file mov EAX, 8 ; file create mov EBX, out_file_name ; pointer to output file name mov ECX, 0700 ; read/write/exe by owner only int 0x80 mov [fd_out], EAX ; store fd for use in write routine
Example-A file copy program cmp EAX,0 ; create error if fd < 0 jge repeat_read PutStr out_file_err_msg newline jmp close_exit ; close the input file & exit repeat_read: ; read input file mov EAX, 3 ; file read mov EBX, [fd_in] ; file descriptor mov ECX, in_buf ; input buffer mov EDX,BUF_SIZE ; size int 0x80 ; write to output file mov EDX,EAX ; byte count mov EAX,4 ; file write mov EBX,[fd_out] ; file descriptor mov ECX,in_buf ; input buffer int 0x80
Example-A file copy program cmp EDX,BUF_SIZE ; EDX = # bytes read jl copy_done ; EDX < BUF_SIZE ; indicates end-of-file jmp repeat_read copy_done: mov EAX,6 ; close output file mov EBX,[fd_out] int 0x80 close_exit: mov EAX,6 ; close input file mov EBX,[fd_in] int 0x80 done: .EXIT
Real-Mode Interrupts • DOS and BIOS provide several software interrupt services. • I/O devices can be accessed in three ways. • DOS and BIOS provide two ways of interacting with the system I/O devices. • The third method involves direct I/O access. This method is low level in nature and more complicated than the high-level access provided by DOS and BIOS. • Direct access of I/O devices is supported by in and out instructions.
Interrupt Processing • Upon the occurrence of an interrupt occurs, the following are performed: • Push flags register onto the stack; • Clear interrupt and trap flags to disable further interrupts; • Push CS and IP registers onto the stack; • Load CS with the 16-bit data at memory address (interrupt-type * 4 + 2); • Load IP with the 16-bit data at memory address (interrupt-type * 4).
Interrupt Processing • on iret instruction, the following are performed: • Pop the 16-bit value on top of the stack into IP register; • Pop the 16-bit value on top of the stack into CS register; • Pop the 16-bit value on top of the stack into the flags register. • A typical ISR structure is shown below. <save the registers used in the ISR> sti ; enable further interrupts . . . <ISR body> . . . <restore the saved registers> iret ; return to the interrupted program
Dos and Bios Int • Both DOS and BIOS provide several interrupt service routines to access I/O devices. • DOS services are provided by int 21H. • DOS provides more than 80 different services (called functions). • The interrupt services provided by DOS and BIOS are not mutually exclusive. • Bios keyboard services use int 16H
A string read code STR_LENGTH EQU 81 %include "io.mac" .STACK 100H .DATA prompt_msg1 db "Please enter maximum string length: ",0 prompt_msg2 db "Please enter a string: ",0 string_msg db "The string entered is: ",0 error_msg db "No string read. Buffer size must be at least 1.",0 .UDATA temp_buf resb STR_LENGTH+2 in_string resb STR_LENGTH .CODE .STARTUP PutStr prompt_msg1 GetInt CX ; max. string length PutStr prompt_msg2 mov BX,in_string ; BX = pinter to input buffer call read_string ; to call read_string procedure PutStr string_msg PutStr in_string 32: .EXIT
A string read code ;Get string (of maximum length 80) from keyboard. ; BX <-- pointer to a buffer to store the input string ; CX <-- buffer size = string length + 1 for NULL ; If CX <2, reports error and terminates. ; If CX > 81, CX = 81 is used to read at most 80 characters. ;----------------------------------------------------------- read_string: pusha ; ES = DS for use by the string instruction--movsb mov DX,DS mov ES,DX mov DI,BX ; DI = buffer pointer inc CX ; space for NULL ; check CX value cmp CX, 2 jl bailout cmp CX, 81 jle read_str mov CX, 81 read_str:
A string read code ; use temporary buffer temp_buf to read the string ; using functin 0AH of int 21H mov DX,temp_buf mov SI,DX mov [SI],CL ; first byte = # chars. to read mov AH,0AH int 21H inc SI ; second byte = # chars. read mov CL,[SI] ; CX = # bytes to copy inc SI ; SI = input string first char. cld ; forward direction for copy rep movsb mov byte[DI],0 ; append NULL jmp done bailout: PutStr error_msg done: popa ret