Beruflich Dokumente
Kultur Dokumente
Week 6 Slide 1
SS
Top of Stack
SS:SP
SP
PUSH
End of Stack
SS:0001h SS:0000h
Week 8 Slide 2
ret
Pop saved IP and if necessary the saved CS and restores their values in the registers
Electrical and Computer Engineering EE3612 Microprocessor Systems Instructor: Li Bai, PhD Week 8 Slide 3
Procedure example
STACK_SEG SEGMENT STACK 'STACK' DB 64 DUP(?) STACK_SEG ENDS MATLAB DATA_SEG SEGMENT TOTAL DW 1234H DATA_SEG ENDS CODE_SEG SEGMENT EX612 'CODE' BL=12H SQUARE(BL) function y=SQUARE(x) y=x*x;
PROC FAR ASSUME CS:CODE_SEG, SS:STACK_SEG, DS:DATA_SEG PUSH DS MOV AX, 0 PUSH AX MOV AX, DATA_SEG MOV DS, AX
MOV BL, 12H CALL SQUARE RET EX612 ENDP SQUARE PROC NEAR PUSH AX MOV AL, BL IMUL BL MOV BX, AX POP AX RET SQUARE ENDP CODE_SEG ENDS
Week 8 Slide 4
When you need to allocate/deallocate memory in the stack you manipulate directly the SP
Electrical and Computer Engineering EE3612 Microprocessor Systems Instructor: Li Bai, PhD Week 8 Slide 5
Procedures at a glance g
Procedures can access global variables declared at the beginning of the program Procedures can access global variables stored in registers Procedures may have parameters passed to them
Registers with global variables is a form of parameter passing Pushing parameters to the stack is another form of parameter passing i
Procedures may need to preserve registers Procedures may return results to the caller in registers or write results in memory
Week 8 Slide 6
D not push and pop registers th t will store Do t h d i t that ill t return values
Electrical and Computer Engineering EE3612 Microprocessor Systems Instructor: Li Bai, PhD Week 8 Slide 7
Macros
Procedures have some extra overhead to execute (call/ret statements, push/pop IP, CS and data from the stack) A macro is a piece of code which is macroexpanded whenever the name of the macro is encountered Note the difference, a procedure is called, while a macro is just , p , j expanded/inlined in your program Macros are faster than procedures (no call instructions, stack management etc.) g ) But they might
Significantly increase code size Hard to debug
Week 8 Slide 8
Position MACRO Row, Column PUSH AX PUSH BX PUSH DX MOV AH, 02H AH MOV DH, Row MOV DL, Column MOV BH, 0 INT 10H POP DX POP BX POP AX ENDM
Position 8, 6
Week 8 Slide 9
Break the program into logical components that can be easily translated to procedures in your code d Use descriptive names for variables
N Noun_type f types t for t Nouns for variables Verbs for procedures
Electrical and Computer Engineering EE3612 Microprocessor Systems Instructor: Li Bai, PhD Week 8 Slide 10
Tracing bugs
The debugging process:
Set breakpoints in your programs and use them as checkpoints for checking the contents of registers/memory Comment out code, this might help you find out whether the commented out code contains the bug
Use print statements (and you might not need the debugger!)
Display the values of critical data Display the status of the program
Tracing bugs
Force registers and variables to test the output of the procedure
Helps you debug the procedure using as many inputs as possible
Procedures
Labeled sections of code that you can jump to or return from any point in your program A procedure in your assembler is merely a non-dotted non dotted label Use dotted labels if you want to set jump points within a procedure (l i t ithi d (local l b l ) l labels)
640K RAM Resident part of COMMAND.COM DOS Kernel, Device Drivers, etc. Software BIOS 00400 00000 DOS Data Area Interrupt Vector Table
Offset (Segment 0400) 0000 - 0007 0008 000F 0010 - 0011 0012 0013 0014 0015 0016 0017 0018 0019 001A 001B 001C 001D 001E 003D 003E 0048 0049 004A 004B 004C 004D 004E 004F 0050 005F 0060 0061 0062 0063 - 0064 0065 0066 0067 006B 006C 0070
Description Port addresses, COM1 COM4 Port addresses, LPT1 LPT4 Installed hardware list Initialization flag Memory size, in Kbytes Memory in I/O channel Keyboard status flags Alternate key entry storage Keyboard buffer p y pointer (head) ( ) Keyboard buffer pointer (tail) Keyboard typeahead buffer Diskette data area Current video mode Number of screen columns Regen buffer length, in bytes Regen buffer starting offset Cursor positions, video pages 1 - 8 Cursor end line Cursor start line Currently displayed video page number Active display base address CRT mode register (MDA, CGA) Register for CGA Cassette data area Timer data area
Interrupts
Provide a mechanism for immediate changing of the program environment. Transfers program control to what is called an interrupt-service routine. 8088 & 8086 can i l implement any of 256 t f different types of interrupts.
Week 15 Slide 18
Interrupts
Main program Instruction N Instruction N + 1
When the interrupt p service routine is finished, a return from interrupt instruction will return program p g control to instruction N + 1 of the main program. Interrupt X occurs during p g the execution of instruction N in the main program. Before going to instruction B f i t i t ti N+1, program control is passed to the first instruction for interrupt X.
First instruction Interrupt service routine for Interrupt X Interr pt X. Return from Int.
Interrupts
How does it know where to go, i.e., where this g , , interrupt service routine is located?
an Interrupt Vector Table
There are 256 interrupt vectors that correspond to the 256 interrupt types. The vector table is located in memory starting at address 0 Each vector is 4 bytes (2 words) in length and contains the CS and IP values of its respective interrupt service routine.
User Available
Vector 3210 Vector 3110
Reserved
16 14 12 10 0E 0C 0A 08 06 04 02 00 CS 5 IP 5 CS 4 IP 4 CS 3 IP 3 CS 2 IP 2 CS 1 IP 1 CS Value Vector 0 (CS 0) IP Value Vector 0 (IP 0) Vector 5 Vector 4 Overflow Vector 3 Breakpoint Vector V t 2 NMI Vector 1 Single-Step Vector 0 Divide Error
Interrupts
Divided into 5 groups (in order of priority)
Reset (highest priority) Internal interrupts and exceptions Software interrupts Non maskable interrupt Non-maskable External hardware interrupts (lowest priority)
Interrupt Priority
Hardware, software and internal interrupts are serviced on a priority basis. The second level of priority is based on the type number:
0 highest p g priority y 255 lowest priority
If an interrupt service routine is running, p g, only devices with a higher priority are allowed to interrupt this service routine.
Interrupt Flag
Affects only external hardware interrupts interrupts. Does not and can not mask out the other 4 interrupt groups:
software interrupts non-maskable i t k bl interrupt t internal interrupt reset
OF DF IF TF SF ZF
Intel Reserved
2. IF & TF are cleared in the Status Register 3. New CS & IP are loaded from Vector Table location defined by n. (This results in an unconditional jump to CS:IP)
This results in the program continuing with the next instruction that would have occurred without an interrupt.
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 processed. Exception: An automatically generated trap in response to an exceptional condition such as division by zero.
32
Hardware Interrupts
Device Program
0 4 1 1
Interrupt table
3
Interrupt Code
2 5
7
Stack
33
36
Software Interrupt
Software interrupt. Software interrupts are something like a S ft i t t thi lik procedure call except the name of the procedure is not known to the caller caller. Works much the same way as hardware interrupts 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. d i d
37
Software Interrupt
In many respects interrupts are like procedure calls. Difference - In procedures, the address of the procedure i specified i th program. I i t d is ifi d in the In interrupts, t the address is specified in the interrupt vector table. In IBM design, the interrupt vector table is stored in design the lowest 1K of memory. Each entry is a 4 byte segment/offset address (That allows 100h = 256d address. entries.)
38
Interrupt Table
Stored in bytes numbered 0-1023 0 1023 256 four byte entries E t i are th segment/offset address of Entries the t/ ff t dd f the interrupt handler (the code used to process th t interrupt) that i t t)
39
Interrupts
0 , 2, 4, 6, 7 Processor and memory errors 1, 1 3 used by debuggers 5 print screen 8-0Fh Hardware interrupts ( p (IRQ0 IRQ7) ) 10h-20h Various BIOS interrupts 20h 33h, 3F-7F MS-DOS 34-3E Floating point emulation 80-F0 Reserved for ROM BASIC F1-FF F1 FF A il bl f application programs Available for li ti
40
Most interrupts have multiple functions and use AH to specify the desired operation (function)
41
I/O Model
Programs
Languages
BIOS
Hardware
42
I/O Model
DOS: Redirectable slow device Redirectable, slow, independent, easiest to use, treats I/O in a g generic fashion BIOS: Not redirectable, faster, device independent, provides much more control, understands video output Hardware: Not redirectable, fastest device redirectable fastest, dependent, hardest to use. Inappropriate use p can cause serious problems.
43
.code code main PROC ; set up data segment g mov ax, @data mov ds, ax ; print the string "Hello" Hello mov dx, OFFSET outBuffer Uses function 9h of interrupt mov ah, 9h 21h int 21h ; terminate exit mov ax, 4C00h C int 21h main ENDP 44 END main
45
Hardware Interrupts
Minimum-mode 8086 Minimum mode
ALE M/IO* /O RD* WR* 8086 MPU AD0-AD15 AD INTR INTA INTA* DT/R* DEN* External Hardware H d Interrupt Circuitry INT32 INT33 INT34 INT35
INT255
Hardware Interrupts
The microprocessor samples the INTR input signal during the last T state of each y instruction cycle. The INTRs active level (1) must be held y p until tested by the microprocessor. It must be removed before returning from p its Interrupt Service Routine, otherwise the same interrupt may get processed a second time.
Basic Features
8086 announced in 1978; 8086 is a 16 bit microprocessor with a 16 bit data bus p 8088 announced in 1979; 8088 is a 16 bit microprocessor with an 8 bit data bus Both manufactured using High-performance Metal Oxide Semiconductor (HMOS) technology Both contain about 29000 transistors Both are packaged in 40 pin dual-in-line package (DIP)
GND AD14 AD13 AD12 AD11 AD10 AD9 AD8 AD7 AD6 AD5 AD4 AD3 AD2 AD1 AD0 NMI INTR CLK GND
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
8086
40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21
VCC AD15 A16/S3 A17/S4 A18/S5 A19/S6 BHE/S7 MN/MX RD HOLD HLDA WR M/IO DT/R DEN ALE INTA TEST READY RESET
GND A14 A13 A12 A11 A10 A9 A8 AD7 AD6 AD5 AD4 AD3 AD2 AD1 AD0 NMI INTR CLK GND
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
8088
40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21
VCC A15 A16/S3 A17/S4 A18/S5 A19/S6 SS0 MN/MX RD HOLD HLDA WR IO/M DT/R DEN ALE INTA TEST READY RESET
8088
8086
8086
Maximum mode
Pull MN/MX logic 0 Larger systems with more than one processor (designed to be used when a coprocessor (8087) exists in the system)
8086
Vcc
8086
GND
Min Mode
Max Mode
+5V RES
Clock generator
AEN2 AEN1 F/C
Control Bus
Wait-State Generator
ALE
STB OE
8086 CPU C
8282 Latch
BHE
Wait-State Generator
ALE
8086 CPU C
STB OE
RES
READY
AD0-AD15 A16-A19
8282 Latch
T OE
8286 Transceiver
DATA
GND AD14 AD13 AD12 AD11 AD10 AD9 AD8 AD7 AD6 AD5 AD4 AD3 AD2 AD1 AD0 NMI INTR CLK GND
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
8086
40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21
VCC AD15 A16/S3 A17/S4 A18/S5 A19/S6 BHE/S7 MN/MX RD HOLD HLDA WR M/IO DT/R DEN ALE INTA TEST READY RESET
Vcc
GND AD14 AD13 AD12 AD11 AD10 AD9 AD8 AD7 AD6 AD5 AD4 AD3 AD2 AD1 AD0 NMI INTR CLK GND
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
8086
40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21
VCC AD15 A16/S3 A17/S4 A18/S5 A19/S6 BHE/S7 MN/MX RD RQ/GT0 RQ/GT1 LOCK S2 S1 S0 QS0 QS1 TEST READY RESET
GND
Min Mode
Max Mode
Function
Extra segment access Stack segment access Code segment access Data segment access
0 0 1 1
0 1 0 1