Lab_1
Lab_1
Lab Objective:
This lab introduces fundamental data movement operations in assembly language.
You will learn how to load values into registers, copy data between registers, and
transfer data from memory to registers.
Contents:
1. Intel IA-32 Processor Architecture
2. Basic Program Execution Registers
3. FLAT Memory Model and Protected-Address Model
4. FLAT Memory Model Program Template
5. Loading Values into Registers
6. Copying Data Between Registers
7. Memory to Register Transfer
Prepared by: Asad Ullah Khan Iqra University H-9 Campus Islamabad
CSC-221 Lab Manual Lab 1: Introduction to Assembly Language Programming
EAX ESI
EBX EDI
ECX EBP
EDX ESP
EFLAGS EIP
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.
Some general-purpose registers have specialized uses. EAX is called the extended
accumulator and is used by multiplication and division instructions. ECX is the
extended counter register and is used as a loop counter. ESP is the extended stack
pointer and is used to point to the top of the stack. EBP is the extended base pointer
and is used to access function parameters and local variables on the stack. ESI is
called the extended source index and EDI is called the extended destination index.
They are used by string instructions. We will learn more about these instructions
later during the semester.
The EIP register is called the extended instruction pointer and contains the address
of the next instruction to be executed. EFLAGS is the extended flags register that
consists of individual bits that either control the operation of the CPU or reflect the
outcome of some CPU operations. We will learn more about these flags later.
Prepared by: Asad Ullah Khan Iqra University H-9 Campus Islamabad
CSC-221 Lab Manual Lab 1: Introduction to Assembly Language Programming
Rather than using real memory addresses, a program uses virtual addresses. These
virtual addresses are mapped onto real (physical) addresses by the operating
system through a scheme called paging. The processor translates virtual addresses
into real addresses as the program is running. With virtual memory, the processor
runs in protected mode. This means that each program can access only the memory
that was assigned to it by the operating system and cannot access the memory of
other programs.
and macros. Executable instructions generate machine code for the processor to
execute at runtime. Assembler directives provide information to the assembler while
translating the program. Macros are shorthand for a sequence of instructions,
directives, or even other macros. We will learn more about instructions, directives,
and macros throughout the semester.
TITLE FLAT Memory Program Template (template.asm)
; Program Description:
; Author:
; Date Created:
; Last Modified:
.686
.MODEL FLAT, STDCALL
.STACK
INCLUDE Irvine32.inc
; (insert symbol definitions here)
.DATA
; (insert variables here)
.CODE
main PROC
; (insert executable instructions here)
exit ; exit to operating system
main ENDP
; (insert additional procedures here)
END main
The first line of an assembly language program is the TITLE line. This line is
optional. It contains a brief heading of the program and the disk file name. The next
few lines are line comments. They begin with a semicolon (;) and terminate with the
end of the line. They are ignored and not processed by the assembler. However, they
are used to document a program and are of prime importance to the assembly
language programmer, because assembly language code is not easy to read or
understand. Insert comments at the beginning of a program to describe the program,
its author, the date when it was first written and the date when it was last modified.
You need also comments to document your data and your code.
The .MODEL is a directive that specifies the memory configuration for the
assembly language program. For our purposes, the FLAT memory model will be
used. The .686 is a processor directive used before the .MODEL FLAT directive to
Prepared by: Asad Ullah Khan Iqra University H-9 Campus Islamabad
CSC-221 Lab Manual Lab 1: Introduction to Assembly Language Programming
provide access to the 32-bit instructions and registers available in the Pentium
Processor. The STDCALL directive tells the assembler to use standard conventions
for names and procedure calls.
The .STACK is a directive that tells the assembler to define a stack for the program.
The size of the stack can be optionally specified by this directive. The stack is
required for procedure calls. We will learn more about the stack and procedures later
during the semester.
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.
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.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:main PROC. . .main
ENDP. 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.The END
is a directive that marks the last line of the program. It also identifies the name (main)
of the program’s startup procedure, where program execution begins.
Prepared by: Asad Ullah Khan Iqra University H-9 Campus Islamabad
CSC-221 Lab Manual Lab 1: Introduction to Assembly Language Programming
INCLUDE Irvine32.inc
.code
main PROC
exit
main ENDP
END main
Assemble and link the code using the command make32. Use a Microsoft Windows
Debugger to step through the code and observe the values in the registers after each
MOV instruction. Use command windbg –QY –G filename.exe.
Task: Experiment the above program with different immediate values and register
sizes.
Prepared by: Asad Ullah Khan Iqra University H-9 Campus Islamabad
CSC-221 Lab Manual Lab 1: Introduction to Assembly Language Programming
exit
main ENDP
END main
Assemble and link the code using the command make32. Use a Microsoft Windows
Debugger to step through the code and observe the values in the registers after each
MOV instruction. Use command windbg –QY –G filename.exe.
exit
main ENDP
END main
Prepared by: Asad Ullah Khan Iqra University H-9 Campus Islamabad
CSC-221 Lab Manual Lab 1: Introduction to Assembly Language Programming
Lab Questions:
Q#1. Explain the difference between MOV AX, 1234h and MOV AX, [1234h].
Q#2. What happens if you try to copy a 16-bit register value into an 8-bit register?
Q#3. Explain the purpose of the OFFSET operator.
Q#4. What are the x86 processor’s three basic modes of operation?.
Q#5. How would you transfer a DWORD value from memory to a register?
Programming Exercise:
Q#1. Execute the following program and debug the code.
Q#2. 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.
Q#3. Rewrite the above program using the 16-bit registers: AX, BX, CX, and DX.
Prepared by: Asad Ullah Khan Iqra University H-9 Campus Islamabad