Kind of new: function implementation questions, MASM
From: Ross A. Finlayson (spamtrap_at_crayne.org)
Date: 09/17/04
- Next message: Betov : "Re: A Register Preservation Macro for Procedures"
- Previous message: spamtrap_at_crayne.org: "Re: (Z80) Setting a BITMAP"
- Next in thread: hutch--: "Re: Kind of new: function implementation questions, MASM"
- Reply: hutch--: "Re: Kind of new: function implementation questions, MASM"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 17 Sep 2004 09:04:49 +0000 (UTC)
Learning how to use the Microsoft Assembler
I program using Microsoft Visual C++ 6 service pack 3, and
occasionally Intel C/C++ Compiler 4.5, and now I want to begin to
understand assembly language. I use ProjectBuilder with Apple's
cc/gcc, and cc or gcc on other UNIX machines.
So, I start to read about assembler. I browse comp.lang.asm.x86, and
read some of Randall Hyde's website back in the 90's. I have open in
Acrobat Reader this real great book by Paul A. Carter called PC
Assembly Language, pc-asm.pdf. I browse the Intel Pentium processor
manuals because I figure the instructions I will use are available on
all x86 platforms, and Windows only runs on x86, Alpha, Hitachi SH-3,
Intel StrongARM, etcetera, 99% x86 in PC systems. I look to the MSDN,
it has a section about inline assembly, and about how __fastcall
calling convention passes the first two DWORD arguments through ecx
and edx, which are names for general purpose 32-bit registers on the
x86 (Intel x86 series) processors.
I've read that there are basically two variants of assembly language:
AT&T and Intel. There are many more variants as microcode runs almost
all general purpose processors. Seeing as how I'm concerned with
Intel format until I try and program the PowerPC or SPARC systems, I
shall focus upon the Intel assembly language format.
I can use inline assembly within my C programs without too much extra
stuff, I never have, I've seen some of the listings and generated
assembler output from gcc and seen the asembler output in the VC
debugger, but I really think I should use a standalone assembler. To
that end, I look through my asm folder for more information about
assembler. Within the masm archive folder, where MASM is the
Microsoft Assembler, I have here some archived files that I think
contain the necessary programs to install a working version of MASM
6.14 so that I can compile assembly listings using the Intel MMX
instructions.
I make a folder for MASM on the D drive and unzip the MASM
programmer's guide to it. It says "Programmer's Guide, Microsoft
MASM, Assembly Language Development System, Version 6.1". I make a
folder and copy those files into the ProgrammersGuide folder. So, I
run the file ml614.exe. It surprisingly unpacks and it contains a
patch program for MASM 6.1 to 6.14. I read the Readme.txt file. It
tells me I need a MASM 6.11 root directory. I look through the
archived files in the MASM directory.
Volume in drive C is CDISK
Volume Serial Number is 191E-1701
Directory of C:\asm\masm
. <DIR> 05-02-99 7:18a .
.. <DIR> 05-02-99 7:18a ..
MASM_CN3 ZIP 8,257 05-02-99 7:18a masm_cn3.zip
READOBJ ZIP 16,197 05-02-99 7:29a readobj.zip
ASMHELP ZIP 16,039 07-31-99 1:46a asmhelp.zip
SALLY ASM 2,730 05-02-99 7:32a sally.asm
WIN32ASM TXT 134,557 07-30-99 6:24p WIN32ASM.TXT
ERRMSG TXT 9,197 07-30-99 6:25p errmsg.txt
PENTIUM TXT 37,049 07-30-99 6:25p pentium.txt
SUPPORT TXT 6,832 07-30-99 6:25p support.txt
README TXT 27,994 07-30-99 6:26p readme.txt
VXD_DOC ZIP 60,066 07-31-99 1:49a vxd_doc.zip
INTEL_~1 PDF 2,727,843 07-31-99 2:17a intel_opcodes_24319101.pdf
MASM61~1 ZIP 11,625,463 09-19-99 8:44p MASM611_c.zip
MASM611 ZIP 2,733,355 07-31-99 2:06a masm611.zip
ML613 EXE 834,436 07-30-99 6:49p ML613.EXE
OPCODES TXT 111,029 07-31-99 1:53a opcodes.txt
OPCODES ZIP 67,076 07-31-99 1:54a opcodes.zip
WIN_ASM ZIP 504,408 07-31-99 1:51a win_asm.zip
386INTEL ZIP 209,396 08-08-99 12:48a 386intel.zip
PCASM-~1 ZIP 668,334 08-08-99 1:00a pcasm-pdf.zip
4P_V33~1 ZIP 343,768 08-08-99 12:59a 4P_V331_i586.ZIP
MTL-OPTI ZIP 7,346 08-08-99 12:58a MTL-OPTI.ZIP
OPTIMIZE ZIP 17,174 08-08-99 12:59a OPTIMIZE.ZIP
PENTIUM ZIP 164,896 08-08-99 1:01a PENTIUM.ZIP
PENTIUN ZIP 163,890 08-08-99 1:02a PENTIUN.ZIP
ML614 EXE 878,594 08-18-99 8:52p ml614.exe
MASM50 ZIP 96,784 08-24-99 2:04a Masm50.zip
MASM61~2 ZIP 4,165,904 08-24-99 2:45a MASM611_b.zip
ML611D ZIP 499,935 08-24-99 3:48a ml611d.zip
16BITL~1 EXE 281,082 07-30-99 6:40p 16bitlink_lnk563.exe
PROGRA~1 ZIP 878,681 09-06-01 1:30p ProgrammersGuide.zip
MASMRE~1 ZIP 271,204 09-06-01 1:32p MasmReference.zip
ENVIRO~1 ZIP 1,669,785 09-06-01 1:37p EnvironTools.zip
32 file(s) 29,239,301 bytes
2 dir(s) 182.39 MB free
Ah, there's a file masm611.zip. I'm interested in what's in
MASM611_c.zip, an 11 megabyte compressed file. It could be that it
took several attempts to download masm611. Each of those archives
appears to be a good archive, the _b archive contains some help files,
and the _c archive contains an extended number of samples and
reference documents, and apparently the 6.13 patch.
I move the ml614.exe and its extracted files into an ml614 directory,
and try and figure out which installation of 6.11 to use. The _c
archive appears to contain images of the floppy disks. The masm50.zip
file contains Debug.com, Exe2bin.exe, Link.exe, and Masm.exe. I tihnk
those are DOS mode programs, and would have no support for flat 32-bit
which I use because I am ignorant of segments, near and far, and tiny,
small, medium, large, and huge models, and DOS extenders. It looks
cool, the files are dated 1984. Anyways, I look back to MASM 6.11 as
I need a working MASM 6.11 directory install the patch to update it to
MASM 6.14 to work with the Pentium MMX instructions.
I make a bin file in my masm directory
D:\masm>mkdir bin
D:\masm>cd bin
D:\masm\bin>
Alright. Now, I need to put files into the bin directory... I look in
the zip files containing the MASM 6.11 software to see which files go
into the bin directory. I extract masm.exe from the _c (1993) archive
and see what it does. It is named masm.ex$ and meant to be installed
from floppies as it is in the disk1 directory. So I would extract it
to d:\masm\bin and change its name to masm.exe. I'm leery of doing
that because it looks like it is linked to some files that would go in
the lib directory and then I would have to modify the path. I look
back at the _b archive, it also says 1993. It has stuff to go into
the bin, binr, include, lib, help, init, and samples directories. I
sit and think a while about just using the 1993 software, and play
some Hearts. I get second and only shot the moon once, twice losing
by one card. With that bad omen, I extract masm611_b.zip to the masm
folder.
Microsoft(R) Windows 98
(C)Copyright Microsoft Corp 1981-1998.
C:\WINDOWS>d:
D:\masm\ProgrammersGuide>cd ..
D:\masm>doskey
DOSKey installed
D:\masm>masm
Bad command or file name
D:\masm>cd bin
D:\masm\bin>masm
Microsoft (R) MASM Compatibility Driver Version 6.11
Copyright (C) Microsoft Corp 1993. All rights reserved.
usage: MASM [option...]
source(.asm),[out(.obj)],[list(.lst)],[cref(.crf)][;]
Run "MASM /H" for more info
D:\masm\bin>
D:\masm\bin>masm
Microsoft (R) MASM Compatibility Driver Version 6.11
Copyright (C) Microsoft Corp 1993. All rights reserved.
usage: MASM [option...]
source(.asm),[out(.obj)],[list(.lst)],[cref(.crf)][;]
Run "MASM /H" for more info
D:\masm\bin>masm /h
usage: masm /options
source(.asm),[out(.obj)],[list(.lst)],[cref(.crf)][;]
/c Generate cross-reference
/D<sym>[=<val>] Define symbol
/e Emulate floating point instructions and IEEE format
/I<path> Search directory for include files
/l[a] Generate listing, a-list all
/M{lxu} Preserve case of labels: l-All, x-Globals, u-Uppercase
Globals
/n Suppress symbol tables in listing
/t Suppress messages for successful assembly
/w{012} Set warning level: 0-None, 1-Serious, 2-Advisory
/X List false conditionals
/Zi Generate symbolic information for CodeView
/Zd Generate line-number information
D:\masm\bin>
Ahh... so it looks like I have run MASM. Following the instructions
in the patch for MASM 6.14, I copy the patch.* files into the masm
directory, and then run patch. It says two patches were successfully
applied. I delete the patch files. I run masm and the version
information is the same. Continuing the patch instructions, I copy
h2inc.exe and h2inc .err to the bin directory. They are the same size
as the files the copy over. Then, I copy win.inc to the include
directory after looking at win.inc. Copying it over, it as well is
the same file size as the original.
Now I'm confused because I thought the Pentium.txt file said something
about MMX and SSE instructions. It does, it says they're described in
the Intel DRG web site documents, for example:
24143004 PDF 4,366,017 11-30-00 10:25a 24143004.pdf
PENTOPT ZIP 97,239 12-14-00 9:28a pentopt.zip
PENTOPTH ZIP 124,478 12-14-00 9:28a pentopth.zip
MACROS ZIP 6,641 12-14-00 9:28a macros.zip
TESTP ZIP 29,157 12-14-00 9:28a testp.zip
24319101 PDF 2,727,843 12-17-00 5:29a 24319101.PDF
PENTOP~1 ZIP 97,239 12-18-00 3:42p pentopt-b.zip
PENTOP~2 ZIP 124,478 12-18-00 3:42p pentopth-b.zip
24547201 PDF 6,057,801 12-23-00 3:22p 24547201.pdf
PENTOP~1 HTM 412,947 09-08-01 7:45p pentopt.html
It's difficult to find the clock count reference ***. That
pentopt.html is a copy of one of Agner Fog's readable expositions.
I set the path variable to include d:\masm\bin and the helpfiles
variable to d:\masm\help for use on restart, adding to autoexec.bat:
set PATH = %PATH%;D:\masm\bin
set HELPFILES = d:\masm\help
Going back to the archive folder, I extract the documents in
MasmReference.zip to the masm\MasmReference folder. It says:
"Reference Microsoft MASM Asembly-Language Development System Version
6.1", from 1992.
D:\masm\SAMPLES\WINCLOCK>masm appentry.asm appentry.obj
Microsoft (R) MASM Compatibility Driver
Copyright (C) Microsoft Corp 1993. All rights reserved.
Invoking: ML.EXE /I. /Zm /c /Ta appentry.asm
Microsoft (R) Macro Assembler Version 6.14.8444
Copyright (C) Microsoft Corp 1981-1997. All rights reserved.
Assembling: appentry.asm
appentry.asm(32) : error A2052: forced error : MODEL hasn't been
defined
I try to run it with -DMODEL=.586. Errors ensue. I copy the command
line from the sample file.
D:\masm\SAMPLES\WINCLOCK>ml -c -DMODEL=small appentry.asm
Microsoft (R) Macro Assembler Version 6.14.8444
Copyright (C) Microsoft Corp 1981-1997. All rights reserved.
Assembling: appentry.asm
D:\masm\SAMPLES\WINCLOCK>dir
Volume in drive D is DDISK
Volume Serial Number is 1458-3EBD
Directory of D:\masm\SAMPLES\WINCLOCK
. <DIR> 09-16-04 11:20p .
.. <DIR> 09-16-04 11:20p ..
APPENTRY ASM 3,927 09-08-92 6:55p APPENTRY.ASM
APPENTRY OBJ 421 09-16-04 11:51p appentry.obj
2 file(s) 4,348 bytes
2 dir(s) 1,842.14 MB free
D:\masm\SAMPLES\WINCLOCK>
Now what? I want to generate a function that I can call from C that
uses the __fastcall directive that passes the first two DWORDs on ecx
and edx and the other operands on the stack. For example:
extern "C" __fastcall process (uint32 a1, uint32 a2, uint32* x1,
uint32* x2, void* v1, void* v2, void* v3);
I think I'm supposed to preserve ebx. I want to have that definition
in the C file and then generate an object file with MASM here and then
link that into a program. Basically there are four or more
general-purpose registers. I think I need to define the model. Then
there are various segments, calling themselves ".data", ".text",
".label", "stack", ".586", "...". Then there's something along the
lines of PROC ... ENDPROC. I'm not against using __stdcall, __cdecl,
or other calling conventions, I just want it to be fast.
Basically the idea is to use this function for the inner loop of an
algorithm and to use instructions that are pairable because they are
simple and use no immediate or memory operands, something about the U
and V pipes, and to use a register at most every other instruction, to
utilize the processor, expoit, even.
I think I am only using mov, jmp, bt, shr, or, and perhaps dec
instructions, but later I want to utilize the MMX 64-bit vectorized
registers for integers.
As I've said, I'm an assembler NOO-B. I can use high level languages,
and think something like Tommesani's Quexal is just awesome, in, using
linear equations with the known interdependencies to maximize the
execution speed of code.
I want to use the assembler to put my detailed instructions into a
routine to minimize the clock (cycle?) count of the executed
instructions. Those void* arguments are addresses of memory buffers,
one of them is less than four kilobytes and is read many times but
never written, the next is arbitrary in extent and read once 32 bits
at a time, the third arbitrary in extent and written once 16 bits at a
time. So I want to allocate, align, and use the memory arrays so they
are cached in a coherently-compatible manner, and that the MOV
instructions only take two or less cycles, where each void* is treated
as an array of 32-bit words, perhaps by caching lines each 8k of data
with a dummy access. I don't plan to parse the cache information from
the specifications, identify the CPU, and provide cache hints to the
compiler, for this function.
Besides masm, I'm also interested in having the same or adequately
similar algorithms implemented for nasm, the Netwide assembler, or
gas, the GNU assembler, that is. I've heard of that a86, TASM, and
some others from reading this newsgroup and other various sources.
I read the "masm32" thread, I agree with that programmer guy. Abut
IA64, basically I just want to know how to implement for post-Pentium
Pro 32 bit processors, blazing fast.
I'm kind of unclear about how the code is in the "code" segment and
the instruction pointer refers to data in the code segment that is
somewhere on the stack?
I'm hoping you can describe to me the minimal and near optimal source
code skeletons with cogent advice.
"MASM Tutorial":
http://www.google.com/search?q=MASM+Tutorial
http://search.msn.com/results.aspx?FORM=SRCHWB&q=MASM Tutorial
Oh, hey, there's a great amount of advice.
Thank you for your advice.
Regards,
Ross Finlayson
- Next message: Betov : "Re: A Register Preservation Macro for Procedures"
- Previous message: spamtrap_at_crayne.org: "Re: (Z80) Setting a BITMAP"
- Next in thread: hutch--: "Re: Kind of new: function implementation questions, MASM"
- Reply: hutch--: "Re: Kind of new: function implementation questions, MASM"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]