0% found this document useful (0 votes)
17 views

Lab_1

The lab manual introduces assembly language programming, focusing on data movement operations such as loading values into registers, copying data between registers, and transferring data from memory to registers. It covers the Intel IA-32 processor architecture, basic program execution registers, the FLAT memory model, and provides a program template for writing assembly code. Additionally, it includes practical exercises and questions to reinforce understanding of the concepts presented.

Uploaded by

haxansaleem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Lab_1

The lab manual introduces assembly language programming, focusing on data movement operations such as loading values into registers, copying data between registers, and transferring data from memory to registers. It covers the Intel IA-32 processor architecture, basic program execution registers, the FLAT memory model, and provides a program template for writing assembly code. Additionally, it includes practical exercises and questions to reinforce understanding of the concepts presented.

Uploaded by

haxansaleem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

CSC-221 Lab Manual Lab 1: Introduction to Assembly Language Programming

Lab 1: Introduction to Assembly Language Programming

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

1. Intel IA-32 Processor Architecture:


Intel introduced the 8086 processor in 1979. It has a 20-bit address bus and a 16-bit
data bus.The maximum physical memory that this processor can access is 220 bytes
or 1MB. This processor uses segmentation to address memory. The maximum size
of a segment is restricted to 216 bytes or 64 KB, since all registers were 16 bits at
that time.With the advent of 32-bit processors, starting with the 80386 in 1985, and
continuing up with the latest Pentium family of processors, Intel has introduced a
32-bit architecture known as IA-32. This family of processors allows the use of 32-
bit addresses that can address up to 4 Gigabytes of memory. These 32-bit processors
remove the limitations of the earlier 16-bit 8086 processor.

2. Basic Program Execution Registers:


There are eight 32-bit general-purpose registers (EAX, EBX, ECX, EDX, ESI, EDI,
EBP,ESP), a 32-bit register that holds the processor flags (EFLAGS), and a 32-bit
instruction pointer (EIP). These registers are shown below.

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

3. FLAT Memory Model and Protected-Address Model


The FLAT memory model is used in 32-bit operating systems (like Windows XP)
running on a 32-bit processor. Each program addresses up to 4GB of memory. All
code and data go into a single 32-bit (4- GigaByte) address space. Linear 32-bit
addresses are used in each program to access the program instructions and data in
memory.

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.

4. FLAT Memory Model Program Template


Writing an assembly language program is a complicated task, particularly for a
beginner. We will simplify this task by hiding those details that are irrelevant. We
will use the following template for writing FLAT memory programs. This template
consists of three types of statements: executable instructions, assembler directives,
Prepared by: Asad Ullah Khan Iqra University H-9 Campus Islamabad
CSC-221 Lab Manual Lab 1: Introduction to Assembly Language Programming

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.

5. Loading Values into Registers:


The following program loads values (constants) into different registers.
TITLE Add and Subtract (loadvalues.asm)
; This program loads values into registers
.686
.MODEL flat, stdcall
.STACK

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

MOV AX, 1234h ; Load 1234h into AX


MOV BX, 5678h ; Load 5678h into BX
MOV CX, 0Ah ; Load 0Ah into CX
MOV DX, 0FFh ; Load 0FFh into DX
MOV AL, 42h ; Load 42h into AL
MOV BL, 99h ; Load 99h into BL
MOV AH, 00h ; Load 00h into AH

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.

6. Copying Data Between Registers:


In the following program you will learn how to copy data between registers of the
same size.
TITLE Add and Subtract (movevalues.asm)
; This program loads values into registers
.686
.MODEL flat, stdcall
.STACK
INCLUDE Irvine32.inc
.code
main PROC

MOV AX, 1234h ; Load 1234h into AX


MOV BX, AX ; Copy the value of AX into BX
MOV CL, AH ; Copy the high byte of AX into CL

Prepared by: Asad Ullah Khan Iqra University H-9 Campus Islamabad
CSC-221 Lab Manual Lab 1: Introduction to Assembly Language Programming

MOV DH, BL ; Copy the low byte of BX into DH

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 register pairs.

7. Memory to Register Transfer:


In this program you will learn how to transfer data from memory to registers using
different addressing modes.
TITLE Add and Subtract (movevalues.asm)
; This program loads values into registers
.686
.MODEL flat, stdcall
.STACK
INCLUDE Irvine32.inc
.DATA
myWord word 5A2Bh
myByte byte 0C3h
.code
main PROC

MOV AX, myWord ; Direct addressing: Load myWord into AX


MOV BL, myByte ; Direct addressing: Load myByte into BL
MOV SI, OFFSET myWord ; Load the address of myWord into SI
;Indirect addressing: Load the value at
;the address pointed to by SI into CX
MOV CX, [SI]

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.

TITLE Add and Subtract (addsub.asm)


; This program adds and subtracts integers
.686
.MODEL flat, stdcall
.STACK
INCLUDE Irvine32.inc
.code
main PROC
mov eax, 60000h ; EAX = 60000h
add eax, 80000h ; EAX = EAX + 80000h
sub eax, 20000h ; EAX = EAX - 20000h
exit
main ENDP
END main

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

You might also like