home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / ASMVEG3.ZIP / ASMVEG3.TXT next >
Text File  |  1996-12-23  |  19KB  |  441 lines

  1.       Assembly Language for Veggies (And C programmers)      Part 3.
  2.  
  3. Welcome back to assembly for veggies....  Well I've said a lot about real
  4. simple stuff now.. perhaps it's time to provide you lot with some decent
  5. assorted infos?
  6.  
  7. Where to start is the BIG question.. there's a hell of a lot to know about your
  8. PC, and it's overall structure....  I said you'd have to get akin with the
  9. hardware, so why don't we leap into some hardware discussions this section
  10. (consider it a primer to bring you into a betterunderstanding - because you
  11. use the hardware directly quite a bit in ASM)..
  12.  
  13. I will leap in by talking a bit about the IBM machine...  (This could be a
  14. long story ;-)...)
  15.  
  16. The original IBM PC was based around the Intel 8088 CPU. CPU Stands for
  17. Central Processing Unit and is the "Brain" of the computer.. In fact, a
  18. computer can be correlated very well with the human body (thus the body is
  19. often referred to as a biological computer!!).
  20.  
  21. The CPU is equivilant to the human brain. It controlls everything else, and
  22. does MOST of the workload. The people who made the 8088 (Intel) soon
  23. progressed with the 80286, 386 and 486 chips which are Upward compatible with
  24. the 8088 - this means that they will correctly run anything made for an
  25. EARLIER version (IE 386 runs all 286, 8088 stuff too) of the CPU. Each new CPU
  26. added extra features, not found in earlier models, thus the later models are
  27. NOT Downward compatible. (OK the order here may seem wrong, but if you follow
  28. the logic that everything is compatible as you go up the chain (88 - 286 - 386
  29. -486), but not down the chain (486 - 386 - 286 - 88) you'll understand.
  30.  
  31. The CPU alone is the clever but, but put it on it's own and it won't do a
  32. lot...  you need to add INTERFACING around it. you also need to provide a
  33. source of POWER to run it, and give it a TASK to perform. Remember the
  34. highlighted words - we'll look at each of them now...
  35.  
  36. POWER : In human terms, we'd be looking at the digestive, lung - blood system.
  37. to simplify things, we say food goes in, gets converted into the right sor of
  38. energy to run the brain, piped there via the blood/veins and is uses by the
  39. brain in it's normal operation.
  40.  
  41. In computer terms we have a POWER SUPPLY - it takes mains voltage, converts it
  42. to the DC 5v and 12v required for the CPU and pipes it there via metal wires
  43. where it is used in the action of processing.
  44.  
  45. INTERFACING :  This topic is huge and basicly covers the rest of the body...
  46. it ill the input and output devices... there are many sub-systems here to be
  47. considered - for example the eyeball-brain connection is like the VGA-CPU
  48. connection and so forth.. I'll have a lot more to say about this lter... Oh,
  49. and it's often called I/O, but I don't like that term as it is easily confused
  50. with I/O external (serial, parallel ports etc) to the machine... the kind of
  51. interfacing I mean is between the CPU and the other components inside the
  52. case...
  53.  
  54. TASK : Well, you're always doing something - take breathing for example... so
  55. too the CPU must always be doing something - it's forever running a program of
  56. some sort - remember,  DOS or whatever is just a program to the CPU, and it
  57. makes no distinction between one program and another...  
  58.  
  59. So what do we have so far?  A CPU capable of processing instructions fed to
  60. it, and the unknown quantity known as Interfacing...
  61.  
  62. Ok...  well believe it or not the CPU is really pretty dumb...  it has a set
  63. of about 150 or so separate instructions that it recognises and can do things
  64. with..  It has no memory WHAT SO EVER!!!
  65.  
  66. As you know, it has some registers and that's it!  Pretty poor, eh? well
  67. that's because it's designed as a CONTROLLER... just as your muscles take
  68. instructions from the brain and actually do the work, so too the CPU tells
  69. it's "workers" what to do and that's what actually produces results...
  70.  
  71. Let's look at all the bits put in round the CPU to support it.
  72.  
  73. The original IBM Machine was designed around standard off-the-shelf chips. In
  74. fact you can take a bare IBM Motherboard (XT I mean), wander down to Dick
  75. Smith, Radio parts and maybe one other shop and buy all the bits (with one
  76. important exception) for no more than a dollar or two each - there's about 60
  77. of them to buy, and a complete set at today's prices is about $180. There's
  78. only 1 set of chips you can't get - because they contain the BIOS and have to
  79. be pre-programmed by IBM before you buy... 
  80.  
  81. Anyhow, I digress! The point is that everybody (indeed right thru to the 486
  82. machines) has remained compatible with this original design - thus anything
  83. you write to work with the basic setup will run on anything...
  84.  
  85. Here's what they put in:
  86.  
  87. RAM     up to 640k of RAM in 10 banks
  88. ROM     up to 64k in 1 bank
  89.  
  90. 8237    a chip that controls the DMA
  91. 8255    parallel I/O chip for miscellaneous control - NOT the printer ports!!
  92. 8253    programmable timer for assorted timing (including the clock!)
  93. 8259    interrupt controller for handling important hardware events
  94.  
  95. An exhaustive discussion of the actual functions of each chip would take YONKS
  96. so I'll leave it here for now...  suffice to say that that's the stuff that
  97. does all the basic I/O internal to the motherboard.... it is essential that
  98. all these bits work before the machine will fire up..
  99.  
  100. IBM Then added the expansion bus (wonderfull idea!!) and designed the video
  101. card, the serial card, the parallel and floppy disk drive cards. These used
  102. more garden variety chips that slotted into pre-allocated spaces inside the
  103. design of the PC.. This allocation of specic locations to specific parts of
  104. the PC was to be a bad shortcoming indeed (if they allowed us to relocate the
  105. hardware elswhere we'd be able to have as much as 768k of RAM to use in most
  106. cases, maybe even as much as 896k in good designs!!!).
  107.  
  108.  
  109.  
  110.                      BASIC PC MEMORY MAP
  111.  
  112.     FFFF
  113.  
  114.     F000   F000-FFFF System BIOS lives here (usually only top 8k used)
  115.  
  116.     E000   E000-EFFF Traditionally add on BIOS (Normally free)
  117.  
  118.     D000   D000-DFFF Traditionally add on BIOS (Normally free)
  119.            C800-CFFF Add on card useage (Usually XT Type HDD contolleer BIOS)
  120.     C000   C000-C7FF Add on card useage (Usually extra VGA bios)
  121.            B800-BFFF CGA Video RAM
  122.     B000   B000-B7FF MONO Video RAM
  123.            A000-AFFF EGA/VGA Video RAM (Graphics modes)
  124.     A000   --- 640k RAM ---  LIMIT of user RAM
  125.  
  126.     9000   --- 576k RAM
  127.  
  128.     8000   --- 512k RAM
  129.  
  130.     7000   --- 448k RAM
  131.  
  132.     6000   --- 384k RAM
  133.  
  134.     5000   --- 320k RAM
  135.  
  136.     4000   --- 256k RAM
  137.  
  138.     3000   --- 192k RAM
  139.  
  140.     2000   --- 128k RAM
  141.  
  142.     1000   --- 64k RAM
  143.  
  144.     0000   --- bottom of memory ---
  145.  
  146.  
  147. In these modern times, things like EMS make use sof the "empty" D and E
  148. blocks... You should be able to see that it is impossible to add more
  149. "conventional" memory becuse of the hard design that says you have to use the
  150. A block as VGA RAM (If you didn't have VGA you COULD add extra RAM on some
  151. sort of custom design card, hack DOS and use the extra 64k as normal RAM, but
  152. why bother!!)
  153.  
  154. It is now normal to reserve all of the A and B blocks for video RAM, the
  155. E000-E800 for video BIOS, the E800-EFFF block for XT Hard disk BIOS's, and the
  156. D & E blocks for EMS drivers. The F block is usually left alone, it's use
  157. bieng decided by the particular designers of any given motherboard (for
  158. example to save a wire, IBM actually mde their bios appear TWICE in memory -
  159. once from F000:FA00 - F000:FFFF, and once some 16k lower...  there was only
  160. one physical copy in the hardware, but because they left out a wire
  161. (apparently for simpstic design of the motherboard) it appears twice in the
  162. memory map. Many true-blue PC Programs relied on this fact (Flight simulator 1
  163. bieng the most memorable) by directly calling routines from their 16-lower
  164. address instead of using the correct method of going via the interrupts...
  165. clone makers didn't copy IBM's BUG and so the software would crash becaus the
  166. routines it wanted weren't there!!  (But soon the clone makers got wise and
  167. fixed that one up - by making the same bug as IBM!!!  one motherboard I saw
  168. physically had had the wire cut with a sharp knife (Effectively leaving it
  169. out) to make the board more compatible!!)
  170.  
  171. note that the 80xxx range of chips (after a reset or power on) always begin
  172. executing from address F000:FFF0 - thus the bios should always be present in
  173. that location!! normally there'd be a JMP instruction to lower down (seeing as
  174. that address is only 16 bytes from the end of the memory map!) in the BIOS
  175. where the initial self test & startup routines execute. The initial self test
  176. routines are often called the POST (Power on self test) - they initialize the
  177. video, floppy, hard disks, set up all the onboard 82xx chips and so on... then
  178. search for the first bootable device.
  179.  
  180. The first 64k of RAM is used in part by the 80xxx and the BIOS and by DOS...
  181.  
  182. 0000-0100 - 256 4 byte entries hold the actual addresses that each interrupt
  183. relates to - so if you want to know where interrupt 2F is pointing, look at
  184. the 4 bytes starting at offset (2Fh=47d  47x4=188d=BCh) 00BC...  (or use the
  185. DOS getintvector call!)
  186.  
  187. 0101-03FF - undoccumented mystery - probably used by BIOS internals or even
  188. free. 
  189.  
  190. 0400-04FF - BIOS Data table - holds data critical to normal working of BIOS
  191.  
  192. 0500-05FF - DOS Data table - holds critical data that BIOS and DOS use
  193.  
  194. 0600-7BFF - undoccumented mystery - apparently free or for later expansion.
  195.  
  196. 7C00-xxxx - When you boot up, the BIOS reads track 0, head 0, sector one of
  197. the first bootable disk into memory starting here at 0000:7C00 and hands
  198. control to 0000:7C00. Everything from this point on is dependant on what this
  199. boot track tells the machine to do. In DOS based systems, it loads the two
  200. hidden system files into RAM (thus starting DOS) or proclaims "non-system disk
  201. or disk error..."
  202.  
  203. There's also a similar I/O map for the hardware chips - the 80xxx series can
  204. access up to 64k worth of I/O ports, but the design of the IBM allows the use
  205. of only the first 400h (1024d) ports (which is heaps!!)
  206.  
  207.  
  208.                          I/O port Map
  209.  
  210. Port #           Use                  Chip Number used by IBM
  211. 000-00F       DMA Registers               8237
  212. 010-01F       AT Second DMA Chip          8237 #2
  213. 020-027       Interrupt controller        8259
  214. 028-03F       AT Interrupt controller     8259 #2
  215. 040-047       Timer chip                  8253
  216. 048-05F       AT Timer chip               8253 #2
  217. 060-06F       Parallel Chip (NOT LPTx!)   8255
  218. 070-07F       AT NMI Mask registers
  219. 080-083       DMA Page registers
  220. 084-09F       AT DMA Page registers
  221. 0Ax           Free
  222. 0Bx           Free
  223. 0Cx           Free
  224. 0Dx           Free
  225. 0Ex           Free
  226. 0Fx           Free
  227. 100-1FF       Due to a stuff up in IBM, all ports in the range 000-0FF appear
  228.               again in this range - they didn't decode address line A8!!
  229. 200-20F       Game (Joystick) D/A converters & fire buttons
  230. 210-217       XT Expansion unit
  231. 218-277       Free (soundblaster usually at 220h)
  232. 278-27F       LPT3:                       74LS273/74LS373
  233. 280-2E7       Free
  234. 2E8-2EF       COM4:                       8250
  235. 2F0-2F7       Free
  236. 2F8-2FF       COM2:                       8250
  237. 300-31F       Prototype card
  238. 320-32F       Hard disk use               WD10x00 MFM HDD Series Chipset
  239. 330-377       Free
  240. 378-37F       LPT2:                       74LS273/74LS373
  241. 380-38F       SDLF Communications Card (IBM Failure of a design!!)
  242. 390-39F       Free
  243. 3A0-3AF       Binary Synchronous Comms (Another IBM Failed design!)
  244. 3B0-3BB       MONO Video                  MC6845
  245. 3BC-3BF       LPT1:                       74LS273/74LS373
  246. 3C0-3CF       EGA/VGA Video
  247. 3D0-3DF       CGA Video                   MC6845
  248. 3E0-3E7       Free
  249. 3E8-3EF       COM3:                       8250
  250. 3F0-3F7       Floppy Disk controller      NEC uPD765c
  251. 3F8-3FF       COM1:                       8250
  252.  
  253. Basicly that's it!!  As I said before, an exhaustive referance to individual
  254. chips would be a large hassle!, not to mention hundreds of pages long!! So I
  255. won't bother!
  256.  
  257.  
  258.                       INTERRUPTS
  259.  
  260. Interrupts are the other big deal on the PC .. there's too many to list
  261. individually, so i'll cover them in blocks..
  262.  
  263. INT#    Made by          Use
  264.   0      80xxx    divide by Zero error
  265.   1      80xxx    single stepper
  266.   2      80xxx    NMI
  267.   3      80xxx    Breakpoint
  268.   4      80xxx    Overflow
  269.   5      BIOS     Printscreen (Why the fuk did they use 5!!!)
  270. 6,7      IBM      Reserved
  271.   8      8259     IRQ0 - Timer tick - timeclock basicly
  272.   9      8259     IRQ1 - Keypressed - calls BIOS readkey from 8048 KB CPU
  273.   A      8259     IRQ2 - EGA/VGA Vertical retrace, 8087 error
  274.   B      8259     IRQ3 - COM2: I/O done COM4: I/O done
  275.   C      8259     IRQ4 - COM1: I/O done COM3: I/O done
  276.   D      8259     IRQ5 - Hard disk data ready/needed
  277.   E      8259     IRQ6 - Floppy disk data ready/needed
  278.   F      8259     IRQ7 - Parallel printer, SoundBlaster loadx
  279.  
  280. 10 to
  281. 1F       BIOS     Bios routines, pointers etc.
  282.  
  283. 20 to
  284. 3F       DOS      DOS routines, pointers, drivers etc.
  285.  
  286. 40       HDD      HDD Puts original floppy INT13 call here
  287. 41       HDD      Points to table holding HDD head, cyl etc. data
  288.  
  289. 42       EGA/VGA  Puts original video routines here
  290. 43       EGA/VGA  Low 128 video char font pointer
  291. 44       EGA/VGA  6845 data for intializing a video mode
  292.  
  293. 45       ?        ?
  294.  
  295. 46       HDD      As 41, but for HDD #2
  296.  
  297. 47       ?        ?
  298.  
  299. 48       PCjr     Keyboard translate table pointer
  300. 49       PCjr     non keyboard translate table pointer!
  301. 4A       AT-BIOS  User alarm routine pointer
  302.  
  303. 4B to
  304. 5F       ?        DesqView redirects IRQ0-7 to int 50-57
  305.                   5C often used by networking software
  306.  
  307. 60 to
  308. 67       User     Free for user use - INT67 often software call for EMS drivers
  309.  
  310. 68 to
  311. 6F       ?        ?
  312.  
  313. 70       AT 8259  IRQ8 AT Real time Clock
  314. 71       AT 8259  IRQ9 AT LAN (often patched to IRQ2)
  315. 72       AT 8259  IRQA ?
  316. 73       AT 8259  IRQB ?
  317. 74       AT 8259  IRQC ?
  318. 75       AT 8259  IRQD AT 80287 Error (patched to IRQ2 for compatibility)
  319. 76       AT 8259  IRQE AT Hard disk (IDE types only)
  320. 77       AT 8259  IRQF ?
  321.  
  322. 78 to
  323. 7F       ?        ?
  324.  
  325. 80 to
  326. F0       BASIC    Used bu GWBASIC, BASICA, otherwise ?
  327.  
  328. F1 to
  329. FF       ?        ?
  330.  
  331. As you can see, some interrupts are used as simple xxxx:xxxx address pointers,
  332. some are for calling software routines, and some are generated by hardware and
  333. call a user routine automaticly..
  334.  
  335.  
  336.  
  337. SO! What does all this mean?  What it means is probably not a hell of a lot at
  338. this stage... in the future, you will find this is a valuable referance of low
  339. level tables when you come to nutting out a program that mysteriously performs
  340. OUT's, direct memory reads etc...  
  341.  
  342. For more in-depth instruction on the BIOS and dOS data areas, see norton's
  343. programmer's guide to the IBM PC (first ed. pages 51-58 - good reading!!)
  344.  
  345. to round things off, I'll finish with another small demo ASM program that
  346. shows off some more instructions...
  347.  
  348.  
  349. Ever needed to move memory about FAST?  how about fast reading from sequential
  350. memory locations (or indeed writing..)???   There's a set of instructions
  351. designed just for this. They are:
  352.  
  353. STOSB - write byte
  354. STOSW - write word
  355. LODSB - read byte
  356. LODSW - read word
  357. MOVSB - move byte
  358. MOVSW - move word
  359. *OUTSB - output byte to port
  360. *OUTSW - output word to port
  361. *INSB  - input byte from port
  362. *INSW  - input word from port
  363.  
  364. * = only executeable on 286 or above!
  365.  
  366. All of these routines can be looped a given number of times - place the number
  367. of repititions to be performed into the CX register and preface the
  368. instruction with the letters REP  - IE: REP MOVSB ... 
  369.  
  370. The data comes from DS:SI (for LOD or OUT types) and goes to ES:DI (for STO or
  371. OUT types). further, if you use OUT or IN instructions, they use the port in
  372. DX for their reading/writing.
  373.  
  374. further note that the instructions alter their assosiated pointer register!! -
  375. thus if you have SI=5312 and you do a LODS, atferwards SI will be one greater
  376. or lower, depending on the setting of the Direction flag! - the direction flag
  377. should always be set by you before using these instructions otherwise thids
  378. could go the wrong way!!  - for incrementing after read/writng, first execute
  379. a CLD (Clear direction flag) instruction. For decrement after Read/Write,
  380. first execute a STD (set direction flag) instruction. Note that a maximum of
  381. 64k can be moved in any one go, and that the segment registers are not altered
  382. as a result of an instruction, only the offset pointers, so a 20 byte read
  383. starting at 1000:FFFC will NOT read on into the next segment, but rather the
  384. last 7 bytes will come starting from 1000:0000!!!!!  this is a bad limitation
  385. of the instructions, so don't get caught out!!!
  386.  
  387. THESE ARE VERY POWERFULL INSTRUCTIONS!!!  ... let's say you wish to move a
  388. chunk of memory from 3C00:2745 to 3212:5678 (assume a length of 2c0 bytes)
  389.  
  390. this does it:
  391.  
  392. CLD
  393. MOV CX,02C0
  394.  
  395. MOV DS,3C00
  396. MOV SI,2745
  397.  
  398. MOV ES,3212
  399. MOV DI,5678
  400.  
  401. REP MOVSB
  402.  
  403.  
  404. Dead simple, eh? If one divides CX by two and uses REP MOVSW then one gets a
  405. 2x speed transfer on 16 bit machines!!
  406.  
  407. This is te method used to scroll display memory in my second VGA Demo - it's
  408. too slow to do smooth scrolling animation, but quite quick enough for simple
  409. memory moves. 
  410.  
  411. The actual speed can be calculated thus: 
  412.  
  413. Instructions are measured in clock ticks...  a clock tick is 1 divided by the
  414. CPU clock frequency...  On a 20MHz machine, each clock tick is 1/20million -
  415. about 0.05 of a microsecond!!!
  416.  
  417. Let's take a MOVSW instruction - this takes 1 tick to read the instruction, 1
  418. tick to process it, 2 ticks to read the word in, 1 tick to process, 2 ticks to
  419. write it out, along with 2 RAM refresh ticks in the middle..  a total of 9
  420. tics - so each MOVSB takes 9 x 0.05 uS = about 1/2 a microsecond....
  421.  
  422. A 64k move takes 65535x0.5uS = about 32.8 milliseconds!!!!
  423.  
  424. This translates into bieng able to move about 1.9MB in a second (At 20MHZ!!!!!)
  425. The old 4.77MHz XT did about 1/4,770,000=21uS/tick - 0.18mS/MOVSW - 64k moved
  426. in about eleven SECONDS!!!  (That's indirectly why a 4.77MHz XT 640 RAM Test
  427. [on bootup] takes about 3 minutes!)
  428.  
  429. The 386 and 486 are capable of running faster at the same clock speed because
  430. they takes less ticks to execute the same instruction (They only do 1 RAM
  431. refresh, and only take 1 tickto read or write a word) .. this is why a 286 and
  432. a 386 (NOT 386 SX!!) don't run at the same speed even though they have th same
  433. clock speed (all the instructions take less ticks on a 386 - not just the
  434. MOVSW!) 
  435.  
  436. If that isn't enough to keep your minds flowing, then I'll go he! It's
  437. midnight, I'm going to bed!  Until ASMVEG4, Cya's!!
  438.  
  439.                                                         .\\erlin
  440.  
  441.