... Masm Tutorial #2 ...

Pulse Reversing Force

This is the second Pulse Team tutorial for MASM. In it you'll make more interesting things than the previous MessageBox - you'll
do a keygen. I'll comment the code below to make all clear to you. If something is not clear to you that means it is in the
previous tutorial or my english is supa bad ;), but I don't think that I missed something. This tutorial have more API functions than
the previous, so if you don't have win32.hlp (help about all apis), please download it from here. Open kgn.asm (which is in this zip)
with Qedit.exe. Now you must begin with the code below. In white is the code, and in gray is the comment:
---------------------------------------------------------------------------------------------------------
 
  .386
  .model flat, stdcall
  option casemap :none
      
  include \masm32\include\windows.inc
  include \masm32\include\user32.inc
  include \masm32\include\kernel32.inc

  includelib \masm32\lib\user32.lib
  includelib \masm32\lib\kernel32.lib

   WndProc PROTO :DWORD,:DWORD,:DWORD,:DWORD
   ; The upper row is a procedure prototype.
   ; In comparison with the high level languages MASM bind the user to write prototypes of his
   ; procedures to show to the compiler how many parameters to search and what type are they
   ; Syntax: Name PROTO :Type of param1[,:Type of param2]
 
  .const
   ; Here are the constants for the IDs of the controls in the dialog.
   ; The constants definition syntax is:
   ; Name equ Value
 
   ID_GEN       equ 201
   ID_EXIT      equ 202
 
   ID_TXTUSER   equ 101
   ID_TXTSERIAL equ 102
 
   ; Now you don't have to remember the IDs - you can easy use the constants
 
  .data
   ; Here are the initialized variables (with starting value)

   dlgname       db "keygen",0

   hInstance     dd 0
   cnt           dd 0
   strlen        dd 0
   err           db "Enter more than 5 charz",0
   err2          db "Error",0
        
   .data?
   ; I'll use uninitialized variables section
   ; to separate space in the memory for the serial and the string buffer for the calculations.
  
   serial   dd 5   dup(?)
   buff     db 100 dup(?)

  .code
  start:

   invoke GetModuleHandle,0
   mov hInstance, eax
        
   invoke DialogBoxParam,hInstance,offset dlgname,0,offset WndProc,0
   ; This function calls the dialog dlgname from the program's resources.
 
   ; The procedures in MASM (like this below) have this syntax (don't forget about the proto):
   ;
   ; Procedure_Name proc [param1:TYPE, param2:TYPE]
   ; ..code..
   ; ret
   ; Procedure_Name endp
 
WndProc proc hWin:DWORD,uMsg:DWORD,wParam:DWORD,lParam:DWORD
   ; This is the most important part of the application. When something happens with your dialog
   ; this procedure will be called. It has 4 params: hWin, uMsg, wParam è lParam.
   ; hWin - this is the handle of the created window (handle is a unique identifier in windows)
   ; uMsg - the message type sended to the window ex. WM_COMMAND
   ; wParam and lParam have different values toward the type of message. To be all clear see win32.hlp
   ; wParam - the control's ID (at WM_COMMAND)
   ; lParam - the handle of the control (at WM_COMMAND)

 .if uMsg == WM_COMMAND
   ; Here I use the macro .if (for opportunities :) to check is the message sended to
   ; the dialog is for click. For these who don't know the C++'s 'if' syntax I have an example:
   ; == - equal, ! - not, != - not equal, > - greater, < - less and etc...

  .if wParam == ID_GEN
   ; If the message sended to the dialog is WM_COMMAND checks is the identifier (ID) is equal
   ; to the generate button's ID 
 
   mov serial, 0
   ; Nulls the serial every time, because You don't want to change
   ; it if there is more than one calculation. 

   invoke GetDlgItemText,hWin,ID_TXTUSER,offset buff,100
   mov strlen, eax 
   ; Gets the text inputted in the text field and put it in buff. The length is returned EAX 
   
   .if eax < 5
   ; If the length of the inputted text is less than 5 symbols shows a message and returns

   invoke MessageBox,hWin,offset err,offset err2,MB_OK+MB_ICONEXCLAMATION
   ret
   .endif
  
   dec strlen
   ; If the length of the inputted text is larger or equal 5 symbols the program continues, but
   ; decreases the length by 1, because the string is null terminated and You don't need the zero :)

   mov cnt, 0
   ; Nulls the counter of consecutive bytes and automatically jumps to the label below.

   l00p:

   ; Simple calculation algo for the serial (edx is the ascii of the consecutive char)
   mov ecx, cnt
   movsx edx, [buff+ecx]
  
   add edx, 3
   dec edx
   xor edx, 2
   imul edx, 7
   add serial, edx
   ; Adds the changed ascii code of the consecutive char to the variable serial.
 
   inc cnt
   ; Increases the counter
 
   mov eax, strlen
   cmp eax, cnt
   ; Compares the counter of consecutive bytes with the length of the string.
   ; If it is not equal repeats all again.
 
   jne l00p

   invoke SetDlgItemInt,hWin,ID_TXTSERIAL,serial,FALSE
   ; After the program is summed the changed ascii codes of the chars it
   ; shows them in the second text field.

  .elseif wParam == ID_EXIT
   ; If the message sended to the dialog is WM_COMMAND and the ID of the button for
   ; exit is equal to wParam - the program quits.

   invoke ExitProcess,0   
  .endif 
 .elseif uMsg == WM_CLOSE
    invoke ExitProcess,0
   ; If the user wants to close the application, the program quits.
 
.endif
    xor eax, eax
    ret
   ; Nulls eax and returns
 
WndProc endp

end start
   ; End of the program label and the application
---------------------------------------------------------------------------------------------------------
Now just select Project->Build All and you're done. If in the folder exists file with the name rsrc.rc
MASM automatically links it with the program. With the .asm file comes and .rc file who you can view in 
notepad or directly with ms visual c++ :). The next tutorial will be for nfo viewer, so check our
website if you're interested :).
---------------------------------------------------------------------------------------------------------
    greetz goin to :
    ..shade.........
    ..dim_cr........
    ..pumqara.......
    ..nre...........
    ..sometimes.....
    ..buko..........
    ..jeux..........
 
 
---------------------------------------------------------------------------------------------------2k3---
  Date   : 15..2îî3
  Author : plux ~ PuLSe
  E-Mail : p.l.u.x@mail.bg
  Site   : www.pulse-team.tk