Procedures and Interrupts Chapter 5 n Stack n







![Procedures n n n 8 Procedures are defined like this: name PROC [type]. . Procedures n n n 8 Procedures are defined like this: name PROC [type]. .](https://slidetodoc.com/presentation_image_h2/333e4357243467fd69f97ad8f679df11/image-8.jpg)





































- Slides: 45

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 1

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

n PUSH source 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 u n Ex: (see figure) mov ax, 06 push ax mov ax, 0 A 5 h push ax n n 3 This is for a source of type word (reg 16 or mem 16). imm 16 are allowed only on 286 and later processors) PUSH (16 -bit case)

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

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 5 The destination operand cannot be imm

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 21 h ; prints message pop dx ; restore DX pop ax ; restore AX 6

More Saving and Restoring n n 7 PUSHA (. 286) pushes AX, CX, DX, BX, SP, BP, SI, DI on stack and POPA pops the same registers in reverse order PUSHAD (. 386) pushes EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI on stack and POPAD pops the same registers in reverse order PUSHF and POPF pushes and pops the FLAGS register onto and from the stack PUSHFD and POPFD (. 386) pushes and pops the EFLAGS register onto and from the stack
![Procedures n n n 8 Procedures are defined like this name PROC type Procedures n n n 8 Procedures are defined like this: name PROC [type]. .](https://slidetodoc.com/presentation_image_h2/333e4357243467fd69f97ad8f679df11/image-8.jpg)
Procedures n n n 8 Procedures are defined like this: name PROC [type]. . . set of instructions. . . RET name ENDP The “type” is either NEAR or FAR To transfer control to the procedure “name” we do: CALL [type PTR] name RET transfers control to the instr. following CALL The default for “type” and “type PTR” is: u NEAR: for memory models: tiny, small, compact u FAR: for memory models: medium, large, huge

CALL & RET (NEAR Procedures) n 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 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 9 instruction pointed by IP is then executed)

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

CALL & RET (FAR Procedures) n n 11 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 A RET from a FAR procedure effectively does: u POP IP u POP CS F Hence: the instruction at CS: IP is then executed

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

When does a procedure needs to be FAR? n n A NEAR CALL is faster than a FAR CALL 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 13

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) 14

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: 15 fastest way to pass arguments

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 16 Proc. Ex. html

Interrupts n n 17 The term interrupt is used in many different ways A hardware interrupt is a signal generated by any part of the hardware that needs immediate attention of the processor 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 A processor exception is an automatically generated trap in response to an exceptional condition (abnormal program execution). Ex: divide overflow, coprocessor not available. . .

Hardware Interrupts n n 18 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 The CPU uses this interrupt number to index the interrupt vector table (IVT) located at physical addresses 00000 h to 003 FFh (pp. 33) Each entry of this table, called an interrupt vector, contains the segment: offset address of the Interrupt Handler (ISR) servicing that interrupt. To service an interrupt, the CPU transfers control to the corresponding ISR

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

Interrupt Processing n n The same mechanisms are used to handle all types of interrupts (hardware, software, exception) When an interrupt occurs: 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) u 20

Ex: using INT 10 h BIOS video services 21

Interrupt Service Routines n n 22 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 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 How to write a ISR and how to initialize the corresponding entry in the IVT? (chap 15) 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

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

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

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

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

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

Ex: AH=06 h clear_keyboard Clear_keyboard proc push ax push dx L 1: mov ah, 6 mov dl, 0 FFh int 21 h jnz L 1 pop dx pop ax ret clear_keyboard endp 28

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

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

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

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: F 1 -F 12, arrows, Home, Pg. Up, Pg. Dn, End, Ins, Del 32

Scan Codes n n n 33 Only ASCII keys have an ASCII code but all keys have a SCAN CODE (1 byte). See scancodes. html When we strike a key: u The keyboard interrupts (INT 9 h) the CPU and sends the Scan Code to I/O port 60 h u The BIOS INT 9 h 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) 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

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

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). . 35

Video Modes n n 36 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) 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

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 n n 37 Each of these words is stored in the video buffer starting at physical address B 80000 h One screen of text (80 X 25 X 2 = 4000 bytes) requires 1 video page of 4 KB VGA (and better) adapters can hold 8 video pages: page 0 to 7

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 3 rd word displays the char at (2, 0). . . u. . . the last word displays the char at (24, 79) n 38 (other pages can be modified while the active page is being displayed)

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

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

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

Some BIOS INT 10 h functions n n n 42 n Function 00 h: 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 10 h ; mode is set Function 05 h: 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 10 h ; display chosen page Page 0 is the usual page displayed by DOS Each page has its own cursor.

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

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

Trace Program Recursion main proc 0000 mov ax, 8 0003 push ax 0004 call Factorial 0007 mov ax, 4 C 00 h 000 A int 21 h main endp 45 Factorial proc 000 C push bp 000 D mov bp, sp 000 F mov ax, [bp+4] 0012 cmp ax, 1 0015 ja L 1 0017 mov ax, 1 001 A jmp L 2 001 D L 1: dec ax 001 E push ax 001 F call Factorial 0022 mov bx, [bp+4] 0025 mul bx 0027 L 2: pop bp 0028 ret 2 Factorial endp