; +-------------------------------------------+
; | Program ...... FASMCam                    |
; | Author ....... Marcus Araujo              |
; | Description .. this application shows how |
; |                to access webcam via FASM. |
; +-------------------------------------------+


format PE GUI 4.0
entry codestart

include 'win32a.inc'

  IDD_MAIN                     =  100
  WM_CAP_DRIVER_CONNECT        =  WM_USER + 10
  WM_CAP_DRIVER_DISCONNECT     =  WM_USER + 11
  WM_CAP_FILE_SAVEDIB          =  WM_USER + 25
  WM_CAP_SET_PREVIEW           =  WM_USER + 50
  WM_CAP_SET_PREVIEWRATE       =  WM_USER + 52
  WM_CAP_SET_SCALE             =  WM_USER + 53
  ID_START                     =  201
  ID_STOP                      =  202
  ID_CLICK                     =  203
  _camtitle                    db 'FASMWEBCAM'

  _filename  db 'IMAGE.BMP'    ; Filename
  nDevice    dd   0            ; Device Number -> It can range from 0 through 9
  nFPS       dd 100            ; Frames per second. Must be 1000/FPS. E.g. 20 FPS = 50

section '.data' data readable writeable
  hInstance     dd ?
  hWebcam       dd ?

section '.code' code readable executable
  codestart:
    invoke  GetModuleHandle, 0
    mov     [hInstance], eax
    invoke  DialogBoxParam, eax, IDD_MAIN, HWND_DESKTOP, MainDlg, 0
    invoke  ExitProcess, 0

  proc MainDlg hdlg, msg, wparam, lparam
    push    ebx esi edi
    cmp     [msg], WM_INITDIALOG
    je      .wminitdlg
    cmp     [msg], WM_COMMAND
    je      .wmcommand
    cmp     [msg], WM_CLOSE
    je      .wmclose
    xor     eax, eax
    jmp     .finish
    .wminitdlg:
      invoke  capCreateCaptureWindow, _camtitle, WS_VISIBLE + WS_CHILD, 10, 10,\
                                        266, 252, [hdlg], 0
      mov     [hWebcam], eax
      jmp     .finish
    .wmcommand:
      cmp     [wparam], BN_CLICKED shl 16 + ID_START
      je      .startbutton
      cmp     [wparam], BN_CLICKED shl 16 + ID_STOP
      je      .stopbutton
      cmp     [wparam], BN_CLICKED shl 16 + ID_CLICK
      je      .clickbutton
    .wmclose:
      invoke  SendMessage, [hWebcam], WM_CAP_DRIVER_DISCONNECT, _camtitle, 0
      invoke  EndDialog, [hdlg], 0
    .finish:
      pop     edi esi ebx
      ret
    .startbutton:
      invoke  SendMessage,  [hWebcam], WM_CAP_DRIVER_CONNECT, [nDevice], 0
      invoke  SendMessage,  [hWebcam], WM_CAP_SET_SCALE, TRUE, 0
      invoke  SendMessage,  [hWebcam], WM_CAP_SET_PREVIEWRATE, [nFPS], 0
      invoke  SendMessage,  [hWebcam], WM_CAP_SET_PREVIEW, TRUE, 0
      jmp     .finish
    .stopbutton:
      invoke  SendMessage, [hWebcam], WM_CAP_DRIVER_DISCONNECT, _camtitle, 0
      jmp     .finish
    .clickbutton:
      invoke  SendMessage, [hWebcam], WM_CAP_FILE_SAVEDIB, 0, _filename
      jmp     .finish
  endp

section '.idata' import data readable writeable

  library kernel, 'KERNEL32.DLL',\
          user,   'USER32.DLL',\
          avicap, 'AVICAP32.DLL'

  import  kernel,\
          GetModuleHandle,'GetModuleHandleA',\
          ExitProcess,    'ExitProcess'

  import  user,\
          DialogBoxParam, 'DialogBoxParamA',\
          EndDialog,      'EndDialog',\
          SendMessage,    'SendMessageA'

  import  avicap,\
          capCreateCaptureWindow, 'capCreateCaptureWindowA'

section '.rsrc' resource data readable
  directory     RT_DIALOG, dialogs
  resource      dialogs,\
                IDD_MAIN, LANG_ENGLISH + SUBLANG_DEFAULT, main_dialog
  dialog        main_dialog, 'FASM Webcam', 0, 0, 190, 200, WS_CAPTION + WS_POPUP + WS_SYSMENU +\
                                                            DS_MODALFRAME + DS_CENTER
                dialogitem 'BUTTON', 'START', ID_START,  10, 170, 50, 20, WS_VISIBLE + WS_TABSTOP
                dialogitem 'BUTTON', 'STOP',  ID_STOP,   70, 170, 50, 20, WS_VISIBLE + WS_TABSTOP
                dialogitem 'BUTTON', 'CLICK', ID_CLICK, 130, 170, 50, 20, WS_VISIBLE + WS_TABSTOP
  enddialog 

Assembly Online Compiler

Write, Run & Share Assembly code online using OneCompiler's Assembly online compiler for free. It's one of the robust, feature-rich online compilers for Assembly language. Getting started with the OneCompiler's Assembly compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Assembly and start coding.

About Assembly

Assembly language(asm) is a low-level programming language, where the language instructions will be more similar to machine code instructions.

Every assembler may have it's own assembly language designed for a specific computers or an operating system.

Assembly language requires less execution time and memory. It is more helful for direct hardware manipulation, real-time critical applications. It is used in device drivers, low-level embedded systems etc.

Syntax help

Assembly language usually consists of three sections,

  1. Data section

    To initialize variables and constants, buffer size these values doesn't change at runtime.

  2. bss section

    To declare variables

  3. text section

    _start specifies the starting of this section where the actually code is written.

Variables

There are various define directives to allocate space for variables for both initialized and uninitialized data.

1. To allocate storage space to Initialized data

Syntax

variable-name    define-directive    initial-value 
Define DirectiveDescriptionAllocated Space
DBDefine Byte1 byte
DWDefine Word2 bytes
DDDefine Doubleword4 bytes
DQDefine Quadword8 bytes
DTDefine Ten Bytes10 bytes

2. To allocate storage space to un-initialized data

Define DirectiveDescription
RESBReserve a Byte
RESWReserve a Word
RESDReserve a Doubleword
RESQReserve a Quadword
RESTReserve a Ten Bytes

Constants

Constants can be defined using

1. equ

  • To define numeric constants
CONSTANT_NAME EQU regular-exp or value

2. %assign

  • To define numeric constants.
%assign constant_name value

3. %define

  • To define numeric or string constants.
%define constant_name value

Loops

Loops are used to iterate a set of statements for a specific number of times.

mov ECX,n
L1:
;<loop body>
loop L1

where n specifies the no of times loops should iterate.

Procedures

Procedure is a sub-routine which contains set of statements. Usually procedures are written when multiple calls are required to same set of statements which increases re-usuability and modularity.

procedure_name:
   ;procedure body
   ret