Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Procedures and Interrupts Chapter 5 n Stack n Procedure n Software Interrupt u BIOS-level access u DOS-level access n Video Display u Direct Video access.

Similar presentations


Presentation on theme: "1 Procedures and Interrupts Chapter 5 n Stack n Procedure n Software Interrupt u BIOS-level access u DOS-level access n Video Display u Direct Video access."— Presentation transcript:

1 1 Procedures and Interrupts Chapter 5 n Stack n Procedure n Software Interrupt u BIOS-level access u DOS-level access n Video Display u Direct Video access

2 2 The Stack n The stack resides in the stack segment (in main memory) who’s segment number is in the SS register n The SP register holds the offset address of the last element added to the stack n If the stack was allocated with the directive.STACK 100h : u Then SP should, initially, contain 100h (pointing to the top of the empty stack)

3 3 PUSH (16-bit case) n PUSH source u will decrement SP by 2 u and copy the content of source into word at SS:SP u little endian: low order byte at lowest offset n Ex: (see figure) mov ax,06 push ax mov ax,0A5h push ax n This is for a source of type word (reg16 or mem16). n imm16 are allowed only on 286 and later processors)

4 4 PUSH (32-bit case) n With a 32-bit operand (.386 directive): push source n Decrements SP by 4 and copies the content of source into the double word at address SS:SP n Little endian convention. Ex: mov eax,12345678h push eax n will decrease SP by 4 and will move: u 78h at SS:SP u 56h at SS:SP+1 u 34h at SS:SP+2 u 12h at SS:SP+4

5 5 POP n The POP instruction undoes the action of PUSH POP destination n For a 16-bit destination operand: u the word at SS:SP is copied into destination u SP is incremented by 2 n For a 32-bit destination operand: u the dword at SS:SP is copied into destination u SP is incremented by 4 n The destination operand cannot be imm

6 6 Ex: saving and restoring registers.data message db “Hello world $”.code push ax;save AX push dx;save DX, SP points to copy of DX mov ah,9 mov dx, offset message int 21h;prints message pop dx;restore DX pop ax;restore AX

7 7 More Saving and Restoring n PUSHA (.286) pushes AX, CX, DX, BX, SP, BP, SI, DI on stack and POPA pops the same registers in reverse order n PUSHAD (.386) pushes EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI on stack and POPAD pops the same registers in reverse order n PUSHF and POPF pushes and pops the FLAGS register onto and from the stack n PUSHFD and POPFD (.386) pushes and pops the EFLAGS register onto and from the stack

8 8 Procedures n Procedures are defined like this: name PROC [type]... set of instructions... RET name ENDP n The “type” is either NEAR or FAR n To transfer control to the procedure “name” we do: CALL [type PTR] name n RET transfers control to the instr. following CALL n The default for “type” and “type PTR” is: u NEAR: for memory models: tiny, small, compact u FAR: for memory models: medium, large, huge

9 9 CALL & RET (NEAR Procedures) n Upon a CALL to a NEAR procedure: u SP is decremented by 2 u The content of IP is copied at SS:SP F this is the offset address of the instruction following CALL (where the procedure must return) u The offset address of the first instruction in the called procedure is copied into IP F this will thus be the next instruction to execute n Upon a RET from a NEAR procedure: u the word at SS:SP is popped into IP (so that SP is automatically incremented by 2) u (the instruction pointed by IP is then executed)

10 10 CALL & RET (NEAR Procedures) 0006 IP 0080 IP 0009 IP

11 11 CALL & RET (FAR Procedures) n Upon a CALL to a FAR procedure: u CS and then IP are pushed onto the stack F this is the segment:offset address of the instruction following CALL (where the procedure must return) u The segment:offset address of the first instruction in the called procedure is copied into CS:IP F this will thus be the next instruction to execute n A RET from a FAR procedure effectively does: u POP IP u POP CS F Hence: the instruction at CS:IP is then executed

12 12 CALL & RET (FAR Procedures) 0006 IP 0080 IP 0009 IP 2FC0 CS 2FC0 CS 2FC0 CS

13 13 When does a procedure needs to be FAR? n A NEAR CALL is faster than a FAR CALL n Procedures located in the same segment as the code that CALLs them can be of type NEAR u since the code segment number (in CS) is the same for both the procedure and the caller n Procedures located in a different segment than the code that CALLs them must be of type FAR u since the procedure and the caller have a different code segment number

14 14 Using Procedures in irvine.lib n Separately assembled procedures under the.model small will be combined, by the linker, into the same code segment u this is the case for the procedures in irvine.lib u so use a NEAR call to call these procedures u you should also use.model small for your code that call procedures in irvine.lib u other memory models will be used when linking with high level language (HLL) procedures (chap 9 and 13)

15 15 Passing Arguments to Procedures n Arguments can be passed to procedures via u the stack: this is the technique used in HLLs. We will use this only later (chap 9) u global variables: the scope of a variable is the.ASM file into which it is defined F must use PUBLIC and EXTRN directive to make them visible to other.ASM files F contrary to modular programming practice u registers: fastest way to pass arguments

16 16 Using Procedures n When a procedure returns to the caller it should preserve the content of the registers (except those used to return a value) u should save first the content of the registers that it will modify and restore them just before returning to the caller n Caution on stack usage: u SP points to the return address when entering the procedure. Make sure that this is the case just before executing RET !! n ProcEx.html ProcEx.html

17 17 Interrupts n The term interrupt is used in many different ways n A hardware interrupt is a signal generated by any part of the hardware that needs immediate attention of the processor n A software interrupt (sometimes called a Trap) is a call to an Interrupt Service Routine (ISR) of the Operating System (here: either DOS or BIOS) u produced by the instruction INT n in a program n A processor exception is an automatically generated trap in response to an exceptional condition (abnormal program execution). Ex: divide overflow, coprocessor not available...

18 18 Hardware Interrupts n When a hardware component (ex: a peripheral device) needs CPU attention, the controller associated with this component sends a Interrupt Request (INTR) signal to the CPU and puts an Interrupt Number (0 to FFh) onto the data bus n The CPU uses this interrupt number to index the interrupt vector table (IVT) located at physical addresses 00000h to 003FFh (pp.33) n Each entry of this table, called an interrupt vector, contains the segment:offset address of the Interrupt Handler (ISR) servicing that interrupt. n To service an interrupt, the CPU transfers control to the corresponding ISR

19 19 The Interrupt Vector Table (IVT) n Each entry of the IVT occupies 4 bytes n At entry 0 of the IVT we have the offset address and then the segment address of the ISR handling INT 0 n At entry n of the IVT we have the offset address and then the segment address of the ISR handling INT n

20 20 Interrupt Processing n The same mechanisms are used to handle all types of interrupts (hardware, software, exception) n When an interrupt occurs: u The CPU pushes the FLAGS register onto the stack u The CPU pushes onto the stack the far (segment:offset) return address (ie: that of the next instruction) u From the interrupt number N, the CPU fetches the Nth entry of the IVT and transfers control to that ISR u The ISR execute a IRET instruction to return control to the program at the point of interruption (this pops off the stack the far return address and the FLAGS register)

21 21 Ex: using INT 10h BIOS video services

22 22 Interrupt Service Routines n A ISR is like a procedure except that: u a transfer to a ISR pushes FLAGS in addition to a far return address u a ISR returns with IRET instead of RET n But since the point of interruption can occur anywhere in a program, it is crucial for a ISR to not modify the content of any register n How to write a ISR and how to initialize the corresponding entry in the IVT? (chap 15) n For now let us examine what are the ISRs that are provided by DOS and BIOS (and how to use them) to perform I/O operations

23 23 Common Software Interrupts n Int 10h Video Services n Int 16h Keyboard Services n Int 17h Printer Services n Int 1Ah Time of Day n Int 1Ch User Timer Interrupt n Int 21h DOS Services

24 24 MS-DOS Function Calls n A MS-DOS function is called upon the execution of INT 21h u The actual function to be performed depends on the function number stored in AH u about 90 different functions are supported n We have already seen functions 01h, 02h, 09h and 4Ch n We now briefly view some other functions u see more details in section 5.5 of your textbook

25 25 Output Functions n 02h: Character Output n 05h: Printer Output n 06h: Direct Output n 09h: String Output

26 26 Input Functions n 01h: Filtered Input With Echo n 06h: Direct Input Without Waiting n 07h: Direct Input, No Ctrl-Break n 08h: Direct Input with Ctrl-Break n 0Ah: Buffered Input n 0Bh: Get Input Status n 0Ch: Clear Input Buffer, Invoke Input Function n 3Fh: Read From File or Device

27 27 Single Character input (DOS) n For all these functions, the next character in the keyboard buffer is stored in AL n Wait for keystroke: function 6 (with DL=FFh) always returns even when the buffer is empty n Function 1 and 8 will return control to DOS when Ctrl-Break is entered

28 28 Ex: AH=06h clear_keyboard Clear_keyboard proc pushax pushdx L1: mov ah, 6 mov dl, 0FFh int21h jnzL1 pop dx popax ret clear_keyboard endp

29 29 Buffered Input (DOS) n Function 0Ah reads (from stdin) a string of up to 255 characters and stores it in a buffer n User input is terminated with 0Dh (CR) n Non ASCII keys (ex: PgUp, arrows, Fn...) are filtered out and Ctrl-Break is active n DX contains the offset of the Buffer u 1st char = max number of char allowed (including 0Dh) u 2nd char = number of chars actually entered (excluding 0Dh)

30 30 Ex: Using buffered input function 0Ah. data keyboardArea label byte maxkeys db 32 ;max # chars allowed charsInput db ? ;# of chars actually entered buffer db 32 dup('0') ;holds input string.code mov ah,0Ah mov dx,offset keyboardArea int 21h n the CR (0Dh) is the last char entered in the buffer

31 31 Date/Time Functions n 2Ah: Get Date n 2Bh: Set Date n 2Ch: Get Time n 2Dh: Set Time cx: year dh: month dl: day ch: hour cl: minute dh: second

32 32 Keyboard Keys n ASCII keys: u those that have an ASCII code: letters, digits, punctuation’s, arithmitic’s, Esc, CR, Bksp, Tab n Shift Keys: u normally used in combination with another key: left and right shifts, Caps Lock, Ctrl, Alt, Num Lock, Scroll Lock n Function Keys: u used in programs to perform special functions: F1-F12, arrows, Home, PgUp, PgDn, End, Ins, Del

33 33 Scan Codes n Only ASCII keys have an ASCII code but all keys have a SCAN CODE (1byte). See scancodes.htmlscancodes.html n When we strike a key: u The keyboard interrupts (INT 9h) the CPU and sends the Scan Code to I/O port 60h u The BIOS INT 9h reads this I/O port and uses the scan code to index a table to get the ASCII code. Both codes are sent to the keyboard buffer only if it is not a shift key (used alone) n For each word in the keyboard buffer: u low byte = ASCII code of the key, or 0 if it is not an ASCII key u high byte = Scan Code of key

34 34 BIOS input function INT 16h n When AH=10h, INT 16h will load AX with the next word in the keyboard buffer: mov ah,10h  int 16h ; AH = Scan Code, AL = ASCII code n The input character will not be echoed on screen n Useful for reading (and identify) the function key pressed by the user u they can be identified only with their scan code n Keyboard input cannot be redirected on the DOS command line (unlike INT 21h)

35 35 Video Adapters n Screen display is controlled by a video adapter which consists of: u A memory (video buffer) which contains all the information displayed on screen u A video controller that displays on screen the content of the video buffer n Typical resolutions (in pixels X pixels): u 640 X 480 (standard VGA) u 800 X 600 (super VGA) u 1024 X 768 (extended VGA) u....(higher resolutions)....

36 36 Video Modes n We have two classes of video modes u graphic modes: used to display arbitrary graphics, including text (not discussed here) u text modes: only characters (from the IBM extended ASCII character set) can be displayed. (the subject till the end of chapter) n From the many available text modes (mode 0, 1, 2, 3, 7) we discuss only mode 3 (most important one) u displays text on 80 columns and 25 rows F first row = row 0 = top of the screen F first column = column 0 = left of screen u 16 colors are available

37 37 Video Pages n Each character displayed is represented by 1 word u low order byte = ASCII code (IBM extended) u high order byte = Attribute Byte (specify how the character will be displayed) n Each of these words is stored in the video buffer starting at physical address B80000h n One screen of text (80 X 25 X 2 = 4000 bytes) requires 1 video page of 4KB n VGA (and better) adapters can hold 8 video pages: page 0 to 7

38 38 Video Pages (cont.) n only the active page is displayed: u the first word of the page displays the character at the upper left corner: (row,column) = (0,0) u the second word displays the character at (row, column) = (1,0) u the 3rd word displays the char at (2,0)... u...the last word displays the char at (24,79) n (other pages can be modified while the active page is being displayed)

39 39 The Attribute Byte n The foreground bits determine the color of the character n The background bits determine the color of the background n The msb of foreground is an intensity bit n The blinking bit applies only to foreground

40 40 Foreground Colors n Background colors are the same as foreground colors with msb = 0

41 41 Ways to write on the screen n We can write directly to the video buffer to display text. See Direct2Videomem.htmlDirect2Videomem.html u this is the fastest method but also the most complex. Cannot redirect the output with DOS. n We can use DOS INT 21h functions u very slow to go through DOS u Output can be redirected (DOS command line) n We can use BIOS-LEVEL INT 10h functions u faster than DOS but slower than direct access u Cannot redirect the output

42 42 Some BIOS INT 10h functions n Function 00h: set video mode. AL contains the desired text mode. Ex: u mov ah,0 ;set video mode u mov al,3 ;choose text mode 3 u int 10h ;mode is set n Function 05h: set active display page. AL contains the desired page number. Ex: u mov ah,5 ;set display page u mov al,1 ;page # to display u int 10h ;display chosen page n Page 0 is the usual page displayed by DOS n Each page has its own cursor.

43 43 Some BIOS INT 10h functions (cont.) n Function 02h: Set cursor position. u Input: BH = chosen page number u DH = chosen row, DL = chosen column F mov ah,2 ;set cursor position F mov dh,10 ;row 10 F mov dl,18 ;column 18 F int 10h ;cursor is set n Function 03h: Get cursor position. u Input: BH = chosen page number u Output: DH = row, DL = column F mov ah,3 ;get cursor position F int 10h ;DH=row, DL=column

44 44 Other BIOS INT 10h functions n See chap 5 of textbook for details n 08h: Read Character and Attribute at cursor position n 09h: Set Character and Attribute at cursor position n 06h: Scroll window up (by n rows) n 07h: Scroll window down (by n rows) n...and many more!!

45 45 Trace Program Recursion main proc 0000mov ax, 8 0003push ax 0004 call Factorial 0007 mov ax, 4C00h 000A int 21h main endp Factorial proc 000Cpush bp 000Dmov bp, sp 000Fmovax, [bp+4] 0012cmp ax, 1 0015jaL1 0017mov ax, 1 001Ajmp L2 001DL1: decax 001Epushax 001Fcall Factorial 0022movbx, [bp+4] 0025mulbx 0027L2:popbp 0028ret2 Factorial endp


Download ppt "1 Procedures and Interrupts Chapter 5 n Stack n Procedure n Software Interrupt u BIOS-level access u DOS-level access n Video Display u Direct Video access."

Similar presentations


Ads by Google