Upload
others
View
9
Download
0
Embed Size (px)
Citation preview
MASM
MASM (Microsoft Macro Assembler)
converts a symbolic program into a binary
program that executes on a personal
computer.
ML.EXE is the MASM program.
ML assembles a program and also links
the program.
ML uses a source file that has en
extension .asm (but any extension can be
used)
ML generates an .obj file from the source
file.
The object file is an intermediary file that is
input to a linker program (LINK.EXE).
The linker program (called by ML.EXE)
converts the .obj file into either an .exe or
.com file.
The .exe (execute) file or .com (command)
file is executed from the command prompt
by typing the name of the file.
MASM
Libraries
The LIB program creates and maintains
library files that can be linked to source
files so common functions need only be
written once.
Library files have an extension of .lib and a
special form of the library file used with
windows (dynamic library files) have an
extension .dll
Macros
Macro files are files that contain
programming steps that the assembler
inserts into a program when the macro is
invoked.
Macros have replaceable parameters that
are passed to the macro.
Macros only function in a purely assembly
language environment.
PUBLIC and EXTRN
The PUBLIC statement makes labels
public to other programming modules.
The name of each function in a library file
or a program file can be made public.
The EXTRN (assembly language)
statement informs the assembler that a
label exists that is defined outside of the
current programming module.
Keyboard
The keyboard in Windows generates messages whenever a key is typed.
The WM_CHAR message contains the ASCII key-code.
Keyboard character are at times intercepted by a program using the PreTranslateMessage function.
PreTranslateMessage receives the message before anything else.
Filtering Keys
If a simple application using a single edit
box is created (see next slide) one can
illustrate the use of PreTranslateMessage
and filtering.
The software to filter just the numbers 0-9
is illustrated in the slide that follows the
next slide.
BOOL CssssDlg::PreTranslateMessage(MSG* pMsg)
{
if ( pMsg->message == WM_CHAR )
{
UINT key = pMsg->wParam; //get ASCII
if ( key < '0' || key > ‘9' )
return true; //ignore
}
return CDialog::PreTranslateMessage(pMsg);
}
/*This software only accepts 0 through 9, anything else is ignored. Any key or range of keys can be filtered in the same manner.*/
SetFocus
Focus is an important concept
If focus is set to an edit box or any control
that accepts keyboard data the cursor is
placed in the object.
To set focus use the SetFocus() member
function for the object.
To set focus to Edit1 (edit box) use
Edit1.SetFocus();
Obtaining Data from a Stock
Object
To obtain data from a stock control use the GetDlgItemText() or GetDlgItemInt() MFC functions.
To retrieve the text from an edit box called IDC_EDIT1 (the name assigned by Windows) use:
CString str1; GetDlgItemText(str1,IDC_EDIT1);
TO obtain an integer from an edit box called IDC_EDIT1 by the Visual Studio editor use:
int fred = GetDlgItemInt(IDC_EDIT1);
Note: The names of objects assigned by Visual Studio are in the resource file called Resource.h of each project. Also note that there is a number assigned to each name that can also be used in the Get and Set DlgItem functions as in GetDlgItemInt(101);
Placing data in a stock object
Use SetDlgItemText() or SetDlgItemInt to place data into a stock object.
For example, to place “Wow” in an edit control called IDC_EDIT1 by the framework use:
SetDlgItemText(IDC_EDIT1,”WOW”);
or
CString str1 = “WOW”;
SetDlgItemText(IDC_EDIT1,str1);
To place the number 234 into an edit control called IDC_EDIT1 use:
int numb1 = 234;
SetDlgItemInt(IDC_EDIT1,numb1);
Conversions
Converting from Hexadecimal to decimal is accomplished by subtracting 30H (0-9) from each digit or 37H (A-F) from each digit.
If AL contains a single hexadecimal digit:
SUB AL,30H
CMP AL,9
JBE LATER ;if 0-9
SUB AL,7
LATER:
Converting from to hexadecimal is accomplished by adding 30H (0-9) or adding 37H (A-F).
For example, if AL contains 0-F use:
ADD AL,30H
CMP AL,39H
JBE LATER ;if 0-9
ADD AL,7
LATER:
Example Hex to Decimal
The prior slide shows a simple application that contains a few stock labels and edit boxes. The edit boxes are IDC_EDIT1 and IDC_EDIT2 as assigned by Visual Studio.
The task is to read the number from IDC_EDIT1 (in hexadecimal) and display it (in decimal) on IDC_EDIT2.
Problem 1: We cannot read a hexadecimal number from an edit box. Only a decimal integer or a string can be read. Here we need to read a string.
Problem 2: The string needs to be converted to a decimal integer so it can be displayed on edit box 2.
CString str1;
int result = 0;
GetDlgItemText(str1,IDC_EDIT1) //get hex
for (int a = 0; a < str1.GetLength(); a++)
{
result <<=4; //shift left 4 places
char temp = str1.GetAt(a); //get digit
_asm
{
mov al,temp ;convert digit
sub al,30h
cmp al,9
jbe SOL1
sub al,7
SOL1:
mov temp,al
}
result |= temp; //put digit in result
}
SetDlgItemInt(IDC_EDIT2,result);
//in strictly C++ only
CString str1;
int result = 0;
GetDlgItemText(str1,IDC_EDIT1) //get hex
for (int a = 0; a < str1.GetLength(); a++)
{
result <<=4; //shift left 4 places
if (str1.GetAt(a) > „9‟ )
result |= str1.GetAt(a) – 0x37;
else
result |= str1.GetAt(a) – 0x30;
}
SetDlgItemInt(IDC_EDIT2,result);
Respond to the Enter Key
The enter key is 13 decimal or 0DH.
The enter key can be intercepted with the PreTranslateMessage function by trapping the WM_KEYDOWN message.
BOOL CssssDlg::PreTranslateMessage(MSG* pMsg)
{
if ( pMsg->message == WM_KEYDOWN ) //on any key down
{
if ( pMsg->wParam == 13 ) //on enter
{
//process enter key (earlier software)
return true; //we handle enter
}
}
return CDialog::PreTranslateMessage(pMsg); //framework
}