; Example 1: adds 158 to number in memory
; Author: R. Detmer
; Date: 10/2004
.386.MODEL FLAT
ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD.STACK 4096 ; reserve 4096-byte stack (*)
.DATA ; reserve storage for data (*)number DWORD -105
sum DWORD ?
.CODE ; start of main program code (*)
_start:
mov eax, number ; sum = number + 158
add
eax, 158
mov sum, eax
INVOKE ExitProcess, 0 ; exit with return code 0 (*)
PUBLIC _start ; make entry point public (*)
END ; end of source code (*)
There are other options, and other assemblers want different things. To run the program in masm32, you must put _start on the last line after END (that is, END _start) to signify the entry point. The label does not have to begin with an underbar and does not have to be called start.
The flat model is used under Windows, which does not support segmentation.
Caps are not necessary or expected or even preferred.
The book puts the directives (starting with dots) at the left margin; I prefer this format. You can do whichever you prefer.
.386 can be .486 or .586; this line enables assembler statements used only in these processors and higher. .386 enables 32-bit addressing and 32-bit operands; that is sufficient for our purposes.
DWORD is the same as dword and as dd
WORD is the same as word and as dw
BYTE is the same as byte and as db
It is not possible to use int 21h in these versions of assembler under Windows. ExitProcess (from kernel32.lib) enables exiting the program with a return code of 0. Similarly, printing results is carried out through higher level macros and/or C commands rather than through int 21h.
This text does not provide any files necessary for printing, but I have made them available (with Detmer's permission) on my Web site.