1 / 62

Chapter 5: Procedures and Interrupts

Chapter 5: Procedures and Interrupts. Assembly Language for Intel-Based Computers , Kip R. Irvine 3rd edition. 3/17/2000. 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

tivona
Download Presentation

Chapter 5: Procedures and Interrupts

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 OperationsWhy 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. Memory Highest addr. 5.1 Stack OperationsStacks • 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 Stack SP SS 0

  5. SP 0000 Stack 0024 xxyy 0000 AX 0024 BX 01AB 01AB SP ... 01AB 0024 SP 0000 5.1 Stack OperationsStacks: Push and Pop Push AX Push BX

  6. Push AX 01AB 0024 Push BX SP SP 0000 Stack Pop BX 2400 0024 0024 xxyy xxyy 0000 AX 0024 BX 01AB 01AB SP SP SP ... 01AB 0024 SP 0000 5.1 Stack OperationsStacks: Push and Pop Pop AX

  7. 5.1 Stack OperationsStacks 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 DSpop Valpush 246 ; .186 required • *32 bit push on 386+

  8. 5.1 Stack OperationsAdditional Stacks Operations • PUSHF and POPFPush 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 ProceduresTerminology • 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

  10. 5.2 ProceduresGeneral format of a procedure • .codemain proc; main program…call Sub2; call subroutine…mov AX, 4c00h; exit programint 21hmain endpSub2 proc; a near procedure…ret; return from procSub2 endpend main; start execution in main

  11. 5.2 ProceduresComments • 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 endmainprogram to specify the procedure where execution is to begin

  12. 5.2 ProceduresSample 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.

  13. 5.2 ProceduresNesting 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

  14. 5.2 ProceduresFar procedures • Format of a far procedure callcall far ptr sub1 • Format of a far proceduresub1 proc far… retsub1 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

  15. xxxxs xxxxs xxxxs xxxxs xxxxs SP SP SP SP offsets SP 5.2 ProceduresProcedure stack usage • Near procedureBefore during after • Far procedureBefore during after xxxxs segs offsets SP

  16. 5.3 Procedures ParametersPassing Parameters • In registers - Fastest • In global variables - Hard to reuse, poor programming practice • On the stack - Used by high level languages (Chapter 9)

  17. 5.3 Procedures ParametersProcedure writing goals • Correctness • Convenience • Reusability • Ease of use • Save and restore registers (except those used to return values) • Document • Efficient

  18. 5.4A InterruptsInterrupts: • 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 InterruptsHardware Interrupts • Device Program Interrupt table Interrupt Code • Stack 0 4 4 3 3 1 1 1 2 2 5 5 7 6 6

  20. 5.4A InterruptsHardware 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 InterruptsHardware 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 InterruptsHardware 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.

  23. 5.4 Software InterruptsSoftware Interrupt • Software interrupt. Works much the same way but steps 1 and 2 are replaced by an INT xxinstruction in the code. The number xx determines the table entry specifying location of the interrupt handler desired.

  24. 5.4 Software InterruptsSoftware 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.)

  25. 5.4 Software InterruptsCommon 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)

  26. 5.4 Software InterruptsRedirection of I/O • DOS permits UNIX style redirection of the standard I/O devices. MyProg < COM1 > PrnThis means get the input from COM1, send the output to Prn. • We use files as the standard input or output devices. MyProg < Input.txt > Output.txt

  27. 5.4 Software InterruptsStandard 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.

  28. 5.4 Software InterruptsStandard DOS Device Names • Device NameDescription • CON console keyboard and monitor • LPT1 or PTR 1st parallel printer • LPT2 2nd parallel printer • AUX or COM1 1st serial port • COM2, COM3, COM4 2nd – 4th serial port • NUL Dummy device

  29. 5.5 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 charactermov DL, aChar; character to be printed int 21h

  30. 5.5 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 charactermov DL, 'B'int 21h

  31. 5.5 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, 09hmov dx, offset aStringint 21h

  32. 5.5 MS-DOS Function Calls (int 21H)Keyboard Input functions • INT 1 7 8 6 0AhWait Y Y Y N YFilters Y N N N YEcho Y N N N YCtrl-Break Y N Y N YStrings - - - - Y

  33. 5.5 MS-DOS Function Call (int 21H)Functions 1, 7, 8: Input characters • mov ah,n ; n = 01h, 07h, 08hint 21hmov 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

  34. 5.5 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 waitmov dl, 0FFhint 21hjz NoCharWaitingmov aChar, al • This function is also used for input

  35. 5.5 MS-DOS Function Calls (int 21H)Function 0Ah: Input string • The input buffer 0 1 2 … characters input number of characters input (excluding <CR>)max characters allowed (including <CR>)

  36. 5.5 MS-DOS Function Calls (int 21H)Function 0Ah: Input string • Input up to 9 characters (plus <CR>)inputBuffer label bytemaxChar db 10numinput db ?buffer db 10 dup (0)... mov ah, 0Ah ; string input mov dx, offset inputBuffer int 21h

  37. 5.5 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.)

  38. 5.5 MS-DOS Function Calls (int 21H)Function 3Fh: Read from a File or Device • aName db 82 dup (?); 80 char. plus CR & LFMAX_LENGTH = $ - aNamelenName dw ? ; number of characters typed...mov ah, 3Fh; print to file or devicemov bx, 0 ; device = std. inputmov cx, MAX_LENGTH ; set lengthmov dx, offset aName ; and offset of helloint 21h ; print stringssub ax, 2; if appropriate: find string lengthmov lenName, ax ; and save it

  39. 5.5 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 (??)

  40. 5.5 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 devicemov bx, 0 ; device = Std. outputmov cx, HELLO_LENGTH ; set lengthmov dx, offset hello ; and offset of helloint 21h ; print string

  41. 5.6 and 5.7 BIOS-Level I/OBIOS I/O functions • Advantages: • Faster • More control • "Understands" video monitors • Disadvantages • Cannot be redirected • Somewhat harder to use

  42. 5.6 BIOS-Level Input (int 16H)Function 10h: Wait for Key • Purpose: Wait and read keyboard • mov ah, 10hint 16hmov scanCode, ahmov 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.

  43. 5.6 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

  44. 5.6 BIOS-Level Input (int 16H)Scan Codes: Examples • CharacterScanASCII A 1E 41 a 1E 61 ^A 1E 01 Alt A 1E 00 • F1 3B 00 Shift F1 54 00 ^ F1 5E 00 Alt F1 68 00 • Home 47 E0 Right arrow 4D E0

  45. 5.6 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 codeof 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.

  46. 46 5.5 MS-DOS Function Calls (int 21H)Processing scan codes in DOS • mov ah, 01h ; Input char - DOSint 21h ; Input the ASCII codecmp al, 00h ; is it ASCII 0?jne processASCIIcharint 21h ; read scancodemov aScanCode, al

  47. 5.6 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 ASCIIKeyscankey: ; process scancode in ah

  48. 5.7 BIOS-Level Video Control (int 10H)Three Levels of Video • LevelCompatibilitySpeedRedirection • DOS high slow yes • BIOS high medium no • Direct medium high no • 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

  49. 5.7 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

  50. 5.7 BIOS-Level Video Control (int 10H)Memory requirements: Graphic modes

More Related