Title: assembly programming
1assembly programming
- A little background on using the software
2Irvine text examples etc
- The newer text (edition 5) contains chapter
examples along with VC and uses this MS
interface for editing and assembling assembly
code. - The Irvine assembly text editon 4 contains a CD
with a version of the assembler, various batch
files, as well as examples in chapter
directories. I have this CD and it is on the
Math Lab machines for you to copy. - The assembler itself may be available in F200 and
even in other campus labs.
3Machine configuration
- You will typically create assembly program files
using a text editor, so you may want to install
Textpad on your own machine. This is shareware
and is available from Helios.com. Notepad can
also be used. As can VC. - The only other sw needed is on the text CD. (The
assembler is part of MS Windows, but is likely
buried somewhere so you cant find it and may not
have all versions we will use.) - The text assembles and runs programs in the VC
environment but I will use DOS.
4Assembly is in the labs
- In the labs, assembler and linker are tools off
Textpad. (Currently just 32 bit but I will ask
them to add 16 bit). - There is a shortcut to your p drive. From here,
you can run your program
5Entering a program into textpad (in lab)
6Selecting tools option build 32-bit MASM from
textpad in labs
- Assembling addsub.asm
- LINK32 LNK6004 addsub.exe not found or not
built by the last incremental link performing
full link - Volume in drive P is Faculty P Drives
- Volume Serial Number is 6255-760D
- Directory of P\assembly
- 08/28/2008 0658 AM 306 addsub.asm
- 08/28/2008 0659 AM 28,711 addsub.exe
- 08/28/2008 0659 AM 33,324 addsub.ilk
- 08/28/2008 0659 AM 15,246 addsub.lst
- 08/28/2008 0659 AM 6,938 addsub.map
- 08/28/2008 0659 AM 3,714 addsub.obj
- 08/28/2008 0658 AM 91,136 addsub.pdb
- 7 File(s) 179,375 bytes
- 0 Dir(s) 9,986,265,088 bytes
free - Tool completed successfully
7Looks like this on p drive
8Submissions
- Typically, youll submit screenshots of your
program assembly/run along with an electronic
copy of your code. (see previous slides) - Both can be put (pasted) in an html file to which
you send me the link.
9On your own machineRun setup on the 4th edition
CD to copy the assembler and text examples to
your C drive
10Path settings
- Youll have to put the assembler in your path
settings to assemble and run asm programs, or
move the asm files to the directory where masm is
located. - There are batch files provided which will run
masm and then run the linker. These are named
make16.bat, make32.bat. - Make sure all included files are in the directory
from which you are assembling or fix paths
appropriately. Make sure all file modules are in
the directory from which you link or fix paths
appropriately.
11extensions
- .asm marks an asm file. You may use textpad or
notepad to create and edit these. Note You can
also configure textpad to assemble and run the
code. - When writing your own assembly code, at least in
the beginning, you may want to cut/paste to start
with using one of my or one of the texts
examples, then edit it. - .obj marks an object file. It must be linked
before it can be run. The assembler creates an
obj file from an .asm file. The linker resolves
symbol references and can create a single exe
from several obj files. The first obj in the
linker commandline list is the default main. - .exe marks an executable. It can be run by
clicking it with the mouse or typing its name on
the commandline (assuming it is in the path). - .lst marks a listing file of the assembly we
wont use this.
12More on how it works
- Masm.exe creates an .obj file from an .asm file.
- Link.exe creates an .exe file from a .obj file.
- The batch files provided assume the proper
file-designation extensions. But you will get an
error if you type the .asm extension when running
the batch files. - Use make16.bat to assemble and link a 16-bit
format assembly program. - Use make32.bat to assemble and link a 32-bit
format assembly program. - Or run the assembler and linker (exe files)
directly, yourself, on the commandline.
13Here is a text example (I renamed it
example16.asm)
- TITLE Add and Subtract (16-bit.asm)
- This program adds and subtracts 32-bit
integers. - Last update 2/1/02
- INCLUDE Irvine16.inc
- .code
- main PROC
- mov ax,_at_data
- mov ds,ax
- mov eax,10000h EAX 10000h
- add eax,40000h EAX 50000h
- sub eax,20000h EAX 30000h
- call DumpRegs
- exit
- main ENDP
- END main
14Below I copied the previous16-bit example to my
masm directory to run it
- C\MASM615gtmake16 example16
- Assembling example16.asm
- Volume in drive C has no label.
- Volume Serial Number is 84F7-99D3
- Directory of C\Masm615
- 08/18/2006 1114 AM 343
example16.asm - 08/18/2006 1115 AM 6,824
example16.exe - 08/18/2006 1115 AM 7,961
example16.lst - 08/18/2006 1115 AM 2,424
example16.obj - 4 File(s) 17,552 bytes
- 0 Dir(s) 144,214,294,528 bytes
free - Press any key to continue . . .
- C\MASM615gtexample16
- EAX00030000 EBX00000000 ECX000000FF
EDX00001050 - ESI00000000 EDI00000400 EBP0000091E
ESP00000400 - EIP0000001A EFL00003206 CF0 SF0 ZF0
OF0
15Format and examples
- Here is another example, a 32 bit assembly
program which assigns values to 3 registers and
does some simple arithmetic. A procedure (not
shown) is used to dump register contents to the
DOS window. - The example shown is from chapter 3.
16program from the text shown earlier in textpad
youll need to run make32.bat to assemble it.
- TITLE Add and Subtract (AddSub.asm)
- This program adds and subtracts 32-bit
integers. - Last update 2/1/02
- INCLUDE Irvine32.inc
- .code
- main PROC
- mov eax,10000h EAX 10000h
- add eax,40000h EAX 50000h
- sub eax,20000h EAX 30000h
- call DumpRegs
- exit
- main ENDP
- END main
17the blackscreen DOS window assemble run the
file
- Assembling addsub.asm
- Volume in drive C has no label.
- Volume Serial Number is 84F7-99D3
- Directory of C\Masm615
- 8/18/2006 1123 AM 316 AddSub.asm
- 8/18/2006 1124 AM 28,710 addsub.exe
- 8/18/2006 1124 AM 33,300 addsub.ilk
- 8/18/2006 1124 AM 15,256 addsub.lst
- 8/18/2006 1124 AM 6,938 addsub.map
- 8/18/2006 1124 AM 3,684 addsub.obj
- 8/18/2006 1124 AM 91,136 addsub.pdb
- 7 File(s) 179,340 bytes
- 0 Dir(s) 144,214,036,480 bytes
free - ress any key to continue . . .
- \MASM615gtaddsub
- EAX00030000 EBX7FFDD000 ECX0012FFB0
EDX7C90EB94 - ESI00000000 EDI00000014 EBP0012FFF0
ESP0012FFC4 - EIP00401024 EFL00000206 CF0 SF0 ZF0
OF0
18More remarks
- The 16-bit programs use an include file
irvine16.inc and the 32-bit programs use
irvine32.inc. - These are just text files containing assembly
code, but they must be in the current directory
for the assembler to find and include them in the
listing. - Well write much of the code they contain as the
semester progresses.
19A fancier example Using procedures from Chapt 5
- This example uses procedures which the author
provides and which are not shown here to
read/write ints and write strings. We will use
these utilities for quite a while, until we learn
to replace them with our own later in the course. - They are
- writestring
- readint
- writeint
20Fancier example page 1
- TITLE IOExamples (IOEx.asm)
- INCLUDE Irvine32.inc
- CR 0Dh carriage return
- LF 0Ah line feed
- .data this is the data for the program
- prompt2 BYTE "Enter a 32-bit signed int",0
- prompt4 BYTE "enter another",0
- result byte "answer is",0
- val1 DWORD ?
- val2 dword ?
21page 2, code section
- .code
- main PROCstart a proc called main
- Set text color to black text on white
background - mov eax,black (white 16)
- call SetTextColor
- call Clrscr clear the screen
- mov edx,OFFSET prompt2 "Enter a 32-bit..."
- call WriteString
- call ReadInt input the integer
- mov val1,eax save in a variable
- call Crlf new line
- call WriteInt display in signed decimal
- mov edx,OFFSET prompt4 "another..."
- call WriteString
- call ReadInt input the integer
-
22page 3, more code
- mov val2,eax save in a variable...maybe use
it later - call Crlf new line
- call WriteInt display in signed decimal
- call Crlf
- mov ebx,val1
- add eax,ebx
- mov edx, offset result
- call WriteString
- call Crlf new line
- call WriteInt display in signed decimal
- call Crlf
- exit
- main ENDPend a proc named main
- END mainname of proc to run when this file is
loaded
23Output to a white popup DOS window
- Enter a 32-bit signed int123456
- 123456enter another900000
- 900000
- answer is
- 1023456
- C\Masm615\Examples\ch05gt
24An assembly tutorial
- I provided a tutorial (link on classpage)
detailing how to install the 4th edition software
and examples we will use to your computer, and
showing how to run them. - If this ppt is not clear, go run the tutorial.