Assembly and Cracking From the Ground Up
An Introduction by Greythorne the Technomancer

Some Necessary Code Snippets



I learn best by example.

Very often, few good examples can be found that are short and to the point. It can be very hard for a new assembly programmer to glean useful info out of a large uncommented (or badly commented) assembly program which looks more like alphabet soup than code.

These examples can be cut and pasted into your own programs rather painlessly.

I have been asked recently and often where is the best place to find assembly information. One place (rather unfair answer) is the net. My favorite info comes in hardcopy in the form of books however. Some of the best assembly books around are the oldest ones, with 8086 optimized code in them. I tend to pick up rather expensive manuals at less than a dollar at secondhand bookstores - with a wealth of code for all manner of needs.

Any old pc-dos or assembly manual by Peter Norton is worth checking out - just open it up and look for code, you won't regret it.


Covered here is (will be) code to do several of the following:

String Operations
Displaying Numerals in Assembly
File Operations
Search Functions
Other Fun Code



STRING BASICS
(HELLO WORLD AND SUCH)

The first thing any instructor will do to you in a programming course is teach you how to display a hello world message.

Well, this time i am going to give you a pair of them.

You don't have to understand them just yet,
the idea here is for you to have code you can use to make
your own programs, and learn how it works by using it.

I will also show you this as a full program so you can see how
easy it is to incorporate this piece of code (and a few others).

DOS

To Call This Routine:
   message    db     'hello world','$'
   mov        dx,     offset message
   call       DisplayString
The Routine: (Tiny because DOS takes care of most of the code)
; outputs string in dx using: int 21h, ah=9

DisplayString: 
     mov     ax,cs
mov ds,ax
mov ah,9 ; DOS FUNCTION: display message
int 21h ; Call the DOS interrupt
ret


BIOS

Here is another way of displaying a text string,
this time we are not using the DOS int 21 call, but the BIOS int 10.

The reason for this is twofold really, considering that many
programs we must crack are not all relying on the easy DOS
method, and without this code it would be impossible to write
code for non DOS operating systems, like LINUX for example.

To Call This Routine:
   message    db     'hello world','$'
   mov        dx,     offset message
   call       BiosDisplayString
The Routine:
; outputs string in dx using: int 10h, ah=14

BiosDisplayString:              

                mov     si, dx  ; bios wants si rather than dx really
                                ; dx is used to make it act the same as
                                ; the DOS system service int 21h,ah=9

                mov     ax, cs  ; use current segment...
                mov     ds, ax  ; ...for the data to be displayed

bnxtchar:       lodsb           ; get next character of message

                push    ax      ; preserve ax so it doesnt get clobbered
                cmp     al, '$' ; end of string marker
                jz      endbprtstr
                pop     ax      ; restore ax

                call    BiosDisplayChar
       
                jmp     bnxtchar

endbprtstr:     pop     ax      ; cleanup
               
                ret

; Notice that we have to display the string one character at a time

BiosDisplayChar:                ; outputs character in al

                mov     ah, 0Eh  ; BIOS FUNCTION: DISPLAY CHARACTER
                xor     bx, bx
                xor     dx, dx  
                int     10h     ; Call the BIOS interrupt
                ret

By no means is that all of them, there are other int 10h calls for text display,
including ah=13h, ah=09h (see HelpPC's interrupt services to see what they do)
For that matter, int 21 ah=02 prints a single character to the screen as well.

Regardless, knowing the interrupts can be quite handy when breaking nagscreens.



AND THE NUMBERS!
(Assembly code to display numbers in any base)

To Call This Routine:
   mov        ax, 0402h
   call       DisplayWord
The Routine:
; Displays a Word in AX

DigitBase       dw      010h    ; using base 16 digits
; change the above from 10h to 0Ah for decimal digit display DisplayWord proc near mov si,offset DigitBase mov di,offset TempNum NextDgt: xor dx,dx div si add dx,30h ; convert to ascii digit mov [di],dl dec di cmp ax,0 ; any digits left? ja NextDgt inc di mov dx,di mov ah,9 int 21h ; DOS display char string at ds:dx retn DisplayWord endp ; The following is a workspace buffer for the word to be displayed db 4 dup (20h) ; maximum number of digits TempNum db 20h db 24h, 90h

Note that in this above example, we could have
called the Non-DOS string display above instead.



+gthorne'97