1 Chapter 5: Procedures and Interrupts Assembly Language for Intel-Based Computers, Kip R. Irvine 3rd edition 3/17/2000.

Slides:



Advertisements
Similar presentations
DOS and BIOS Interrupts DOS and BIOS interrupts are used to perform some very useful functions, such as displaying data to the monitor, reading data from.
Advertisements

More about procedures and Video Processing. Lesson plan Review existing concepts More about procedures and boolean expression Video processing.
Video systems (continue). Practice Modify the program to get a string from a keyboard to display the input string on the middle of the screen with reverse.
Set 20 Interrupts. INTERRUPTS The Pentium has a mechanism whereby external devices can interrupt it. Devices such as the keyboard, the monitor, hard disks.
Assembly Language for Intel-Based Computers Chapter 15: BIOS-Level Programming (c) Pearson Education, All rights reserved. You may modify and.
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.
1 Hardware and Software Architecture Chapter 2 n The Intel Processor Architecture n History of PC Memory Usage (Real Mode)
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
CS2422 Assembly Language & System Programming November 2, 2006.
Chapter 7 Programming with DOS and BIOS Function Calls Objectives: The use of DOS and BIOS function call How to read the PC’s keyboard How to send text.
Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS.
Kip Irvine: Assembly Language for Intel-Based Computers
Chapter 15 BIOS-Level Programming
ICS312 Set 4 Program Structure. Outline for a SMALL Model Program Note the quiz at the next lecture will be to reproduce this slide.MODEL SMALL.586 ;
Micro-Computer Applications: Procedures & Interrupts Dr. Eng. Amr T. Abdel-Hamid ELECT 707 Fall 2011.
ORG ; FOUR INT 21H and INT 10H Programming and Macros Dec Hex Bin
Introduction to Subroutines. All the combinations in which a subroutine can be written 1. The subroutine may be: a. Internal or b. External 2. The type.
Lecture 11 Last notes on interrupts and exam review Dr. Dimitrios S. Nikolopoulos CSL/UIUC.
BIOS1 Basic Input Output System BIOS BIOS refers to a set of procedures or functions that enable the programmer have access to the hardware of the computer.
ECE291 Computer Engineering II Lecture 9 Josh Potts University of Illinois at Urbana- Champaign.
Stack Operations LIFO structure (last-in,first-out) –The last value put into the stack is the first value taken out Runtime stack –A memory array that.
1/2002JNM1 Positional Notation (Hex Digits). 1/2002JNM2 Problem The 8086 has a 20-bit address bus. Therefore, it can access 1,048,576 bytes of memory.
BIOS and DOS Programming in DOS INT 10 and 21H. Interrupts There are some extremely useful subroutines within BIOS or DOS that are available to the user.
1 Screen and Keyboard Operations Suthida Chaichomchuen
Chapter 3 Examining Computer Memory and Executing Instructions.
Chapter 2 Instruction Addressing and Execution. Lesson plan Review some concepts in the first week First assembly program with EMU8086 Related concepts.
Text-Mode Programming Question #1 What are the three levels of access to the video display when writing characters on the screen in text mode?
CSNB374: Microprocessor Systems Chapter 5: Procedures and Interrupts.
Strings, Procedures and Macros
Video systems. Lesson plan Review the code for the previous exercise Video systems Review for midterm exam.
8086 Microprocessor Interrupts By: Vijay Kumar. K Reference From Slide Share.
21/11/2005CAP2411 Input & Output Instructions CPU communicates with the peripherals through I/O registers called I/O ports. There are 2 instructions, IN.
10H Interrupt. Option 0H – Sets video mode. Registers used: – AH = 0H – AL = Video Mode. 3H - CGA Color text of 80X25 7H - Monochrome text of 80X25 Ex:
String Instructions String instructions were designed to operate on large data structures. The SI and DI registers are used as pointers to the data structures.
EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of.
Microprocessors Monday, Apr. 16 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.
Control Structure vs. Assembly Language NASM. If-then-else If conditional then then_actions jump to endif else else_actions endif.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 13: 16-Bit MS-DOS Programming Interrupts (c) Pearson Education, All rights reserved.
BIOS and DOS Interrupts Basic Input /Outpu System Disk Operating System.
Internal Programming Architecture or Model
Lecture 11 Text mode video
File Operations. FILE PROCESSING For the purposes of the following discussion, reading means copying all or part of an existing file into memory Writing.
Chapter 5: Procedures and Interrupts
Computer Organization & Assembly Language University of Sargodha, Lahore Campus Prepared by Ali Saeed.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
ECE291 Computer Engineering II Lecture 12 Josh Potts University of Illinois at Urbana- Champaign.
Stack Operations Dr. Hadi AL Saadi.
Assembly language programming
Format of Assembly language
1st prog! Q: Read a char – from a keyboard & display it at the beginning of the next line! ====== A.
Microprocessor and Assembly Language
Computer Organization & Assembly Language Chapter 3
Instruksi Set Prosesor 8088
Microprocessor and Assembly Language
(The Stack and Procedures)
9/17/2018 Kiến Trúc Máy Tính.
Microprocessor and Assembly Language
Symbolic Instruction and Addressing
8086 Registers Module M14.2 Sections 9.2, 10.1.
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Symbolic Instruction and Addressing
(The Stack and Procedures)
Symbolic Instruction and Addressing
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Unit:08 Software Interrupts
Chapter 6 –Symbolic Instruction and Addressing
Process.
(The Stack and Procedures)
By Nasser Halasa Assembly Language.
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 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