MASM Made Simple: Easy Code Snippets to Start

Written by

in

5 Easy Code Examples for MASM Beginners Assembly language can feel intimidating at first. Microsoft Macro Assembler (MASM) simplifies this by providing a structured way to write low-level code for Windows environments. The best way to learn MASM is by examining and running basic programs.

Here are five beginner-friendly MASM code examples to help you understand syntax, memory layout, and registers. 1. The Classic “Hello, World!”

Every programming journey starts here. This example demonstrates how to set up a basic MASM structure and print text to the console using a standard Windows API function.

.386 .model flat, stdcall .stack 4096 ExitProcess PROTO, dwExitCode:DWORD INCLUDE Irvine32.inc .data message DB “Hello, World!”, 0Dh, 0Ah, 0 .code main PROC mov edx, OFFSET message call WriteString INVOKE ExitProcess, 0 main ENDP END main Use code with caution.

Key Concept: .data holds your variables. OFFSET tells the CPU the memory address where the string begins. WriteString (from Kip Irvine’s library) reads that address until it hits the 0 byte (null terminator). 2. Basic Addition with Registers

Registers are the CPU’s internal high-speed storage locations. This program shows how to move integers into registers and add them together.

.386 .model flat, stdcall .stack 4096 ExitProcess PROTO, dwExitCode:DWORD .code main PROC mov eax, 5 ; Move the value 5 into the EAX register mov ebx, 10 ; Move the value 10 into the EBX register add eax, ebx ; EAX = EAX + EBX (EAX now holds 15) INVOKE ExitProcess, 0 main ENDP END main Use code with caution.

Key Concept: mov copies data from the right operand to the left operand. add modifies the destination operand (the first one) by adding the source operand to it. 3. Subtracting Variables from Memory

Instead of keeping numbers directly in registers, you can store them in RAM variables. This example defines variables in the data segment and subtracts them.

.386 .model flat, stdcall .stack 4096 ExitProcess PROTO, dwExitCode:DWORD .data val1 DWORD 25 val2 DWORD 11 result DWORD ? .code main PROC mov eax, val1 ; Load 25 into EAX sub eax, val2 ; Subtract 11 from EAX (EAX now holds 14) mov result, eax ; Store the final answer in ‘result’ INVOKE ExitProcess, 0 main ENDP END main Use code with caution.

Key Concept: DWORD stands for Doubleword, which allocates 4 bytes of memory. The ? symbol defines an uninitialized variable, meaning its value will be determined during runtime. 4. Implementing a Basic Loop

Loops allow you to repeat instructions. In MASM, the LOOP instruction relies specifically on the ECX register as a counter.

.386 .model flat, stdcall .stack 4096 ExitProcess PROTO, dwExitCode:DWORD .code main PROC mov ecx, 5 ; Set loop counter to 5 mov eax, 0 ; Clear EAX to 0 L1: inc eax ; Increment EAX by 1 loop L1 ; Decrement ECX, jump to L1 if ECX > 0 INVOKE ExitProcess, 0 main ENDP END main Use code with caution.

Key Concept: The LOOP instruction automatically subtracts 1 from ECX every time it runs. If ECX is not zero, it jumps back to the specified label (L1). EAX will equal 5 when this loop finishes. 5. Conditional Logic (If/Else Equivalent)

Assembly language does not have built-in if statements. Instead, you compare values using cmp and use conditional jumps like jg (jump if greater) to control the flow of execution.

.386 .model flat, stdcall .stack 4096 ExitProcess PROTO, dwExitCode:DWORD .data num1 DWORD 40 num2 DWORD 20 max DWORD ? .code main PROC mov eax, num1 cmp eax, num2 ; Compare num1 (EAX) and num2 jg Num1IsGreater ; If EAX > num2, jump to label ; If not greater, execution continues here mov ebx, num2 mov max, ebx jmp EndProg ; Skip the next section Num1IsGreater: mov max, eax EndProg: INVOKE ExitProcess, 0 main ENDP END main Use code with caution.

Key Concept: cmp subtracts the second operand from the first behind the scenes to set CPU flags. The conditional jump instruction (jg) reads those flags to decide whether to change the program execution path. If you want to try running these, tell me:

What IDE or editor are you currently using? (Visual Studio, SASM, etc.)

Do you need help setting up the Irvine32 library for the first example?

I can provide step-by-step instructions to compile your first file.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *