From the course: Learning Assembly Language

Setting up a program skeleton - Python Tutorial

From the course: Learning Assembly Language

Start my 1-month free trial

Setting up a program skeleton

- [Instructor] We've already seen a couple of simple programs in Assembler, so let's look at their structure. In our first MASM program, we can see that we start with an include file from the MASM32 folder. Let's see what this looks like. The file we've included starts after its initial comments by setting some key MASM configuration items. At line 32, we can see the library specifies a 486 architecture, which sets the assembler up for 32 bit code. It uses the flat 32 bit addressing memory model and establishes case sensitivity. It then brings in a set of include files and their corresponding libraries for Windows so that we're ready to make calls to the Windows application programming interface, Note that we have include statements for kernel32.lib and user32.lib. The next thing we see in our MASM program is that our code is divided into two sections. The first starting at line two with a .data declaration and the second starting at line four with .code. At line five, we have the start label and at line eight, the end start statement identifies start as the entry point for the program. So the basic structure for a program is to set up the processor and memory model and bring in any external files, which in this case we do by including masm32rt.inc, define data in a .data segment, define code in the .code segment, and then end the program with the name of the entry point. Let's close the project and select File, New Project. We'll again, make it a MASM project and the classic console application. And we'll take the default name. Easy Code has provided a program skeleton for us. This has a couple of extra sections that we didn't use. The .Const section is used to declare Constance. This allows us to have a named constant rather than just using the value in our code. For instance, we could add a line, answer EQU 42, and note that you can also use equal sign instead of EQU, and then at line 18, we can put in mov, eax comma, answer. If we select Build, Compile Project1, it assembles just fine. The data question mark section at line five is used to declare variables that we don't need to initialize. This saves a significant amount of space in the program by not having to carry a pre-initialized data area. The skeleton is designed to allow us to use the Windows API and so starts by making a call at line 13 to get module handle, which returns a handle to the program itself in the register Eax. This is then stored in hinst so that we can use it when we need to during our coding. The skeleton code uses an exit process as the preferred method of closing down the program which also ensures any attached DLLs are properly closed down. Setting zero on the exit process indicates that the program is exiting with no errors. Okay, I'll close this project. We'll not be focusing on the intricacies of creating fully featured Windows GUI programs in Assembler, but let's see what the program skeleton looks like when we do elect to build a standard Windows executable. We'll select File, New Project, and we'll select a MASM Windows executable. MASM Windows executable file. The program skeleton is somewhat more complicated as this provides the code necessary to create a window. Note in particular that at line 18 it invokes to get command line and then invokes its own WinMain function which appears further down. We'll learn more about Windows calls later in the course.

Contents