MP Lab Manual

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 67

EXP NO: 01

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:

 To understand assembly language programming instruction set


 To understand different assembler directives with example
 To apply instruction set for implementing X86/64 bit assembly language programs

ENVIRONMENT:
 Operating System: 64-bit Open source Linux or its derivative.
 Programming Tools: Preferably using Linux equivalent or MASM/TASM/NASM/FASM.
 Text Editor: geditor

THEORY:

Introduction to Assembly Language Programming:

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

Assembly Basic Syntax:


An assembly program can be divided into three sections:
The data section
The bss section
The text section

The order in which these sections fall in your program really isn’t important, but by convention the
.data section comes first, followed by the .bss section, and then the .text section.

The .data Section


The .data section contains data definitions of initialized data items. Initialized data is data that has a
value before the program begins running. These values are part of the executable file. They are loaded
into memory when the executable file is loaded into memory for execution. You don’t have to load
them with their values, and no machine cycles are used in their creation beyond what it takes to load the
program as a whole into memory. The important thing to remember about the .data section is that the
more initialized data items you define, the larger the executable file will be, and the longer it will take
to load it from disk into memory when you run it.

The .bss Section


Not all data items need to have values before the program begins running. When you’re reading
data from a disk file, for example, you need to have a place for the data to go after it comes in from
disk. Data buffers like that are defined in the .bss section of your program. You set aside some
number of bytes for a buffer and give the buffer a name, but you don’t say what values are to be
present in the
buffer. There’s a crucial difference between data items defined in the .data section and data items
defined in the .bss section: data items in the .data section add to the size of your executable file. Data
items in the .bss section do not.

The .text Section

The actual machine instructions that make up your program go into the .text section. Ordinarily, no data
items are defined in .text. The .text section contains symbols called labels that identify locations in the
program code for jumps and calls, but beyond your instruction mnemonics, that’s about it.
All global labels must be declared in the .text section, or the labels cannot be ‘‘seen’’ outside your
program by the Linux linker or the Linux loader. Let’s look at the labels issue a little more closely.

Labels
A label is a sort of bookmark, describing a place in the program code and giving it a name that’s easier
to remember than a naked memory address. Labels are used to indicate the places where jump
instructions should jump to, and they give names to callable assembly language procedures.
Here are the most important things to know about labels:
Labels must begin with a letter, or else with an underscore, period, or question mark. These last
three have special meanings to the assembler, so don’t use them until you know how NASM
interprets them.
Labels must be followed by a colon when they are defined. This is basically what tells NASM
that the identifier being defined is a label. NASM will punt if no colon is there and will not flag
an error, but the colon nails it, and prevents a mistyped instruction mnemonic from being
mistaken for a label. Use the colon!
Labels are case sensitive. So yikes:, Yikes:, and YIKES: are three completely different labels.

Assembly Language Statements

Assembly language programs consist of three types of statements:


Executable instructions or instructions
Assembler directives or pseudo-ops
Macros

Syntax of Assembly Language Statements

[label] mnemonic [operands] [;comment]

LIST OF INTERRRUPTS USED: NA

LIST OF ASSEMBLER DIRECTIVES USED: EQU,DB

LIST OF MACROS USED: NA

LIST OF PROCEDURES USED: NA


ALGORITHM:

INPUT: ARRAY

OUTPUT: ARRAY

STEP 1: Start.
STEP 2: Initialize the data segment.

STEP 3: Display msg1 “Accept array from user. “

STEP 4: Initialize counter to 05 and rbx as 00

STEP 5: Store element in array.

STEP 6: Move rdx by 17.

STEP 7: Add 17 to rbx. TEP 8: Decrement

Counter.

STEP 9: Jump to step 5 until counter value is not zero. STEP 9: Display msg2.

STEP 10: Initialize counter to 05 and rbx as 00 TEP 11: Display element

of array.

STEP 12: Move rdx by 17.

STEP 13: Add 17 to rbx. STEP 14: Decrement

Counter.

STEP 15: Jump to step 11 until counter value is not zero. STEP 16: Stop

FLOWCHART:
PROGRAM:
section .data
msg1 db 10,13,"Enter 5 64 bit numbers"
len1 equ $-msg1
msg2 db 10,13,"Entered 5 64 bit numbers"
len2 equ $-msg2
section .bss
array resd 200
counter resb 1
section .text
global _start
_start:
;display
mov Rax,1
mov Rdi,1
mov Rsi,msg1
mov Rdx,len1
syscall
;accept
mov byte[counter],05
mov rbx,00
loop1:
mov rax,0 ; 0 for read
mov rdi,0 ; 0 for keyboard
mov rsi, array ;move pointer to start of
array add rsi,rbx
mov rdx,17
syscall
add rbx,17 ;to move counter
dec byte[counter]
JNZ loop1
;display
mov Rax,1
mov Rdi,1
mov Rsi,msg2
mov Rdx,len2
syscall
;display
mov byte[counter],05
mov rbx,00
loop2:
mov rax,1 ;1 for write
mov rdi, 1 ;1 for
monitor mov rsi, array
add rsi,rbx
mov rdx,17 ;16 bit +1 for enter
syscall
add rbx,17
dec byte[counter]
JNZ loop2
;exit system call
mov rax ,60
mov rdi,0
syscall
;output
:~$ cd ~/Desktop
:~/Desktop$ nasm -f elf64 ass1.asm
:~/Desktop$ ld -o ass1 ass1.o
~/Desktop$ ./ass1

;Enter 5 64 bit numbers12


;23
;34
;45
;56

;Entered 5 64 bit numbers12


;23
;34
;45
;56
EXP NO: 02

AIM: Write an X86/64 ALP to accept a string and to display its length.

OBJECTIVES:

 To understand assembly language programming instruction set.


 To understand different assembler directives with example.
 To apply instruction set for implementing X86/64 bit assembly language programs

ENVIRONMENT:
 Operating System: 64-bit Open source Linux or its derivative.
 Programming Tools: Preferably using Linux equivalent or MASM/TASM/NASM/FASM.
 Text Editor: geditor

THEORY:

MACRO:

Writing a macro is another way of ensuring modular programming in assembly language.


 A macro is a sequence of instructions, assigned by a name and could be used anywhere in the
program.
 In NASM, macros are defined with %macro and %endmacro directives.
 The macro begins with the %macro directive and ends with the %endmacro
directive. The Syntax for macro definition −
%macro macro_name number_of_params
<macro body>
%endmacro
Where, number_of_params specifies the number parameters, macro_name specifies the name of the
macro.
The macro is invoked by using the macro name along with the necessary parameters. When you need
to use some sequence of instructions many times in a program, you can put those instructions in a
macro and use it instead of writing the instructions all the time.
PROCEDURE:

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

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.

Following is the syntax to define a procedure −

LIST OF INTERRRUPTS USED: NA

LIST OF ASSEMBLER DIRECTIVES USED: EQU, PROC, GLOBAL, DB,

LIST OF MACROS USED: DISPMSG

LIST OF PROCEDURES USED: DISPLAY

ALGORITHM:

INPUT: String

OUTPUT: Length of String in hex

STEP 1: Start.

STEP 2: Initialize data section.

STEP 3: Display msg1 on monitor

STEP 4: accept string from user and store it in Rsi Register (Its length gets stored in Rax register by
default).

STEP 5: Display the result using “display” procedure. Load length of string in data register.

STEP 6. Take counter as 16 int cnt variable

STEP 7: move address of “result” variable into rdi.

STEP 8: Rotate left rbx register by 4 bit.

STEP 9: Move bl into al.


STEP 10: And al with 0fh

STEP 11: Compare al with

09h

STEP 12: If greater add 37h into al

STEP 13: else add 30h into al

STEP 14: Move al into memory location pointed by

rdi STEP 14: Increment rdi

STEP 15: Loop the statement till counter value becomes zero

STEP 16: Call macro dispmsg and pass result variable and length to it. It will print length of string.

mov rax,0
mov rdi,0
mov rsi,str1
mov rdx,200
syscall

call display

;exit system call


mov Rax ,60
mov Rdi,0
syscall

%macro dispmsg 2
mov Rax,1
mov Rdi,1
mov rsi,%1
mov rdx,%2
syscall
%endmacro

display:
mov rbx,rax ; store no in rbx
mov rdi,result ;point rdi to result variable
mov cx,16 ;load count of rotation in cl

up1:
rol rbx,04 ;rotate no of left by four bits
mov al,bl ; move lower byte in al
and al,0fh ;get only LSB
cmp al,09h ;compare with 39h
jg add_37 ;if greater than 39h skip add 37
add al,30h
jmp skip ;else add 30
add_37:
add al,37h
skip:
mov [rdi],al ;store ascii code in result variable
inc rdi ; point to next byte
dec cx ; decrement counter
jnz up1 ; if not zero jump to repeat
dispmsg result,16 ;call to macro
ret
EXP NO: 03

AIM: Write an X86/64 ALP to find the largest of given Byte/Word/Dword/64-bit numbers

OBJECTIVES:

 To understand assembly language programming instruction set.


 To understand different assembler directives with example.
 To apply instruction set for implementing X86/64 bit assembly language programs

ENVIRONMENT:

 Operating System: 64-bit Open source Linux or its derivative.


 Programming Tools: Preferably using Linux equivalent or MASM/TASM/NASM/FASM.

Text Editor: geditor
THEORY:

Datatype in 80386:

Datatypes of 80386:
The 80386 supports the following data types they are


Bit

Bit Field: A group of at the most 32 bits (4bytes)

Bit String: A string of contiguous bits of maximum 4Gbytes in length.

Signed Byte: Signed byte data

Unsigned Byte: Unsigned byte data.

Integer word: Signed 16-bit data.

Long Integer: 32-bit signed data represented in 2's complement form.

Unsigned Integer Word: Unsigned 16-bit data

Unsigned Long Integer: Unsigned 32-bit data

Signed Quad Word: A signed 64-bit data or four word data.

Unsigned Quad Word: An unsigned 64-bit data.

Offset: 16/32-bit displacement that points a memory location using any of the addressing modes.

Pointer: This consists of a pair of 16-bit selector and 16/32-bit offset.

Character: An ASCII equivalent to any of the alphanumeric or control characters.

Strings: These are the sequences of bytes, words or double words. A string may contain minimum one byte and maximum
4 Gigabytes.

BCD: Decimal digits from 0-9 represented by unpacked bytes.

Packed BCD: This represents two packed BCD digits using a byte, i.e. from 00 to 99.

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

Size of operands in an Intel assembler instruction

 Specifying the size of an operand in Intel


 The size of the operand (byte, word, double word) is conveyed by the operand itself
 EAX means: a 32 bit operand
 AX means: a 16 bit operand
 AL means: a 8 bit operand The size of the source operand and the destination operand must be equal

Addressing modes in 80386:

The purpose of using addressing modes is as follows:

1. To give the programming versatility to the user.


2. To reduce the number of bits in addressing field of instruction.

1. Register addressing mode: MOV EAX, EDX


2. Immediate Addressing modes: MOV ECX, 20305060H
3. Direct Addressing mode: MOV AX, [1897 H]
4. Register Indirect Addressing mode MOV EBX, [ECX]
5. Based Mode MOV ESI, [EAX+23H]
6. Index Mode SUB COUNT [EDI], EAX
7. Scaled Index Mode MOV [ESI*8], ECX
8. Based Indexed Mode MOV ESI, [ECX][EBX]
9. Based Index Mode with displacement EA=EBX+EBP+1245678H
10. Based Scaled Index Mode with displacement MOV [EBX*8] [ECX+5678H], ECX
11. String Addressing modes:
12. Implied Addressing modes:
EXPERIMENT NO. 04

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 understand assembly language programming instruction set.
 To understand different assembler directives with example.
 To apply instruction set for implementing X86/64 bit assembly language programs

ENVIRONMENT:

 Operating System: 64-bit Open source Linux or its derivative.


 Programming Tools: Preferably using Linux equivalent or MASM/TASM/NASM/FASM.
 Text Editor: geditor

THEORY:

1. 80386

Architecture of 80386

The Internal Architecture of 80386 is divided into 3 sections.

• Central processing unit

• Memory management unit

• Bus interface unit

• Central processing unit is further divided into Execution unit and Instruction unit

• Execution unit has 8 General purpose and 8 Special purpose registers which are either used
for handling data or calculating offset addresses.

The Instruction unit decodes the opcode bytes received from the 16-byte instruction code queue and arranges
them in a 3- instruction decoded instruction queue.

• After decoding them pass it to the control section for deriving the necessary control signals. The barrel
shifter increases the speed of all shift and rotate operations.

• The multiply / divide logic implements the bit-shift-rotate algorithms to complete the operations in
minimum time.

• Even 32- bit multiplications can be executed within one microsecond by the multiply / divide logic.
The Memory management unit consists of a Segmentation unit and a Paging unit.
• Segmentation unit allows the use of two address components, viz. segment and offset for relocability
and sharing of code and data.

• Segmentation unit allows segments of size 4Gbytes at max.

• The Paging unit organizes the physical memory in terms of pages of 4kbytes size each.

• Paging unit works under the control of the segmentation unit, i.e. each segment is further divided into
pages. The virtual memory is also organizes in terms of segments and pages by the memory
management unit. The Segmentation unit provides a 4 level protection mechanism for protecting and
isolating the system code and data from those of the application program.

• Paging unit converts linear addresses into physical addresses.

• The control and attribute PLA checks the privileges at the page level. Each of the pages maintains
the paging information of the task. The limit and attribute PLA checks segment limits and attributes at
segment level to avoid invalid accesses to code and data in the memory segments. The Bus control
unit has a prioritizer to resolve the priority of the various bus requests.

• This controls the access of the bus. The address driver drives the bus enable and address signal A0
–A31.

The pipeline and dynamic bus sizing unit handle the related control signals.

• The data buffers interface the internal data bus with the system bus.

Processor Registers

There are sixteen registers that are of use to general purpose programmers. (There are several other
registers for system level programming that are not discussed in this guide.)

General Registers
There are 8 general purpose, 32-bit registers in the 80386. They are EAX, EBX, ECX, EDX, EBP, ESP,
ESI, and EDI. Each register can hold a doubleword containing any of the data types listed above.

A backwards compatible feature is built into the 80386. The lower word of each register can be
addressed as a separate unit. This is useful for handling 16-bit data items and for compatibility with
older 8086 and 80286 programs. The registers are named AX, BX, CX, DX, BP, SP, SI and DI. Note,
the upper word of each register cannot be addressed separately.

Furthermore, each byte of the four 16-bit registers AX, BX, CX, and DX can be separately addressed.
The high bytes are named AH, BH, CH and DH. The lower bytes are named AL, BL, CL and DL.

All of the general purpose registers are available for addressing calculations and for the results of most
calculations. However, a number of functions expect their data to be in specific registers. This allows
for more compact and efficient instructions.

Segment Registers

There are six segment registers, CS, DS, SS, ES, FS, and GS. They are used to identify the current six
segments in use by a program. The CS registers contains the address of the currently running code
segment. The DS register contains the address of the currently accessible data segment. The SS register
contains the address of the current stack segment. The ES, FS, and GS registers contains additional
segments as required by the program.

The flat memory model used by GAS, has only one segment and therefore programmer's don't normally
need too worry about the segment registers. They are usually loaded up with selectors for descriptors
that contain the entire 32-bit linear address space. Once loaded, there is no need to change them, and the
32-bit pointers can address the entire program. You don't need to worry about this initialization since
defaults are selected when your program is loaded. (Selectors and descriptors are part of protected mode
programming.)

While GAS does not use the segmentation model of the 80386, it has it's own segmentation model using
sections. As a minimum there are 3 sections, the text section containing code, the data section and the
bss section, which contains initialization data. You can also create your own sections. All sections are
contained within the same segment. Sections with the same name are combined together during linking.

LIST OF INTERRRUPTS USED: 80h


LIST OF ASSEMBLER DIRECTIVES USED: equ, db

LIST OF MACROS USED: scall

LIST OF PROCEDURES USED: add_proc, sub_proc, mul_proc, div_proc, disp64num

ALGORITHM:

FLOWCHART:

PROGRAM:

section .data
menumsg db 10,'****** Menu ******',
db 10,'1: Addition'
db 10,'2: Subtraction'
db 10,'3: Multiplication'
db 10,'4: Division'
db 10,10,'Enter your choice:: '

menumsg_len: equ $-menumsg

addmsg db 10,'Welcome to additon',10


addmsg_len equ $-addmsg

submsg db 10,'Welcome to subtraction',10


submsg_len equ $-submsg

mulmsg db 10,'Welcome to Multiplication',10


mulmsg_len equ $-mulmsg

divmsg db 10,'Welcome to Division',10


divmsg_len equ $-divmsg

wrchmsg db 10,10,'You Entered a Wrong Choice. .. !',10


wrchmsg_len equ $-wrchmsg
no1 dq 08h
no2 dq 02h

nummsg db 10
result dq 0
resmsg db 10,'Result is:'
resmsg_len equ $-resmsg

case1:cmp byte[choice],'1'
jne case2
call add_proc
jmp up
case2:

cmp byte[choice],'2'
jne case3
call sub_proc
jmp up

case3:
cmp byte[choice],'3'
jne case4
call mul_proc
jmp up
case4:
cmp byte[choice],'4'
jne caseinv
call div_proc
jmp up
caseinv:
scall 1,1, wrchmsg,wrchmsg_len

exit:
mov eax,01
mov ebx,0
int 80h

add_proc:
mov rax,[no1]
adc rax,[no2]
mov [result],rax
scall 1,1,resmsg,resmsg_len
mov rbx,[result]
call disp64num
scall 1,1,nummsg,1
ret

sub_proc:

mov rax,[no1]
subb rax,[no2]
mov [result],rax
scall 1,1,resmsg,resmsg_len
mov rbx,[result]
call disp64num
scall 1,1,nummsg,1
ret

mul_proc:
scall 1,1,mulmsg,mulmsg_len
mov rax,[no1]
mov rbx,[no2]
mul rbx
mov [resh],rdx
mov [resl],rax
scall 1,1, resmsg,resmsg_len
mov rbx,[resh]
call disp64num
mov rbx,[resl]
call disp64num
scall 1,1,nwmsg,1
ret
div_proc:
scall 1,1,divmsg,divmsg_len
mov rax,[no1]
mov rdx,0
mov rbx,[no2]
div rbx
mov [resh],rdx ;Remainder
mov [resl],rax ;Quotient
scall 1,1, rmsg,rmsg_len
mov rbx,[resh]
call disp64num
scall 1,1, qmsg,qmsg_len
mov rbx,[resl]
call disp64num
scall 1,1, nwmsg,1
ret
disp64num:
mov ecx,16
mov edi,dispbuff
dup1:
rol rbx,4
mov al,bl
and al,0fh
cmp al,09
jbe dskip
add al,07h
dskip: add al,30h
mov [edi],al
inc edi
loop dup1
scall 1,1,dispbuff,16
ret
AIM: Write an X86/64 ALP to count number of positive and negative numbers from the array.

OBJECTIVES:
 To understand assembly language programming instruction set.
 To understand different assembler directives with example.
 To apply instruction set for implementing X86/64 bit assembly language programs

ENVIRONMENT:

 Operating System: 64-bit Open source Linux or its derivative.


 Programming Tools: Preferably using Linux equivalent or MASM/TASM/NASM/FASM.
 Text Editor: geditor

THEORY:

Mathematical numbers are generally made up of a sign and a value (magnitude) in which the sign
indicates whether the number is positive, ( + ) or negative, ( – ) with the value indicating the size of the
number, for example 23, +156 or -274. Presenting numbers is this fashion is called “sign-magnitude”
representation since the left most digit can be used to indicate the sign and the remaining digits the
magnitude or value of the number.
Sign-magnitude notation is the simplest and one of the most common methods of representing positive
and negative numbers either side of zero, (0). Thus negative numbers are obtained simply by changing
the sign of the corresponding positive number as each positive or unsigned number will have a signed
opposite, for example, +2 and -2, +10 and -10, etc.
But how do we represent signed binary numbers if all we have is a bunch of one’s and zero’s. We
know that binary digits, or bits only have two values, either a “1” or a “0” and conveniently for us, a
sign also has only two values, being a “+” or a “–“.
Then we can use a single bit to identify the sign of a signed binary number as being positive or negative
in value. So to represent a positive binary number (+n) and a negative (-n) binary number, we can use
them with the addition of a sign.
For signed binary numbers the most significant bit (MSB) is used as the sign bit. If the sign bit is “0”,
this means the number is positive in value. If the sign bit is “1”, then the number is negative in value.
The remaining bits in the number are used to represent the magnitude of the binary number in the
usual unsigned binary number format way.
Then we can see that the Sign-and-Magnitude (SM) notation stores positive and negative values by
dividing the “n” total bits into two parts: 1 bit for the sign and n–1 bits for the value which is a pure
binary number. For example, the decimal number 53 can be expressed as an 8-bit signed binary
number as follows.

Positive Signed Binary Numbers


Negative Signed Binary Numbers

LIST OF INTERRRUPTS USED: 80h

LIST OF ASSEMBLER DIRECTIVES USED: equ, db

LIST OF MACROS USED: print

LIST OF PROCEDURES USED: disp8num

ALGORITHM:

STEP 1: Initialize index register with the offset of array of signed numbers
STEP 2: Initialize ECX with array element count
STEP 3: Initialize positive number count and negative number count to
zero STEP 4: Perform MSB test of array element
STEP 5: If set jump to step 7
STEP 6: Else Increment positive number count and jump to step 8
STEP 7: Increment negative number count and continue
STEP 8: Point index register to the next element
STEP 9: Decrement the array element count from ECX, if not zero jump to step 4, else continue
STEP 10: Display Positive number message and then display positive number count
STEP 11: Display Negative number message and then display negative number count
STEP 12: EXIT

FLOWCHART:

PROGRAM:

;Write an ALP to count no. of positive and negative numbers from the array.
section .data

welmsg db 10,'Welcome to count positive and negative numbers in an array',10


welmsg_len equ $-welmsg

pmsg db 10,'Count of +ve numbers::'


pmsg_len equ $-pmsg
nmsg db 10,'Count of -ve numbers::'
nmsg_len equ $-nmsg

nwline db 10

array dw 8505h,90ffh,87h,88h,8a9fh,0adh,02h,8507h

arrcnt equ 8

pcnt db 0
ncnt db 0

section .bss
dispbuff resb 2

%macro print 2 ;defining print function


mov eax, 4 ; this 4 commands signifies the print
sequence mov ebx, 1
mov ecx, %1 ; first parameter
mov edx, %2 ;second parameter
int 80h ;interrupt command
%endmacro

section .text ;code segment


global _start ;must be declared for linker
_start: ;tells linker the entry point ;i.e start of code
print welmsg,welmsg_len ;print title
mov esi,array
mov ecx,arrcnt ;store array count in extended counter reg

up1: ;label
bt word[esi],15
;bit test the array number (15th byte) pointed by esi.
;It sets the carray flag as the bit tested
jnc pnxt ;jump if no carry to label pskip

inc byte[ncnt] ;if the 15th bit is 1 it signifies it is a ;negative no and so we ;use this command to
increment ncnt counter.
jmp pskip ;unconditional jump to label skip

pnxt: inc byte[pcnt] ;label pnxt if there no carry then it is ;positive no


;and so pcnt is incremented
pskip: inc esi ;increment the source index but this ;instruction only increments it by 8 bit
but the no’s in array ;are 16 bit word and hence it needs to be incremented twice.

inc esi
loop up1 ;loop it ends as soon as the array end “count” or

;ecx=0 loop automatically assums ecx has the counter

print pmsg,pmsg_len ;prints pmsg


mov bl,[pcnt] ;move the positive no count to lower 8 bit of B reg
call disp8num ;call disp8num subroutine
print nmsg,nmsg_len ;prints nmsg
mov bl,[ncnt] ;move the negative no count to lower 8 bits of b reg
call disp8num ;call disp8num subroutine

print nwline,1 ;New line char


exit:
mov eax,01
mov ebx,0
int 80h

disp8num:
mov ecx,2 ;move 2 in ecx ;Number digits to display
mov edi,dispbuff ;Temp buffer

dup1: ;this command sequence which converts hex to bcd


rol bl,4 ;Rotate number from bl to get MS digit to LS digit
mov al,bl ;Move bl i.e. rotated number to AL
and al,0fh ;Mask upper digit (logical AND the contents ;of lower8 bits of accumulator
with 0fh )

cmp al,09 ;Compare al with 9

jbe dskip ;If number below or equal to 9 go to add only 30h


;add al,07h ;Else first add 07h to accumulator

dskip:
add al,30h ;Add 30h to accumulator
mov [edi],al ;Store ASCII code in temp buff (move contents ;of accumulator to the location
pointed by edi)
inc edi
;Increment destination index i.e. pointer to ;next location in temp
buff loop dup1 ;repeat till ecx becomes zero

print dispbuff,2 ;display the value from temp


buff ret ;return to calling program

OUTPUT:
;[root@comppl2022 ~]# nasm -f elf64 Exp5.asm
;[root@comppl2022 ~]# ld -o Exp6 Exp5.o
;[root@comppl2022 ~]# ./Exp5
;Welcome to count +ve and -ve numbers in an array
;Count of +ve numbers::05
;Count of -ve numbers::03
;[root@comppl2022 ~]
EXP NO: 06

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 understand assembly language programming instruction set.


 To understand different assembler directives with example.
 To apply instruction set for implementing X86/64 bit assembly language programs

ENVIRONMENT:

 Operating System: 64-bit Open source Linux or its derivative.


 Programming Tools: Preferably using Linux equivalent or MASM/TASM/NASM/FASM.
 Text Editor: geditor

THEORY:

Hexadecimal Number System:

The “Hexadecimal” or simply “Hex” numbering system uses the Base of 16 system and are a popular
choice for representing long binary values because their format is quite compact and much easier to
understand compared to the long binary strings of 1’s and 0’s.
Being a Base-16 system, the hexadecimal numbering system therefore uses 16 (sixteen) different digits
with a combination of numbers from 0 to 9 and A to F.
Hexadecimal Numbers is a more complex system than using just binary or decimal and is mainly used
when dealing with computers and memory address locations.

Binary Coded Decimal(BCD) Number System:

Binary coded decimal (BCD) is a system of writing numerals that assigns a four-digit binary code to
each digit 0 through 9 in a decimal (base-10) numeral. The four-bit BCD code for any particular single
base-10 digit is its representation in binary notation, as follows:

0 = 0000
1 = 0001
2 = 0010
3 = 0011
4 = 0100
5 = 0101
6 = 0110
7 = 0111
8 = 1000
9 = 1001
Numbers larger than 9, having two or more digits in the decimal system, are expressed digit by
digit. For example, the BCD rendition of the base-10 number 1895 is
0001 1000 1001 0101
The binary equivalents of 1, 8, 9, and 5, always in a four-digit format, go from left to right.
The BCD representation of a number is not the same, in general, as its simple binary representation.
In binary form, for example, the decimal quantity 1895 appears as
11101100111

BCD Number
Decimal Number 4-bit Binary Number Hexadecimal Number

0 0000
0 0000 0000

1 0001
1 0000 0001

2 0010
2 0000 0010

3 0011
3 0000 0011

4 0100
4 0000 0100

5 0101
5 0000 0101

6 0110
6 0000 0110

7 0111
7 0000 0111

8 1000
8 0000 1000

9 1001
9 0000 1001

10 1010
A 0001 0000

11 1011
B 0001 0001

12 1100
C 0001 0010

13 1101 0001 0011


D
14 1110 0001 0100
E
15 1111 0001 0101
F
16 0001 0000 0001 0110
10 (1+0)
17 0001 0001 0001 0111
11 (1+1)

HEX to BCD
Divide FFFF by 10 this FFFF is as decimal 65535 so
Division
65535 / 10 Quotient = 6553 Reminder = 5
6553 / 10 Quotient = 655 Reminder = 3
655 / 10 Quotient = 65 Reminder = 5
65 / 10 Quotient = 6 Reminder = 5
6 / 10 Quotient = 0 Reminder = 6
and we are pushing Reminder on stack and then printing it in reverse order.

BCD to HEX
1 LOOP : DL = 06 ; RAX = RAX * RBX = 0 ; RAX = RAX + RDX = 06
2 LOOP : DL = 05 ; 60 = 06 * 10 ; 65 = 60 + 5
3 LOOP : DL = 05 ; 650 = 60 * 10 ; 655 = 650 + 5
4 LOOP : DL = 03 ; 6550 = 655 * 10 ; 6553 = 6550 + 3
5 LOOP : DL = 06 ; 65530 = 6553 * 10 ; 65535 = 65530 + 5
Hence final result is in RAX = 65535 which is 1111 1111 1111 1111 and when we
print this it is represented as FFFF.

LIST OF INTERRRUPTS USED:

LIST OF ASSEMBLER DIRECTIVES USED:

LIST OF MACROS USED:

LIST OF PROCEDURES USED:

ALGORITHM:

STEP 1: Start
STEP 2: Initialize data section.
STEP 3: Using Macro display the Menu for HEX to BCD, BCD to HEX and exit. Accept the
choice from user.
STEP 4: If choice = 1, call procedure for HEX to BCD
conversion. STEP 5: If choice = 2, call procedure for BCD to
HEX conversion. STEP 6: If choice = 3, terminate the program.

Algorithm for procedure for HEX to BCD

conversion: STEP 7: Accept 4-digit hex number from

user.
STEP 8: Make count in RCX register 0.
STEP 9: Move accepted hex number in BX to AX.
STEP 10: Move base of Decimal number that is 10 in
BX. STEP 11: Move zero in DX.
STEP 12: Divide accepted hex number by 10. Remainder will return in DX.
STEP 13: Push remainder in DX on to stack.
STEP 14: Increment RCX counter.
STEP 15: Check whether AX contents are zero.
STEP 16: If it is not zero then go to step 5.
STEP 17: If AX contents are zero then pop remainders in stack in RDX.
STEP 18: Add 30 to get the BCD number.
STEP 19: Increment RDI for next digit and go to step 11.

Algorithm for procedure for BCD to HEX:


STEP 1: Accept 5-digit BCD number from user.
STEP 2: Take count RCX equal to 05.
STEP 3: Move 0A that is 10 in EBX.
STEP 4: Move zero in RDX register.
STEP 5: Multiply EBX with contents in EAX.
STEP 6: Move contents at RSI that is number accepted from user to DL.
STEP 7: Subtract 30 from DL.
STEP 8: Add contents of RDX to RAX and result will be in RAX.
STEP 9: Increment RSI for next digit and go to step 4 and repeat till RCX becomes zero.
STEP 10: Move result in EAX to EBX and call display procedure.

FLOWCHART:

PROGRAM
section .data
msg1 db 10,10,'###### Menu for Code Conversion ######' db 10,'1: Hex to BCD'
db 10,'2: BCD to Hex' db 10,'3: Exit'
db 10,10,'Enter Choice:' msg1length equ $-msg1

msg2 db 10,10,'Enter 4 digit hex number::' msg2length equ $-msg2

msg3 db 10,10,'BCD Equivalent:' msg3length equ $-msg3

msg4 db 10,10,'Enter 5 digit BCD number::' msg4length equ $-msg4

msg5 db 10,10,'Wrong Choice Entered... Please try again!!!',10,10 msg5length equ $-


msg5

msg6 db 10,10,'Hex Equivalent::' msg6length equ $-msg6


cnt db 0
section .bss
arr resb 06;common buffer for choice, hex and bcd input dispbuff resb 08
ans resb 01

%macro disp 2
mov rax,01
mov rdi,01
mov rsi,%1
mov rdx,%2
syscall
%endmacro

%macro accept 2
mov rax,0
mov rdi,0
mov rsi,%1
mov rdx,%2
syscall
%endmacro

section .text
global _start
_start:

menu:

disp msg1,msg1length
accept arr,2 ; choice either 1,2,3 + enter

cmp byte [arr],'1'


jne l1
call hex2bcd_proc

jmp menu

l1: cmp byte [arr],'2'


jne l2
call bcd2hex_proc
jmp menu

l2: cmp byte [arr],'3'


je exit
disp msg5,msg5length
jmp menu

exit:
mov rax,60
mov rbx,0
syscall

hex2bcd_proc:
disp msg2,msg2length
accept arr,5 ; 4 digits + enter
call conversion
mov rcx,0
mov ax,bx
mov bx,10 ;Base of Decimal No. system
l33: mov dx,0
div bx ; Divide the no by 10
push rdx ; Push the remainder on stack
inc rcx
inc byte[cnt]
cmp ax,0
jne l33
disp msg3,msg3length
l44: pop rdx ; pop the last pushed remainder from
stack add dl,30h ; convert it to ascii
mov [ans],dl
disp ans,1
dec byte[cnt]
jnz l44
ret

bcd2hex_proc:
disp msg4,msg4length
accept arr,6 ; 5 digits + 1 for enter

disp msg6,msg6length

mov rsi,arr
mov rcx,05
mov rax,0
mov ebx,0ah

l55: mov rdx,0


mul ebx ; ebx * eax = edx:eax
mov dl,[rsi]
sub dl,30h
add rax,rdx
inc rsi
dec rcx
jnz
l55 mov ebx,eax ; store the result in ebx
call disp32_num
ret

conversion:
mov bx,0
mov ecx,04
mov esi,arr
up1:
rol bx,04
mov al,[esi]
cmp al,39h
jbe l22
sub al,07h
l22: sub al,30h
add bl,al
inc esi
loop up1
ret

; the below procedure is to display 32 bit result in ebx why 32 bit & not 16 ;bit; because 5 digit bcd no ranges
between 00000 to 99999 & for ;65535 ans ;is FFFF
; i.e if u enter the no between 00000-65535 u are getting the answer between
;0000-FFFF, but u enter i/p as 99999 urans is greater than 16 bit which is ;not; fitted in 16 bit register so 32 bit
register is taken frresult

disp32_num:
mov rdi,dispbuff
mov rcx,08 ; since no is 32 bit,no of digits 8

l77:
rol ebx,4
mov dl,bl
and dl,0fh
add dl,30h
cmp dl,39h
jbe l66
add dl,07h

l66:
mov [rdi],dl
inc rdi
dec rcx
jnz l77
disp dispbuff+3,5 ;Dispays only lower 5 digits as upper three are '0'

ret

;OUTPUT OF PROGRAM

;[admin@localhost ~]$ vi conv.nasm


;[admin@localhost ~]$ nasm -f elf64 conv.nasm -o conv.o
;[admin@localhost ~]$ ld -o conv conv.o
;[admin@localhost ~]$ ./conv

;###### Menu for Code Conversion ######


;1: Hex to BCD
;2: BCD to Hex
;3: Exit

;Enter Choice:1
;Enter 4 digit hex number::FFFF

;BCD Equivalent::65535

;###### Menu for Code Conversion ######


1: Hex to BCD
;2: BCD to Hex
;3: Exit

;Enter Choice:1

;Enter 4 digit hex number::00FF

;BCD Equivalent::255
;###### Menu for Code Conversion ######
;1: Hex to BCD
;2: BCD to Hex
;3: Exit

;Enter Choice:1

;Enter 4 digit hex number::000F

;BCD Equivalent::15

;###### Menu for Code Conversion ######


;1: Hex to BCD
;2: BCD to Hex
;3: Exit

;Enter Choice:2

;Enter 5 digit BCD number::65535

;Hex Equivalent::0FFFF

;###### Menu for Code Conversion ######


;1: Hex to BCD
;2: BCD to Hex
;3: Exit

;Enter Choice:2
;Enter 5 digit BCD number::00255

;Hex Equivalent::000FF

;###### Menu for Code Conversion ######


;1: Hex to BCD
;2: BCD to Hex
;3: Exit
EXP NO: 07

AIM: Write X86/64 ALP to detect protected mode and display the values of GDTR, LDTR, IDTR, TR
and MSW Registers also identify CPU type using CPUID instruction.

OBJECTIVES:

 To understand assembly language programming instruction set.


 To understand different assembler directives with example.
 To apply instruction set for implementing X86/64 bit assembly language programs

ENVIRONMENT:

 Operating System: 64-bit Open source Linux or its derivative.


 Programming Tools: Preferably using Linux equivalent or MASM/TASM/NASM/FASM.
 Text Editor: geditor
THEORY:

The DOS Protected Mode Interface (DPMI) allows DOS program to access the advance features of
80286,386,486 based PC’s in a well-behaved hardware-independent fashion that does not compromise
system protection, DPMI function are defined to manage the local descriptor tables, perform mode
switching, allocates extended memory, allocate DOS memory, Control the interrupt subsystem,
communicate with real mode programs, and read or write certain CPU control registers.

Protected mode multitasking environment, memory managers, or operating systems that


implement the DPMI function are called DPMI host. Protected mode applications that repress DPMI
functions are called DPMI clients.

Interrupt Function:-

Int 2FH

AX=1687H

Return Real –to-Protected Mode Switch Entry Point

Returns:-

If function successful

AX=0
BX=flags

Bit significance
0 0=32-bit programs are not
supported 1=32-bit programs are
supported
1-15 Not used

CL= Processor type

Value Processor type


02H 80286
03H 80386
04H 80486
05H-FFH Reserved for future Intel processors

DH=DPMI major version number as a decimal no

DL=DPMI minor version number as a decimal no

If function unsuccessful that is no DPMI host present AX=non-zero.

Int 2fh is the so-called DOS Multiplex Interrupt, used by many different drivers and resident
utilities,and most of the DPMI functions supported on Int 2FH can be called in either real mode or
protected mode.

Difference Between Real Mode And Protected Mode :-

 In real mode 16-bit CPU can access only 1MB memory.


 In protected mode 16-bit CPU can access more than 1MB memory.
 By default all modern CPU run in protected mode so that you can access 64GB+RAM.
 Protected mode offers multitasking and stability,memory protection,a paging system and
hardware sup[port for virtual memory. Most modern X86 operating systems can run in
protected mode including Linux and version of Microsoft Windows.

6.5. Algorithm

1. Declare data segment.


2. Declare “welmsg” variable as string.
3. Declare wmsg_len variable and store length of “welmsg” in it.
4. Declare variable “gdtmsg” as string.
5. Declare variable “gmsg_len” and store length of “gdtmsg” in it.
6. Declare variable “ldtmsg” as string.
7. Declare variable “idtmsg” as string.
8. Declare variable “imsg_len” as string.
9. Declare variable “nxline” and store new line ascii value in it.
10. Declare variable colmsg and store ‘:’ in it.
11. Declare variable “rmodemsg” as a string.
12. Declare variable “rmsg_len” and store length of “rmodemsg” in it.
13. Declare varaible “pmodemsg” as a string.
14. Declare variable “pmsg_len” and store “pmodemsg” length in it.
15. Declare extra segment.
16. Declare variable “gdt” and reserve 48 bits for it.
17. Declare variable “ldt” and reserve 16 bits for it.
18. Declare variable “idt” and reserve 48 bits for it.
19. Declare variable “dnum_buff” and reserve 4 bytes for it.
20. Declare variable “cr0_data” and reserve 32 bits for it.
21. Declare macro “disp” with 2 arguments.
Move 04H into eax register.
Move 01H into ebx register.
Move first argument of macro into ecx register.
Move second argument of macro into edx register.
Call interrupt 80H.
End the macro.
Declare code segment.
Define entry point label “_start”.
Call macro “disp” and display “welmsg” string.
Store machine status word (CR0 register) into eax register.
Move eax value into “cr0_data” variable.
Rotate right eax by 1 bit.
Jump if carry to “prmode” label.
Call macro “disp” to display “rmodemsg” string.
Jump to “nxt1”.
Mark label “nxt1”.
Store gdtr value into “gdt” variable.
Store ldtr value into “ldt” variable.
Store idtr value into “idt” variable.
Call “disp” macro and display “gdtmsg” string.
Mov upper most 2 bytes of “gdt” variable into bx.
Call “disp_num” procedure.
Move middle 2 bytes of “gdt” variable into bx.
Call “disp_num” procedure.
Move lower 2 bytes of “gdt” variable and store it in bx register.
Call “disp_num” procedure.
Call “disp” macro and display “colmsg”.
Move lowest 2 bytes of “gdt” variable and store it in bx register.
50. Call disp_num procedure.
51 Call “disp” macro and display “ldtmsg”.
52. Move “ldt” variable value into bx.
53. Call “disp_num” procedure.
54. Mov upper most 2 bytes of “idt” variable into bx.
55. Call “disp_num” procedure.
56. Move middle 2 bytes of “idt” variable into bx.
57. Call “disp_num” procedure.
58. Move lower 2 bytes of “idt” variable and store it in bx register.
59. Call “disp_num” procedure.
60. Call “disp” macro and display “colmsg”.
61. Move lowest 2 bytes of “idt” variable and store it in bx register.
62. Call disp_num procedure.
63. Move 01H into eax register.
64. Move 00H into ebx register.
65. Call interrupt 80H
AIM: Write X86/64 ALP to perform non-overlapped block transfer without string specific instructions. Block
containing data can be defined in the data segment.
OBJECTIVES:

 To understand assembly language programming instruction set.


 To understand different assembler directives with example.
 To apply instruction set for implementing X86/64 bit assembly language programs

ENVIRONMENT:

 Operating System: 64-bit Open source Linux or its derivative.


 Programming Tools: Preferably using Linux equivalent or MASM/TASM/NASM/FASM.
 Text Editor: geditor

THEORY:

Addressing modes:
 It is an aspect of the instruction set architecture in most central processing unit (CPU) designs.
The various addressing modes that are defined in a given instruction set architecture define how
machine language instructions in that architecture identify the operand(s) of each instruction.
 An addressing mode specifies how to calculate the effective memory address of an operand by
using information held in registers and/or constants contained within a machine instruction or
elsewhere.
1. Data addressing modes
2. Memory addressing modes, and
3. Stack addressing
The data-addressing modes include:-
1. Register
2. Immediate
3. Direct
4. Register indirect
5. Base plus-index
6. Register relative
7. Base relative-plus-index
8. Scaled-index

The program memory-addressing modes include


1. Program relative
2. Direct
3. Indirect.
The operation of the stack memory is explained so that the PUSH and POP instructions are understood.
Data-Addressing Modes
The MOV instruction is a common and flexible instruction, it provides a basis for the explanation
of the data-addressing modes. The MOV instruction and defines the direction of data flow. The source
is to the right and the destination is to the left, next to the opcode MOV. An opcode, or operation code,
tells the microprocessor which operation to perform.

MOV AX, BX instruction transfers the word contents of the source register (BX) into the destination
register (AX). The source never changes, but the destination usually changes. It is essential to remember
that a MOV instruction always copies the source data and into the destination. The MOV never actually
picks up the data and moves it. Also, note that the flag register remains unaffected by most data transfer
instructions. The source and destination are often called operands.

e.g. MOV AX, BX


The data-addressing modes are as follows:

1. Register Addressing

Register addressing is the most common form of data addressing. The microprocessor contains the
following 8-bit registers used with register addressing: AH, AL, BH, BL, CH, CL, DH, and DL. Also
present are the following 16-bit registers: AX, BX, CX, DX, SP, BP, SI, and DI. In the 80386 and
above, the extended 32-bit registers are EAX, EBX, ECX, EDX, ESP, EBP, EDI, and ESI. With register
addressing, some MOV instructions, and the PUSH and POP instructions, also use the 16 -bit segment
registers (CS, ES, DS, SS, FS, and GS).
It is important for instructions to use registers that are the same size. Never mix an 8 -bit register with a
16-bit register, an 8-bit register with a 32-bit register, or a 16-bit register with 32-bit register because
this is not allowed by the microprocessor and results in an error when assembled. This is even true
when a MOV AX,AL or a MOV EAX,AL instruction may seem to make sense. Of course, the MOV
AX,AL or MOV EAX,AL instruction is not allowed because these registers are of different sizes. Note
that a few instructions, such as SHL DX,CL, are exceptions to this rule. It is also important to note that
none of the MOV instructions affect the flag bits.

It is impossible to show all combinations because there are too many. A segment-to-segment register
MOV instruction is not allowed. Note that the code segment register is not normally changed by a MOV
instruction because the address of the next instruction is found in both IP/EIP and CS. If only CS were
changed, the address of the next instruction would be unpredictable. Therefore, changing the CS register
with a MOV instruction is not allowed.

e.g. MOV AL,BL


e.g. MOV EBX,ECX

Operation of the MOV BX,CX instruction. The source register’s contents do not change, but the
destination register’s contents do change. The instruction copies a 1234H from register CX into register
BX. This erases the old contents (76AFH) of register BX, but the contents of CX remain unchanged.
The contents of the destination register or destination memory location change for all instructions
except the CMP and TEST instructions. The MOV BX, CX instruction does not affect the leftmost 16
bits of register EBX.
2. Direct Data Addressing:

Most instructions can use the direct data-addressing mode. In fact, direct data addressing is applied
to many instructions in a typical program. There are two basic forms of direct data addressing:

Direct addressing, which applies to a MOV between a memory location and AL, AX, or EAX, and
Displacement addressing, this applies to almost any instruction in the instruction set. In either case, the
address is formed by adding the displacement to the default data segment address or an alternate
segment address.

Direct Addressing; Direct addressing with a MOV instruction transfers data between a memory
location, located within the data segment, and the AL (8-bit), AX (16-bit), or EAX (32-bit) register. A
MOV instruction using this type of addressing is usually a 3 -byte long instruction. (In the 80386 and
above, a register size prefix may appear before the instruction, causing it to exceed three bytes in
length.)

The MOV AL, DATA instruction, as represented by most assemblers, loads AL from data segment
memory location DATA (1234H). Memory location DATA is a symbolic memory location, while the
1234H is the actual hexadecimal location. With many assemblers, this instruction is represented as a
MOV AL, [1234H]. The [1234H] is an absolute memory location that is not allowed by all assembler
programs. This may need to be formed as MOV AL,DS:[l234Hj with some assemblers, to show that the
address is in the data segment. Figure 3—5 shows how this instruction transfers a copy of the byte-sized
contents of memory location 1234H into AL. The effective address is formed by adding 1234H (the
offset address) to 1000H (the data segment address of I000H) in a system operating in the real mode.

e.g. MOV AL,[1234H]

3. Register Indirect Addressing:

Register indirect addressing allows data to be addressed at any memory location through an offset
address held in any of the following registers: BP, BX, DI, and SI. For example, if register BX contains
a l000H and the MOV AX,[BX] instruction executes, the word contents of data segment offset address I
000H are copied into register AX. If the microprocessor is operated in the real mode and DS = OIOOH,
this instruction addresses a word stored at memory bytes 2000H and 2001H, and transfers it into register
AX (see Figure 3—6). Note that the contents of 2000H are moved into AL and the contents of 2001H
are moved into AH. The [ ] symbols denote indirect addressing in assembly language. In addition to
using the BP, BX, DI, and SI registers to indirectly address memory, the 80386 and above allow register
indirect addressing with any extended register except.

The data segment is used by default with register indirect addressing or any other addressing mode that
uses BX, DI, or SI to address memory. If the BP register addresses memory, the stack segment is used
by default. These settings are considered the default for these four index and base registers. For the
80386 and above, EBP addresses memory in the stack segment by default; FAX, EBX, ECX, EDX,
EDT, and ESI address memory in the data segment by default. When using a 32 -bit register to address
memory in the real mode, the contents of the 32 -bit register must never exceed 0000FFFFH. In the
protected mode, any value can be used in a 32-bit register that is used to indirectly address memory, as
long as it does not access a location outside of the segment, as dictated by the access rights byte. An
example 80386/80486/Pentium II instruction is MOV EAX,[EBX]. This instruction loads FAX with the
double word-sized number stored at the data segment offset address indexed by EBX.

e.g. MOV EAX,[EBX]

In some cases, indirect addressing requires specifying the size of the data are specified with the special
assembler directive BYTE PTR, WORD PTR, or DWORD PTR. These directives indicate the size of
the memory data addressed by the memory pointer (PTR). For example, the MOV AL,[DI] instruction
is clearly a byte-sized move instruction, but the MOV [DI], 1OH instruction is ambiguous. Does the
MOV [DI],10H instruction address a byte-, word-, or double-word-sized memory location? The
assembler can’t determine the size of the IOH. The instruction MOV BYTE PTR [DI],10H clearly
designates the location addressed by DI as a byte-sized memory location. Likewise, the MOV DWORD
PTR [DI],10H clearly identifies memory location as double word-sized. The BYTE PTR, WORD PTR,
and DWORD PTR directives are used only with instructions that address a memory location through a
pointer or index register with immediate data.

1. Declare data segment.


2. Declare variable “menumsg” to store string messages.
3. Calculate length of “menumsg” in “menumsg_len”.
4. Declare variable “wrchmsg” as string.
5. Calculate length of string “wrchmsg” and store it in “wrchmsg_len”.
6. Declare variable “wrchmsg” as string.
7. Calculate length of “wrchmsg” and store it in “wrchmsg_len”.
8. Declare string “blk_bfrmsg”.
9. Calculate length of “blk_bfrmsg” and store it in “blk_bfrmsg_len”.
10. Declare variable “blk_afrmsg” as a string.
11. Declare “srcmsg” as string.
12. Declare “srcmsg_len” and store the length of “srcmsg” in it.
13. Declare “dstmsg” variable as a string.
14. Declare “dstmsg_len” and store the length of “dstmsg” into it.
15. Declare “srcblk” array containing byte sized 5 elelements.
15 Declare “dstblk” array containing 5 elements initialized to
0.
16. Declare “cnt” variable and initialize it to 05H
17. Declare variable “spacechar” and initialize it to 20H i.e. the ascii value of space.
18. Declare “lfmsg” variable and initialize it to ascii value of new line.
19. Declare extra segment.
20. Declare variable optionbuff and initialize its size to 2 bytes.
21. Declare variable “dispbuff” and initialize itsw size to 02H.
22. Declare macro “dispmsg” with 2 arguments.
a. Move 04H into eax register.
b. Move 01H to ebx register.
c. Move first argument of macro to ecx.
d. Move second argument of macro to edx.
e. Call interrupt 80H.
23. Declare macro “accept” with 2 arguments.
a. Move 03H into eax register.
b. Move 00H to ebx register.
c. Move first argument of macro to ecx.
d. Move second argument of macro to edx.
e. Call interrupt 80H.
24. Declare code segment.
25. Declare entry point label “ _start” .
26. Display “blk_bfrmsg” variable using macro “dispmsg”.
27. Call “showblks” procedure.
28. Declare label “menu”.
29. Call macro “dispmsg” and display “menumsg” string.
30. Call macro “accept” and read value from keyboard into “optionbuff”.
31. Compare “optionbuff” with asccii value of 1.
32. Jump if not equal to label “case2”.
33. Call procedure “blkxferwo_proc”.
34. Jump to label “exit1”.
35. Mark label “case2”.
36. Call procedure “blkxferwo_proc”.
37. Jump to label “exit1”.
38. Mark label “case2”.
39. Compare optionbuff with ‘1’
40. Jump if not equal to “case3”
41. Call proceduree “blkxfer_proc”.
42. Jump to label “exit1”.
43. Mark label “case3”.
44. Compare “optionbuff” variable with ‘3’.
45. Jump if equal to “exit”.
46. Call macro “dispmsg” and pass argument “wrchmsg” to display it.
47. Jump to “menu” label.
48. Mark label “exit1”.
49. Call macro “dispmsg” and display “blk_afrmsg”.
50. Call “showblks” macro.
51.Call macro “dispmsg” and insert new line.
56. Mark label “exit”.
57. Move 01H into eax
register. 58.Move 00H into ebx
register.
59. Call interrupt 0x80H.

“dispblk_proc” procedure
1. Move “cnt” into rcx register.
2. Mark label “rdisp”.
3. Push rcx into stack.
4. Move memory location pointed by esi into bl.
5. Call “disp8_proc” procedure.
6. Increment esi.
7. Call macro “dispmsg” and display “spacechar” variable.
8. Pop top of stack value into rcx.
9. Loop the lines of code till label “rdisp” till ecx becomes zero.
10. Return from procedure.

“blkxferwo_proc” procedure:
1. Move starting address of “srcblk” into esi.
2. Move starting address of “dstblk” into edi.
3. Move “cnt” variable value into ecx.
4. Mark label “blkup1”.
5. Move memory value pointed by esi into al.
6. Move value in register al into memory location pointed by edi.
7. Increment esi.
8. Increment edi.
9. Loop the statements till “blkup1” till ecx becomes zero..
10. Return from procedure.

“blkxferw_proc” procedure:
1. Move starting address of “srcblk” into esi.
2. Move starting address of “dstblk” into edi.
3. Clear direction flag.
4. Transfer string pointed by esi into string pointed by edi.
5. Return from procedure.

“showblks” procedure:
1. Call macro “dispmsg” and display “srcmsg” string.
2. Move starting address of “srcblk” into esi.
3. Call “dispblk_proc” procedure.
4. Call macro “dispmsg” and dispaly “dstmsg” variable.
5. Move staring address of “dstblk” into esi.
6. Call “dispblk_proc” procedure.
7. Return from procedure.

“disp8_proc” procedure:
1. . Move 02H into ecx. 6. Compare al with 09H.
2. . Jump if below or equal to label “dskip”.
3. .. Add al with 07H and store result in al.
4. . Mark label dskip.
5. . Add 30H into al register..
6. . Move al into memory location pointed by edi.
7. . Increment edi.
8. . Loop the instructions from “dup1” till ecx becomes zero.
9. Call “dispbuff” macro and display “dispbuff” variable.
10. . Return from procedure.

11. Move starting address of “dispbuff” into edi.


12. Marl label “dup1”.
13. Rotate bl by 4 bits.
14. And al with 0FH.
LIST OF INTERRRUPTS USED:
LIST OF ASSEMBLER DIRECTIVES USED:
EXP NO: 09

AIM: Write X86/64 ALP to perform overlapped block transfer with string specific instructions
Block containing data can be defined in the data segment.

OBJECTIVES:

 To understand assembly language programming instruction set.


 To understand different assembler directives with example.
 To apply instruction set for implementing X86/64 bit assembly language programs

ENVIRONMENT:

 Operating System: 64-bit Open source Linux or its derivative.


 Programming Tools: Preferably using Linux equivalent or MASM/TASM/NASM/FASM.
 Text Editor: geditor

THEORY:

Addressing modes:
 It is an aspect of the instruction set architecture in most central processing unit (CPU) designs.
The various addressing modes that are defined in a given instruction set architecture define how
machine language instructions in that architecture identify the operand(s) of each instruction.
 An addressing mode specifies how to calculate the effective memory address of an operand by
using information held in registers and/or constants contained within a machine instruction or
elsewhere.
1. Data addressing modes
2. Memory addressing modes, and
3. Stack addressing
The data-addressing modes include:-
1. Register
2. Immediate
3. Direct
4. Register indirect
5. Base plus-index
6. Register relative
7. Base relative-plus-index
8. Scaled-index

. Base-Plus-Index Addressing:

Base-plus-index addressing is similar to indirect addressing because it indirectly addresses memory


data. In the 8086 through the 80286, this type of addressing uses one base register (BP or BX), and one
index
register (DI or SI) to indirectly address memory. The base register often holds the beginning location of
a memory array, while the index register holds the relative position of an element in the array.
Remember that whenever BP addresses memory data, both the stack segment register and BP generate
the effective address.

In the 80386 and above, this type of addressing allows the combination of any two 32-bit extended
registers except ESP. For example, the MOV DL,[EAX+EBX] instruction is an example using EAX (as
the base) plus EBX (as the index). If the EBP register is used, the data are located in the stack segment
instead of in the data segment.

E.g. MOV DL,[EAX+EBX]


5. Register Relative Addressing:

Register relative addressing is similar to base-plus-index addressing and displacement addressing. In


register relative addressing, the data in a segment of memory are addressed by adding the displacement
to the contents of a base or an index register (BP, BX, DI, or SI).
e.g. MOV AL,DATA[DI]

6. Base Relative-Plus-Index Addressing:

The base relative-plus-index addressing mode is similar to the base-plus-index addressing mode. but it
adds a displacement, besides using a base register and an index register, to form the memory address.
This type of addressing mode often addresses a two-dimensional array of memory data.

e.g. MOV AX,[BX+SI]

7. Scaled-Index Addressing:

Scaled-index addressing is the last type of data-addressing mode discussed. This data-addressing mode
is unique to the 80386 through the Pentium II microprocessors. Scaled-index addressing uses two 32-bit
registers (a base register and an index register) to access the memory. The second register (index) is
multiplied by a scaling factor. The scaling factor can be 1X, 2X, 4X, or 8X. A scaling factor of lX is
implied and need not be included in the assembly language instruction (MOV AL,[EBX+ECX]). A
scaling factor of 2X is used to address word-sized memory arrays, a scaling factor of 4X is used with
doubleword-sized memory arrays, and a scaling factor of 8X is used with quadword-sized memory
anays.

e.g. MOV AX,[EDI+2*ECX]

Program memory addressing

modes: Register addressing:


Transfers a copy of a byte or word from the source register or memory location to the destination
register or memory location.

Example: the MOV CX,DX instruction copies the word-sized contents of register DX into register CX.
In the 80386 and above, a doubleword can be transferred from the source register or memory location to
the destination register or memory location.
Example: the MOV ECX,EDX instruction copies the doubleword-sized contents of register EDX into
register ECX.

Immediate addressing:

Transfers the source-immediate byte or word of data into the destination register or memory
location.

Example: the MOV AL,22H instruction copies a byte-sized 22H into register AL. In the 80386 and
above, a doubleword of immediate data can be transferred into a register or memory location.

Example: the MOV EBX, 12345678H instruction copies a double word-sized 12345678H into the 32-
bit wide EBX register.

Direct addressing:

Moves a byte or word between a memory location and a register. The instruction set does not
support a memory-to-memory transfer, except for the MOVS instruction.

Example: the MOV CX,LIST instruction copies the word-sized contents of memory location LIST into
register Coxing the 80386 and above, a double word-sized memory location can also be addressed.

Example: the MOV ESI,LIST instruction copies a 32-bit number, stored in four consecutive bytes of
memory, from location LIST into register ESI.

Register indirect addressing:

Transfers a byte or word between a register and a memory location addressed by an index or
base register. The index and base registers are BP, BX, DI, and SI.

Example: the MOV [AX,IBX] instruction copies the word-sized data from the data segment offset
address indexed by BX into register AX. In the 80386 and above, a byte, word, or double-word is
transferred between a register and a memory location addressed by any register: EAX, EBX, ECX,
EDX, EBP. ED, or ESI.

Example: the MOV AL,[ECX] instruction loads AL from the data segment offset address selected by
the contents of ECX.

Base-plus-index addressing:
Transfers a byte or word between a register and the memory location addressed by a base
register (BP or BX) plus an index register (DI or SI). Example: the MOV [BX+DH,CL] instruction
copies the bytesized contents of register CL into the data segment memory location addressed by BX
plus DI. In the 80386 and above, any register EAX, EBX, ECX, EDX, EBP, EDI, or ESI may be
combined to generate the memory address.

Example: the MOV [EAX+EBX],CL instruction copies the byte-sized contents of register CL into the
data segment memory location addressed by EAX plus EBX.

Register relative addressing:

Moves a byte or word between a register and the memory location addressed by an index or base
register plus a displacement.

Example: MOV AX, [BX+4] or MOV AX, ARRAY[BX].


The first instruction loads AX from the data segment address formed by BX plus 4. The second
instruction loads AX from the data segment memory location in ARRAY plus the contents of BX. The
80386 and above use any register to address memory.

e.g. MOV AX,[ECX+4] or MOV AX,ARRAY[EBX]. The first instruction loads AX from the data
segment address formed by ECX plus 4. The second instruction loads AX from the data segment
memory location ARRAY plus the contents of EBX.

Base relative-plus-index addressing:

Transfers a byte or word between a register and the memory location addressed by a base and an
index register plus a displacement. (Example:

MOV AX,ARRAY[BX+DI] or MOV AX,IBX±DI+41. These instructions load AX from a data


segment memory location. The first instruction uses an address formed by adding ARRAY. BX, and Dl
and the second by adding BX, DI. and 4.) In the 80386 and above, MOV EAX,ARRAY[EBX+ECX]
loads EAX from the data segment memory location accessed by the sum of ARRAY, EBX, and ECX.

Scaled-index addressing:

Is available only in the 80386 through the Pentium Pro microprocessor. The second register of a
pair of registers is modified by the scale factor of 2X, 4X, or 8X to generate the operand memory
address.
LIST OF INTERRRUPTS USED:

LIST OF ASSEMBLER DIRECTIVES USED:

LIST OF MACROS USED:LIST OF

PROCEDURES USED: ALGORITHM:

FLOWCHART:
EXP NO: 10
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 understand assembly language programming instruction set.


 To understand different assembler directives with example.
 To apply instruction set for implementing X86/64 bit assembly language programs

ENVIRONMENT:

 Operating System: 64-bit Open source Linux or its derivative.


 Programming Tools: Preferably using Linux equivalent or MASM/TASM/NASM/FASM.
 Text Editor: geditor

THEORY:
LIST OF INTERRRUPTS USED:

LIST OF ASSEMBLER DIRECTIVES USED:

LIST OF MACROS USED:

LIST OF PROCEDURES USED:

ALGORITHM:

1. Add and Shift Method :

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
00

CY
20
Step II : Now decrement counter count = 3.
Check for carry, carry is not there so add number with itself.

AX =22
+ 22

44 H

Rotate BL to left,

BL = 0 ¬ 0 1 0 0 0 0 0

0 CY

40

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 0 0 0 0 0 0 0

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 0 0 0 0 0 0 0 0

CY 0 0

Carry is there.
Step V : Decrement counter =

0. Carry is present.

4.5 Algorithm

1. Declare macro “dispmsg” with 2 arguments.


2. Move 01H into rax , system call number for display.
3. Move 01H into rdi , handle for screen.
4. Move first argument to macro into rsi , variable to be displayed.
5. Move second argument into rdx , size of variable.
6. Declare macro “accept” with 2 arguments.
7. Move 00H into rax , system call number for reading from keyboard.
8. Move 00H into rdi , handle to screen.
9. Move first argument into rsi , variable to be displayed.
10. Make syscall.
11. End macro.
12. Declare data segment.
13. Declare variable “msg” as string.
14. Declare variable “msg_len” and store the length of “msg” variable in it.
15. Declare varaible “res” as a string.
16. Declare variable “res_len” and store “res” length in it.
17. Declare variable “choice” as string storing menu related messages.
18. Declare variable “choice_len” and store size of “choice” variable in it.
19. Declare extra segment.
20. Declare “num” variable of 3 bytes size.
21. Declare “num1” variable of 1 byte size.
22. Declare “result” variable of 4 byte size.
23. Declare “cho” variable of 2 byte size.
24. Declare code segment.
25. Define entry point label “_start”.
26. XOR register rax with itself to make it zero.
27. XOR register rbx with itself to make it zero.
28. XOR register rcx with itself to make it zero.
29. XOR register rdx with itself to make it zero.
30. Move 00H into “result” variable.
31. Move 00H into “num” variable.
32. Move 00H into “num1” variable.
33. Call “dispmsg” macro and display “choice” variable.
34. Call macro “accept” and read “cho” from keyboard.
35. Compare “cho” with 31H.
36. Jump if equal to label “b”.
37. Jump to label “exit”.
38. Mark label “b”.
39. Call procedure “Succe_addition”.
40. Jump to entry point label “_start”.
41. Mark label “exit”.
42. Move 60H into rax.
43. Move 00H into rdi.
44. Make system call “syscall”.

Convert procedure
1. XOR rbx with rbx to initialize it to 0.
2. XOR rcx with rcx to initialize it to 0.
3. XOR rax with rax to initialize it to 0.
4. Move 02H to rcx register.
5. Move the address of “num” variable into rsi.
6. Mark label “up1”.
7. Rotate left bl by 4 bits.
8. Move value at memory location pointed by rsi into al.
9. Compare al with 39H.
10. Jump if greater to “p1”.
11. Subtract 30H from al.
12. Jump to “p2”.
13. Mark label “p1”
14. Subtract 37H from al.
15. Mark label “p2”.
16. Add al into bl.
17. Increment rsi.
18. Loop the statements from label “up1” till ecx becomes zero.
19. Return from procedure.

Display procedure
1. Move 04H into rcx.
2. Move address of “result” variable into rdi.
3. Mark label “dup1”.
4. Rotate left bx register by 4 bits.
5. Move bl into al.
6. And al with 0FH.
7. Compare al with 09H.
8. Compare al with 09H.
9. Jump if greater to label “p3”.
10. Add 30H into al.
11. Jump to label “p4”.
12. Mark label “p3”.
13. Add 37H into al.
14. Mark label “p4”.
15. Move al into memory location pointed by rdi.
16. Increment rdi.
17. Loop the statements starting from label “dup1” till ecx becomes zero.
18. Return from procedure.
Succe_addition procedure
1. Call “dispmsg” macro and display string msg.
2. Call “accept” macro and read “num” variable from keyboard.
3. Call “convert” procedure.
4. Move bl into memory location for “num1” variable.
5. Call “dispmsg” macro and display “msg” string.
6. Call “accept” macro and read “num” variable from keyboard.
7. Call “convert” procedure.
8. XOR rcx with rcx to make it zero.
9. XOR rax with rax to make it zero.
10. Move num1 into rax.
11. Mark label “repet”.
12. Add rax into rcx.
13. Decrement bl register.
14. Jump if not zero to label “repet”.
15. Move rcx into “result” variable.
16. Call “dispmsg” macro and display “res” message.
17. Move “result” variable into rbx register.
18. Call display procedure.
19. Return from procedure.
Add_shift procedure

1. Call macro “dispmsg” and display message “msg”.


2. Call “accept” macro and read “num” value from keyboard.
3. Call “convert” procedure.
4. Move bl into variable “num1”.
5. Call macro “dispmsg” and display “msg” variable.
6. Call “accept” macro and read “num” value from keyboard.
7. Call procedure convert.
8. Move bl into “num” variable.
9. XOR rbx with rbx to make it zero.
10. XOR rcx with rcx to make it zero.
11. XOR rdx with rdx to make it zero.
12. XOR rax with rax to make it zero.
13. Move 08H into dl.
14. Move value of variable “num1” into al.
15. Move value of “num” into bl.
16. Mark label “p11”.
17. Shift right the value of register bx by 01 bit.
18. Jump if no carry to label “p”.
19. Add value of ax into cx.
20. Mark label “p”.
Shift left value in register ax by 01 bit.
Decrement dl.
Jump if not zero to label “p11”.
Move value in register rcx into variable result.
Call macro “dispmsg” and display “res” variable.
Move “result” variable into rbx.
Call procedure “display”
Return from procedure

FLOWCHART:
EXP NO: 11
AIM: Write X86 Assembly Language Program (ALP) to implement following OS commands
i) COPY, ii) TYPE Using file operations. User is supposed to provide command line arguments
OBJECTIVES:

 To understand assembly language programming instruction set.


 To understand different assembler directives with example.
 To apply instruction set for implementing X86/64 bit assembly language programs

ENVIRONMENT:

 Operating System: 64-bit Open source Linux or its derivative.


 Programming Tools: Preferably using Linux equivalent or MASM/TASM/NASM/FASM.
 Text Editor: geditor

THEORY:

The Program Segment Prefix (PSP) is a data structure used in DOS systems to store the state of a program. It
has the following structure:
Offset Size Contents
00-01 2 bytes (code) CP/M exit (always contain INT
20) 02-03 word (2 bytes) Memory size in paragraphs
04 Byte Reserved
05-09 5 bytes (code) Far call to CP/M compatibility code within DOS
0A-0D dword (4 bytes) Terminate address of previous program (old INT
22) 0E-11 Dword Break address of previous program (old INT 23)
12-15 Dword Critical error address of previous program (old INT 24)
16-17 Word Caller's PSP segment (usually command.com -
internal) 18-2B 20 bytes Job File Table (internal)
2C-2D Word Environment segment
2E-31 Dword SS:SP on entry to last INT 21 call
(Internal) 32-33 Word Max open files (Internal - see
below)
34-37 Dword Handle-entries address (Internal - see
below) 38-4F 24 bytes Reserved
50-52 3 bytes (code) Far call to DOS (always contain INT 21 +
RETF) 53-5B 9 bytes Reserved
5C-6B 16 bytes Unopened Standard RFCB 1
6C-7F 20 bytes Unopened Standard FCB 2 (overwritten if FCB 1 is opened)
80 1 byte Number of bytes on command-line
81-FF 127 bytes Command-line (terminated by a 0RDh)

The PSP is most often used to get the command line arguments of a DOS program, for example the
command "foo.exe -a -f" executes foo.exe with the arguments '-a' and '-f'.
The segment address of the PSP is passed in the DS register when the program is executed. It can also
be determined later by using interrupt 21 sub function 62. This interrupt will return the PSP address in
register BX.

Alternatively, in .COM programs, one can address the PSP directly just by using the offsets listed
above. 00h points to the beginning of the PSP, FFh points to the end, etc. For example, the following
code displays the command line arguments:

org 100h
; int 21h subfunction 9 requires '$' to terminate string
xorbx, bx
movbl, [80h]
mov byte [bx + 81h], '$'

; print the
string mov ah,
9 mov dx, 81h
int 21h

; exit
mov ax, 4C00h
int 21h

Intterupt Function Used:

INT 21H
Function 62H

Get PSP address

Obtains the segment address of program segment prefix for currently executing program.

Call with:
AH=4CH.

Returns:
BX=address of PSP.

AL= return code.

Function 3CH
Create a file

Given an ASCIIZ pathname, create a new file in the designator default directory on the
designated disk drive. If the specified file already exists, it is truncated to zero length. In either case, the
file opened and a handle is returned that can be used by the program for subsequent access to the file.

Call with:
AH=3CH
attribute (Bit may be combined)

Bits(s) Significant(If set)


0 Read-only
1 Hidden
2 System
3 Volume label
4 Reserved(0)
5 Archive
6-15 Reserved(0)

DS:DX= Segment: offset of ASCIIZ pathname.

Returns:
If function successful then carry
flag = Clear AX = handle

If function failed
carry flag = set AX=
error code

FUNCTION 0AH

Buffered keyboard input

Read line from keyboard and places it in the user-designated buffer. The character are echoed to
display. Read the string of bytes foem standard upto and including an ASCII carriage return (0DH).

Call with:
AH=0AH DS:AX=
Segment : offset of buffer.

Returns:
Nothing.

FUNCTION 40H
Write file or device

Given a valid file handle from a previous open or create operation, a buffer Address, and a
length in byte, transfer data from the buffer into the file and then updates the file pointer.

Call with:

AH=40H

BX=handle

CX=No. of bytes to write.

DS:DX=Segment : Offset of Buffer.

Returns:
If function successful

Carry flag =

clear AX=bytes

If function unsuccessful

Carry flag=set

AX==Error code.

FUNCTION 3FH

Read file or device

Given a valid file handle from a previous open or create operation, a buffer Address, and a
length in byte, transfers data at the current file-pointer position from the file into the buffer and then
updates the file pointer position.

Call with:

AH= 3FH

BX=Handle

CX=No. of bytes to read.

DS:DX=Segment: Offset of Buffer.

Returns:
If function successful

Carry flag = clear

If function unsuccessful

Carry flag=set

AX==Error code.

FUNCTION 09H

Display string

Sends a string of characters to display sends a string of characters to the standard output devices,
output may be redirected.
Call with:

AH=09H

DS:DX=Segment: Offset of string

Returns:

Nothing.

FUNCTION 3DH

Open file

Given a ASCIIZ path name opens the specified file in designated or default directory on the
designated or default driver.

Call with:

AH=3DH

AL=Access mode

Bit(s) Significance
0-2 Access mode

LIST OF INTERRRUPTS USED:

LIST OF ASSEMBLER DIRECTIVES USED:


LIST OF MACROS USED:

LIST OF PROCEDURES USED:

ALGORITHM:

FLOWCHART:

EXP NO: 12

AIM: Write X86 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 PUBLIC and EXTERN directives is mandatory.

OBJECTIVES:

 To understand assembly language programming instruction set.


 To understand different assembler directives with example.
 To apply instruction set for implementing X86/64 bit assembly language programs

ENVIRONMENT:

 Operating System: 64-bit Open source Linux or its derivative.


 Programming Tools: Preferably using Linux equivalent or MASM/TASM/NASM/FASM.
 Text Editor: geditor

THEORY:

LIST OF INTERRRUPTS USED:

LIST OF ASSEMBLER DIRECTIVES USED:

LIST OF MACROS USED:

LIST OF PROCEDURES USED:

ALGORITHM:

FLOWCHART
EXP NO: 13
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.

OBJECTIVES:

 To understand assembly language programming instruction set.


 To understand different assembler directives with example.
 To apply instruction set for implementing X86/64 bit assembly language programs

ENVIRONMENT:

 Operating System: 64-bit Open source Linux or its derivative.


 Programming Tools: Preferably using Linux equivalent or MASM/TASM/NASM/FASM.
 Text Editor: geditor

THEORY:

The factorial number system is a mixed radix numeral system: the i-th digit from the right has base i,
which means that the digit must be strictly less than i, and that (taking into account the bases of the less
significant digits) its value to be multiplied by (i − 1)! (its place value).

he factorial number system is sometimes defined with the 0! Place omitted because it is always zero.
In this article, a factorial number representation will be flagged by a subscript "!", so for instance
341010! stands for 364514031201, whose value is

= 3×5! + 4×4! + 1×3! + 0×2! + 1×1! + 0×0!

= ((((3×5 + 4)×4 + 1)×3 + 0)×2 + 1)×1 + 0

= 46310.
(Note that the place value is one less than the radix position, which is why these equations begin with
5!.)
General properties of mixed radix number systems also apply to the factorial number system. For
instance, one can convert a number into factorial representation producing digits from right to left, by
repeatedly dividing the number by the place values (1, 2, 3, ...), taking the remainder as digits, and
continuing with the integer quotient, until this quotient becomes 0.
For example, 46310 can be transformed into a factorial representation by these successive divisions:

463 ÷ 1 = 463, remainder 0


463 ÷ 2 = 231, remainder 1

231 ÷ 3 = 77, remainder 0

77 ÷ 4 = 19, remainder 1

19 ÷ 5 = 3, remainder 4

3 ÷ 6 = 0, remainder 3
The process terminates when the quotient reaches zero. Reading the remainders
backward gives 341010!.
In principle, this system may be extended to represent fractional numbers, though rather
than the natural extension of place values (−1)!, (−2)!, etc., which are undefined, the
symmetric choice of radix values n
= 0, 1, 2, 3, 4, etc. after the point may be used instead. Again, the 0 and 1 places may
be omitted as these are always zero. The corresponding place values are therefore
1/1, 1/1, 1/2, 1/6, 1/24... 1/n!, etc.

LIST OF INTERRRUPTS USED:

LIST OF ASSEMBLER

DIRECTIVES USED: LIST OF

MACROS USED:

LIST OF PROCEDURES

USED: ALGORITHM:

FLOWCHART:

You might also like