1 Chapter 5: Procedures and Interrupts Assembly Language for Intel-Based Computers, Kip R. Irvine 3rd edition 3/17/2000
2 Chapter 5: Procedures and Interrupts 5.1 Stack Operations 5.2 Procedures 5.3 Procedure Parameters 5.4A Interrupts 5.4 Software Interrupts 5.4C Layers of IO 5.5 MS-DOS function calls (INT 21h) 5.6 BIOS-Level Keyboard Input (INT 16h) 5.7 BIOS-Level Video Controld (INT 10h) 5.7A Writing directly to Video memory
3 5.1 Stack Operations Why stacks? Recall data structures class. Recall how procedure (functions) used. Procedure calling is a stack operation. We use to stack to keep track of return addresses. Parameters and local variables are also put on the stack.
4 5.1 Stack Operations Stacks Stacks are an important part of computer hardware and software In x86 architecture (like many other computers), stacks grow down from high memory to low memory SP points to the top of the stack SS points to the beginning of the stack segment In 16 bit mode, stack instructions work with 16 bits at a time Memory 0 SS SP Highest addr. Stack
5 5.1 Stack Operations Stacks: Push and Pop Push AX Push BX Stack SP xx yy AB AX BX AB SP 01 AB SP
6 5.1 Stack Operations Stacks: Push and Pop Pop AX Stack SP xx yy AB AX BX AB Push AX Push BX Pop BX SP 01 AB SP SP 01AB SP 0024 SP xx yy
7 5.1 Stack Operations Stacks Operations Push and Pop: You can push and pop –16/32* registers –16/32 memory location –16/32 bit immediate values (PUSH, 186+) Examples: push DS pop Val push 246 ;.186 required *32 bit push on 386+
8 5.1 Stack Operations Additional Stacks Operations PUSHF and POPF Push and pops the Flag register. There are no operands PUSHA and POPA (286+) Pushes registers on the stack in this order: AX, CX, DX, BX, SP, BP, SI, DI and pops them in reverse order PUSHAD and POPAD (386+) The same except they work with 32 bit registers
9 5.2 Procedures Terminology Subroutines and procedures are blocks of code that are called and must be returned from A function is a procedure that returns a value It is not legal to jump out of a procedure
Procedures General format of a procedure.code main proc ; main program … call Sub2 ; call subroutine … mov AX, 4c00h ; exit program int 21h main endp Sub2 proc ; a near procedure … ret ; return from proc Sub2 endp end main ; start execution in main
Procedures Comments Procedures begin with itsname proc and terminate with itsname endp The procedure must have at least one ret statement Use call itsname to call the procedure Use end mainprogram to specify the procedure where execution is to begin
Procedures Sample procedure See subs.asm It is a highly desirable to preserve registers when writing a procedure. Save at beginning and restore before returning. This makes it much easier to reuse the procedure Remember to restore registers in reverse order Calling protocol: The rules needed to use the procedure.
Procedures Nesting procedures One procedure can call another. Call is like a jmp except the address of the next instruction is pushed on the stack The return pops the return address from the stack and puts it in the IP NEAR procedures are in the same segment. A 16 bit offset of the return address is pushed on the stack by the call statement FAR procedures may be in a different segment. Both the 16 bit segment and offset are pushed on the stack
Procedures Far procedures Format of a far procedure call call far ptr sub1 Format of a far procedure sub1 proc far … ret sub1 endp Far procedures use RETF, near procedures use RETN. The assembler picks the right one The above assumes that we are using the small or compact model
Procedures Procedure stack usage Near procedure Before during after Far procedure Before during after xxxx s SP offset s SP xxxx s SP xxxx s SP xxxx s offset s SP xxxx s SP xxxx s seg s
Procedures Parameters Passing Parameters In registers - Fastest In global variables - Hard to reuse, poor programming practice On the stack - Used by high level languages (Chapter 9)
Procedures Parameters Procedure writing goals Correctness Convenience Reusability –Ease of use –Save and restore registers (except those used to return values) –Document Efficient
18 5.4A Interrupts Interrupts: Hardware interrupt: in response to a request by a hardware device that needs attention. Hardware interrupts occur at "unexpected" times. Software interrupt: A call to DOS or BIOS in response to a interrupt instruction (INT) in the program being processed. Exception: An automatically generated trap in response to an exceptional condition such as division by zero.
19 5.4A Interrupts Hardware Interrupts Device Program Interrupt table Interrupt Code Stack
20 5.4A Interrupts Hardware Interrupt Steps 0. The program is executing in the CPU 1. The hardware device needs attention and requests an interrupt. 2. The CPU finishes processing the current instruction. It the saves its "state" (flags and CS:IP) on the stack. 3. The address of the interrupt handler is obtained from the Interrupt Vector Table.
21 5.4A Interrupts Hardware Interrupt Steps 4. The CPU loads the address of the interrupt handler into the IP. 5. The interrupt handler code is processed. 6. When the interrupt handler is finished, the CPU pops the "state" (CS:IP and Flags) back from the stack. 7. Execution of original program continues beginning at the next instruction.
22 5.4A Interrupts Hardware Interrupt Comments Some interrupt operations are so critical that the interrupt driver may disable other interrupts until the critical operation is completed. In IBM terminology, hardware interrupts are known as an IRQ. Each device is assigned a number which determines the Interrupt Vector entry of the appropriate interrupt handler.
Software Interrupts Software Interrupt Software interrupt. Works much the same way but steps 1 and 2 are replaced by an INT xx instruction in the code. The number xx determines the table entry specifying location of the interrupt handler desired.
Software Interrupts Software Interrupt In many respects interrupts are like procedure calls. Difference - In procedures, the address of the procedure is specified in the program. In interrupts, the address is specified in the interrupt vector table. In IBM design, the interrupt vector table is stored in the lowest 1K of memory. Each entry is a 4 byte segment/offset address. (That allows 256 entries.) (See chapter 2.)
Software Interrupts Common Software Interrupts INT 10h Video services INT 16h Keyboard services INT 17h Printer services INT 1Ah Time of day INT 1Ch User Timer Interrupt INT 21h DOS services - "DOS function calls" Most interrupts use AH to specify the desired operation (function)
Software Interrupts Redirection of I/O DOS permits UNIX style redirection of the standard I/O devices. MyProg Prn This means get the input from COM1, send the output to Prn. We use files as the standard input or output devices. MyProg Output.txt
Software Interrupts Standard DOS Device Names The standard input and output device is CON, the console which consists of the keyboard and monitor. DOS can be instructed to redirect I/O from the standard input and output devices to other units.
Software Interrupts Standard DOS Device Names Device Name Description CON console keyboard and monitor LPT1 or PTR 1st parallel printer LPT2 2nd parallel printer AUX or COM1 1st serial port COM2, COM3, COM4 2 nd – 4 th serial port NUL Dummy device
MS-DOS Function Calls (int 21H) Function 02h: character output Function 2h: Single character output DL: Character to be printed (Caution) AL: May be modified mov AH, 2h ; output character mov DL, aChar ; character to be printed int 21h
MS-DOS Function Calls (int 21H) Function 06h: Direct Output Can do both input and output single characters For output DL holds character to be printed mov AH, 6h ; print character mov DL, 'B' int 21h
MS-DOS Function Calls (int 21H) Function 09h: String output prints "$" terminated string DX holds offset of string to be printed aString db "String", 0Dh, 0Ah, "$"... mov ah, 09h mov dx, offset aString int 21h
MS-DOS Function Calls (int 21H) Keyboard Input functions INT17860Ah WaitYYYNY FiltersYNNNY EchoY NNNY Ctrl-BreakYNYN Y Strings----Y
MS-DOS Function Call (int 21H) Functions 1, 7, 8: Input characters mov ah, n ; n = 01h, 07h, 08h int 21h mov aChar, al Notes: –Character input is put in AL –All wait for a key press –Functions 7 and 8 do not echo –Function 7 does not recognize ctrl-break –^G, ^H, ^I, ^J, ^M react as specified by ASCII, other control characters print special characters
MS-DOS Function Calls (int 21H) Function 06h: Input character/no wait Sample application: Games where the game proceeds until user presses a key mov ah, 06h ; Input char/no wait mov dl, 0FFh int 21h jz NoCharWaiting mov aChar, al This function is also used for input
MS-DOS Function Calls (int 21H) Function 0Ah: Input string The input buffer … characters input number of characters input (excluding ) max characters allowed (including )
MS-DOS Function Calls (int 21H) Function 0Ah: Input string Input up to 9 characters (plus ) inputBuffer label byte maxChar db 10 numinput db ? buffer db 10 dup (0)... mov ah, 0Ah ; string input mov dx, offset inputBuffer int 21h
MS-DOS Function Calls (int 21H) Function 3Fh: Read from a File or Device Used to input a string This function is designed to read from a disk file but can also read from the keyboard. (Keyboard input) 0Ah like editing operations enabled. Reads until buffer full or until CR pressed. Put 0 in BX to specify "standard input" Returns count of characters actually typed in AX 0Dh and 0Ah are stored in the input buffer and included in the count The input can be redirected from a file. (Reads until buffer full or end of file.)
MS-DOS Function Calls (int 21H) Function 3Fh: Read from a File or Device aName db 82 dup (?) ; 80 char. plus CR & LF MAX_LENGTH = $ - aName lenName dw ? ; number of characters typed... mov ah, 3Fh ; print to file or device mov bx, 0 ; device = std. input mov cx, MAX_LENGTH ; set length mov dx, offset aName ; and offset of hello int 21h ; print strings sub ax, 2 ; if appropriate: find string length mov lenName, ax ; and save it
MS-DOS Function Calls (int 21H) Function 40h: Write to a File or Device Designed to write to a file Put 0 in BX to specify "standard output" Output cannot be redirected successfully Output tends to get lost if input is redirected (??)
MS-DOS Function Calls (int 21H) Function 40h: Write to a File or Device hello db "Hello " HELLO_LENGTH = $ - hello... mov ah, 40h ; print to file or device mov bx, 0 ; device = Std. output mov cx, HELLO_LENGTH ; set length mov dx, offset hello ; and offset of hello int 21h ; print string
and 5.7 BIOS-Level I/O BIOS I/O functions Advantages: –Faster –More control –"Understands" video monitors Disadvantages –Cannot be redirected –Somewhat harder to use
BIOS-Level Input (int 16H) Function 10h: Wait for Key Purpose: Wait and read keyboard mov ah, 10h int 16h mov scanCode, ah mov ASCIIChar, al Every key has a scan code Does not echo Keys for non-ASCII characters have 00h or E0h as the ASCII code Can't be redirected.
BIOS-Level Input (int 16H) Keyboard input: ASCII and Scan Codes ASCII code: one of the 127 characters in ASCII alphabet. Values from 1 to 127. Many keys do not have an ASCII code. Examples: All Alt keys like Alt A, F1, Shift F2, Control F3, right arrow, Home, Insert,... Non-ASCII keys have an ASCII code of 00 or E0h. BIOS functions provide both ASCII code and Scan code
BIOS-Level Input (int 16H) Scan Codes: Examples Character ScanASCII A 1E 41 a 1E61 ^A1E 01 Alt A1E 00 F13B00 Shift F15400 ^ F15E00 Alt F16800 Home 47E0 Right arrow 4DE0
BIOS-Level Input (int 16H) Scan Codes: BIOS vs. DOS BIOS: You get scan code and ASCII value every time! Special keys have a ASCII code of either 00h or E0h DOS: You only get the scan code if the ASCII code is 00h. Codes come one at a time with the ASCII code first.
MS-DOS Function Calls (int 21H) Processing scan codes in DOS mov ah, 01h ; Input char - DOS int 21h ; Input the ASCII code cmp al, 00h ; is it ASCII 0? jne processASCIIchar int 21h ; read scancode mov aScanCode, al 46
BIOS-Level Input (int 16H) Processing scan codes in BIOS mov ah, 10h ; Input char - BIOS int 16h ; get scan and ASCII cmp al, 0E0h ; is it an extended key? je scankey cmp al, 00h ; is it an ASCII key? jne ASCIIKey scankey: ; process scancode in ah
BIOS-Level Video Control (int 10H) Three Levels of Video LevelCompatibilitySpeedRedirection DOShighslowyes BIOShighmediumno Direct mediumhighno DOS output is generic. There are "no" special video effects BIOS video "understands" video and allows special effects Direct video allows everything but do it yourself
BIOS-Level Video Control (int 10H) Adapters, Modes Adapters include Monochrome, CGA, Hercules, EGA, VGA, SVGA CGA: 320x200 pixels (4 colors at a time) or 640x200 pixels (2 colors) EGA: 640x350 VGA: 640x480 SVGA: 800x600, 1024x768, 1152x864,1280x1024 General modes: text or graphics
BIOS-Level Video Control (int 10H) Memory requirements: Graphic modes
BIOS-Level Video Control (int 10H) Text modes Mode Rows Columns Type 00h Mono Color (16) Mono Color (16) (Standard) Color (16) (Best EGA) 5A132 60Color (16)
BIOS-Level Video Control (int 10H) Text attributes To get 16 colors of text, we need ___ bits To get 8 colors of background, we need ___ bits To turn blinking on and off, we need ___ bit Text mode stores two bytes per character: ASCII character and the character's attribute
53 A 5.7 BIOS-Level Video Control (int 10H) Text attributes Blink Background Foreground White character 000 Black background 0 Do not blink
BIOS-Level Video Control (int 10H) Pages Most adapters and most modes allow multiple pages BIOS allows writing to any of the pages even if it is not currently displayed Example uses: –Write to hidden page and then display that page to create an "instantaneous" switch in the display –Save old screen data while doing a special page
BIOS-Level Video Control (int 10H) Polite programs If the program changes screen colors, mode, and/or page, it should restore the original when it terminates: Methods: –Save the information –Change pages
56 Background 5.7 BIOS-Level Video Control (int 10H) Text Mode Colors Foreground CodeColorCode Code 0000black1000 gray 0001blue1001 light blue 0010green1010 light green 0011cyan1011 light cyan 0100red1100 light red 0101magenta1101 light magenta 0110brown1110 yellow 0111white1111 bright white
BIOS-Level Video Control (int 10H) Function 0Fh: Get Video Mode Function 00h: Set Video Mode mov ah, 0Fh ; Get video mode int 10h mov vmodeOld, al mov pageOld, bh mov ah, 00h ; Set video mode mov al, vmodeNew int 10h
BIOS-Level Video Control (int 10H) Function 09h: Write character and attribute mov ah, 09h ; Write char. and attr mov al, aChar mov bh, videoPage mov bl, theAttribute ; <-- mov cx, repeatCount int 10h
BIOS-Level Video Control (int 10H) Function 0Ah: Write character Just like 09h except that the attribute is unchanged mov ah, 0Ah ; Write char mov al, aChar mov bh, videoPage mov cx, repeatCount int 10h
BIOS-Level Video Control (int 10H) Functions 06h (07h): Scroll up (down) Specify a window by its corners Specify the number of lines to scroll in window Number lines = 0 means all lines Row and columns numbering starts with 0 mov ah, 06h ; scroll window up mov al, numLines mov ch, topRow mov cl, leftColumn mov dh, bottomRow mov dl, rightColumn mov bh, theAttribute int 10h AB Scrool up one line
BIOS-Level Video Control (int 10H) Restore the screen Recall that after changing the video mode, pages, or attributes we should restore the screen before terminating the program mov ah, 0h ; Set video mode mov al, vmodeOld int 10h mov ah, 05h ; Set video page mov al, pageOld int 10h (See slide 55)
BIOS-Level Video Control (int 10H) Function 0Eh: Teletype output Print the character, move cursor to right, go to beginning of next line if needed mov ah, 0Eh ; Write char ; and advance cursor mov al, aChar mov bh, videoPage int 10h