MPL Labmanual
MPL Labmanual
LAB MANUAL
SUBJECT: MICROPROCESSOR LAB (210257)
CLASS: S.E. SEMESTER: IV
ASSIGNMENT NUMBER: 1
ASSIGNMENT NO. 1
TITLE Display the five 64 bit Hexadecimal numbers from the user
PROBLEM Write an X86/64 ALP to accept five 64 bit Hexadecimal numbers from
STATEME the user and store them in an array and display the accepted numbers.
NT
/DEFINITION
OBJECTIVE To learn the procedure how to store and display N hexadecimal numbers in
memory..
OUTCOME Students are able to Understand and apply various addressing modes
and instruction set to implement assembly language programs.
1. Date
INSTRUCTIONS 2. Assignment no.
FOR WRITING 3. Problem definition
JOURNAL 4. Learning objective
5. Learning Outcome
6. Concepts related Theory
7. Algorithm
8. Test cases
10. Conclusion/Analysis
Prerequisites: COA
The Assembler is a system software that converts an assembly language code to machine code. It takes basic
Computer commands and converts them into Binary Code that Computer’s Processor can use to perform its Basic
Operations.
These instructions are assembler language or assembly language.Assembly language uses opcode for the
instructions.
An opcode basically gives information about the particular instruction. The symbolic representation of the opcode
(machine level instruction) is called mnemonics. Programmers use them to remember the operations in assembly
language.
For example ADD A,B
Here, ADD is the mnemonic that tells the processor that it has to perform additional functions. Moreover, A and
B are the operands. Also, SUB, MUL, DIVC, etc. are other mnemonics.
. There are many good assembler programs, such as −
● Microsoft Assembler (MASM)
● Borland Turbo Assembler (TASM)
● The GNU assembler (GAS)
Assembly language is dependent upon the instruction set and the architecture of the processor.We focus on Intel-
32 processors like Pentium. minimum requirements are:
● The Netwide Assembler, NASM, is an 80x86 and x86-64 assembler designed for portability and modularity.
● It supports a range of object file formats, including :Linux and *BSD a.out, ELF (Executable and Linkable
Format) , Mach-O, 16-bit and 32-bit ,.obj (OMF),binary format.
● COFF (Common Object File Format)is a format for executable, object code, and shared library (including
its Win32 and Win64 variants.)
● Open Source Software
● NASM Is Case-Sensitive
● NASM Requires Square Brackets For Memory References
● It can also output plain binary files, Intel hex and Motorola'S-Record formats.
● It supports all currently known x86 architectural extensions, and has strong support for macros.
1. Assembler:It translates assembly code to the machine code and then stores the result in an object file. This file
2. Linker:The linker takes all object files as input, resolves all memory references, and finally merges these object
1.data section:
3.text section
● It is used for keeping the actual code. This section must begin with the declaration global _start, which tells
the kernel where the program execution begins.
● The syntax for declaring text section is −
section .text
global _start ;must be declared for linker (ld)
_start: ;start actual Program coding or tells linker entry point
Syntax of assembly language statement
For Example:
There are ten 32-bit and six 16-bit processor registers in IA-32 architecture. The
The general registers are further divided into the following groups −
1. Data registers,
2. Pointer registers, and
3. Index registers.
1. Data registers
2. Pointer registers
3. Index registers
Instructions Needed:
1. EQU: The equate directive is used to substitute values for symbols or labels. The format is 'label: EQU value',
so whenever the assembler encounters 'label', it replaces this with 'value'.
2. MOV: copies the data item referred to by its second operand (i.e. register contents, memory contents, or a constant
value) into the location referred to by its first operand (i.e. a register or memory).
3. CALL: The call instruction calls near procedures using a full pointer. call causes the procedure named in the
operand to be executed.
4. JNZ: The Jump if not equal to zero transfers control to the specified address if the value in the accumulator is
not 0. If the accumulator has a value of 0, the next instruction is executed. Neither the accumulator nor any flags
are modified by this instruction.
5. JNC:The JNC instruction transfers program control to the specified address if the carry flag is 0. Otherwise,
execution continues with the next instruction. No flags are affected by this instruction.
Algorithm
1 Start
2. Declare & initialize the variables in the .data section.
3. Declare uninitialized variables in the .bss section.
4. print message1 accepts the five 64-bit numbers
ASSINGMENT NO. 2
TITLE To display length of the String
PROBLEM Write an X86/64 ALP to accept a string and to display its
STATE length.
MENT
/DEFINITION
OBJECTIVE To learn and understand the operation of accept and display
string
OUTCOME On completion of this practical, students will be able to
calculate length of string without using inbuilt function and
HEX to ASCII conversion
Linux 64bit OS
Editor-any
Text Editor
Assembler –
NASM
Debugger- GDB/TD
REFERENCES 1. “Microprocessor and Interfacing Techniques”,
DouglasHall
2. “IBM PC Assembly language and
programming”, PeterAble
3. “Advances MS-DOS programming”, Ray Duncan
STEPS 1. START
2. Get string from user stored in variable
3. Check string character by character till end of the
string
4. Stored the counter in a cx register
5. Then convert that number in to ASCII number to
display
6. Display length of string
7. STOP
1. Date
INSTRUCTIONS FOR
WRITING JOURNAL 2. Assignment no.
3. Problem definition
4. Learning objective
5. Learning Outcome
6. Concepts related Theory
7. Algorithm
8. Test cases
10. Conclusion/Analysis
Prerequisites: DEL
Theory:
All members of the 80 x 86 families support five different string instructions: MOVS, CMPS, SCAS, LODS and
STOS. They are the string primitives since you can build most other string operations from these five instructions.
How the String Instructions Operate: The string instructions operate on blocks (contiguous linear arrays) of
memory. For example, the MOVS instruction moves a sequence of bytes from one memory location to another.
The CMPS instruction compares two blocks of memory. The SCAS instruction scans a block of memory for a
particular value. These string instructions often require three operands a destination block address a source block
address and (optionally) an element count. For example, when using the MOVS instruction to copy a string you
need a source address a destination address and a count (the number of string elements to move).
Unlike other instructions which operate on memory the string instructions are single-byte instructions which
don't have any explicit operands. The operands for the string instructions include
The SI (source index) register
The DI (destination index) register
Besides the SI, DI and ax registers one other register controls the 80x86's string instructions - the flags register.
Specifically, the direction flag in the flags register controls how the CPU processes strings.
If the direction flag is clear the CPU increments SI and DI after operating upon each string element. For example
if the direction flag is clear then executing MOVS will move the byte word or double word at DS:SI to ES:DI
and will increment SI and DI by one two or four. When specifying the REP prefix before this instruction the CPU
increments SI and DI for each element in the string. At completion the SI and DI registers will be pointing at the
first item beyond the string.
If the direction flag is set, then the 80x86 decrements si and di after processing each string element. After a
repeated string operation, the si and di registers will be pointing at the first byte or word before the strings if the
direction flag was set.
The direction flag may be set or cleared using the cld (clear direction flag) and std (set direction flag) instructions.
When using these instructions inside a procedure keep in mind that they modify the machine state. Therefore you
may need to save the direction flag during the execution of that procedure.
ALGORITHM
STEP2: Decrement EAL register by1 stored in RBX (length is stored in EAL register after write system call)
No: ADD 30 H
Conclusion: In this way we studied about how to calculate length of string without inbuild function and display length of
string using HEX to ASCII conversion strings using 80386 microprocessors
Linux 64bit OS
Editor-any
Text Editor
Assembler –
NASM
Debugger- GDB/TD
REFERENCES 1. “Microprocessor and Interfacing
Techniques”, DouglasHall
2. “IBM PC Assembly language and
programming”, PeterAble
3. “Advances MS-DOS programming”, Ray
Duncan
STEPS 1. START
2. Get 5 elements of array from user
3. Find largest number and stored in AL
4. Then convert that number in to ASCII number to
display
5. Display largest number
6. STOP
1. Date
INSTRUCTIONS FOR
WRITING JOURNAL 2. Assignment no.
3. Problem definition
4. Learning objective
5. Learning Outcome
7. Algorithm
8. Test cases
9. 10. Conclusion/Analysis
Prerequisites: DEL
Theory:
All members of the 80 x 86 families support five different string instructions: MOVS, CMPS, SCAS, LODS and
STOS. They are the string primitives since you can build most other string operations from these five instructions.
How the String Instructions Operate: The string instructions operate on blocks (contiguous linear arrays) of
memory. For example, the MOVS instruction moves a sequence of bytes from one memory location to another.
The CMPS instruction compares two blocks of memory. The SCAS instruction scans a block of memory for a
particular value. These string instructions often require three operands a destination block address a source block
address and (optionally) an element count. For example, when using the MOVS instruction to copy a string you
need a source address a destination address and a count (the number of string elements to move).
Unlike other instructions which operate on memory the string instructions are single-byte instructions which
don't have any explicit operands. The operands for the string instructions include
The SI (source index) register
The DI (destination index) register
For example one variant of the MOVS (move string) instruction copies a string from the source address specified
by DS:SI to the destination address sspecified by ES:DI of length CX. Likewise, the CMPS instruction compares
the string pointed at by DS:SI of length CX to the string pointed at by ES: DI.
Not all instructions have source and destination operands (only MOVS and CMPS support them). For example,
the SCAS instruction (scan a string) compares the value in the accumulator to values in memory. Despite their
differences the 80x86's string instructions all have one thing in common - using them requires that you deal with
two segments the data segment and the extra segment.
Flag
Besides the SI, DI and ax registers one other register controls the 80x86's string instructions - the flags register.
Specifically, the direction flag in the flags register controls how the CPU processes strings.
If the direction flag is clear the CPU increments SI and DI after operating upon each string element. For example
if the direction flag is clear then executing MOVS will move the byte word or double word at DS:SI to ES:DI
and will increment SI and DI by one two or four. When specifying the REP prefix before this instruction the CPU
increments SI and DI for each element in the string. At completion the SI and DI registers will be pointing at the
first item beyond the string.
If the direction flag is set, then the 80x86 decrements si and di after processing each string element. After a
repeated string operation, the si and di registers will be pointing at the first byte or word before the strings if the
direction flag was set.
The direction flag may be set or cleared using the cld (clear direction flag) and std (set direction flag) instructions.
When using these instructions inside a procedure keep in mind that they modify the machine state. Therefore you
may need to save the direction flag during the execution of that procedure.
ALGORITHM
STEP10: Display largest number which is in AL register using HEX to ASCII conversion
Output : The output shows us largest 64bit number from given array.
Conclusion: In this way we studied about compare instruction using 80386 microprocessors
ASSIGNMENT NUMBER: 4
ASSINGMENT NO. 4
TITLE Menu driven basic arithmetic operations
PROBLEM STATEMENT Write a switch case driven X86/64 ALP to perform 64-bit hexadecimal
/DEFINITION arithmetic operations (+,-,*, /) using suitable macros. Define procedure
for each operation
OBJECTIVE
1. To learn the procedures in assembly language programing
2. To learn code conversion(ascii to Hexa decimal)
3. To perform arithmetic operations using menu driven
OUTCOME Students will be able to do menu driven arithmetic operations
S/W PACKAGES AND Core 2 duo/i3/i5/i7 - 64bit
HARDWARE
processorOS – Linux 64bit OS
APPARATUSUSED
Editor- gedit /vi
Assembler –NASM
Debugger- GDB/TD
REFERENCES 1. “Microprocessor and Interfacing Techniques”, Douglas
Hall
2. “IBM PC Assembly language and programming”, Peter
Able
3. INTEL 80386 PROGRAMMER'S REFERENCE MANUAL
1986
STEPS
1. Display menu of 4 basic arithmetic operations
2. Accept choice from user
3. Convert ascii value to hexa-decimal value
4. Based on choice perform the arithmetic operation
5. Convert the result into ascii code for displaying
the result
6. Print the result on the screen and stop
1. Date
INSTRUCTIONS FOR 2. Assignment no.
Prerequisites’
Procedure
A called procedure (or subroutine) is a section of code that perform a clearly defined task. A code block
may contain any number of procedures.
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
Following is the syntax to define a procedure –
Proc_name :
Procedure body
…
ret
The procedure is called from another function by using the CALL instruction. The CALL instruction
should have the name of the called procedure as an argument as shown below −
CALL proc_name
The called procedure returns the control to the calling procedure by using the RET instruction.
Assembly Language allows us to perform basic unsigned integer arithmetic operations. These
operations are:
1. Addition (add)
2. Subtraction (sub)
3. Multiplication (mul)
4. Division (div)
1. Arithmetic Instructions
Addition
ADD (Add Integers) replaces the destination operand with the sum of the source and destination
operands. Sets CF if overflow.
ADD performs an integer addition of the two operands (DEST and SRC). The result of the addition is
assigned to the first operand (DEST), and the flags are set accordingly.
When an immediate byte is added to a word or doubleword operand, the immediate value is sign-extended
to the size of the word or doubleword operand.
Flags Affected
OF, SF, ZF, AF, CF, and PF
Subtraction
P:F-LTL-UG/03/R1
SUB destination, source
SUB (Subtract Integers) subtracts the source operand from the destination operand and replaces the
destination operand with the result. If a borrow is required, the CF is set. The operands may be signed or
unsigned bytes, words, or doublewords.
SUB subtracts the second operand (SRC) from the first operand (DEST). The first operand is assigned
the result of the subtraction, and the flags are set accordingly.
When an immediate byte value is subtracted from a word operand, the immediate value is first sign-
extended to the size of the destination operand.
Flags Affected
OF, SF, ZF, AF, PF, and CF
Multiplication
The 80386 has separate multiply instructions for unsigned and signed operands. MUL operates on
unsigned numbers, while IMUL operates on signed integers as well as unsigned.
MUL performs unsigned multiplication. Its actions depend on the size of its operand, as follows:
● A byte operand is multiplied by AL; the result is left in AX. The carry and overflow flags are set to 0
if AH is 0; otherwise, they are set to 1.
● A word operand is multiplied by AX; the result is left in DX:AX. DX contains the high-order 16 bits
of the product. The carry and overflow flags are set to 0 if DX is 0; otherwise, they are set to 1.
● A doubleword operand is multiplied by EAX and the result is left in EDX:EAX. EDX contains the
high-order 32 bits of the product. The carry and overflow flags are set to 0 if EDX is 0; otherwise, they
are
set to 1.
Flags Affected
OF and CF
Division
The 80386 has separate division instructions for unsigned and signed operands. DIV operates on unsigned
numbers, while IDIV operates on signed integers as well as unsigned. In either case, an exception
(interrupt zero) occurs if the divisor is zero or if the quotient is too large for AL, AX, or EAX.
DIV performs an unsigned division. The dividend is implicit; only the divisor is given as an operand. The
remainder is always less than the divisor. The type of the divisor determines which registers to use:
Flags Affected
OF, SF, ZF, AR, PF, CF
Algorithm:
Conclusion:
Hence we performed switch case driven arithmetic operations using procedure calls.
Review Questions:
P:F-LTL-UG/03/R1
ASSIGNMENT NUMBER: 5
ASSINGMENT NO. 1
PROBLEM STATEMENT Write X86/64 ALP to count number of positive and negative
/DEFINITION numbers from the array
OUTCOME Students will be e able to use different index registers and find
positive and negative numbers from array.
Assembler –NASM
Debugger- GDB/TD
P:F-LTL-UG/03/R1
STEPS Start.
2. Initialize an array of 10 numbers or accept 10 numbers from
user and store them in one array.
3. Initialize pos_counter=0, neg_counter=0, index_reg=array
address, counter=10
2. Read the number from index_reg into a register.
3. Perform addition with 00H and check sign bit
4. If sign bit==1 then
increment neg_counter=neg_counter+1
else
increment pos_counter=pos_counter+1
end if
5. Increment index_reg= index_reg+1
6. Decrement counter=counter-1
7. If counter!=0 then goto step number 4 else
continue
8. Print message “Positive numbers are:” and print pos_counter.
9. Print message “Negative numbers are:” and print neg_counter.
10. Exit.
9. Date
Prerequisites: COA
Any number above zero is a positive number. Positive numbers are written with no sign
or a + sign in front of them and they are counted from zero to the right. Any number below
zero is a negative number. Negative numbers are written with a - sign in front of them and
they are counted from zero to the left. Always look at the sign in front of a number to see
if it is positive or negative.
P:F-LTL-UG/03/R1
In Computer Systems, the negative number is represented by different ways:
1. Sign-Magnitude representation: -
Negative numbers are essential and any computer not capable of dealing with them would
not be particularly useful. But how can such numbers be represented? There are several
methods which can be used to represent negative numbers in Binary. One of them is called
the Sign-Magnitude Method.
The Sign-Magnitude Method is quite easy to understand. In fact, it is simply an ordinary
binary number with one extra digit placed in front to represent the sign. If this extra digit
is a '1', it means that the rest of the digits represent a negative number. However if the same
set of digits are used but the extra digit is a '0', it means that the number is a positive one.
The following examples explain the Sign-Magnitude method better. Let us assume that we
have an 8-bit register. This means that we have 7 bits which represent a number and the
other bit to represent the sign of the number (the Sign Bit).This is how
numbers are represented:
The red digit means that the number is positive. The rest of the digits represent 37. Thus,
the above number in sign-magnitude representation means +37.
And this is how -37 is represented:
Binary Value
0000 +0
0001 +1
0010 +2
0011 +3
0100 +4
0101 +5
0110 +6
0111 +7
1000 -0
1001 -1
1010 -2
1011 -3
1100 -4
1101 -5
1110 -6
1111 -7
P:F-LTL-UG/03/R1
Assign the leftmost (most significant) bit to be the sign bit. If the sign bit is 0, this means
the number is positive. If the sign bit is 1, then the number is negative. The remaining
m-1 bits are used to represent the magnitude of the binary number in the unsigned binary
notation.
The 1‟s complement of a binary number is the number that results when we change all 1s
to zeros and the zeros to ones.
Add 1 = 1
P:F-LTL-UG/03/R1
• Advantages of 2’s complement representation: -
1. There are distinct +0 and -0 representations in both the sign-magnitude and 1‟s
complement systems, but the 2‟s complement system has only a +0 representation.
2. For 4 bit numbers, the value -8 is represent able only in the 2‟s complement system and
not in other systems.
3. It is more efficient for logic circuit implementation, and one often used in computers
for addition and subtraction operations.
Algorithm:
1. Start.
2. Initialize an array of 10 numbers or accept 10 numbers from
user and store them in one array.
3. Initialize pos_counter=0, neg_counter=0, index_reg=array address, counter=10
4. Read the number from index_reg into a register.
5. Perform addition with 00H and check sign bit
6. If sign bit==1 then
increment neg_counter=neg_counter+1
else
increment pos_counter=pos_counter+1
end if
7. Increment index_reg= index_reg+1
8. Decrement counter=counter-1
9. If counter!=0 then goto step number 4 else
P:F-LTL-UG/03/R1
continue
10. Print message “Positive numbers are:” and print pos_counter.
11. Print message “Negative numbers are:” and print neg_counter.
12. Exit.
Conclusion:
We are able to use different index registers and find positive and negative numbers from
array.
Review Questions:
P:F-LTL-UG/03/R1
ASSIGNMENT NUMBER:8
ASSINGMENT NO. 8
PROBLEM STATEMENT Write x86 ALP to perform non-overlapped and overlapped block
/DEFINITION
transfer (with and without string specific instructions). Block
containing data can be defined in the data segment.
OBJECTIVE To learn
• Overlapped / Non – overlapped data transfer in segments
• Block transfer instruction of 8086
• Data storage in the memory and segments
OUTCOME Students will study different block transfer instructions and also
understood block transfer within different segments.
Assembler –NASM
Debugger- GDB/TD
P:F-LTL-UG/03/R1
6. repeat the steps 4 and 5 to add all N numbers
7. Print the result /Sum
8. End.
B] Non Overlapping
1. Declare a source array.
1. Date
INSTRUCTIONS FOR 2. Assignment no.
Concepts related Theory: One of the frequent operations used in programming is shifting/
P:F-LTL-UG/03/R1
transferring the data form one memory location to another memory location. These operations can
be with simple mov instructions which may result in more number of operations. We can make
use of instructions like MOVSB to transfer the data. The relevant instructions are LOOP /
MOVSB.
The data can be transferred either in overlapped fashion or non overlapped Fashion. In case
of overlapped address the two possible situations are for the Source address to be greater than the
destination address in which case the first element in the source is to be moved first or for the
source address to be less than the destination address which requires the last element of the source
to be moved first.
Algorithm: A] Overlapping
1. Study system call to read and display character on the screen.
2. Accept the Value of „N‟ i.e. how many numbers to add
3. initialize Sum =0
4. Read a number (two digit)
5. add it to sum
6. repeat the steps 4 and 5 to add all N numbers
7. Print the result /Sum
8. End.
B] Non Overlapping
1. Declare a source array.
2. Load the address of source array in one of the registers. (Index register)
4. Increment the pointer/SI by length of array which becomes the starting Address of
destination array.
6. In case of overlapping mode based on the destination address either move the first Element
or last element in beginning of transfer operation.
P:F-LTL-UG/03/R1
Conclusion:
We have studied different block transfer instructions and also understood block transfer
within different segments.
Review Questions:
P:F-LTL-UG/03/R1
ASSIGNMENT NUMBER: 6
ASSINGMENT NO. 6
PROBLEM STATEMENT Write 64 bit ALP to convert 4-digit Hex number into its equivalent
/DEFINITION
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
OBJECTIVE To learn
• Data representation and conversion
• Understand the stack operations
OUTCOME Students will studuy use of stack operations and number conversion
in ALP.
Assembler: NASM
Debugger: GDB
P:F-LTL-UG/03/R1
6. Display answer
7. Exit
1. Date
INSTRUCTIONS FOR 2. Assignment no.
10. Conclusion/Analysis
The different numbers systems can be used with the computer such as Hexadecimal, Octal, Binary
and BCD. The number can be converted to any number system from any source other number
system.
Decimal
5 2 9
BCD 0101 0010 1001
P:F-LTL-UG/03/R1
Example:
4
6
6
0
Pop from stack and display result = 4660
BCD to HEX
• Initialize sum = 0
• Accept the first digit multiplicand
• Multiply the number by 10,000 multiplier
• Add result to sum
• Divide the multiplier by 10
• Repeat the step 2 to 4 till multiplier becomes zero
• Display the sum which is result of converting BCD number to HEX
Example
BCD number =4660
4*1000 + 6*100 + 6*10 + 6*0 = 1234H
FA0 + 258 +3C + 0 =1234H
P:F-LTL-UG/03/R1
Algorithm:
1. Start.
2. Take i/p as hex to bcd or bcd to hex.
3. Convert input to ASCII.
4. Convert i/p to hex or bcd as required using function.
5. Convert answer to ASCII to display
6. Display answer
7. Exit
Conclusion:
Review Questions:
P:F-LTL-UG/03/R1
ASSIGNMENT NUMBER:
ASSINGMENT NO.
Assembler –NASM
Debugger- GDB/TD
P:F-LTL-UG/03/R1
1. Start
2. Accept two 2-digit numbers.(Multiplier and
Multiplicand)
3. Store multiplier to BL and
Multiplicand to CL, Initialize AX with 00.
4. Shift BL to left by 1 bit. (Shiftedbit will be stored to carry
flag)
5. If carry flag is set, Add CL toAL, and shift AL to left
by 1 bit.
6. If carry flag is reset, shift AL toleft by 1 bit.
7. Repeat step 4 to 6 for 8 times.(As 2 digit numbers
contains 8 bits)
8. Print the result from AX.
9. Stop
1. Date
INSTRUCTIONS FOR 2. Assignment no.
Prerequisites: COA
Consider that a byte is present in the AL register and second byte is present in the BL register.
➢ We have to multiply the byte in AL with the byte in BL.
➢ We will multiply the numbers using successive addition method.
➢ In successive addition method, one number is accepted and other number is taken as a
counter. The first number is added with itself, till the counter decrements to zero.
➢ Result is stored in DX register. Display the result, using display routine.
P:F-LTL-UG/03/R1
➢ For example : AL = 12 H, BL = 10 H
• Result = 12H + 12H + 12H + 12H + 12H + 12H + 12H + 12H +
12H + 12H
• Result = 0120 H
Consider that one byte is present in the AL register and another byte is present in the BL register.
We have to multiply the byte in AL with the byte in BL.
We will multiply the numbers using add and shift method. In this method, you add number with
itself and rotate the other number each time and shift it by one bit to left alongwith carry. If carry
is present add the two numbers.
Initialize the count to 4 as we are scanning for 4 digits. Decrement counter each time the bits are
added. The result is stored in AX. Display the result.
For example : AL = 11 H, BL = 10 H, Count = 4
Step I :
AX = 11
+ 11
22 H
Rotate BL by one bit to left along with carry.
BL = 10 H 0 ¬ 0 0 0 1 0 0 0 0
CY
BL = 0 0 0 1 0 0 0 0 0
CY 2 0
Step II : Now decrement counter count = 3.
Check for carry, carry is not there so add number with itself.
AX = 22
+ 22
P:F-LTL-UG/03/R1
44 H
Rotate BL to left,
BL = 0 ¬0 100 0000
CY 4 0
Carry is not there.
Decrement count, count=2
Step III : Add number with itself
AX = 44
+ 44
88 H
Rotate BL to left,
BL = 0 1 000 0000
CY 8 0
Carry is not there.
Step IV : Decrement counter count = 1.
Add number with itself as carry is not there.
AX = 88
+ 88
110 H
Rotate BL to left,
BL = 1 0000 0000
CY 0 0
Carry is there.
Step V : Decrement counter = 0.
Carry is present.
\ add AX, BX
\ 0110 i.e. 11 H
+ 0000 ´ 10 H
0110 H 0110 H
P:F-LTL-UG/03/R1
Algorithm:
Successive Addition:
1. Start
2. Accept two 2-digit numbers. (Multiplier and Multiplicand)
3. Set Multiplicand value as a counter value.
4. Add Multiplier with itself “Counter-1” number of times.
5. Print The answer.
1. Start
2. Accept two 2-digit numbers. (Multiplier and Multiplicand)
3. Store multiplier to BL and Multiplicand to CL, Initialize AXwith 00.
4. Shift BL to left by 1 bit. (Shifted bit will be stored to carryflag)
5. If carry flag is set, Add CL to AL, and shift AL to left by 1bit.
6. If carry flag is reset, shift AL to left by 1 bit.
7. Repeat step 4 to 6 for 8 times. (As 2 digit numbers contains 8
P:F-LTL-UG/03/R1
bits)
8. Print the result from AX.
9. Stop
Conclusion:
Review Questions:
P:F-LTL-UG/03/R1
ASSIGNMENT NUMBER: 9
ASSINGMENT NO. 9
PROBLEM STATEMENT Write X86 ALP to find, a) Number of Blank spaces b) Number of
/DEFINITION lines c) Occurrence of a particular character. Accept the data from
the text file. The text file has to be accessed during Program_1
execution and write FAR PROCEDURES in Program_2 for the rest
of the processing. Use of GLOBAL and EXTERN directives is
mandatory.
OUTCOME Students will study NEAR and FAR procedure and there
application.
Assembler: NASM
Debugger: GDB
P:F-LTL-UG/03/R1
STEPS Create a one Folder which contain 2 program file and 1
text file.
Define all the procedure (required to count and print the number
To find the Number of Spaces from the text file compare each
entry from the text with 20H (ASCII Value of space) ,count the
value and print.
To find the Number of Enter from the text file compare each entry
from the text with 0x0A (ASCII Value of enter) ,count the value and
print.
Accept the letter whose number of occurrences find in the text file.
To find the Number of occurrences from the text file compare each
entry from the text with ASCII Value of entered text,count the value
and print.
Exit.
P:F-LTL-UG/03/R1
INSTRUCTIONS FOR 1. Title
Assignment No. 5
• Aim: Write X-86 ALP to find, a) Number of Blank spaces b) Number of lines c) Occurrence
of a particular character. Accept the data from the text file. The text file has to be accessed
during Program_1 execution and write FAR PROCEDURES in Program_2 for the rest of the
processing. Use of GLOBAL and EXTERN directives is mandatory.
• OPEN File
Syscall
• READ File
P:F-LTL-UG/03/R1
mov rdi, [fd_in] ; file Pointer
Syscall
• WRITE File
Syscall
• CLOSE File
mov rax,3
mov rdi,[fd_in]
syscall
• Conclusion
We have studied Near and Far process and there application.
• Review Questions:
P:F-LTL-UG/03/R1
ASSIGNMENT NUMBER: 7
TITLE To read and display contents pointed by GDTR, LDTR and
IDTR.
PROBLEM STATEMENT Write an ALP to read and display the table content pointed by
/DEFINITION GDTR/LDTR and IDTR
Assembler: NASM
Debugger: GDB
4. Stop
P:F-LTL-UG/03/R1
INSTRUCTIONS FOR 1. Title
Assignment No. 7
P:F-LTL-UG/03/R1
Figure2. System Address and System Segment Registers
LDTR and TR
These registers hold the 16-bit selector for the LDT descriptor and the TSS descriptor, respectively.
The LDT and TSS segments, since they are task specific segments, are defined by selector values
stored in the system segment registers. Note that a segment descriptor register (programmer-
invisible) is associated with each system segment register.
• Conclusion
We have studied different Descriptor tables in system also different registers associated
with it.
• Review Questions:
2. What is Selector?
P:F-LTL-UG/03/R1
ASSIGNMENT NUMBER :
TITLE Write X86 program to sort the list of integers in ascending/descending
order. Read the input from the text file and write the sorted data back to
the same text file using bubble sort
Assembler: NASM
Debugger: GDB
3. Open the text file and check that is it successfully opened or not.
P:F-LTL-UG/03/R1
INSTRUCTIONS 1. Title
FOR 2. Problem Definition
WRITING 3. Objective: Intention behind study
JOURNAL 4. Software & Hardware requirements
5. Explanation of the assignment
6. Algorithm
7. State Diagram
8. Test cases for program.
9. Print outs
10. Conclusion.
Assignment No. 7
• Aim: Write X86 program to sort the list of integers in ascending/descending order. Read the
input from the text file and write the sorted data back to the same text file using bubble sort
• OPEN File
Syscall
• READ File
P:F-LTL-UG/03/R1
mov rdi, [fd_in] ; file Pointer
Syscall
• WRITE File
Syscall
• CLOSE File
mov rax,3
mov rdi,[fd_in]
syscall
• Conclusion
We have studied how to implement bubble sort using ALP.
• Review Questions
P:F-LTL-UG/03/R1
ASSIGNMENT NUMBER :
TITLE Write X86 menu driven Assembly Language Program (ALP)
to implement OS (DOS) commands TYPE, COPY and
DELETE using file operations. User is supposed to provide
command line arguments in all cases.
PROBLEM STATEMENT Write X86 menu driven Assembly Language Program (ALP) to
/DEFINITION implement OS (DOS) commands TYPE, COPY and DELETE
using file operations. User is supposed to provide command line
arguments in all cases.
OUTCOME Students will study different DOS commands and file operations.
Assembler: NASM
Debugger: GDB
P:F-LTL-UG/03/R1
STEPS DOS COPY Command
1. Start
10. Exit.
1. Start
4. Delete file
5. Exit.
1. Start
P:F-LTL-UG/03/R1 7. Exit.
INSTRUCTIONS FOR 1. Title
Assignment No. 8
• Aim: Write X86 menu driven Assembly Language Program (ALP) to implement OS (DOS)
commands TYPE, COPY and DELETE using file operations. User is supposed to provide
command line arguments in all cases.
• OPEN File
mov rsi, 0 ;
Syscall
P:F-LTL-UG/03/R1
mov rdx, 0666o ; permissions set
Syscall
• READ File
Syscall
• WRITE File
Syscall
• DELETE File
mov rax,87
mov rdi,Fname
syscall
• CLOSE File
mov rax,3
mov rdi,[fd_in]
syscall
TYPE Command:
P:F-LTL-UG/03/R1
• Read contents of file using read interrupt.
COPY Command:
DELETE Command:
• Conclusion
We have studied different DOS commands and file operations.
• Review Questions:
P:F-LTL-UG/03/R1
ASSIGNMENT NUMBER: 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.
PROBLEM STATEMENT Write x86 ALP to find the factorial of a given integer number on a
/DEFINITION command line by using recursion. Explicit stack manipulation is
expected in the code.
Assembler: NASM
Debugger: GDB
P:F-LTL-UG/03/R1
STEPS ➢ Start.
➢ End.
Assignment No. 10
• Aim: 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.
• Prerequisites : COA
P:F-LTL-UG/03/R1
PUSH -- Push Operand onto the Stack
PUSH decrements the stack pointer by 2 if the operand-size attribute of the instruction is
16 bits; otherwise, it decrements the stack pointer by 4. PUSH then places the operand on the
new top of stack, which is pointed to by the stack pointer.
The 80386 PUSH eSP instruction pushes the value of eSP as it existed before the
instruction. This differs from the 8086, where PUSH SP pushes the new value (decremented by
2).
Opcode Instruction Clocks Description
11. Conclusion: In this way we studied to use stack segment for recursion.
P:F-LTL-UG/03/R1
ASSIGNMENT NUMBER:
ASSINGMENT NO.
TITLE Write 80387 ALP to find the roots of the quadratic equation. All the
possible cases must be considered in calculating the roots.
PROBLEM STATEMENT Write 80387 ALP to find the roots of the quadratic equation. All the
/DEFINITION possible cases must be considered in calculating the roots.
Editor: gedit/vi
Assembler: NASM
Debugger: GDB
REFERENCES ➢ “The Intel microprocessor”, Barry B. Brey.
➢ “Introduction to 64 bit Intel Assembly Language
Programming for Linux”, 2nd Edition, Ray Seyfarth,
“80386 Microprocessor Handbook”, Chris H. Papas.
STEPS 1. Write msg to enter the Quadratic equation
2. Write msg to enter first coefficient a
3. Read first coefficient
4. Convert the no from ascii to hex
5. Write msg to enter second coefficient b
6. Read second coefficient
7. Convert the no from ascii to hex
8. Write msg to enter third coefficient c
9. Read third coefficient
10. Convert the no from ascii to hex
11. Write down the quadratic formula
12. Identify the values of a, b, and c in the quadratic equation.
P:F-LTL-UG/03/R1
The variable a is the coefficient of the x2 term, b is the
coefficient of the x term, and c is the constant. For the
equation 3x2 -5x - 8 = 0, a = 3, b = -5, and c = -8. Write this
down.
13. Substitute the values of a, b, and c into the equation. Now that
you know the values of the three variables, you can just plug
them into the equation like this:
a. {-b +/-√ (b2 - 4ac)}/2
b. {-(-5) +/-√ ((-5)2 - 4(3)(-8))}/2(3) =
c. {-(-5) +/-√ ((-5)2 - (-96))}/2(3)
14. Do the math. After you've plugged in the
a. numbers, do the remaining math to simplify
b. positive or negative signs, multiply, or square the
c. remaining terms. Here's how you do it:
d. {-(-5) +/-√ ((-5)2 - (-96))}/2(3) =
e. {5 +/-√(25 + 96)}/6
f. {5 +/-√(121)}/6
15. Simplify the square root. If the number under the radical
symbol is a perfect square, you will get a whole number. If
the number is not a perfect square, then simplify to its
simplest radical version. If the number is negative, and you're
sure it's supposed to be negative, then the roots will be
complex. In this example, √(121) = 11. You can write that x =
(5 +/- 11)/6.
16. Convert the final roots from hex to ascii
17. Print the final roots. Real and imaginary.
1. Date
INSTRUCTIONS FOR 2. Assignment no.
WRITING JOURNAL 3. Problem definition
4. Learning objective
5. Learning Outcome
6. Concepts related Theory
7. Algorithm
8. Test cases
10. Conclusion/Analysis
P:F-LTL-UG/03/R1
Assignment No. 10
Aim: Write 80387 ALP to find the roots of the quadratic equation. All the possible cases must be
considered in calculating the roots.
80387 Microprocessor:
The 80387 (387 or i387) is the first Intel coprocessor to be fully compliant with the IEEE 754-
1985 standard. Released in 1987, a full two years after the 386 chip, the i387 includes much
improved speed over Intel's previous 8087/80287 coprocessors, and improved characteristics of
its trigonometric functions. The 8087 and 80287's FPTAN and FPATAN instructions are limited
to an argument in the range ±π/4 (±45°), and the 8087 and 80287 have no direct instructions for
the sin and cos functions.
Without a coprocessor, the 386 normally performs floating-point arithmetic through (slow)
software routines, implemented at runtime through a software exception-handler. When a math
coprocessor is paired with the 386, the coprocessor performs the floating point arithmetic in
hardware, returning results much faster than an (emulating) software library call.
The i387 is compatible only with the standard i386 chip, which has a 32-bit processor bus. The
later cost-reduced i386SX, which has a narrower 16-bit data bus, can not interface with the i387's
32-bit bus. The i386SX requires its own coprocessor, the 80387SX, which is compatible with the
SX's narrower 16-bit data bus.
Instruction set:
All instructions of 80386 MOV, JC,JNC,JG,JZ,JNZ,PUSH POP,INC DEC,CMP, ADD ETC are
used in this program.
P:F-LTL-UG/03/R1
Algorithm:
{5 +/-√(25 + 96)}/6
{5 +/-√(121)}/6
14. Simplify the square root. If the number under the radical symbol is a perfect square, you
will get a whole number. If the number is not a perfect square, then simplify to its
simplest radical version. If the number is negative, and you're sure it's supposed to be
negative, then the roots will be complex. In this example, √(121) = 11. You can write
that x = (5 +/- 11)/6.
15. Convert the final roots from hex to ascii
16. Print the final roots. Real and imaginary.
Conclusion: Thus Roots of Quadratic equations are calculated following above steps.
P:F-LTL-UG/03/R1
Review Questions: Difference between 80386 and 80387 microprocessor?
P:F-LTL-UG/03/R1
ASSIGNMENT NUMBER:
TITLE Write 80387 ALP to plot Sine Wave, Cosine Wave and Sinc
function. Access video memory directly for plotting.
PROBLEM STATEMENT Write 80387 ALP to plot Sine Wave, Cosine Wave and Sinc
/DEFINITION function. Access video memory directly for plotting.
OUTCOME Students will study video memory plotting and regen buffer
concepts..
S/W PACKAGES AND Processor: Core 2 duo/i3/i5/i7
Assembler: NASM
Debugger: GDB
P:F-LTL-UG/03/R1
INSTRUCTIONS FOR 1. Title
Assignment No. 11
• Aim: Write 80387 ALP to plot Sine Wave, Cosine Wave and Sinc function. Access video
memory directly for plotting.
• whereas the appearance of each individual character or graphics pixel on the display
is controlled by a specific location within an area of memory called the regen buffer
or refresh buffer.
• Both the CPU and the video controller access this memory;
• The software updates the display by simply writing character codes or bit patterns
directly into the regen buffer. (This is called memory-mapped I/O.)
Text Mode:
P:F-LTL-UG/03/R1
• 2 Bytes per character (ASCII Code & Attribute)
offset = (y*80 + x) *2
Graphics Mode:
• More complicated
• Each bit or group of bits in regen buffer corresponds to an addressable point or pixel on
the screen
• The memory map is set up so that all the even y coordinates are scanned as a set and all
the odd y coordinates are scanned as a set; this mapping is referred to as the memory
interlace.
Sine Wave:
P:F-LTL-UG/03/R1
Cosine Wave:
Sinc Function:
P:F-LTL-UG/03/R1
• Conclusion
We have studied video memory plotting to plot sine wave, cosine wave and sin function.
• Review Questions:
P:F-LTL-UG/03/R1
ASSIGNMENT NUMBER: 12
ASSINGMENT NO. 12
TITLE Write 80387 ALP to obtain: i) Mean ii) Variance iii) Standard
Deviation Also plot the histogram for the data set. The data elements
are available in a text file.
PROBLEM STATEMENT Write 80387 ALP to obtain: i) Mean ii) Variance iii) Standard
/DEFINITION Deviation Also plot the histogram for the data set. The data elements
are available in a text file.
Editor: gedit/vi
Assembler: NASM
Debugger: GDB
REFERENCES ➢ “The Intel microprocessor”, Barry B. Brey.
➢ “Introduction to 64 bit Intel Assembly Language
Programming for Linux”, 2nd Edition, Ray Seyfarth,
“80386 Microprocessor Handbook”, Chris H. Papas.
STEPS 1. Create a one Folder which contain 1 program file and 1
text file.
2. Write all input text to the text file.
3. Define all parameter required to execute above
procedure in 1st program file.
P:F-LTL-UG/03/R1
4. Define all the procedures
a. Ascii to Hex
b. Hex to ascii
c. Find Mean
d. Find Variance
e. Find Standard Deviation
1. Date
INSTRUCTIONS FOR 2. Assignment no.
WRITING JOURNAL 3. Problem definition
4. Learning objective
5. Learning Outcome
6. Concepts related Theory
7. Algorithm
8. Test cases
10. Conclusion/Analysis
Assignment No. 12
Aim: Write 80387 ALP to obtain: i) Mean ii) Variance iii) Standard Deviation Also
plot the histogram for the data set. The data elements are available in a text file.
Prerequisites: Basic instructions of Assembly Language programming
• OPEN File
P:F-LTL-UG/03/R1
mov rdi, fname1 ; file name
Syscall
• READ File
Syscall
• WRITE File
Syscall
• CLOSE File
mov rax,3
mov rdi,[fd_in]
syscall
Algorithm:
P:F-LTL-UG/03/R1
Data set: Measured height of the dogs
The heights (at the shoulders) are: 600mm, 470mm, 170mm, 430mm and 300mm.
Variance : Variance = s2
To calculate the Variance, take each difference, square it, and then average the result:
And the Standard Deviation is just the square root of Variance, so:
Standard Deviation
σ = √21,704
= 147.32...
= 147 (to the nearest mm)
Conclusion:. Thus we have calculated Mean ,Variance and Standard deviation using ALP
Review Questions:
P:F-LTL-UG/03/R1
ASSIGNMENT NUMBER: 13
ASSIGNMENT NO 13
TITLE Write a Terminate but Stay Resident (TSR) program for a key-
logger. The key-presses during the stipulated time need to be
displayed at the center of the screen
PROBLEM STATEMENT Write a Terminate but Stay Resident (TSR) program for a key-
/DEFINITION logger. The key-presses during the stipulated time need to be
displayed at the center of the screen
OUTCOME Students will study Terminate but Stay Program for a key-logger..
Assembler: NASM
Debugger: GDB
P:F-LTL-UG/03/R1
STEPS 1. Far JUMP to transient portion of memory
2. Clear IF
Assignment No. 13
Aim: Write a Terminate but Stay Resident (TSR) program for a key-logger. The key-presses
during the stipulated time need to be displayed at the center of the screen
• Terminate and Stay Resident (TSR) generally refers to a special class of programs for PC-
compatible computers running DOS.
• When a user exits a normal program running in DOS, the memory that the program used
is usually freed for other programs and tasks; therefore, the program must be reloaded from
a disk back into memory for it to be used again. When you run a TSR program, however,
it loads itself into the computer's memory and remains there for later use. You may run
other programs while the TSR is still alive in memory, and these programs may
P:F-LTL-UG/03/R1
invoke the TSR or be affected by the behavior of the TSR program. For this reason, TSR
programs may give DOS the appearance of multitasking (the ability to perform several
tasks at once) which is built into many other operating systems.
• You may use TSR programs for a wide variety of tasks. Some of these programs are active
only when you press a hot key (a special key or combination of keystrokes that activates
the TSR). An example would be a pop-up calculator program that appears on thescreen
whenever you press Alt-Shift-c, even from within a separate word processing program.
Other TSR programs run continuously in the background and may normally be invisible.
Examples of such programs are some network and communications programs and special
virus scanner programs that monitor the use of a computer's memory and disk drives. Still
other TSR programs may operate in both ways. A visually impaired user might call up a
TSR that intercepts text information sent to the screen and displays it in a larger, easier-to-
read format.
• TSR programs may be quite complex, and are often difficult to program reliably. TSR
programmers must make sure that their programs do not conflict with other programs active
in the computer's memory. The TSR must also not interfere with other programs' use of the
disk drives or other hardware. Whenever a TSR becomes active, it must carefully record
information in memory being used by another program, and restore this information exactly
when it transfers control.
• Because of this complexity, TSR programs are often likely culprits when a PC is behaving
strangely or crashing. Some TSR programs may not be compatible with other programs
because of the way they use memory, or they may conflict with other TSR programs that
are also active. When installing TSR programs, it is a good idea to install them one at a
time, and make sure that other programs such as word processors and spreadsheets are
behaving normally despite the presence of the TSR.
• Key-logger:
key-logger is a type of surveillance software (considered to be either software or spyware) that has
the capability to record every keystroke you make to a log file, usually encrypted. A key-logger
recorder can record instant message, e-mail, and any information you type at any time using your
keyboard. The log file created by the key-logger can then be sent to a specified receiver. Some key-
P:F-LTL-UG/03/R1
logger programs will also record any e-mail addresses you use and Web Site URL's you visit.
• Conclusion
We have studied Terminate but Stay Resident program for key-logger
• Review Questions:
P:F-LTL-UG/03/R1