home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / CODBRK3.ZIP / cb0204.txt < prev    next >
Text File  |  1998-03-25  |  12KB  |  310 lines

  1.  
  2.                             Back To The Basics
  3.                                 by SPo0ky       
  4.  
  5.  
  6.     I wrote this tutorial because some beginners who read the first two
  7.   editions of our magazine told us that they have problems understanding the
  8.   basics of assembly... In this tutorial I'll not try to teach you any virus
  9.   techniques, I'll only try to explain the most basic things of assembly like
  10.   how tasm/tlink work, registers, memory, interrupts,... as simple as
  11.   possible.
  12.   Maybe this sounds boring to you but if you want to code your own viruses
  13.   you have to understand the basics.
  14.   Also this article will not fully teach you assembly! It will only help you
  15.   to understand the basics. To understand assembly you will need at least a
  16.   few weeks with good training (-programming). I also suggest you to buy a
  17.   good book about Assembler and to read many many many virus source codes
  18.   (Thats the way I used to learn Assembler).
  19.   (Also - If you don't have a bookstore in your town you can use the online
  20.    bookstore at http://intertain.com, they have many great books, and they
  21.    are fast and cheap!)
  22.  
  23.  
  24.  1. Why Assembler?
  25.  
  26.     There are some pros and cons why you should (should not) use Assembler.
  27.   Contrary to HLL (High Level Languages), like Pascal, Basic or C++, in
  28.   Assembler you have to tell the CPU each step it has to execute,
  29.   which means that to write a big (complex) Assembler program is very time
  30.   consuming. Thats why most of the time, big programs are not completely
  31.   written in Assembler but Assembler parts are included in HLL-programs.
  32.   Another con of Assembler is that programs can not be used on other brands of
  33.   CPUs which they were written on because each brand of CPU has another
  34.   instruction set (We will use the 80x86 instruction set, which is used in
  35.   the IBM-PC and compatibles), but this gives you the possibility to optimize
  36.   the code for one specific CPU so that you can use all of its capabilities.
  37.   The result is extremely FAST and SMALL code.
  38.  
  39.  
  40.  2. A simple assembly program
  41.  
  42.     Lets start with a short Assembler program. You don't have to understand
  43.   what each instruction is used for now, I'll explain that later.
  44.   Just type this program into an ascii editor (-edit.com) and save it as
  45.   example.asm.
  46.  
  47.     .model tiny
  48.     .code
  49.       org 100h
  50.  
  51.     start:
  52.       mov ah,9h
  53.       mov dx,offset message
  54.       int 21h
  55.  
  56.       mov ax,4c00h
  57.       int 21h
  58.  
  59.       message   db  'CodeBreakers Rule! ;-)',10,13,'$'
  60.     end start
  61.  
  62.  
  63.  3. Assembler and Linker (TASM and TLINK)
  64.  
  65.     Before I continue I'll show you how to use TASM and TLINK to compile such
  66.   a file to an EXE or COM program.
  67.  
  68.   After you saved the above program into a file (example.asm) you can type
  69.  
  70.     TASM EXAMPLE1.ASM
  71.  
  72.   This will generate a so called OBJECT-FILE named EXAMPLE.OBJ.
  73.   Generally this file contains only information for the linker and a
  74.   translated version of the above code into binary (machine code).
  75.  
  76.     Example:
  77.  
  78.       MOV AH,9h
  79.  
  80.     would be translated into 1011 0100 0000 1001
  81.  
  82.   This Object file is not executable yet, to make an executable file (COM or
  83.   EXE) we have to use a LINKER (TLINK).
  84.   This linker will stick one or more object files together to one executable
  85.   file.
  86.   To link the example1.obj file you just type:
  87.  
  88.     TLINK /t EXAMPLE1.OBJ
  89.  
  90.   The /t switch tells the linker that it should produce a COM file, if you
  91.   leave /t away you will get an EXE file. Anyway, now you should have a ready
  92.   to run COM file... Just type EXAMPLE to start it!
  93.  
  94.  
  95.  4. Registers
  96.  
  97.     Registers are extremely fast accessible memory cells in the CPU, they are
  98.   used to address memory, to give instructions to the CPU,... generally they
  99.   are used to store "values".
  100.   All registers can store 16 bits (= 2 bytes) of data and some registers can
  101.   be split into two 8 bit (= 1 byte) registers.
  102.  
  103.    Registers of the 8086 CPU's:
  104.  
  105.      +--------------------+
  106.      | AH  |  AL  >    AX |  -> Accumulator Register
  107.      | BH  |  BL  >    BX |  -> Base Register
  108.      | CH  |  CL  >    CX |  -> Count Register
  109.      | DH  |  DL  >    DX |  -> Data Register
  110.      +-----+--------------+
  111.      |         SI         |  -> Source Index
  112.      |         DI         |  -> Destination Index
  113.      +--------------------+
  114.      |         BP         |  -> Base Pointer
  115.      |         SP         |  -> Stack Pointer
  116.      +--------------------+
  117.      |         CS         |  -> Code Segment
  118.      |         DS         |  -> Data Segment
  119.      |         ES         |  -> Extra Segment
  120.      |         SS         |  -> Stack Segment
  121.      +--------------------+
  122.      |         IP         |  -> Instruction Pointer
  123.      +--------------------+
  124.      |         F          |  -> Flag-Register
  125.      +--------------------+
  126.  
  127.    Only the registers AX, BX, CX and DX can be split into two parts:
  128.    AH, AL, BH, BL, CH, CL, DH, DL. Each of them has only 8 bits instead of
  129.    16!
  130.  
  131.    BTW -  8 bits are called a BYTE
  132.          16 bits are called a WORD
  133.          AH, AL, BH, BL, and so on, are called byte register and all others
  134.          (AX, BX, CX, DX,...) are called WORD registers.
  135.  
  136.  
  137.  5. MOV(e)
  138.  
  139.     Assembler (or the CPU) provides many functions which allow you to
  140.   manipulate (to change) the data stored in a register. One of the most
  141.   important instructions used to manipulate a register is MOV.
  142.   Look back to our example program... we used MOV 3 times (MOV AH,9 /
  143.   MOV DX, OFFSET MESSAGE / MOV AX,4C00H).
  144.   You can change the data in a register by using: MOV <REG>, <DATA>. Where
  145.   <REG> is the 16 or 8 bit register you want to change, and <DATA> is the
  146.   data you wan to store in the register. 
  147.  
  148.     But MOV can't be only used to change the data of registers, it can also
  149.   be used to change data stored at a certain location in MEMORY.
  150.  
  151.  6. MEMORY
  152.  
  153.     When you execute our little example program it just displays text... now,
  154.   how can the computer know WHICH text it should display? Again, look at the
  155.   example programs source code:
  156.  
  157.     MOV AH, 9
  158.     MOV DX, OFFSET MESSAGE
  159.     INT 21H
  160.     .
  161.     .
  162.     MESSAGE DB 'Some text...$'
  163.  
  164.     The first line is used to tell the CPU that it should display text
  165.   (function 9 in AH is used to display text). And the second line tells the
  166.   CPU where it has to look for the text in memory. 
  167.   Each byte in memory gets a number (an address), so the CPU knows exactly
  168.   which byte it has to read/write.
  169.   In the second line, "OFFSET MESSAGE" would return the address where the
  170.   MESSAGE is stored in memory and store it in DX (To display text MS-DOS
  171.   requires that the address of the text is stored in the register DX!).
  172.  
  173.  Some examples:
  174.  
  175.   Let's say this is our memory:
  176.  
  177.  Offset:   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16 
  178.  Data:   | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P |
  179.  
  180.   We want to get the data which is stored at offset 6 into register AH.
  181.   Which instruction would be used?
  182.  
  183.   -> MOV AH, [6]
  184.  
  185.   This would put the data at offset 6 (= 'F') into register AH. The '[' and
  186.   ']' are very important, If you forget them it would put the number 6 into
  187.   AH instead of 'F'!
  188.  
  189.   Remember, AH is a 8 bit register (-> It can store only 1 byte or 8 bit),
  190.   what would happen if we'd use AX (which is a 16 bit register) instead of
  191.   AH?
  192.  
  193.   -> MOV AX, [6]
  194.  
  195.   AX would become 'GF'. Yes, not 'FG'! In the x86's everything you read from
  196.   memory into a word register is turned around! (Thats not that important for
  197.   you yet, but you should know it anyway...)
  198.  
  199.  7. Interrupts
  200.  
  201.     I needed a very long time until I found a (hopefully) good way to explain
  202.   interrupts to a newbie! Finally I decided to use a simple example, the
  203.   MS-DOS Prompt.
  204.   When you are at the MS-DOS prompt you can enter commands, after you press
  205.   RETURN the command gets executed. You could compare the pressing of the
  206.   RETURN key with an interrupt. In assembler you fill the registers with
  207.   values, then you execute the interrupt. The interrupt code would then
  208.   evaluate the values you put into the register, it would decide which
  209.   function it should execute, .... and finally it would return the results
  210.   (in a register, on the screen or on your hdd,...)
  211.  
  212.     Ex.:
  213.       MOV AH,9
  214.       MOV DX,OFFSET MESSAGE
  215.       INT 21H
  216.  
  217.     The first two lines of this example have already been explained above,
  218.   the 3rd line would execute an interrupt, interrupt 21h(ex).
  219.   Interrupts are numbered from 0 to FFh, each interrupt provides other types
  220.   of 'services'. Like
  221.     INT 21H, this is the MS-DOS interrupt, it provides basic DOS functions,
  222.      like input/output of text, file functions (like open, read, write to
  223.      files).
  224.     INT 13H is the BIOS interrupt, it provides many Disk access functions
  225.      like reading/writing/formatting of disk sectors.
  226.     INT 10H is the video interrupt, this interrupt allows you to use many
  227.      functions to make nice graphics :-) It provides functions to change the
  228.      video mode, to draw pixels onto the screen, to change the color of text,
  229.      and so on...
  230.   For a list of all interrupts and their functions download Ralf Browns
  231.   Interrupt List from http://www.cs.cmu.edu/afs/cs.cmu.edu/user/ralf/pub/WWW/
  232.  
  233.  8. The sample program, step-by-step
  234.  
  235.   .model tiny
  236.   -----------
  237.     This is not code which will later be executed... it just tells TASM/TLINK
  238.    that they should use one segment for the whole program. There are also
  239.    .model small, .model huge, etc,... but for small programs (= simple
  240.    viruses ;) model tiny is enough.
  241.  
  242.   .code
  243.   -----
  244.     This tells TASM and TLINK that our executable code begin here. After this
  245.    like we can begin to write our main program.
  246.  
  247.   org 100h
  248.   --------
  249.     All COM files are loaded into memory at offset 100h. ORG tells the
  250.    compiler 'where to store the code in memory'.
  251.  
  252.   start:
  253.   ------
  254.     This is just a lable which is required by TASM...
  255.  
  256.   MOV AH, 9
  257.   ---------
  258.     We want to display text... we will use the dos interrupt to do so.
  259.    INT 21h requires that we put the function number into register AH. So, to
  260.    tell the CPU that we want to display some text we 'MOV(E)' the number of
  261.    the function used to display text (9) into register AH.
  262.  
  263.   MOV DX, OFFSET MESSAGE
  264.   ----------------------
  265.     The CPU needs to know where to find the text it should display... If we
  266.    use INT 21H we have to store this location in register DX. To do so we
  267.    just get the address of the message with 'OFFSET MESSAGE' and move it into
  268.    DX.
  269.  
  270.   INT 21H
  271.   -------
  272.     Now that we have 'collected' enough information (filled the registers
  273.    with many stupid numbers) we can execute the interrupt, which will finally
  274.    get the CPU to display some text for us. :-)
  275.  
  276.   MOV AX,4C00H
  277.   ------------
  278.     Never forget the last two lines of this code! They are used to exit a
  279.    program. If you forget them, your program will crash.
  280.    DOS uses the function 4C to exit programs, 00 means that we will not
  281.    return an ¿Error Code¿ (exit without an error).
  282.  
  283.   INT 21H
  284.   -------
  285.     Now INT 21H will execute the function to exit the program...
  286.  
  287.   message   db  'CodeBreakers Rule! ;-)',10,13,'$'
  288.   ------------------------------------------------
  289.     This is the message we WANT to display :-)
  290.    '10,13' does the same as pressing return, it puts the cursor into the next
  291.    line.
  292.    '$', this sign doesn't mean 'fast money' :) ... somehow DOS needs to know
  293.    where to stop displaying text, Bill G. decided to use '$'.
  294.  
  295.   end start
  296.   ---------
  297.     This indicates the end of the label 'start', also required by TASM.
  298.    btw - This doesn't exit the program! To exit the program you still have to
  299.    use function 4C with INT 21h.
  300.  
  301.  
  302.   Ok, thats all for now... I know that this is a very basic tutorial, and
  303.  that it wasn't written very well... but I hope that it answered at least
  304.  a few of your questions! If you have any further questions feel free to use
  305.  the message board on our homepage at www.codebreakers.org, or email me at
  306.  spo0ky@thepentagon.com (spo0ky with zero! :)... Maybe I'll write a FAQ with
  307.  specific questions for the 4th edition.
  308.  
  309.    --SPo0ky
  310.