Namal University Mianwali: Department of Computer Science
Namal University Mianwali: Department of Computer Science
Lab Manual
Course CSC-241 Computer Organization and Assembly Language
Instructor Muzamil Ahmed Session / Semester 2023-2027 (3rd)
Lecture # 01
Topic Introduction and to MASM, Lab Setup and Basic Assembly Commands
Objective The objective of this first lab is to introduce students to Microsoft
Assembler (MASM), guide them through the installation and setup
process, and familiarize them with basic assembly commands.
What is MASM?
MASM (Microsoft Macro Assembler) is a widely used assembler for the x86 family of
microprocessors. It enables developers to write programs in assembly language, which
directly interacts with the hardware of the computer. MASM is especially popular for its
compatibility with Intel processors and its support for both high-level constructs and low-
level hardware operations.
Why Use Assembly Language?
Assembly language offers direct control over hardware, enabling programmers to optimize
their code for speed and memory usage. It is often used in systems programming, real-time
applications, and situations where performance is critical.
1. Download MASM: You can download MASM from Microsoft's official website or
use the MASM32 SDK, a free version that contains all tools needed to write and
compile assembly programs. [Download Link: https://www.masm32.com/]
2. Installation: After downloading, follow the installation wizard to install MASM.
Ensure that the path to the assembler ( ml.exe) is added to your system's environment
variables to compile programs from the command line.
3. Text Editor (optional): Install the Visual Studio code from given link and install two
extensions 1). MASM and 2). x86 and x86_64 Assembly. [Download Link:
https://code.visualstudio.com/download]
Assemble-Link-Execute cycle
Step 1: A programmer uses a text editor to create an ASCII text file named the source file.
Step 2: The assembler reads the source file and produces an object file, a machine-language
translation of the program. Optionally, it produces a listing file. If any errors occur, the
programmer must return to Step 1 and fix the program.
Step 3: The linker reads the object file and checks to see if the program contains any calls to
procedures in a link library. The linker copies any required procedures from the link library,
combines them with the object file, and produces the executable file.
Step 4: The operating system loader utility reads the executable file into memory and
branches the CPU to the program’s starting address, and the program begins to execute.
You write the assembly code in a text editor and save it with an appropriate file extension
(usually .asm). The assembly code contains instructions, data definitions, and possibly
directives that the assembler will translate into machine code.
.data
; Data section (currently empty)
start:
call main ; Call the main procedure
inkey ; Wait for a key press before exiting
exit ; Exit the program
main proc
cls ; Clear the screen
print "Hello World", 13, 10 ; Print "Hello World"
followed by a newline
ret ; Return from the procedure
main endp ; End of the main procedure
The assembler converts the human-readable assembly code ( .asm) into machine code
(binary format) and produces an object file (typically .obj). The object file contains the
translated machine code instructions. Command to assemble using MASM (Microsoft Macro
Assembler):
ml assembles and links assembly language code into an object file or executable.an
object file (.obj), not an executable file.
/c switch tells MASM to assemble the code only, without linking. The output will be
an object file (.obj), not an executable file.
/Zd generates debugging information in the object file. The debugging information
helps in debugging the program with a debugger, as it includes source file references
(line numbers, variables, etc.).
This is useful for tracking down issues in your assembly code during development.
/coff tells MASM to generate the object file in COFF (Common Object File
Format), which is a standard format used for object files on Windows.
COFF is compatible with modern linkers and debuggers in Windows environments.
The linker takes the object file (hello_world.obj) and combines it with system libraries or
other object files to produce an executable file. This step resolves any external references
(e.g., library calls) and prepares the final binary. Command to link using MASM's linker:
The final step is to run the assembled and linked program (the .exe file). In the case of DOS
or Windows console applications, you can run the executable from the command prompt.
hello_world.exe
If the program is correct, it will execute and display the output (e.g., "Hello, World!" in the
console).
include \masm32\include\masm32rt.inc
.data
sumMessage db "The sum is: ", 0 ; String message to
display before the sum
buffer db 11 dup(0) ; Buffer to hold the
string representation of the sum (up to 10 digits plus null
terminator)
.code
start:
end start
Lab Tasks
Task 1: Write the assembly language program given in the Lab handout, assemble it and run
Task 2: Note down the contents of registers EAX, EBX and ECX as displayed by the
program. CLO-1
Task 3: Do the contents of register ECX match the expected result? If not, what step needs to
be taken? CLO-1
Task 4: Modify the source code to get the right result in the register ECX, re-assemble, and
re-run the program. CLO-1
Task 5: Verify that the contents of the ECX register are now correct. CLO-1
Submission Guidelines
1. Create a folder named with your name + registration number with two subdirectories:
o Lab Task 1 Code
o Lab Task 1 Report
2. The Code subdirectory must include the following files:
o program_name.asm (Assembly source code)
o program_name.obj (Object file)
o program_name.exe (Executable file)
3. The Report subdirectory should contain a detailed explanation of each task, including
relevant assembly code snippets with proper comments.