I posted this in recruiting forum as well.  I am new to this board and just getting back into ASM (I used it back in the win 3.1 days)
Basically here is where I am stuck:  I am doing a very simple encryption routine, read source file and key file, XOR and write to output file.  Simple, right?  the problem is I would like to make this as a .dll and call the procedure from high level languages, in this case VB.  I would like to call it as such:
Proc_Name (input_file_path, key_file_path, output_file_path all as strings and then Offset being a long number which signifies where in the key to start reading)

I have my code below (any input would be great, too) but my problem is how to set it up to be able to be called from VB.  Any advice?  Thanks!


.Data

SourceBuffer    DB 256 Dup (0)
KeyBuffer      DB 256 Dup (0)
OutBuffer      DB 256 Dup (0)
InFileName      DB 512 Dup (0)
KeyFileName      DB 512 Dup (0)
OutFileName      DB 512 Dup (0)

hInFile        DW
hKeyFile      DW
hOutFile      DW
BytesRead      DB

CryptFile Proc Public
;// Open Source file to be crypted
  Mov Ah, 3DH            ;Open function
  Mov Al, 0            ;as Read
  Lea Dx, InFileName      ;Specify which file to open
  Int 21H              ;Open
  Jc _Error_Occured      ;if no error occured then
  Mov hInFile, Ax        ;store handle

;// Open Key file to be used
  Mov Ah, 3DH            ;Open function
  Mov Al, 2            ;as Random access (since I need to jump to offset of encryption start)
  Lea Dx, InFileName      ;Specify which file to open
  Int 21H              ;Open
  Jc _Error_Occured      ;if no error occured then
  Mov hKeyFile, Ax      ;store handle

;// Create output file to be written
  Mov Ah, 3CH            ;Create function
  Xor Cx, Cx            ;blank out cx (for file attributes)
  Lea Dx, InFileName      ;Specify which file to open
  Int 21H              ;Create
  Jc _Error_Occured      ;if no error occured then
  Mov hOutFile, Ax      ;store handle

;// Move key file's pointer to the offset required
  Mov Ah, 42H            ;Seek function
  Mov Al, 0            ;set as offset is specified from the start of key file
  Mov Bx, hKeyFile      ;load key file's handle to bx
                    ;Here Load CX:DX with offset needed in key file
  Int 21H
  Jc _Error_Occured      ;End if error occured (file size vs offset size checked in VB)

;// Begin read and encrypt loop
_ReadAndXOR:
;// Read 512 bytes from Source file
  Mov Ah, 3FH            ;Read file function
  Lea Dx, SourceBuffer  ;With 512 byte source buffer as storage location
  Mov Cx, 256            ;set length to 256
  Mov Bx, hInFile        ;set input file's handel to bx
  Int 21H              ;call int
  Jc _Error_Occured      ;End if error occured
  Cmp Ax, 0            ;AX now holds actual bytes read, make sure data was read (in case file was an even multiple of 256)
  Je _Finished_Reading_Source_File

;// Read from key, same number of btyes
;// Read 512 bytes from Source file
  Mov Cx, Ax            ;move the actual bytes read from AX, to CX
  Mov BytesRead, Cx      ;Store how many bytes were actually read to memory
  Mov Ah, 3FH            ;Read file function
  Lea Dx, KeyBuffer      ;With 512 byte source buffer as storage location
  Mov Bx, hKeyFile      ;set input file's handel to bx
  Int 21H              ;call int
  Jc _Error_Occured      ;End if error occured

;//XOR loop
_XORloop:              ;CX should still hold actual bytes read from source
  Mov Si, 0            ;zero out SI
  Mov Al, SourceBuffer ;Get first byte from source file to al
  Mov Ah, KeyBuffer    ;Get first byte from key file to ah
  Xor Ah, Al            ;xor and store result in ah
  Mov OutBuffer, Ah  ;move XOR'd value to output string
  Inc Si              ;increase SI by 1
  Loop _XORloop        ;Loop for length of string read

;// When all bytes are read
  Mov Cx, BytesRead      ;Store how many bytes were actually read from memory to CX for writing length to crypted file
  Mov Ah, 40H            ;Set ah=write function
  Mov Bx, hOutFile      ;BX = output file's handle
  Lea Dx, OutBuffer      ;point dx to location of output string
  Int 21H              ;and write
  Jc _Error_Occured      ;
  Cmp Ax, 256            ;Does # of bytes written=256?
  Je _ReadAndXOR        ;If not then EOF was reached, so jump to exit.  if yes then do another iteration of read and XORing


_Finished_Reading_Source_File:
_Error_Occured:
  Mov Ah, 3EH            ;close file function
  Mov Bx, hInFile        ;BX= source file handle
  Int 21H              ;close

  Mov Ah, 3EH            ;close file function
  Mov Bx, hKeyFile      ;BX= key file handle
  Int 21H              ;clsoe

  Mov Ah, 3EH            ;close file function
  Mov Bx, hOutFile      ;BX= output file handle
  Int 21H              ;close

  Ret
CryptFile EndP
Posted on 2007-05-11 15:40:34 by Command_Prompt
It depends on what assembler/linker you use. Each has its own parameters. What you want to do is to make this proc 'public' and 'export' it. But know that you can't make a Windows DLL with DOS interrupts (at least not on any modern Windows). You have to use WinAPI calls and make a fully 32-bit/64-bit DLL.
Posted on 2007-05-11 18:13:39 by ti_mo_n
Shoot.  Well thanks for the hasty reply Ti.  Is there a place on this forum that would have a list of the calls?  I really would like to get this done fast although it looks like it will take more catching up  :sad:.  The program I have written already is all in VB and is quite functional but slow in terms of larger files.  This was a hope to upgrade the slow part.  I noticed that the wiki asm book has a lot of links that don't seem to be working yet.  Any other place where I can find info on the new commands?

SOB, my old school asm learning wasted!  ;)
Posted on 2007-05-11 19:24:42 by Command_Prompt
Well, the code is still asm, so you code the same way you used to (with only minor changes ). The real difference lies in the communication with the Operating System: In DOS you use "int xx", while in Windows you 'call' a procedure *1). PE-EXE loader patches these calls with proper addresses when it starts up your exe/dll, so you just 'tell it' which function you want to use at a particular place in your code. Please read "Iczelion's tutorials". If you've ever coded in asm, then you should be able to catch the differences between DOS and Windows very quickly ;) And there is a tutorial on making a DLL, IIRC.


*1) Also, in DOS you store every parameter in registers, while in Windows you 'push' parameters onto the stack before calling a Win32API function.
Posted on 2007-05-11 23:02:19 by ti_mo_n
Cool, thanks for the info.  I am getting started on ICZ's tutorial as we speak.  Seems fairly simply enough.  Hopefully I can get some help when I get the new .dll put together in the newer asm...(DARN YOU BILL GATES!).
Posted on 2007-05-12 06:54:34 by Command_Prompt