1 Chapter 5: Procedures and Interrupts Assembly Language for Intel-Based Computers, Kip R. Irvine...
-
Upload
chrystal-skinner -
Category
Documents
-
view
225 -
download
0
Transcript of 1 Chapter 5: Procedures and Interrupts Assembly Language for Intel-Based Computers, Kip R. Irvine...
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
SPxxyy
0024
01AB
AX
BX
0024
000001AB0024 SP
01AB SP
...
0000
0000
6
5.1 Stack Operations
Stacks: Push and Pop
Pop AX
Stack
SPxxyy
0024
01AB
AX
BX
0024
000001AB
Push AX
Push BX
Pop BX
0024 SP
01AB SP
...
0000
00000024 SP
01AB2400 SP
0024SP
xxyy
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 DSpop Valpush 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
10
5.2 Procedures
General format of a procedure• .codemain proc ; main program … call Sub2 ; call subroutine … mov AX, 4c00h; exit program int 21hmain endpSub2 proc ; a near procedure … ret ; return from procSub2 endpend main ; start execution in main
11
5.2 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
12
5.2 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.
13
5.2 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
145.2 Procedures
Far procedures• Format of a far procedure call
call 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
5.2 Procedures
Procedure stack usage• Near procedure
Before during after
• Far procedureBefore during after
xxxxs SP
offsets SP
xxxxs SP
xxxxs SP xxxxs
offsets SPxxxxs SP
xxxxs
segs
16
5.3 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)
17
5.3 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
11 23
4
5
6
1
5
43
0
2
7 6
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.
23
5.4 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.
24
5.4 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.)
25
5.4 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)
26
5.4 Software Interrupts
Redirection 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 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.
28
5.4 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
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 character mov 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
375.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.)
385.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
395.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 (??)
405.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 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
41
5.6 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
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• Character Scan ASCII
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
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
46
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• LevelCompatibility Speed Redirection• 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
Display CGA EGA VGA SVGAColors 4 16 256 16,777,216 Bits per pixel 2 4 8 24 Width 320 640 640 1,280 Height 200 350 480 1,024 Bits per byte 8 8 8 8 Memory per page 16,000 112,000 307,200 3,932,160
51
5.7 BIOS-Level Video Control (int 10H)
Text modes
• Mode Rows Columns Type00h 25 40 Mono01 25 40 Color (16) 02 25 80 Mono 03 25 80 Color (16) (Standard)51 43 80 Color (16) (Best EGA)5A 132 60 Color (16)
52
5.7 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
• 0 0 0 0 0 1 1 1
0111 White character 000 Black background 0 Do not blink
54
5.7 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
55
5.7 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
Code Color Code Code0000 black 1000 gray0001 blue 1001 light blue0010 green 1010 light green0011 cyan 1011 light cyan0100 red 1100 light red0101 magenta 1101 light magenta0110 brown 1110 yellow0111 white 1111 bright white
57
5.7 BIOS-Level Video Control (int 10H)
Function 0Fh: Get Video ModeFunction 00h: Set Video Mode
• mov ah, 0Fh ; Get video modeint 10hmov vmodeOld, almov pageOld, bh
mov ah, 00h ; Set video modemov al, vmodeNewint 10h
58
5.7 BIOS-Level Video Control (int 10H)
Function 09h: Write character and attribute
• mov ah, 09h ; Write char. and attrmov al, aCharmov bh, videoPagemov bl, theAttribute ; <-- mov cx, repeatCountint 10h
59
5.7 BIOS-Level Video Control (int 10H)
Function 0Ah: Write character
• Just like 09h except that the attribute is unchanged
• mov ah, 0Ah ; Write charmov al, aCharmov bh, videoPagemov cx, repeatCountint 10h
60
5.7 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 upmov al, numLinesmov ch, topRowmov cl, leftColumnmov dh, bottomRowmov dl, rightColumnmov bh, theAttributeint 10h AB
ABAB Scrool
up one line
61
5.7 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 modemov al, vmodeOldint 10h mov ah, 05h ; Set video pagemov al, pageOldint 10h
• (See slide 55)
62
5.7 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 cursormov al, aCharmov bh, videoPageint 10h