Assembly Lab2
Assembly Lab2
Assembly Lab2
Engineering Faculty
Department of Computer Engineering
ECOM 2125: Assembly Language LAB
Lab # 2
Introduction to Assembly
Language Programming
February, 2014
The general-purpose registers are primarily used for arithmetic and data movement. Each
register can be addressed as either a single 32-bit value or a 16-bit value. Some 16-bit registers
(AX, BX, CX, and DX) can be also addressed as two separate 8-bit values. For example, AX can be
addressed as AH and AL, as shown below.
The .DATA is a directive that defines an area in memory for the program data. The program's
variables should be defined under this directive. The assembler will allocate storage for these
variables and initialize their locations in memory.
The .CODE is a directive defines the code section of a program. The code is a sequence of
assembly language instructions. These instructions are translated by the assembler into
machine code and placed in the code area in memory.
INCLUDE Irvine32.inc
The INCLUDE directive causes the assembler to include code from another file. We will include
Irvine32.inc that specifies basic input and output procedures provided by the book author Kip
Irvine, and that can be used to simplify programming. These procedures are defined in the
Irvine32.lib library that we will link to the programs that we will write.
main PROC
main ENDP
Under the code segment, we can define any number of procedures. As a convention, we will
define the first procedure to be the main procedure. This procedure is defined using the PROC
and ENDP directives.
exit
The exit at the end of the main procedure is used to terminate the execution of the program
and exit to the operating system. Note that exit is a macro. It is defined in Irvine32.inc and
provides a simple way to terminate a program.
END main
The END is a directive that marks the last line of the program. It also identifies the name (main)
of the programs startup procedure, where program execution begins.
Instructions
The above program uses 3 instructions.
The mov instruction is used for moving data.
The add instruction is used for adding two value.
The sub instruction is used for subtraction.
An instruction is a statement executed by the processor at runtime after the program has been
loaded into memory and started. An instruction contains four basic parts:
Open the source file addsub.asm from the File menu if it is not already opened. Watch the
registers by selecting Registers in the View menu or by pressing Alt+4. You can make the
Registers window floating or you can dock it as shown above.
Place the cursor at the beginning of the main procedure and press F7 to start the execution of
the main procedure as shown above. Press F10 to step through the execution of the main
procedure. Observe the changes to the EAX register after executing each instruction.
Homework Exercises
1. Using the addsub program as a reference, write a program that moves four integers into
the EAX, EBX, ECX, and EDX registers and then accumulates their sum into the EAX
register. Trace the execution of the program and view the registers using the windows
debugger.
2. Rewrite the above program using the 16-bit registers: AX, BX, CX, and DX.
Best Wishes