Upload
eliza
View
37
Download
5
Tags:
Embed Size (px)
DESCRIPTION
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. - PowerPoint PPT Presentation
Citation preview
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.
Interrupt Classes
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.
The IDT
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 Gates
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.
Protected-mode interrupt invocation.
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 below0 - Division by zero exception1 - Debug exception2 - Non maskable interrupt3 - Breakpoint exception4 - 'Into detected overflow'5 - Out of bounds exception6 - Invalid opcode exception7 - No coprocessor exception8 - Double fault (pushes an error code)9 - Coprocessor segment overrun10 - 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 exception16 - Coprocessor fault17 - Alignment check exception18 - Machine check exception19-31 - Reserved
Interrupt Service Routineisr_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, ax call 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 sti iret ; 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 fileInputs: 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 = 3EBX = file descriptorECX = pointer to input bufferEDX = buffer size (maximum number of bytes to read)Returns: EAX = number of bytes readError: EAX = error code
• System call 4 — Write to a fileInputs: EAX = 4EBX = file descriptorECX = pointer to output bufferEDX = buffer size (number bytes to write)Returns: EAX = number of bytes writtenError: 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:
pushamov [temp_char],ALmov EAX,4 ; 4 = writemov EBX,1 ; 1 = std output (display)mov ECX,temp_char ; pointer to char buffermov EDX,1 ; # bytes = 1int 0x80poparet
Examples;------------------------------------------------------------; Get string procedure receives input buffer pointer in EDI; and the buffer size in ESI.;------------------------------------------------------------getstr:
pushapushfmov EAX,3 ; file read servicemov EBX,0 ; 0 = std input (keyboard)mov ECX,EDI ; pointer to input buffermov EDX,ESI ; input buffer sizeint 0x80dec EAXdone_getstr:mov byte[EDI+EAX],0 ; append NULL characterpopfpoparet
Example-A file copy program%include "io.mac"%define BUF_SIZE 256.DATA
in_fn_prompt db ’Please enter the input file name: ’,0out_fn_prompt db ’Please enter the output file name: ’,0in_file_err_msg db ’Input file open error.’,0out_file_err_msg db ’Cannot create output file.’,0
.UDATAin_file_name resb 30out_file_name resb 30fd_in resd 1fd_out resd 1in_buf resb BUF_SIZE
.CODE
.STARTUPPutStr in_fn_prompt ; request input file nameGetStr in_file_name,30 ; read input file namePutStr out_fn_prompt ; request output file nameGetStr out_file_name,30 ; read output file name
Example-A file copy program;open the input filemov EAX,5 ; file openmov EBX,in_file_name ; pointer to input file namemov ECX,0 ; file access bits (0 = read only)mov EDX,0700 ; file permissionsint 0x80mov [fd_in],EAX ; store fd for use in read routinecmp EAX,0 ; open error if fd < 0jge create_filePutStr in_file_err_msgnewlinejmp done
create_file:;create output filemov EAX, 8 ; file createmov EBX, out_file_name ; pointer to output file namemov ECX, 0700 ; read/write/exe by owner onlyint 0x80mov [fd_out], EAX ; store fd for use in write routine
Example-A file copy programcmp EAX,0 ; create error if fd < 0jge repeat_readPutStr out_file_err_msgnewlinejmp close_exit ; close the input file & exit
repeat_read:; read input filemov EAX, 3 ; file readmov EBX, [fd_in] ; file descriptormov ECX, in_buf ; input buffermov EDX,BUF_SIZE ; sizeint 0x80; write to output filemov EDX,EAX ; byte countmov EAX,4 ; file writemov EBX,[fd_out] ; file descriptormov ECX,in_buf ; input bufferint 0x80
Example-A file copy programcmp EDX,BUF_SIZE ; EDX = # bytes readjl copy_done ; EDX < BUF_SIZE; indicates end-of-filejmp repeat_read
copy_done:mov EAX,6 ; close output filemov EBX,[fd_out]int 0x80
close_exit:mov EAX,6 ; close input filemov 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
Real-mode interrupt vector table.
Software Interrupts
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 codeSTR_LENGTH EQU 81%include "io.mac".STACK 100H.DATA
prompt_msg1 db "Please enter maximum string length: ",0prompt_msg2 db "Please enter a string: ",0string_msg db "The string entered is: ",0error_msg db "No string read. Buffer size must be at least 1.",0
.UDATAtemp_buf resb STR_LENGTH+2in_string resb STR_LENGTH
.CODE
.STARTUPPutStr prompt_msg1GetInt CX ; max. string lengthPutStr prompt_msg2mov BX,in_string ; BX = pinter to input buffercall read_string ; to call read_string procedurePutStr string_msgPutStr 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--movsbmov DX,DSmov ES,DXmov DI,BX ; DI = buffer pointerinc CX ; space for NULL; check CX valuecmp CX, 2jl bailoutcmp CX, 81jle read_strmov CX, 81
read_str:
A string read code; use temporary buffer temp_buf to read the string; using functin 0AH of int 21Hmov DX,temp_bufmov SI,DXmov [SI],CL ; first byte = # chars. to readmov AH,0AHint 21Hinc SI ; second byte = # chars. readmov CL,[SI] ; CX = # bytes to copyinc SI ; SI = input string first char.cld ; forward direction for copyrep movsbmov byte[DI],0 ; append NULLjmp done
bailout:PutStr error_msg
done:poparet