MP - Lab Manual SRCOE
MP - Lab Manual SRCOE
MP - Lab Manual SRCOE
1
Title: Write an X86/64 ALP to accept five 64 bit Hexadecimal numbers
from user and store them in an array and display the accepted numbers
Signature of Staff:
Experiment No. 1
Aim: Write an X86/64 ALP to accept five 64 bit Hexadecimal numbers from user and store them
in an array and display the accepted numbers.
Objectives:
1) To be familiar with the format of assembly language program structure and instructions.
2) To study the format of assembly language program along with different assembler directives
and different functions of the NASM.
Environment:
2) OS:- Windows XP (16 bit Execution ), Fedora 18 (32 & 64 bit Execution)
Software Requirements:
2. OS:- Windows XP (16 bit Execution ), Fedora 18 (32 & 64 bit Execution)
Theory:
Each personal computer has a microprocessor that manages the computer's arithmetical, logical
and control activities. Each family of processors has its own set of instructions for handling
various operations like getting input from keyboard, displaying information on screen and
performing various other jobs. These set of instructions are called 'machine language instruction'.
Processor understands only machine language instructions which are strings of 1s and 0s.
However machine language is too obscure and complex for using in software development. So the
low level assembly language is designed for a specific family of processors that represents various
instructions in symbolic code and a more understandable form. Assembly language is a low-level
programming language for a computer, or other programmable device specific to particular
computer architecture in contrast to most high-level programming languages, which are generally
portable across multiple systems. Assembly language is converted into executable machine code
by a utility program referred to as an assembler like NASM, MASM etc.
Advantages of Assembly Language
An understanding of assembly language provides knowledge of:
Interface of programs with OS, processor and BIOS;
Representation of data in memory and other external devices;
How processor accesses and executes instruction;
How instructions accesses and process data;
How a program access external devices.
Other advantages of using assembly language are:
It requires less memory and execution time;
It allows hardware-specific complex jobs in an easier way;
It is suitable for time-critical jobs;
ALP Step By Step:
Installing NASM:
If you select "Development Tools" while installed Linux, you may NASM installed along with the
Linux operating system and you do not need to download and install it separately. For checking
whether you already have NASM installed, take the following steps:
Open a Linux terminal.
Type whereis nasm and press ENTER.
If it is already installed then a line like, nasm: /usr/bin/nasm appears. Otherwise, you will
see justnasm:, then you need to install NASM.
To install NASM take the following steps:
Open Terminal and run below commands:
sudo apt-get update
sudo apt-get install nasm
Instructions needed:
EQU- Assign single absolute values to symbols.
MOV- Copies byte or word from specified source to specified destination
CALL- The CALL instruction causes the procedure named in the operand to be executed. JNZ-
Jumps if not equal to Zero 16
JNC-Jumps if no carry is generated
ALGORITHM:
1 Start
2. Declare & initialize the variables in .data section.
3. Declare uninitialized variables in .bss section.
4. Declare Macros for print and exit operation.
5. Initialize pointer with source address of array.
6. Initialize count for number of elements.
7. Put the complete summed rbx value to arr[n]
8. Max display of 16 characters and rsi points to _output[16]
9. Dividing by base 16
10. Terminate the process.
11. Declare the Procedure.
12. Stop.
Conclusion:
Here we have accepted 64 bit Hexadecimal numbers from user and store them in an array and
display the accepted numbers.
Experiment No. 2
Title: Write an X86/64 ALP to accept a string and to display its length .
Signature of Staff:
Experiment No. 2
Aim: Write an X86/64 ALP to accept a string and to display its length.
Objectives:
Environment:
2. OS:- Windows XP (16 bit Execution ), Fedora 18 (32 & 64 bit Execution)
Software Requirements
2. OS:- Windows XP (16 bit Execution ), Fedora 18 (32 & 64 bit Execution)
MACRO:
Procedures or subroutines are very important in assembly language, as the assembly language
programs tend to be large in size. Procedures are identified by a name. Following this name, the
body of the procedure is described which performs a well-defined job. End of the procedure is
indicated by a return statement.
Syntax
ALGORITHM:
1 Start
10. setting rdx to null without setting a null byte (a tip i saw on reddit) needed to clean dl for use
11. Declare the Procedure for ascii conversion.
12. Stop
Conclusion
Here we have studied how to accept a string and to display its length.
Experiment No. 3
Title: Write an X86/64 ALP to find the largest of given
Byte/Word/Dword/64-bit numbers.
Signature of Staff:
Experiment No. 3
Aim: Write an X86/64 ALP to find the largest of given Byte/Word/Dword/64-bit numbers.
Objectives:
Environment:
2. OS:- Windows XP (16 bit Execution ), Fedora 18 (32 & 64 bit Execution)
Software Requirements:
2. OS:- Windows XP (16 bit Execution ), Fedora 18 (32 & 64 bit Execution)
Theory:
Registers in 80386:
General Purpose Register: EAX, EBX, ECX, EDX
Pointer register: ESP, EBP
Index register: ESI, EDI
Segment Register: CS, FS, DS, GS, ES, SS
Eflags register: EFLAGS
System Address/Memory management Registers : GDTR, LDTR, IDTR
Control Register: Cr0, Cr1, Cr2, Cr3
Debug Register : DR0, DR,1 DR2, DR3, DR4, DR5, DR6, DR7
Test Register: TR0, TR,1 TR2, TR3, TR4, TR5, TR6, TR7
EAX AX AH,AL
EBX BX BH,BL
ECX CX CH,CL
EDX DX DH,DL
EBP BP
EDI DI
ESI SI
ESP
ALGORITHM:
1 Start
12. Stop.
Conclusion
Thus we have found the largest of given Byte/Word/Dword/64-bit numbers
Experiment No. 4
Signature of Staff:
Experiment No. 4
Aim: Write X86/64 ALP to perform multiplication of two 8-bit hexadecimal numbers. Use
successive addition and add and shift method. (use of 64-bit registers is expected).
Objectives:
To apply instruction set for implementing X86/64 bit assembly language programs
Environment:
Software Requirements :
2. OS:- Windows XP (16 bit Execution ), Fedora 18 (32 & 64 bit Execution)
Theory:
Instructions Needed:
MOV : Move or copy word
The method taught in school for multiplying decimal no. is based on calculated partial
products, shifting it to the left & then adding them together. Shift & add multilplication
is similar to the multiplication performed by paper & pencil. This method adds the
multiplicand X to itself Y times where Y denotes the multiplier. To multiply two nos. by
paper & pencil placing the intermediate product in the appropriate positions to the left
of earlier product.
Step 1:
AX=11 + 11 = 22H
Step 2:
Decrement count =3
Check for carry, carry is not there So Add with itself
AX=22+22=44H
Rotate BL to left
Step 3:
AX=44+44=88H
Rotate BL to left
AX=88+88=110H
Rotate BL to left
Step 5:
Add Ax, BX
0110+0000=0110H
i.e., 11H+10H=0110H
MATHEMATICAL MODEL:
Such that there exists function fx1: x{0, 1} X2={s the two digit multiplicate }
Let x2= b7 b6 b5 b4 b3 b2 b1 b0
Where xb1 € x1 Here exists a function fx2:x2 {00h, 01h, 02h, ...... ffh}
F3= display
Conclusion- From this program we have studied the multiplication of 8 bit nos. and in this
we have studied and implemented the program of successive addition and shift & add
method.
Experiment No. 5
Signature of Staff:
Experiment No. 5
Aim: Write a switch case driven X86/64 ALP to perform 64-bit hexadecimal arithmetic operations (+,-,*, /)
using suitable macros. Define procedure for each operation.
Objectives:
To apply instruction set for implementing X86/64 bit assembly language programs
Environment:
Software Requirements :
2. OS:- Windows XP (16 bit Execution ), Fedora 18 (32 & 64 bit Execution)
Theory:
Conditional Transfer Instructions The conditional transfer instructions are jumps that may or may not
transfer control, depending on the state of the CPU flags when the instruction executes.
The conditional jumps that are listed as pairs are actually the same instruction. The assembler
provides the alternate mnemonics for greater clarity within a program listing. Conditional jump instructions
contain a displacement which is added to the EIP register if the condition is true. The displacement may be a
byte, a word, or a doubleword. The displacement is signed; therefore, it can be used to jump forward or
backward.
Loop Instructions:
The loop instructions are conditional jumps that use a value placed in ECX to specify the number of
repetitions of a software loop. All loop instructions automatically decrement ECX and terminate the
loop when ECX=0. Four of the five loop instructions specify a condition involving ZF that terminates
the loop before ECX reaches zero.
LOOP (Loop While ECX Not Zero) is a conditional transfer tha automatically
decrements the ECX register before testing ECX for the branch condition. If ECX is non- zero, the
program branches to the target label specified in the instruction. The LOOP instruction causes the
repetition of a code section until the operation of the LOOP instruction decrements ECX to a value of
zero. If LOOP finds ECX=0, control transfers to the instruction immediately following the LOOP
instruction. If the value of ECX is initially zero, then the LOOP executes 232 times.
Conclusion Thus we have studied a switch case drivenX86/64ALP to perform 64-
bithexadecimal arithmetic operations.
Experiment No. 6
Title: Write X86/64 ALP to switch from real mode to protected mode
and display the values of GDTR, LDTR, IDTR, TR and MSW Registers
Signature of Staff:
Experiment No. 6
Aim: Write X86/64 ALP to switch from real mode to protected mode and display the values of
GDTR, LDTR, IDTR, TR and MSW Registers.
Objectives:
To apply instruction set for implementing X86/64 bit assembly language programs
Environment:
Software Requirements :
2. OS:- Windows XP (16 bit Execution ), Fedora 18 (32 & 64 bit Execution)
THEORY:
Four registers of the 80386 locate the data structures that control segmented memory
management called as memory management registers:
GDTR :Global Descriptor Table Register
These register point to the segment descriptor tables GDT. Before any segment register
is changed in protected mode, the GDT register must point to a valid GDT. Initialization
of the GDT and GDTR may be done in real-address mode. The GDT (as well as LDTs)
should reside in RAM, because the processor modifies the accessed bit of descriptors.
The instructions LGDT
and SGDT give access to the GDTR.
LDTR :Local Descriptor Table Register
These register point to the segment descriptor tables LDT. The LLDT instruction loads
a linear base address and limit value from a six-byte data operand in memory into the
LDTR. The SLDT instruction always store into all 48 bits of the six-byte data operand.
IDTR Interrupt Descriptor Table Register
This register points to a table of entry points for interrupt handlers (the IDT). The
LIDT instruction loads a linear base address and limit value from a six-byte data
operand in memory into the IDTR. The SIDT instruction always store into all 48 bits
of the six-byte data operand.
TR Task Register
This register points to the information needed by the processor to define the current
task. These registers store the base addresses of the descriptor tables (A descriptor
table is simply a memory array of 8-byte entries that contain
Descriptors and descriptor stores all the information about segment) in the linear
address space and store the segment limits.
SLDT: Store Local Descriptor Table Register
Operation: DEST ← 48-bit BASE/LIMIT register
contents;
Description: SLDT stores the Local Descriptor Table Register (LDTR) in the two-byte
register or memory location indicated by the effective address operand. This register
is a selector that points into the Global Descriptor Table. SLDT is used only in
operating system software. It is not used in application programs.
Flags Affected: None
SGDT: Store Global Descriptor Table Register Operation:
DEST ← 48-bit BASE/LIMIT register contents;
Description: SGDT copies the contents of the descriptor table register the six bytes of memory
indicated by the operand. The LIMIT field of the register is assigned to the first word at the effective
address. If the operand-size attribute is 32 bits, the next three bytes are assigned the BASE field of the
register, and the fourth byte is written with zero. The last byte is undefined. Otherwise, if the operand-
size attribute is 16 bits, the next 4 bytes are assigned the 32-bit BASE field of the register. SGDT and
SIDT are used only in operating system software; they are not used in application programs.
Flags Affected: None
Description: SIDT copies the contents of the descriptor table register the six bytes of memory
indicated by the operand. The LIMIT field of the register is assigned to the first word at the effective
address. If the operand-size attribute is 32 bits, the next three bytes are assigned the BASE field of the
register, and the fourth byte is written with zero. The last byte is undefined. Otherwise, if the operand-
size attribute is 16 bits, the next 4 bytes are assigned the 32-bit BASE field of the register. SGDT and
SIDT are used only in operating system software; they are not used in application programs.
Flags Affected: None
ALGORITHM:
Conclusion- In this way, we use GDTR, LDTR and IDTR in Real mode.
Experiment No. 7
Title: Write X86/64 ALP to convert 4-digit Hex number into its equivalent
BCD number and 5- digit BCD number into its equivalent HEX number
Signature of Staff:
Experiment No. 7
Aim: Write X86/64 ALP to convert 4-digit Hex number into its equivalent BCD number and 5- digit BCD
number into its equivalent HEX number. Make your program user friendly to accept the choice from user
for: (a) HEX to BCD b) BCD to HEX (c) EXIT. Display proper strings to prompt the user while accepting the
input and displaying the result. (Wherever necessary, use 64-bit registers).
Objectives:
To apply instruction set for implementing X86/64 bit assembly language programs
Environment:
Software Requirements :
2. OS:- Windows XP (16 bit Execution ), Fedora 18 (32 & 64 bit Execution)
Fme = { F1,F2,F3,F4}
Where F1= Accept X4
F2= If X4=1, Call Fme1
F3= If X4=2,Call Fme2
F4= If X4=3, Call Fme3
THEORY:
1. Hexadecimal to BCD conversion:
MUL: Unsigned Multiply. For 8-bit operand multiplication result will be stored in
AX and for 16-bit multiplication result is stored in DX:AX Commands.
• To assemble
• To link
ld –o hello hello.o
• To execute
./hello
ALGORITHM:
1. Start
2. Initialize data section
3 Display the Menu Message.
4 Accept the choice from the user.
5 If choice=1 then call Hex to bcd procedure. If choice=2 then call Bcd to Hex If
Procedure
choice=3 then call exit procedure
6 Hex to BCD Procedure:
a) Accept 4 Digit Hexadecimal Number.
b) Number=Number/10 & Number=Quotient
c) Push the remainder on the stack
d) If Number=0, Go to next step otherwise go to step b
e) Pop remainder , Convert into ascii & display untill all remainder are popped out
7 Bcd to hex Procedure:
a. RESULT=0
b. Accept BCD Digit
c. Check whether all BCD Digits are accepted. If YES then go to
step e otherwise go to next step
d. RESULT= RESULT*10 + BCD Digit accepted e. Display RESULT
FLOW CHART:
Conclusion- Hence we conclude that we can perform the Hex to BCD conversion &
BCD to hex Conversion.
Signature of Staff:
Experiment No. 8 & 9
Aim: Write X86/64 ALP to perform non-overlapped block transfer with and without string specific
instructions. Block containing data can be defined in the data segment.
Objectives:
To apply instruction set for implementing X86/64 bit assembly language programs
Environment:
Software Requirements :
2. OS:- Windows XP (16 bit Execution ), Fedora 18 (32 & 64 bit Execution)
Theory:
Non-overlapped blocks:
In memory, two blocks are known as non-overlapped when none of the element is common.
AD
9D Memory Block 2
78
09
FF
28
Memory Block 1
F4
3F
FD
BB
AA
0D
28
F4
Memory Block 1
3F
FD
BB
AA
0D
Instructions needed:
3. AND-AND each bit in a byte or word with corresponding bit in another byte or word
i) Start
ii) Initialize data section
iii) Initialize RSI to point to source block
iv) Initialize RDI to point to destination block
v) Initialize the counter equal to length of block
vi) Get byte from source block & copy it into destination block
vii) Increment source & destination pointer
viii) Decrement counter
ix) If counter is not zero go to step vi
x) Display Destination Block
xi) Stop
i) Start
ii) Initialize data section
iii) Accept the overlapping position from the user
iv) Initialize RSI to point to the end of source block
v) Add RSI with overlapping Position & use it as pointer to point
to End of Destination Block.
vi) Initialize the counter equal to length of block
vii) Get byte from source block & copy it into destination block
viii) Decrement source & destination pointer
ix) Decrement counter
x) If counter is not zero go to step vii
xi) Display Destination Block
xii) Stop
START
Increment SI & DI
CX = CX - 1
If count not zero repeat transfer of data
Display result
STOP
Conclusion- Hence we conclude that we can perform non-overlapped & overlapped block Transfer with &
without using string instructions.
Experiment No. 10
Title:
Write
x86 ALP to find the factorial of a given integer number on a command line by using recursion. Explicit
stack manipulation is expected in the code.
Objectives:
To apply instruction set for implementing X86/64 bit assembly language programs
Environment:
Software Requirements :
2. OS:- Windows XP (16 bit Execution ), Fedora 18 (32 & 64 bit Execution)
Theory:
A recursive procedure is one that calls itself. There are two kind of recursion: direct
and indirect. In direct recursion, the procedure calls itself and in indirect recursion,
the first procedure calls a second procedure, which in turn calls the first procedure.
Instructions needed:
1. AND-AND each bit in a byte or word with corresponding bit in another byte or word
8. ADD- ADD instructions are used for performing simple addition of binary data
in byte, word and doubleword size, i.e., for adding 8-bit, 16-bit or 32-bit
operands, respectively.
ALGORITHM:
MATHEMATICAL MODEL:
Let S = { s , e , X, Y, │Φs }
The factorial function is formally defined by the product
This notation works in a similar way to summation notation (Σ), but in this case we
multiply rather than add terms. For example, if n = 4, we would substitute k = 1, then k
= 2, then k = 3 and finally k = 4 and write:
expression: n!
Commands
• To assemble
nasm –f elf 64hello.nasm -o hello.o
• To link
ld –o hello hello.o
• To execute -
./hello
Conclusion- Hence we conclude that we can perform ALP to find out factorial of a given integer number.
Experiment No. 11
Signature of Staff:
Experiment No. 11
Aim: Write an X86/64 ALP to count number of positive and negative numbers from the array.
Objectives:
To apply instruction set for implementing X86/64 bit assembly language programs
Environment:
Software Requirements :
2. OS:- Windows XP (16 bit Execution ), Fedora 18 (32 & 64 bit Execution)
Theory:
The following code snippet shows the use of the system call sys_exit:
MOV EAX, 1 ; system call number
(sys_exit) INT 0x80 ; call kernel
The following code snippet shows the use of the system call sys_write:
MOV EAX,4; system call number (sys_write)
MOV EBX,1; file descriptor (stdout)
MOV ECX, MSG ; message to
write MOV EDX, 4; message
length INT0x80; call kernel
Sys_read:
MOV
RAX,0
MOV RDI,0
MOV
RSI,array_name
MOV
RDX,array_size
SYSCALL
Sys_exit:
MOV RAX,60
MOV RDI,0
SYSCALL
Assembly Variables
NASM provides various define directives for reserving storage space for variables. The
define Assembler directive is used for allocation of storage space. It can be used to reserve
as well as initialize one or more bytes.
Allocating Storage Space for Initialized Data
There are five basic forms of the define directive:
Instructions needed:
MATHEMATICAL MODEL:
S = System
s =Distinct Start of
System e = Distinct End
Of System X = Set of
Inputs
Y= Set Of outputs
Fme = Central Function
Mem= Memory
Required Φs =
Constraits
X2 Source Array
Let X2 = {{b7-----b0}{ b7-------b0}--------{b7 b6 b5 b4 b3 b2 b1 b0}} where bi Є X1
There exists a function fX2 :X2 { {00h----FFh}{00h----FFh }{00h----FFh} --- }
F2 Y = ∑ ( X2)
F3 Display Output
ALGORITHM:
1 Start
2. Declare & initialize the variables in .data section.
3. Declare uninitialized variables in .bss section.
4. Declare Macros for print and exit operation.
5. Initialize pointer with source address of array.
6. Initialize count for number of elements.
7. Set RBX as Counter register for storing positive numbers count.
SRES’s
SHREE RAMCHANDRA COLLEGE OFENGINEERING
Lonikand, Pune – 412216
Department of Computer Engineering
58
SRES’s
SHREE RAMCHANDRA COLLEGE OFENGINEERING
Lonikand, Pune – 412216
Department of Computer Engineering
Flowchart:
Conclusion: Here we count the 32-bit numbers of positive and negative numbers in the array
in the assembly language.
59