Compilation Tutorial: an example of assembly language programming

Source: Internet
Author: User
Tags include mdb database connect odbc strlen access database

Assembly Language Programming Example one of these chapters, we have to bring together the knowledge we have learned. Specifically, let's write a program that uses ODBC APIs. For simplicity, I use Microsoft's Access database (Microsoft Access 97) in this program.

Note: If you are using a windows.inc of 1.18 and the following version, you should modify one of the minor bugs before you begin compiling. Looking for "Sql_null_handle" in Windows.inc, you get the following line:

Sql_null_handle equ 0L

Delete the "L" behind the 0, like this:

Sql_null_handle equ 0

This program is a dialog based program that has a simple menu. When the user selects Connect, it attempts to connect to the Test.mdb database, and if the connection succeeds, the full connection string returned by the ODBC driver is displayed. Next, the user can select View all Records command, the program uses ListView control to display all the data in the database. The user can also select the "Query" command to query for specific records. The example program will display a small dialog box prompting the user to enter the name of the person you are looking for. When the user presses the OK button or the ENTER key, The program executes a query to find records that match the criteria. When a user completes an operation on a database, you can select the "disconnect" command to disconnect from the database.

Now look at the source program:

.386
. Model Flat,stdcall
Include \masm32\include\windows.inc
Include \masm32\include\kernel32.inc
Include \masm32\include\odbc32.inc
Include \masm32\include\comctl32.inc
Include \masm32\include\user32.inc
Includelib \masm32\lib\odbc32.lib
Includelib \masm32\lib\comctl32.lib
Includelib \masm32\lib\kernel32.lib
Includelib \masm32\lib\user32.lib
IDD_MAINDLG EQU 101
Idr_mainmenu equ 102
Idc_datalist EQU 1000
Idm_connect equ 40001
Idm_disconnect equ 40002
Idm_query equ 40003
Idc_name EQU 1000
IDC_OK equ 1001
Idc_cancel equ 1002
Idm_customquery equ 40004
Idd_querydlg equ 102
Dlgproc Proto Hdlg:dword, Umsg:dword, Wparam:dword, Lparam:dword
Queryproc Proto Hdlg:dword, Umsg:dword, Wparam:dword, Lparam:dword
Switchmenustate Proto:D Word
Odbcconnect Proto:D Word
Odbcdisconnect Proto:D Word
RunQuery Proto:D Word
. Data?
HINSTANCE DD?
HENV DD?
Hconn DD?
HSTMT DD?
Conn DB 256 dup (?)
StrLen DD?
Hmenu DD?; Main Menu Simple Handle
Hlist DD?; ListView control Handle
Thename db DUP (?)
Thesurname db DUP (?)
Telno db DUP (?)
Namelength DD?
Surnamelength DD?
Telnolength DD?
Searchname db DUP (?)
Progpath DB 256 dup (?)
ConnectString db 1024 dup (?)
. Data
SQLStatement db "select * from main", 0
Wherestatement db "where Name=?", 0
StrConnect db "Driver={microsoft Access DRIVER (*.mdb)};D bq=", 0
dbname db "Test.mdb", 0
Connectcaption db "Complete Connection String", 0
Disconnect db "Disconnect successful", 0
AppName db "ODBC Test", 0
Allocenvfail DB "Environment handle allocation failed", 0
Allocconnfail db "Connection handle allocation failed", 0
Setattrfail db "Cannot set desired ODBC version", 0
NoData db "You must type the name in the" edit box ", 0
Executefail db "Execution of SQL statement failed", 0
Connfail db "Connection attempt failed", 0
Allocstmtfail db "Statement handle allocation failed", 0
Heading1 db "Name", 0
Heading2 db "Surname", 0
HEADING3 db "Telephone No.", 0
. Code
Start
Invoke GetModuleHandle, NULL
MOV hinstance,eax
Call Getprogrampath
Invoke Dialogboxparam, hinstance, Idd_maindlg,0,addr dlgproc,0
Invoke Exitprocess,eax
Invoke InitCommonControls
DLGPROC proc Hdlg:dword, Umsg:dword, Wparam:dword, Lparam:dword
. If Umsg==wm_initdialog
Invoke GetMenu, Hdlg
MOV hmenu,eax
Invoke GetDlgItem, Hdlg, idc_datalist
MOV hlist,eax
Call InsertColumn
. ElseIf Umsg==wm_close
Invoke Getmenustate, Hmenu, Idm_connect,mf_bycommand
. If eax==mf_grayed
Invoke Odbcdisconnect, Hdlg
. endif
Invoke Enddialog,hdlg, 0
. ElseIf Umsg==wm_command
. If lparam==0
MOV Eax,wparam
. If Ax==idm_connect
Invoke Odbcconnect,hdlg
. ElseIf Ax==idm_disconnect
Invoke Odbcdisconnect,hdlg
. ElseIf Ax==idm_query
Invoke Runquery,hdlg
. ElseIf Ax==idm_customquery
Invoke Dialogboxparam, HInstance, Idd_querydlg,hdlg, addr queryproc, 0
. endif
. endif
. else
MOV Eax,false
Ret
. endif
MOV eax,true
Ret
Dlgproc ENDP
Getprogrampath proc
Invoke GetModuleFileName, null,addr progpath,sizeof Progpath
Std
MOV Edi,offset Progpath
Add Edi,sizeof ProgPath-1
mov al, "\"
MOV ecx,sizeof Progpath
Repne SCASB
Cld
mov byte ptr [edi+2],0
Ret
Getprogrampath ENDP
switchmenustate proc Flag:dword
. If Flag==true
Invoke EnableMenuItem, Hmenu, Idm_connect, mf_grayed
Invoke EnableMenuItem, Hmenu, Idm_disconnect, mf_enabled
Invoke EnableMenuItem, Hmenu, Idm_query, mf_enabled
Invoke EnableMenuItem, Hmenu, Idm_customquery, mf_enabled
. else
Invoke EnableMenuItem, Hmenu, Idm_connect, mf_enabled
Invoke EnableMenuItem, Hmenu, Idm_disconnect, mf_grayed
Invoke EnableMenuItem, Hmenu, Idm_query, mf_grayed
Invoke EnableMenuItem, Hmenu, Idm_customquery, mf_grayed
. endif
Ret
Switchmenustate ENDP
odbcconnect proc Hdlg:dword
Invoke Sqlallochandle, Sql_handle_env, Sql_null_handle, addr henv
. If Ax==sql_success | | Ax==sql_success_with_info
Invoke SQLSetEnvAttr, Henv,sql_attr_odbc_version, sql_ov_odbc3,0
. If Ax==sql_success | | Ax==sql_success_with_info
Invoke Sqlallochandle, Sql_handle_dbc, henv, addr Hconn
. If Ax==sql_success | | Ax==sql_success_with_info
Invoke Lstrcpy,addr connectstring,addr strconnect
Invoke Lstrcat,addr connectstring, addr Progpath
Invoke Lstrcat, addr connectstring,addr dbname
Invoke SQLDriverConnect, Hconn, hdlg, addr connectstring, sizeof connectstring, addr Conn, sizeof conn,addr StrLen, SQL_DR Iver_complete
. If Ax==sql_success | | Ax==sql_success_with_info
Invoke Switchmenustate,true
Invoke Messagebox,hdlg, addr conn,addr connectcaption,mb_ok+mb_iconinformation
. else
Invoke Sqlfreehandle, SQL_HANDLE_DBC, Hconn
Invoke Sqlfreehandle, Sql_handle_env, henv
Invoke MessageBox, Hdlg, addr connfail, addr AppName, Mb_ok+mb_iconerror
. endif
. else
Invoke Sqlfreehandle, Sql_handle_env, henv
Invoke MessageBox, Hdlg, addr allocconnfail, addr AppName, Mb_ok+mb_iconerror
. endif
. else
Invoke Sqlfreehandle, Sql_handle_env, henv
Invoke MessageBox, Hdlg, addr setattrfail, addr AppName, Mb_ok+mb_iconerror
. endif
. else
Invoke MessageBox, Hdlg, addr allocenvfail, addr AppName, Mb_ok+mb_iconerror
. endif
Ret
Odbcconnect ENDP
odbcdisconnect proc Hdlg:dword
Invoke SQLDisconnect, Hconn
Invoke Sqlfreehandle, SQL_HANDLE_DBC, Hconn
Invoke Sqlfreehandle, Sql_handle_env, henv
Invoke Switchmenustate, FALSE
Invoke Showwindow,hlist, Sw_hide
Invoke Messagebox,hdlg,addr Disconnect, addr appname,mb_ok+mb_iconinformation
Ret
Odbcdisconnect ENDP
InsertColumn proc
Local Lvc:lv_column
MOV lvc.imask,lvcf_text+lvcf_width
MOV Lvc.psztext,offset Heading1
MOV lvc.lx,150
Invoke Sendmessage,hlist, Lvm_insertcolumn,0,addr LVC
MOV Lvc.psztext,offset Heading2
Invoke Sendmessage,hlist, Lvm_insertcolumn, 1, addr LVC
MOV Lvc.psztext,offset Heading3
Invoke Sendmessage,hlist, Lvm_insertcolumn, 3, addr LVC
Ret
InsertColumn ENDP
Filldata proc
Local Lvi:lv_item
Local Row:dword
Invoke SQLBindCol, Hstmt,1,sql_c_char, addr thename, sizeof thename,addr namelength
Invoke SQLBindCol, Hstmt,2,sql_c_char, addr thesurname, sizeof thesurname,addr surnamelength
Invoke SQLBindCol, Hstmt,3,sql_c_char, addr telno, sizeof telno,addr telnolength
MOV row,0
. While TRUE
mov byte ptr ds:[thename],0
mov byte ptr ds:[thesurname],0
mov byte ptr ds:[telno],0
Invoke SQLFetch, hstmt
. If Ax==sql_success | | Ax==sql_success_with_info
MOV Lvi.imask,lvif_text+lvif_param
Push row
Pop Lvi.iitem
MOV lvi.isubitem,0
mov lvi.psztext, offset thename
Push row
Pop Lvi.lparam
Invoke Sendmessage,hlist, lvm_insertitem,0, addr Lvi
MOV Lvi.imask,lvif_text
Inc Lvi.isubitem
MOV Lvi.psztext,offset thesurname
Invoke Sendmessage,hlist,lvm_setitem, 0,addr LVI
Inc Lvi.isubitem
MOV Lvi.psztext,offset Telno
Invoke Sendmessage,hlist,lvm_setitem, 0,addr LVI
Inc row
. else
. break
. endif
. ENDW
Ret
Filldata ENDP
RunQuery proc Hdlg:dword
Invoke ShowWindow, Hlist, Sw_show
Invoke SendMessage, Hlist, lvm_deleteallitems,0,0
Invoke Sqlallochandle, sql_handle_stmt, hconn, addr hstmt
. If Ax==sql_success | | Ax==sql_success_with_info
Invoke SQLExecDirect, hstmt, addr sqlstatement, sizeof SQLStatement
. If Ax==sql_success | | Ax==sql_success_with_info
Invoke Filldata
. else
Invoke ShowWindow, Hlist, Sw_hide
Invoke Messagebox,hdlg,addr Executefail, addr AppName, Mb_ok+mb_iconerror
. endif
Invoke SQLCloseCursor, hstmt
Invoke Sqlfreehandle, sql_handle_stmt, hstmt
. else
Invoke ShowWindow, Hlist, Sw_hide
Invoke Messagebox,hdlg,addr Allocstmtfail, addr AppName, Mb_ok+mb_iconerror
. endif
Ret
RunQuery ENDP
QUERYPROC proc Hdlg:dword, Umsg:dword, Wparam:dword, Lparam:dword
. If Umsg==wm_close
Invoke Sqlfreehandle, sql_handle_stmt, hstmt
Invoke EndDialog, hdlg,0
. ElseIf Umsg==wm_initdialog
Invoke ShowWindow, Hlist, Sw_show
Invoke Sqlallochandle, sql_handle_stmt, hconn, addr hstmt
. If Ax==sql_success | | Ax==sql_success_with_info
Invoke lstrcpy, addr Conn, addr SQLStatement
Invoke Lstrcat, addr Conn, addr wherestatement
Invoke Sqlbindparameter,hstmt, 1, Sql_param_input, Sql_c_char, sql_char,25,0, addr searchname,25,addr
Invoke SQLPrepare, hstmt, addr Conn, sizeof Conn
. else
Invoke ShowWindow, Hlist, Sw_hide
Invoke Messagebox,hdlg,addr Allocstmtfail, addr AppName, Mb_ok+mb_iconerror
Invoke EndDialog, hdlg,0
. endif
. ElseIf Umsg==wm_command
mov eax, WParam
SHR eax,16
. If ax==bn_clicked
MOV Eax,wparam
. If AX==IDC_OK
Invoke GetDlgItemText, Hdlg, idc_name, addr Searchname, 25
. If ax==0
Invoke MessageBox, hdlg,addr NoData, addr AppName, Mb_ok+mb_iconerror
Invoke GetDlgItem, Hdlg, Idc_name
Invoke SetFocus, eax
. else
Invoke Lstrlen,addr Searchname
MOV strlen,eax
Invoke SendMessage, Hlist, lvm_deleteallitems,0,0
Invoke SQLExecute, hstmt
Invoke Filldata
Invoke SQLCloseCursor, hstmt
. endif
. else
Invoke Sqlfreehandle, sql_handle_stmt, hstmt
Invoke EndDialog, hdlg,0
. endif
. endif
. else
MOV Eax,false
Ret
. endif
MOV eax,true
Ret
Queryproc ENDP
End Start

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.