home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Hack-Phreak Scene Programs
/
cleanhpvac.zip
/
cleanhpvac
/
TUTOR.ZIP
/
LESSON1
< prev
next >
Wrap
Text File
|
1995-02-15
|
21KB
|
372 lines
Lesson 1-Intel 8088, 80186, 80286,80386, 80486 Assembly Language Programming:
----------------------------------------------------------------------------
OVERVIEW:
The Intel microprocessors above plus the Pentium are all upward compatible.
That is, a program written for the 8088 will run any processor above it. We
shall start off by studying the first one in the family above, the 8088 and
progress later to the 80286.
The heart of a microcomputer is the microprocessor called the 'central
processing unit,' the CPU. A microcomputer block diagram is below.
┌──────┐
│ │<--------- address bus ---------------
│ │ | |
│ │ | |
│ 8088 │ v v keyboard in
│ │ ----- memory ----- --- I/O --- video out
│ CPU │ ^ ^ ^ ^ port I/Os
│ │ | | | |
│ │ v | v |
│ │<------------------ data bus ------------>|
│ │ | |
│ │ v |
│ │<----------------- control bus ------------
└──────┘
The 8088 has 14 general registers of 16 bits each as illustrated below.
Eight bits = a byte and 2 bytes = a word. Each of the first four 16 bit
registers are divided into eight 8 bit registers also. Case may be ignored.
Count zero as 1.
DATA REGISTERS
15 8 7 0
AX |---ah---|---al---|
BX |---bh---|---bl---| (BX also is an index register)
CX |---ch---|---cl---|
DX |---dh---|---dl---|
POINTER & INDEX REGISTERS
15 0
SP |----------------| stack pointer
BP |----------------| base pointer
SI |----------------| source index
DI |----------------| destination index
SEGMENT REGISTERS
15 0
CS |----------------| code segment
DS |----------------| data segment
SS |----------------| stack segment
ES |----------------| extra segment
INSTRUCTION POINTER & FLAGS
15 0
IP |----------------| instruction point
F |----ODITSZ-A-P-C| flags register
Please read pages 1, 2 and 3 of the iAPX 8088 User Manual I am sent you.
Let us skip theory for a while and write and assemble and run a real working
assembly language program.
We will write our program using an EDITOR. We will use EDLIN.COM modified to
run on any version of the DOS operating system since it has line numbers that
make it easier for us to refer to it. What it creates is called SOURCE code.
We will assemble our source code using the A86 assembler. It translates the
source code into machine code the the CPU understands. It is the program that
we will RUN and it is called a .COM file. If our source code were named
DEMO1.ASM, after assembly it would be named DEMO1.COM, the program to run.
Our first program will be named DEMO1.ASM which we will create using
EDLIN.COM. Type EDLIN DEMO1.ASM and press enter. Then press I1 (insert line
1) and press enter. Now type in the following lines. If you make a mistake,
use the backspace key to backup and erase. Press enter when done with each
line. To escape from input press Ctrl C. Then press L to list, or I line
number to Insert a new line, or press P line number to display 23 lines
starting with the line number. Ctrl C will exit off a line number and await
a command. Then typing in the line number and pressing enter will take you
to it for further edit. the Insert key will insert until it is pressed
again. The Delete key will delete the character above the cursor. When all
done, press Ctrl C and enter, then E for end. You are all done!
Read the DOS 5.0 User's Manual for more EDLIN functions.
DEMO1.ASM:
1: mov ax,3 ;rest video to text mode
2: int 10h ;= clear the screen (CLS)
3: mov es,0b800h ;set es to text video segment
4: mov si,message ;message to display
5: mov di,1760 ;display at line 11 on video
6: mov ah,7 ;medium white color
7:nex1: mov al,cs:[si] ;byte from code seg message
8: inc si ;message +1
9: cmp al,0 ;end of message ?
10: jz done ;if so, jump to done
11: mov es:[di],ax ;ax is a 2 byte word - move byte+color to video
12: add di,2 ;next video word address
13: jmp nex1 ;do next video
14:done: mov ah,0 ;wait for any key pressed
15: int 16h ;keyboard interrupt
16: mov ax,4c00h ;interrupt 21h exit instruct.
17: int 21h ;return to DOS> ready prompt
18:message:
19:db 'Captain Russell is now in command of the Starship Enterprise.',0
Now we are ready to assemble the DEMO1.ASM source code. Type:
A86 +LS DEMO1.ASM and press enter
Eureka! In a fraction of a second the A86 assembler created DEMO1.COM.
To run it type DEMO1.COM and press enter. To return to the DOS> ready prompt,
press any key.
We have frivolously skipped over a great many fundamentals we will cover
later, but at least we have discussed the registers available and created a
real honest to goodness working assembly language program that required only
19 lines of source code. A considerable accomplishment for our first program.
Wait a minute! The cursor was still blinking when we ran DEMO1COM. How may
turn it off? Ok, let us CALL the following routine that will turn it off
or on depending whether we call curson or cursof.
A CALL means to push the instruction pointer on the stack, go to the called
name, do whatever it says and then ret (return to the next instruction after
the call). The instruction pointer always has the address of the next
instruction in it.
curson: mov ah,1 ;set cursor type
mov cx,0607h ;cursor normal type
int 10h ;bios video interrupt
mov dx,0 ;set video top left position
jmp >c1 ;> = forward jump
cursof: mov dx,1900h ;cursor out of view on text page
c1: mov ah,2 ;set cursor position
mov bh,0 ;page zero in text mode
int 10h ;bios video interrupt
ret ;return to call + next instruction
Bios means the computer's Basic Input Output System code instructions that
DOS (the Disk Operating System) loads whenever the computer is turned on.
Very good. Now let us add the above code to our 19 line program.
DEMO2.ASM
1: mov ax,3 ;rest video to text mode
2: int 10h ;= clear the screen (CLS)
3: mov es,0b800h ;set es to text video segment
4: call cursof ;turn off blinking cursor
5: mov si,message ;message to display
6: mov di,1760 ;display at line 11 on video
7: mov ah,7 ;medium white color
8:nex1: mov al,cs:[si] ;byte from code seg message
9: inc si ;message +1
10: cmp al,0 ;end of message ?
11: jz done ;if so, jump to done
12: mov es:[di],ax ;ax is a 2 byte word - move byte+color to video
13: add di,2 ;next video word address
14: jmp nex1 ;do next video
15:done: mov ah,0 ;wait for any key pressed
16: int 16h ;keyboard interrupt
17: call curson ;turn blinking cursor back on
18: mov ax,4c00h ;interrupt 21h exit instruct.
19: int 21h ;return to DOS> ready prompt
20:message:
21:db 'Captain Russell is now in command of the Starship Enterprise.',0
22:curson: mov ah,1 ;set cursor type
23: mov cx,0607h ;cursor normal type
24: int 10h ;bios video interrupt
25: mov dx,0 ;set video top left
26: jmp >c1 ;> = forward jump
27:cursof: mov dx,1900h ;cursor out of view
28:c1: mov ah,2 ;set cursor position
29: mov bh,0 ;page zero in text mode
30: int 10h ;bios video interrupt
31: ret ;return to call + next
Before we display the message, line 4 above, tells the program to turn off
the blinking cursor. Then, right after line 16 waits for a key press, line
17 turns the blinking cursor back on.
Since we frivolously skipped over a great many fundamentals to get our first
programs running, let us take a step back and define a few of them before we
go on.
.COM PROGRAMS:
When a .COM program is first loaded it sets the CS, DS and ES segment
registers to the program's location in memory and the SP (stack pointer
to the top end of that segment. A segment is 65,536 bytes long and your
computer may access any of the 16 segments. A .COM program always resides
in only a single segment which is usually more than enough for most purposes,
but it may switch to and use ANY segment for data and video, or jump far to
a distant segment and use its code there and when desired, jump back to its
original segment. More later when we include EDMOD.COM (edit/modify any all
of memory) and then return to our program as one of the options our program
offers.
INTERRUPTS:
The first 1024 byte page of segment zero may contain up to 256 different
4 byte interrupt addresses. These 4 bytes contain a 2 byte word with the
segment of the interrupt, plus a 2 byte word of the address of the interrupt
within that segment. They are usually located in high memory above the ninth
segment. Interrupts perform many functions including keyboard input, video
output, printer output and many more too numerous to mention. Interrupt
numbers are usually given in hexadecimal rather than decimal.
HEXADECIMAL & BINARY:
Hex is simply another way of counting that is more convenient than decimal
when using computers that all have 8 bit bytes and 16 bit words. Here is an
equivalent table illustrating decimal, hex and binary equivalents for a few
decimal numbers between 1 and 65535. The binary equivalents' right hand bit
is bit 0 and the lefthand bit = bit 15 of the 2 bytes (= word) shown.
DECIMAL HEXADECIMAL BINARY
word word 2nd byte 1st byte
0 0000h 00000000 00000000b
1 0001h 00000000 00000001b
2 0002h 00000000 00000010b
3 0003h 00000000 00000011b
4 0004h 00000000 00000100b
5 0005h 00000000 00000101b
6 0006h 00000000 00000110b
7 0007h 00000000 00000111b
8 0008h 00000000 00001000b
9 0009h 00000000 00001001b
10 000ah 00000000 00001010b
11 000bh 00000000 00001011b
12 000ch 00000000 00001100b
13 000dh 00000000 00001101b
14 000eh 00000000 00001110b
15 000fh 00000000 00001111b
16 0010h 00000000 00010000b
32 0020h 00000000 00100000b
64 0040h 00000000 01000000b
128 0080h 00000000 10000000b
255 00ffh 00000000 11111111b
256 0100h 00000001 00000000b
65535 ffffh 11111111 11111111b
65536 10000h 1 00000000 00000000b
Here is an easy way to convert from decimal to hex and hex to decimal using
the little table that follows.
-------------------------------------------------------------------
| HEX DEC | HEX DEC | HEX DEC | HEX DEC |
-------------------------------------------------------------------
| 0 0 | 0 0 | 0 0 | 0 0 |
| 1 4,096 | 1 256 | 1 16 | 1 1 |
| 2 8,192 | 2 512 | 2 32 | 2 2 |
| 3 12,288 | 3 768 | 3 48 | 3 3 |
| 4 16,384 | 4 1,024 | 4 64 | 4 4 |
| 5 20,480 | 5 1,280 | 5 80 | 5 5 |
| 6 24,576 | 6 1,536 | 6 96 | 6 6 |
| 7 28,672 | 7 1,792 | 7 112 | 7 7 |
| 8 32,768 | 8 2,048 | 8 128 | 8 8 |
| 9 36,864 | 9 2,304 | 9 144 | 9 9 |
| A 40,960 | A 2,560 | A 160 | A 10 |
| B 45,056 | B 2,816 | B 176 | B 11 |
| C 49,152 | C 3,072 | C 192 | C 12 |
| D 53,248 | D 3,328 | D 208 | D 13 |
| E 57,344 | E 3,584 | E 224 | E 14 |
| F 61,440 | F 3,840 | F 240 | F 15 |
-------------------------------------------------------------------
USING THE ABOVE TABLE:
Assume we wish to convert 1111h to decimal. Then take the 1's equivalents
from left to right and add them up. 4096
+ 256
+ 16
+ 1
-----
= 4369 decimal
Converting binary numbers to hex is even easier. Just take the binary number
and divide it into groups of 4 as below and you have got it:
binary number = 1111 1111 1111 1111
hex = F F F F
Enclosed is a printed out table of equivalents for decimal, binary, hex and
ASCII (for IBM compatibles) that I keep handy on my desk in a clear plastic
cover for reference. ASCII (American Standard Code For Information Inter-
change) is the character displayed on video when that byte value is loaded
into video memory in text mode. The values below 32 decimal are mainly
printer control codes so their character equivalent is not printed out. I
have inked in these characters on the printed out version mailed to you.
The table is illustrated below.
IBM - ASCII - DECIMAL - BINARY - HEX - TABLE
0 00000000 00H @ 64 01000000 40H Ç 128 10000000 80H └ 192 11000000 C0H
1 00000001 01H A 65 01000001 41H ü 129 10000001 81H ┴ 193 11000001 C1H
2 00000010 02H B 66 01000010 42H é 130 10000010 82H ┬ 194 11000010 C2H
3 00000011 03H C 67 01000011 43H â 131 10000011 83H ├ 195 11000011 C3H
4 00000100 04H D 68 01000100 44H ä 132 10000100 84H ─ 196 11000100 C4H
5 00000101 05H E 69 01000101 45H à 133 10000101 85H ┼ 197 11000101 C5H
6 00000110 06H F 70 01000110 46H å 134 10000110 86H ╞ 198 11000110 C6H
7 00000111 07H G 71 01000111 47H ç 135 10000111 87H ╟ 199 11000111 C7H
8 00001000 08H H 72 01001000 48H ê 136 10001000 88H ╚ 200 11001000 C8H
9 00001001 09H I 73 01001001 49H ë 137 10001001 89H ╔ 201 11001001 C9H
10 00001010 0AH J 74 01001010 4AH è 138 10001010 8AH ╩ 202 11001010 CAH
11 00001011 0BH K 75 01001011 4BH ï 139 10001011 8BH ╦ 203 11001011 CBH
12 00001100 0CH L 76 01001100 4CH î 140 10001100 8CH ╠ 204 11001100 CCH
13 00001101 0DH M 77 01001101 4DH ì 141 10001101 8DH ═ 205 11001101 CDH
14 00001110 0EH N 78 01001110 4EH Ä 142 10001110 8EH ╬ 206 11001110 CEH
15 00001111 0FH O 79 01001111 4FH Å 143 10001111 8FH ╧ 207 11001111 CFH
16 00010000 10H P 80 01010000 50H É 144 10010000 90H ╨ 208 11010000 D0H
17 00010001 11H Q 81 01010001 51H æ 145 10010001 91H ╤ 209 11010001 D1H
18 00010010 12H R 82 01010010 52H Æ 146 10010010 92H ╥ 210 11010010 D2H
19 00010011 13H S 83 01010011 53H ô 147 10010011 93H ╙ 211 11010011 D3H
20 00010100 14H T 84 01010100 54H ö 148 10010100 94H ╘ 212 11010100 D4H
21 00010101 15H U 85 01010101 55H ò 149 10010101 95H ╒ 213 11010101 D5H
22 00010110 16H V 86 01010110 56H û 150 10010110 96H ╓ 214 11010110 D6H
23 00010111 17H W 87 01010111 57H ù 151 10010111 97H ╫ 215 11010111 D7H
24 00011000 18H X 88 01011000 58H ÿ 152 10011000 98H ╪ 216 11011000 D8H
25 00011001 19H Y 89 01011001 59H Ö 153 10011001 99H ┘ 217 11011001 D9H
26 00011010 1AH Z 90 01011010 5AH Ü 154 10011010 9AH ┌ 218 11011010 DAH
27 00011011 1BH [ 91 01011011 5BH ¢ 155 10011011 9BH █ 219 11011011 DBH
28 00011100 1CH \ 92 01011100 5CH £ 156 10011100 9CH ▄ 220 11011100 DCH
29 00011101 1DH ] 93 01011101 5DH ¥ 157 10011101 9DH ▌ 221 11011101 DDH
30 00011110 1EH ^ 94 01011110 5EH ₧ 158 10011110 9EH ▐ 222 11011110 DEH
31 00011111 1FH _ 95 01011111 5FH ƒ 159 10011111 9FH ▀ 223 11011111 DFH
32 00100000 20H ` 96 01100000 60H á 160 10100000 A0H α 224 11100000 E0H
! 33 00100001 21H a 97 01100001 61H í 161 10100001 A1H ß 225 11100001 E1H
" 34 00100010 22H b 98 01100010 62H ó 162 10100010 A2H Γ 226 11100010 E2H
# 35 00100011 23H c 99 01100011 63H ú 163 10100011 A3H π 227 11100011 E3H
$ 36 00100100 24H d 100 01100100 64H ñ 164 10100100 A4H Σ 228 11100100 E4H
% 37 00100101 25H e 101 01100101 65H Ñ 165 10100101 A5H σ 229 11100101 E5H
& 38 00100110 26H f 102 01100110 66H ª 166 10100110 A6H µ 230 11100110 E6H
' 39 00100111 27H g 103 01100111 67H º 167 10100111 A7H τ 231 11100111 E7H
( 40 00101000 28H h 104 01101000 68H ¿ 168 10101000 A8H Φ 232 11101000 E8H
) 41 00101001 29H i 105 01101001 69H ⌐ 169 10101001 A9H Θ 233 11101001 E9H
* 42 00101010 2AH j 106 01101010 6AH ¬ 170 10101010 AAH Ω 234 11101010 EAH
+ 43 00101011 2BH k 107 01101011 6BH ½ 171 10101011 ABH δ 235 11101011 EBH
, 44 00101100 2CH l 108 01101100 6CH ¼ 172 10101100 ACH ∞ 236 11101100 ECH
- 45 00101101 2DH m 109 01101101 6DH ¡ 173 10101101 ADH φ 237 11101101 EDH
. 46 00101110 2EH n 110 01101110 6EH « 174 10101110 AEH ε 238 11101110 EEH
/ 47 00101111 2FH o 111 01101111 6FH » 175 10101111 AFH ∩ 239 11101111 EFH
0 48 00110000 30H p 112 01110000 70H ░ 176 10110000 B0H ≡ 240 11110000 F0H
1 49 00110001 31H q 113 01110001 71H ▒ 177 10110001 B1H ± 241 11110001 F1H
2 50 00110010 32H r 114 01110010 72H ▓ 178 10110010 B2H ≥ 242 11110010 F2H
3 51 00110011 33H s 115 01110011 73H │ 179 10110011 B3H ≤ 243 11110011 F3H
4 52 00110100 34H t 116 01110100 74H ┤ 180 10110100 B4H ⌠ 244 11110100 F4H
5 53 00110101 35H u 117 01110101 75H ╡ 181 10110101 B5H ⌡ 245 11110101 F5H
6 54 00110110 36H v 118 01110110 76H ╢ 182 10110110 B6H ÷ 246 11110110 F6H
7 55 00110111 37H w 119 01110111 77H ╖ 183 10110111 B7H ≈ 247 11110111 F7H
8 56 00111000 38H x 120 01111000 78H ╕ 184 10111000 B8H ° 248 11111000 F8H
9 57 00111001 39H y 121 01111001 79H ╣ 185 10111001 B9H ∙ 249 11111001 F9H
: 58 00111010 3AH z 122 01111010 7AH ║ 186 10111010 BAH · 250 11111010 FAH
; 59 00111011 3BH { 123 01111011 7BH ╗ 187 10111011 BBH √ 251 11111011 FBH
< 60 00111100 3CH | 124 01111100 7CH ╝ 188 10111100 BCH ⁿ 252 11111100 FCH
= 61 00111101 3DH } 125 01111101 7DH ╜ 189 10111101 BDH ² 253 11111101 FDH
> 62 00111110 3EH ~ 126 01111110 7EH ╛ 190 10111110 BEH ■ 254 11111110 FEH
? 63 00111111 3FH 127 01111111 7FH ┐ 191 10111111 BFH 255 11111111 FFH
We sure have covered a lot of territory in this first week's curriculum. It
should give you a slight inkling of what assembly language is all about.
Since our classrooms are about 1500 miles apart, I have included about 30
lines at the end of the quiz for you to ask questions. Feel free to add as
many more questions as you wish. I will e-mail you the answers to your
questions the day after I receive them.
You now have the Intel microprocessor 8088 User Manual I have ordered for
you.
Love, Grandpa