450 likes | 563 Views
Computer Science 3A Computer Organization and Machine Language Fall Semester 2013 Thursday, Sep 26, 2013 6:00 p.m. - 9:00 p.m. Aparna Subramanian Website: http://cs.saddleback.edu/~aparna/CS3A. 8086 Architecture Interrupts Instruction set Programs Debugging. Today’s class.
E N D
Computer Science 3A Computer Organization and Machine Language Fall Semester 2013 Thursday, Sep 26, 2013 6:00 p.m. - 9:00 p.m. Aparna Subramanian Website: http://cs.saddleback.edu/~aparna/CS3A
8086 Architecture • Interrupts • Instruction set • Programs • Debugging Today’s class
Interrupt - hardware interrupts - e.g. a key is pressed - calls INT 9h - or the mouse is moved - software interrupts - generated by a program - e.g. when input or output is required - calls INT 21h - a signal is sent to the CPU - stops the running program so that another action can be performed - or so that another subprogram can be executed - the address of the subprogram in stored in the Interrupt Vector Table - 1024 bytes in low memory
When an input from the keyboard is required by a program and a key is pressed: - the FLAGS register is saved - the interrupt flag is cleared - to prevent any other hardware interrupt - the CS and IP registers are saved - the address of the code for INT 9h is put in the CS and IP registers - hardware interrupts are enabled again - the INT 9h handler is executed - gets the scan code of the key pressed - tries to convert the scan code to the ASCII code of the character - stores the scan code and ASCII code in the keyboard buffer - the IP, CS and FLAGS registers are restored
To output a character: - INT 21h needs two pieces of information - the function number (02) in AH - the ASCII code of the character in DL - first, the program: MOV DL, 42 ; ‘B’ MOV AH, 02 INT 21 ; output ‘B’ INT 20 ; terminate the program
DEBUG Demo - Character Output To view the program: - use the debug command U (Unassemble) - the format: U address or U address, address -u 100, 107 23E6:0100 B242 MOV DL,42 23E6:0102 B402 MOV AH,02 23E6:0104 CD21 INT 21 23E6:0106 CD20 INT 20
Trace through execution: 23E6:0100 B242 MOV DL, 42 ;'B' Step 1: Move 42 (ASCII code for ‘B’) into DL register. -p AX=0000 BX=0000 CX=0000 DX=0042 SP=FFEE BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0102 NV UP EI PL NZ NA PO NC 23E6:0102 B402 MOV AH, 02
Trace through execution: 23E6:0100 B242 MOV DL, 42 ;'B' Step 1: Move 42 (ASCII code for ‘B’) into DL register. -p AX=0000 BX=0000 CX=0000 DX=0042 SP=FFEE BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0102 NV UP EI PL NZ NA PO NC 23E6:0102 B402 MOV AH, 02 Step 2: Move 2 (function for character output) into AH register. -p AX=0200 BX=0000 CX=0000 DX=0042 SP=FFEE BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0104 NV UP EI PL NZ NA PO NC 23E6:0104 CD21 INT 21 ;Display Character
Step 3: Call Interrupt 21h to print the character. Note the output before the listing of the contents of the AX register. -p B AX=0242 BX=0000 CX=0000 DX=0042 SP=FFEE BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0106 NV UP EI PL NZ NA PO NC 23E6:0106 CD20 INT 20
Step 3: Call Interrupt 21h to print the character. Note the output before the listing of the contents of the AX register. -p B AX=0242 BX=0000 CX=0000 DX=0042 SP=FFEE BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0106 NV UP EI PL NZ NA PO NC 23E6:0106 CD20 INT 20 Step 4: Terminate execution. -p Program terminated normally Exit DEBUG: -q
To output a string: - INT 21h needs two pieces of information - the function number (09) in AH - the offset of the string in DS:DX - the string must be terminated with a ‘$’ - the string must be stored in memory - the program: MOV DX, 0109 ; adress of the string ; immediately after the program MOV AH, 09 INT 21 ; output the string INT 20 ; terminate the program
To store the string in memory: - use the debug command E (Enter) - to enter one or more bytes directly into memory - the format: E address data, with a space between each byte -E 109 57 65 6C 63 6F 6D 65 20 74 6F 20 64 65 62 75 67 21 24 W e l c o m e t o d e b u g ! $
To display the memory: - use the debug command D (Display memory, or dump memory) - the format: D address, address -D 109, 11A 23E6:0100 57 65 6C 63 6F 6D 65 Welcome 23E6:0110 20 74 6F 20 64 65 62 75 67 21 24 to debug!$
DEBUG Demo - String Output View the program: -u 100, 108 23E6:0100 BA0901 MOV DX, 0109 23E6:0103 B409 MOV AH,09 23E6:0105 CD21 INT 21 23E6:0107 CD20 INT 20
Trace through execution: 23E6:0100 BA0901 MOV DX, 0109 Step 1: Move 0109 (address of string) into DX register. -p AX=0000 BX=0000 CX=0000 DX=0109 SP=FFEE BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0103 NV UP EI PL NZ NA PO NC 23E6:0103 B409 MOV AH, 09 Step 2: Move 9 (function for $-terminated string output) into AH register. -p AX=0900 BX=0000 CX=0000 DX=0109 SP=FFEE BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0105 NV UP EI PL NZ NA PO NC 23E6:0105 CD21 INT 21 ;Display String
Step 3: Call Interrupt 21h to print the string. Note the output before the listing of the contents of the AX register. -p Welcome to debug! AX=0924 BX=0000 CX=0000 DX=0109 SP=FFEE BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0107 NV UP EI PL NZ NA PO NC 23E6:0107 CD20 INT 20 Step 4: Terminate execution. -p Program terminated normally Exit DEBUG: -q
DEBUG Demo - Looping 23E6:0100 B90100 MOV CX,0001 ; initialize CX to 1 23E6:0103 B80000 MOV AX,0000 ; zero out accumulator 23E6:0106 01C8 ADD AX,CX ; add contents of CX to AX 23E6:0108 41 INC CX ; increment CX 23E6:0109 83F903 CMP CX,+03 ; is CX <= 3? 23E6:010C 76F8 JBE 0106 ; if yes, jump back to offset 106 23E6:010E CD20 INT 20 ; in no, terminate program
Step through the program 15 times (and show contents of registers): -pf AX=0000 BX=0000 CX=0001 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0103 NV UP EI PL NZ NA PO NC 23E6:0103 B80000 MOV AX,0000
Step through the program 15 times (and show contents of registers): -pf AX=0000 BX=0000 CX=0001 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0103 NV UP EI PL NZ NA PO NC 23E6:0103 B80000 MOV AX,0000 AX=0000 BX=0000 CX=0001 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0106 NV UP EI PL NZ NA PO NC 23E6:0106 03C1 ADD AX,CX
Step through the program 15 times (and show contents of registers): -pf AX=0000 BX=0000 CX=0001 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0103 NV UP EI PL NZ NA PO NC 23E6:0103 B80000 MOV AX,0000 AX=0000 BX=0000 CX=0001 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0106 NV UP EI PL NZ NA PO NC 23E6:0106 03C1 ADD AX,CX AX=0001 BX=0000 CX=0001 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0108 NV UP EI PL NZ NA PO NC 23E6:0108 41 INC CX
Step through the program 15 times (and show contents of registers): -pf AX=0000 BX=0000 CX=0001 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0103 NV UP EI PL NZ NA PO NC 23E6:0103 B80000 MOV AX,0000 AX=0000 BX=0000 CX=0001 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0106 NV UP EI PL NZ NA PO NC 23E6:0106 03C1 ADD AX,CX AX=0001 BX=0000 CX=0001 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0108 NV UP EI PL NZ NA PO NC 23E6:0108 41 INC CX AX=0001 BX=0000 CX=0002 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0109 NV UP EI PL NZ NA PO NC 23E6:0109 83F903 CMP CX,+03
Step through the program 15 times (and show contents of registers): -pf AX=0000 BX=0000 CX=0001 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0103 NV UP EI PL NZ NA PO NC 23E6:0103 B80000 MOV AX,0000 AX=0000 BX=0000 CX=0001 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0106 NV UP EI PL NZ NA PO NC 23E6:0106 03C1 ADD AX,CX AX=0001 BX=0000 CX=0001 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0108 NV UP EI PL NZ NA PO NC 23E6:0108 41 INC CX AX=0001 BX=0000 CX=0002 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0109 NV UP EI PL NZ NA PO NC 23E6:0109 83F903 CMP CX,+03 Note: if CX < 03 (destination < source), CMP sets the carry flag: AX=0001 BX=0000 CX=0002 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=010C NV UP EI NG NZ AC PE CY 23E6:010C 76F8 JBE 0106
AX=0001 BX=0000 CX=0002 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0106 NV UP EI NG NZ AC PE CY 23E6:0106 03C1 ADD AX,CX
AX=0001 BX=0000 CX=0002 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0106 NV UP EI NG NZ AC PE CY 23E6:0106 03C1 ADD AX,CX AX=0003 BX=0000 CX=0002 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0108 NV UP EI PL NZ NA PE NC 23E6:0108 41 INC CX
AX=0001 BX=0000 CX=0002 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0106 NV UP EI NG NZ AC PE CY 23E6:0106 03C1 ADD AX,CX AX=0003 BX=0000 CX=0002 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0108 NV UP EI PL NZ NA PE NC 23E6:0108 41 INC CX AX=0003 BX=0000 CX=0003 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0109 NV UP EI PL NZ NA PE NC 23E6:0109 83F903 CMP CX,+03
AX=0001 BX=0000 CX=0002 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0106 NV UP EI NG NZ AC PE CY 23E6:0106 03C1 ADD AX,CX AX=0003 BX=0000 CX=0002 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0108 NV UP EI PL NZ NA PE NC 23E6:0108 41 INC CX AX=0003 BX=0000 CX=0003 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0109 NV UP EI PL NZ NA PE NC 23E6:0109 83F903 CMP CX,+03 Note: if CX = 03 (destination = source), CMP sets the zero flag: AX=0003 BX=0000 CX=0003 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=010C NV UP EI PL ZR NA PE NC 23E6:010C 76F8 JBE 0106
AX=0001 BX=0000 CX=0002 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0106 NV UP EI NG NZ AC PE CY 23E6:0106 03C1 ADD AX,CX AX=0003 BX=0000 CX=0002 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0108 NV UP EI PL NZ NA PE NC 23E6:0108 41 INC CX AX=0003 BX=0000 CX=0003 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0109 NV UP EI PL NZ NA PE NC 23E6:0109 83F903 CMP CX,+03 Note: if CX = 03 (destination = source), CMP sets the zero flag: AX=0003 BX=0000 CX=0003 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=010C NV UP EI PL ZR NA PE NC 23E6:010C 76F8 JBE 0106 AX=0003 BX=0000 CX=0003 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0106 NV UP EI PL ZR NA PE NC 23E6:0106 03C1 ADD AX,CX
AX=0006 BX=0000 CX=0003 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0108 NV UP EI PL NZ NA PE NC 23E6:0108 41 INC CX
AX=0006 BX=0000 CX=0003 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0108 NV UP EI PL NZ NA PE NC 23E6:0108 41 INC CX AX=0006 BX=0000 CX=0004 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0109 NV UP EI PLNZ NA PO NC 23E6:0109 83F903 CMP CX,+03
AX=0006 BX=0000 CX=0003 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0108 NV UP EI PL NZ NA PE NC 23E6:0108 41 INC CX AX=0006 BX=0000 CX=0004 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0109 NV UP EI PL NZ NA PO NC 23E6:0109 83F903 CMP CX,+03 Note: if CX > 03 (destination > source), CMP does not set the carry flag or the zero flag: AX=0006 BX=0000 CX=0004 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=010C NV UP EI PLNZ NA PO NC 23E6:010C 76F8 JBE 0106
AX=0006 BX=0000 CX=0003 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0108 NV UP EI PL NZ NA PE NC 23E6:0108 41 INC CX AX=0006 BX=0000 CX=0004 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=0109 NV UP EI PL NZ NA PO NC 23E6:0109 83F903 CMP CX,+03 Note: if CX > 03 (destination > source), CMP does not set the carry flag or the zero flag: AX=0006 BX=0000 CX=0004 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=010C NV UP EI PL NZ NA PO NC 23E6:010C 76F8 JBE 0106 Only instruction left to be executed is INT 20 to terminate the program: AX=0006 BX=0000 CX=0004 DX=0000 SP=DB09 BP=0000 SI=0000 DI=0000 DS=23E6 ES=23E6 SS=23E6 CS=23E6 IP=010E NV UP EI PL NZ NA PO NC 23E6:010E CD20 INT 20
DEBUG Demo - Looping If we make some changes: 23E6:0100 B90100 MOV CX,0001 ; initialize CX to 1 23E6:0103 B80000 MOV AX,0000 ; zero out accumulator 23E6:0106 01C8 ADD AX,CX ; add contents of CX to AX 23E6:0108 41 INC CX ; increment CX 23E6:0109 83F903 CMP CX,+03 ; is CX <= 3? 23E6:010C 76F8 JBE 0106 ; if yes, jump back to offset 106 23E6:010E CD20 INT 20 ; in no, terminate program
DEBUG Demo - Looping If we make some changes: 23E6:0100 B90300 MOV CX,0003 ; initialize CX to 3 23E6:0103 B80000 MOV AX,0000 ; zero out accumulator 23E6:0106 01C8 ADD AX,CX ; add contents of CX to AX 23E6:0108 41 INC CX ; increment CX 23E6:0109 83F903 CMP CX,+03 ; is CX <= 3? 23E6:010C 76F8 JBE 0106 ; if yes, jump back to offset 106 23E6:010E CD20 INT 20 ; in no, terminate program
DEBUG Demo - Looping If we make some changes: 23E6:0100 B90300 MOV CX,0003 ; initialize CX to 3 23E6:0103 B80000 MOV AX,0000 ; zero out accumulator 23E6:0106 01C8 ADD AX,CX ; add contents of CX to AX 23E6:0108 49DEC CX ; decrement CX 23E6:0109 83F903 CMP CX,+03 ; is CX <= 3? 23E6:010C 76F8 JBE 0106 ; if yes, jump back to offset 106 23E6:010E CD20 INT 20 ; in no, terminate program
DEBUG Demo - Looping If we make some changes: 23E6:0100 B90300 MOV CX,0003 ; initialize CX to 3 23E6:0103 B80000 MOV AX,0000 ; zero out accumulator 23E6:0106 01C8 ADD AX,CX ; add contents of CX to AX 23E6:0108 49DEC CX ; decrement CX 23E6:0109 83F900 CMP CX,0 ; is CX > 0 23E6:010C 76F8 JBE 0106 ; if yes, jump back to offset 106 23E6:010E CD20 INT 20 ; in no, terminate program
DEBUG Demo - Looping If we make some changes: 23E6:0100 B90300 MOV CX,0003 ; initialize CX to 3 23E6:0103 B80000 MOV AX,0000 ; zero out accumulator 23E6:0106 01C8 ADD AX,CX ; add contents of CX to AX 23E6:0108 49DEC CX ; decrement CX 23E6:0109 83F900 CMP CX,0 ; is CX > 0 23E6:010C 77F8 JA 0106 ; if yes, jump back to offset 106 23E6:010E CD20 INT 20 ; in no, terminate program
DEBUG Demo - Looping If we make some changes: 23E6:0100 B90300 MOV CX,0003 ; initialize CX to 3 23E6:0103 B80000 MOV AX,0000 ; zero out accumulator 23E6:0106 01C8 ADD AX,CX ; add contents of CX to AX 23E6:0108 49DEC CX ; decrement CX 23E6:0109 83F900 CMP CX,0 ; is CX > 0 23E6:010C 77F8 JA 0106 ; if yes, jump back to offset 106 23E6:010E CD20 INT 20 ; in no, terminate program
DEBUG Demo - Looping 2 Using the LOOP instruction (works with CX): MOV CX ,0003 MOV AX, 0000 ADD AX, CX DEC CX CMP CX, 0000 JA 0106 INT 20 or LOOP 106
INTEL 8086 Instructions The following Intel assembly language instructions will be used in the DEBUG assignments and exercises: ADD ADD destination, source - adds source to destination and stores result in destination *destination, source may be: Register, Register Register, Memory location Register, Immediate value Memory location, Register Memory location, Immediate value CMP CMP destination, source *see above - compares destination to source by subtraction - works with conditional jumps ahead DEC DEC destination - subtracts 1 from destination INC INC destination - adds 1 to destination JMP JMP address - unconditional jump to specified address
INTEL 8086 Instructions Conditional jumps (used with CMP): JE jump if equal JNE jump if not equal JA jump if above JAE jump if above or equal JB jump if below JBE jump if below or equal LOOP LOOP address - subtracts 1 from CX and jumps to specified address if CX is not equal to 0 MOV MOV destination, source *see above - copies contents of source to destination SUB SUB destination, source *see above - subtracts source from destination and stores result in destination
Debug Commands A [address] - assemble C range address - compare D [range] - dump memory E address [list] - enter F range list - fill G [=address] [address...] - go H value1 value2 - hex I port - input from port L [address] [drive] [firstsector] [number] - load M range address - move N [pathname] [arglist] - name O port byte - output to port P [=address] [number] - program step Q - quit R [register] - register S range list - search T [=address] [value] - trace U [range] - unassemble V [range] - view source lines W [address [drive[ [firstsector] [number] - write allocate expanded memory XA [#pages] deallocate expanded memory XD [handle] map expanded memory pages XM [Lpage] [Ppage] [handle] display expanded memory status XS
Lab #8: Using Immediate Addressing for the operations: MOV AX, 0017 SUB AX, 0004 SUB AX, 0006 INT 20
Lab #8: Using Immediate Addressing for the operations, and using Direct Addressing to store the result in memory: MOV AX, 0017 SUB AX, 0004 SUB AX, 0006 MOV [120], AX INT 20
Lab #8: Using Direct Addressing for the whole program: MOV AX, [122] SUB AX, [124] SUB AX, [126] MOV [120], AX INT 20
8086 Architecture basics Review