assembly programming A little background on using the software Irvine 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. Machine 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 can’t 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. Assembly 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 Entering a program into textpad (in lab) Selecting tools option “build 32bit 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 06:58 AM 306 addsub.asm 08/28/2008 06:59 AM 28,711 addsub.exe 08/28/2008 06:59 AM 33,324 addsub.ilk 08/28/2008 06:59 AM 15,246 addsub.lst 08/28/2008 06:59 AM 6,938 addsub.map 08/28/2008 06:59 AM 3,714 addsub.obj 08/28/2008 06:58 AM 91,136 addsub.pdb 7 File(s) 179,375 bytes 0 Dir(s) 9,986,265,088 bytes free • Tool completed successfully Looks like this on p drive Submissions • Typically, you’ll 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. On your own machine…Run setup on the 4th edition CD to copy the assembler and text examples to your C drive Path settings • You’ll 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. extensions • .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 text’s 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 it’s name on the commandline (assuming it is in the path). • .lst marks a listing file of the assembly – we won’t use this. More 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. Here 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,@data mov ds,ax mov eax,10000h add eax,40000h sub eax,20000h call DumpRegs exit main ENDP END main ; EAX = 10000h ; EAX = 50000h ; EAX = 30000h Below I copied the previous16-bit example to my masm directory to run it C:\MASM615>make16 example16 Assembling: example16.asm Volume in drive C has no label. Volume Serial Number is 84F7-99D3 Directory of C:\Masm615 08/18/2006 11:14 AM 343 example16.asm 08/18/2006 11:15 AM 6,824 example16.exe 08/18/2006 11:15 AM 7,961 example16.lst 08/18/2006 11:15 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:\MASM615>example16 EAX=00030000 EBX=00000000 ECX=000000FF EDX=00001050 ESI=00000000 EDI=00000400 EBP=0000091E ESP=00000400 EIP=0000001A EFL=00003206 CF=0 SF=0 ZF=0 OF=0 C:\MASM615> Format 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. program from the text shown earlier in textpad : you’ll 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 the 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 11:23 AM 316 AddSub.asm 8/18/2006 11:24 AM 28,710 addsub.exe 8/18/2006 11:24 AM 33,300 addsub.ilk 8/18/2006 11:24 AM 15,256 addsub.lst 8/18/2006 11:24 AM 6,938 addsub.map 8/18/2006 11:24 AM 3,684 addsub.obj 8/18/2006 11:24 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 . . . :\MASM615>addsub EAX=00030000 EBX=7FFDD000 ECX=0012FFB0 EDX=7C90EB94 ESI=00000000 EDI=00000014 EBP=0012FFF0 ESP=0012FFC4 EIP=00401024 EFL=00000206 CF=0 SF=0 ZF=0 OF=0 More 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. • We’ll write much of the code they contain as the semester progresses. A 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 Fancier 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 ? page 2, code section .code main PROC;;start 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 page 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 ENDP;;;end a proc named main END main;;name of proc to run when this file is loaded Output to a white “popup” DOS window Enter a 32-bit signed int123456 +123456enter another:900000 +900000 answer is: +1023456 C:\Masm615\Examples\ch05> An 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.