MP Lab Manual
MP Lab Manual
MP Lab Manual
1
Title: Write an X86/64 ALP to accept five 64 bit Hexadecimal numbers
from user and store them in an array and display the accepted numbers
Signature of Staff:
Experiment No. 1
Aim: Write an X86/64 ALP to accept five 64 bit Hexadecimal numbers from user and store them in an array
and display the accepted numbers.
Objectives:
1) To be familiar with the format of assembly language program structure and instructions.
2) To study the format of assembly language program along with different assembler directives and
different functions of the NASM.
Environment:
2) OS:- Windows XP (16 bit Execution ), Fedora 18 (32 & 64 bit Execution)
Software Requirements:
2. OS:- Windows XP (16 bit Execution ), Fedora 18 (32 & 64 bit Execution)
Theory:
Each personal computer has a microprocessor that manages the computer's arithmetical, logical and control
activities. Each family of processors has its own set of instructions for handling various operations like
getting input from keyboard, displaying information on screen and performing various other jobs. These set
of instructions are called 'machine language instruction'. Processor understands only machine language
instructions which are strings of 1s and 0s. However machine language is too obscure and complex for using
in software development. So the low level assembly language is designed for a specific family of processors
that represents various instructions in symbolic code and a more understandable form. Assembly language
is a low-level programming language for a computer, or other programmable device specific to particular
computer architecture in contrast to most high-level programming languages, which are generally portable
across multiple systems. Assembly language is converted into executable machine code by a utility program
referred to as an assembler like NASM, MASM etc.
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.
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.
Instructions needed:
EQU- Assign single absolute values to symbols.
MOV- Copies byte or word from specified source to specified destination
CALL- The CALL instruction causes the procedure named in the operand to be executed. JNZ-Jumps if not
equal to Zero 16
JNC-Jumps if no carry is generated
ALGORITHM:
1 Start
2. Declare & initialize the variables in .data section.
3. Declare uninitialized variables in .bss section.
4. Declare Macros for print and exit operation.
5. Initialize pointer with source address of array.
6. Initialize count for number of elements.
7. Put the complete summed rbx value to arr[n]
8. Max display of 16 characters and rsi points to _output[16]
9. Dividing by base 16
10. Terminate the process.
11. Declare the Procedure.
12. Stop.
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:
Conclusion:
Experiment No. 2
Title: Write an X86/64 ALP to accept a string and to display its length .
Signature of Staff:
Experiment No. 2
Aim: Write an X86/64 ALP to accept a string and to display its length.
Objectives:
2. OS:- Windows XP (16 bit Execution ), Fedora 18 (32 & 64 bit Execution)
Software Requirements
2. OS:- Windows XP (16 bit Execution ), Fedora 18 (32 & 64 bit Execution)
Theory:
MACRO:
Procedures or subroutines are very important in assembly language, as the assembly language programs
tend to be large in size. Procedures are identified by a name. Following this name, the body of the
procedure is described which performs a well-defined job. End of the procedure is indicated by a return
statement.
Syntax
ALGORITHM:
1 Start
10. setting rdx to null without setting a null byte (a tip i saw on reddit) needed to clean dl for use
12. Stop
PROGRAM:
section .data
msg1 db 10,13,"Enter a string:"
len1 equ $-msg1
section .bss
str1 resb 200 ;
string declaration
result resb 16
section .text
global _start
_start:;
display
mov Rax,1
mov Rdi,1
mov Rsi,msg1
mov Rdx,len1
syscall;
store 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
Output:
enter string
Entered
Mumbai
Length is
06
Exiting now
Conclusion:
Experiment No. 3
Title: Write an X86/64 ALP to find the largest of given
Byte/Word/Dword/64-bit numbers.
Signature of Staff:
Experiment No. 3
Aim: Write an X86/64 ALP to find the largest of given Byte/Word/Dword/64-bit numbers.
Objectives:
Environment:
2. OS:- Windows XP (16 bit Execution ), Fedora 18 (32 & 64 bit Execution)
Software Requirements:
Theory:
Datatype in 80386:
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.
Regirs in 80386:
General Purpose Register: EAX, EBX, ECX, EDX
Pointer register: ESP, EBP
Index register: ESI, EDI
Segment Register: CS, FS, DS, GS, ES, SS
Eflags register: EFLAGS
System Address/Memory management Registers : GDTR, LDTR, IDTR
Control Register: Cr0, Cr1, Cr2, Cr3
Debug Register : DR0, DR,1 DR2, DR3, DR4, DR5, DR6, DR7
Test Register: TR0, TR,1 TR2, TR3, TR4, TR5, TR6, TR7
EAX AX
AH,AL
EBX BX
BH,BL
ECX CX
CH,CL
EDX DX
DH,DL
EBP BP
EDI DI
ESI SI
ESP
ALGORITHM:
1 Start
12. Stop.
Program:
section .data
array db 11h,59h,33h,22h,44h
section .bss
counter resb 1
result resb 4
%macro write 2
mov rax,1
mov rdi,1
mov rsi,%1
mov rdx,%2
syscall
%endmacro
section .text
global _start
_start:
mov rsi,array
push rsi
call disp
pop rsi
inc rsi
dec byte[counter]
jnz next
mov byte[counter],05
jg skip
mov al,[rsi]
dec byte[counter]
Jnz repeat
call disp
mov rax,60
mov rdi,1
syscall
disp:
up1:
add al,30h
write result , 4
ret
Output:
Experiment No. 4
Signature of Staff:
Title: Write a switch case driven X86/64 ALP to perform 64-bit hexadecimal
hexadecimal arithmetic operations (+,-,*, /) using suitable macros
.
Experiment No. 4
Aim: Write a switch case driven X86/64 ALP to perform 64-bit hexadecimal arithmetic operations (+,-,*, /)
using suitable macros. Define procedure for each operation.
Objectives:
To apply instruction set for implementing X86/64 bit assembly language programs
Environment:
Software Requirements :
2. OS:- Windows XP (16 bit Execution ), Fedora 18 (32 & 64 bit Execution)
Theory:
Conditional Transfer Instructions The conditional transfer instructions are jumps that may or may not
transfer control, depending on the state of the CPU flags when the instruction executes.
The conditional jumps that are listed as pairs are actually the same instruction. The assembler
provides the alternate mnemonics for greater clarity within a program listing. Conditional jump instructions
contain a displacement which is added to the EIP register if the condition is true. The displacement may be a
byte, a word, or a doubleword. The displacement is signed; therefore, it can be used to jump forward or
backward
Loop Instructions:
The loop instructions are conditional jumps that use a value placed in ECX to specify
the number of repetitions of a software loop. All loop instructions automatically
decrement ECX and terminate the loop when ECX=0. Four of the five loop instructions
specify a condition involving ZF that terminates the loop before ECX reaches zero.
LOOP (Loop While ECX Not Zero) is a conditional transfer tha automatically
decrements the ECX register before testing ECX for the branch condition. If ECX is non- zero,
the program branches to the target label specified in the instruction. The LOOP instruction
causes the repetition of a code section until the operation of the LOOP instruction decrements
ECX to a value of zero. If LOOP finds ECX=0, control transfers to the instruction immediately
following the LOOP instruction. If the value of ECX is initially zero, then the LOOP executes
232 times.
FLOWCHART:
Program:
%macro IO 4
mov rax,%1
mov rdi,%2
mov rsi,%3
mov rdx,%4
syscall
%endmacro
section .data
m1 db "enter choice (+,-,*, /)" ,10 ; 10d -> line feed
l1 equ $-m1
m2 db "Write a switch case driven X86/64 ALP to perform 64-bit hexadecimal arithmetic operations
(+,-,*, /) using suitable macros. Define procedure for each operation." ,10
l2 equ $-m2
m3 db "rahul ghosh 3236" ,10
l3 equ $-m3
madd db "addition here" ,10
l4 equ $-madd
msub db "subtraction here" ,10
l5 equ $-msub
mmul db "multiplication here" ,10
l6 equ $-mmul
mdiv db "division here" ,10
l7 equ $-mdiv
mspace db 10
m_result db "result is "
m_result_l equ $-m_result
m_qou db "qoutient is "
m_qou_l equ $-m_qou
m_rem db "remainder is "
m_rem_l equ $-m_rem
m_default db "enter correct choice",10
m_default_l equ $-m_default
section .bss
choice resb 2
_output resq 1
_n1 resq 1
_n2 resq 1
temp_1 resq 1
temp_2 resq 1
section .text
global _start
_start:
IO 1,1,m2,l2
IO 1,1,m3,l3
IO 1,1,m1,l1
IO 0,0,choice,2
cmp byte [choice],'+'
jne case2
call add_fun
jmp exit
case2:
cmp byte [choice],'-'
jne case3
call sub_fun
jmp exit
case3:
cmp byte [choice],'*'
jne case4
call mul_fun
jmp exit
case4:
cmp byte [choice],'/'
jne case5
call div_fun
jmp exit
case5:
cmp byte [choice],'a'
jne error
call add_fun
call sub_fun
call mul_fun
call div_fun
jmp exit
error:
IO 1,1,m_default,m_default_l
jmp exit
exit:
mov rax, 60
mov rdi, 0
syscall
add_fun:
IO 1,1,madd,l4
mov qword[_output],0
IO 0,0,_n1,17
IO 1,1,_n1,17
call ascii_to_hex
add qword[_output],rbx
IO 0,0,_n1,17
IO 1,1,_n1,17
call ascii_to_hex
add qword[_output],rbx
mov rbx,[_output]
IO 1,1,mspace,1
IO 1,1,m_result,m_result_l
call hex_to_ascii
ret
sub_fun:
IO 1,1,msub,l5
mov qword[_output],0
IO 0,0,_n1,17
IO 1,1,_n1,17
;IO 1,1,mspace,1
call ascii_to_hex
add qword[_output],rbx
IO 0,0,_n1,17
IO 1,1,_n1,17
;IO 1,1,mspace,1
call ascii_to_hex
sub qword[_output],rbx
mov rbx,[_output]
IO 1,1,mspace,1
IO 1,1,m_result,m_result_l
call hex_to_ascii
ret
mul_fun:
IO 1,1,mmul,l6 ; message
IO 0,0,_n1,17 ; n1 input
IO 1,1,_n1,17
call ascii_to_hex; conversion returns hex value in rbx
mov [temp_1],rbx ; storing hex in temp_1
IO 0,0,_n1,17 ;n2 input
IO 1,1,_n1,17
call ascii_to_hex
mov [temp_2],rbx ; putting hex of n2 in temp_2
mov rax,[temp_1] ; temp_1->rax
mov rbx,[temp_2] ;temp_2->rbx
mul rbx ; multiplication
push rax
push rdx
IO 1,1,mspace,1
IO 1,1,m_result,m_result_l
pop rdx
mov rbx,rdx; setting rbx value for conversion
call hex_to_ascii
pop rax
mov rbx,rax; setting rbx value for conversion
call hex_to_ascii ; final output
ret
div_fun:
IO 1,1,mdiv,l7
IO 0,0,_n1,17 ; n1 input
IO 1,1,_n1,17
call ascii_to_hex; conversion returns hex value in rbx
mov [temp_1],rbx ; storing hex in temp_1
IO 0,0,_n1,17 ;n2 input
IO 1,1,_n1,17
call ascii_to_hex
mov [temp_2],rbx ; putting hex of n2 in temp_2
mov rax,[temp_1] ; temp_1->rax
mov rbx,[temp_2] ;temp_2->rbx
xor rdx,rdx
mov rax,[temp_1] ; temp_1->rax
mov rbx,[temp_2] ; temp_2->rbx
div rbx ; div
push rax
push rdx
IO 1,1,mspace,1
IO 1,1,m_rem,m_rem_l
pop rdx
mov rbx,rdx
call hex_to_ascii; remainder output
IO 1,1,mspace,1
IO 1,1,m_qou,m_qou_l
pop rax
mov rbx,rax
call hex_to_ascii; quotient output
ret
ascii_to_hex:
mov rsi, _n1
mov rcx, 16
xor rbx, rbx
next1:
rol rbx, 4
mov al, [rsi]
cmp al,47h
jge error
cmp al, 39h
jbe sub30h
sub al, 7
sub30h:
sub al, 30h
add bl, al
inc rsi
loop next1
ret
hex_to_ascii:
mov rcx, 16
mov rsi,_output
next2:
rol rbx, 4
mov al, bl
and al, 0Fh
cmp al, 9
jbe add30h
add al, 7
add30h:
add al, 30h
mov [rsi], al
inc rsi
loop next2
IO 1,1,_output,16
IO 1,1,mspace,1
ret
Output:
Conclusion:
Experiment No. 5
Signature of Staff:
Experiment No. 5
Aim: Write an X86/64 ALP to count number of positive and negative numbers from the array.
Objectives:
To apply instruction set for implementing X86/64 bit assembly language programs
Environment:
Software Requirements :
2. OS:- Windows XP (16 bit Execution ), Fedora 18 (32 & 64 bit Execution)
Theory:
The following code snippet shows the use of the system call sys_exit:
MOV EAX, 1 ; system call number
(sys_exit) INT 0x80 ; call kernel
The following code snippet shows the use of the system call sys_write:
MOV EAX,4; system call number (sys_write)
MOV EBX,1; file descriptor (stdout)
MOV ECX, MSG ; message to
write MOV EDX, 4; message
length INT0x80; call kernel
Sys_read:
MOV
RAX,0
MOV RDI,0
MOV
RSI,array_name
MOV
RDX,array_size
SYSCALL
Sys_exit:
MOV RAX,60
MOV RDI,0
SYSCALL
Assembly Variables
NASM provides various define directives for reserving storage space for variables. The
define Assembler directive is used for allocation of storage space. It can be used to reserve
as well as initialize one or more bytes.
Allocating Storage Space for Initialized Data
There are five basic forms of the define directive:
Instructions needed:
MATHEMATICAL MODEL:
S = System
s =Distinct Start of
System e = Distinct End
Of System X = Set of
Inputs
Y= Set Of outputs
Fme = Central Function
Mem= Memory
Required Φs =
Constraits
X2 Source Array
Let X2 = {{b7-----b0}{ b7-------b0}--------{b7 b6 b5 b4 b3 b2 b1 b0}} where bi Є X1
There exists a function fX2 :X2 { {00h----FFh}{00h----FFh }{00h----FFh}- - -}
F2 Y = ∑ ( X2)
F3 Display Output
ALGORITHM:
1 Start
2. Declare & initialize the variables in .data section.
3. Declare uninitialized variables in .bss section.
4. Declare Macros for print and exit operation.
5. Initialize pointer with source address of array.
6. Initialize count for number of elements.
7. Set RBX as Counter register for storing positive numbers count.
8. Set RCX as Counter register for storing negative numbers count.
9. Get the number in RAX register.
10. Rotate the contents of RAX register left 1 bit to check sign bit.
11. Check if MSB is 1. If yes, goto step 12,else goto step 13.
12. Increment count for counting negative numbers.
13. Increment count for counting positive numbers.
14. Increment pointer.
15. Decrement count
16. Check for count = 0. If yes, goto step 17 else goto step 9.
17. Store the positive numbers count and negative numbers count in buffer.
18. Display first message. Call the procedure to display the positive count.
19. Display second message. Call the procedure to display the negative count.
20. Add newline.
21. Terminate the process.
22. Declare the Procedure.
23. Stop.
Flowchart:
Program:
section .data
array db 10,12,-21,-12,-19,-34,41
%macro print 2
mov rax,01
mov rdi,01
mov rsi,%1
mov rdx,%2
syscall
%endmacro
section .bss
count resb 2
pcount resb 2
ncount resb 2
totalcount resb 2
section .text
global _start
_start:
mov byte[count],07
mov byte[pcount],00
mov byte[ncount],00
mov rsi,array
Up:
mov al,00
add al,[rsi]
js neg
inc byte[pcount]
jmp Down
neg:
inc byte[ncount]
Down:
add rsi,01
dec byte[count]
jnz Up
mov bl,[pcount]
mov dl,[ncount]
b1:
print msg1,len1
mov bh,[pcount]
call disp
print msg2,len2
mov bh,[ncount]
call disp
mov rax,60
mov rdi,00
syscall
disp:
mov byte[count],02
loop:
rol bh,04
mov al,bh
AND al,0FH
cmp al,09
jbe l1
add al,07h
l1:add al,30h
mov[totalcount],al
print totalcount,02
dec byte[count]
jnz loop
ret
Output:
Conclusion:
Experiment No. 6
Signature of Staff:
Experiment No. 6
Aim: Write X86/64 ALP to convert 4-digit Hex number into its equivalent BCD number and 5- digit BCD
number into its equivalent HEX number. Make your program user friendly to accept the choice from user
for: (a) HEX to BCD b) BCD to HEX (c) EXIT. Display proper strings to prompt the user while accepting the
input and displaying the result. (Wherever necessary, use 64-bit registers).
Objectives:
To apply instruction set for implementing X86/64 bit assembly language programs
Environment:
Software Requirements :
2. OS:- Windows XP (16 bit Execution ), Fedora 18 (32 & 64 bit Execution)
MATHEMATICAL MODEL:
Fme = { F1,F2,F3,F4}
Where F1= Accept X4
F2= If X4=1, Call Fme1
F3= If X4=2,Call Fme2
F4= If X4=3, Call Fme3
THEORY:
1. Hexadecimal to BCD conversion:
MUL: Unsigned Multiply. For 8-bit operand multiplication result will be stored in
AX and for 16-bit multiplication result is stored in DX:AX Commands.
• To assemble
• To link
ld –o hello hello.o
• To execute
./hello
ALGORITHM:
1. Start
2. Initialize data section
3 Display the Menu Message.
4 Accept the choice from the user.
5 If choice=1 then call Hex to bcd procedure. If choice=2 then call Bcd to Hex If
Procedure
choice=3 then call exit procedure
6 Hex to BCD Procedure:
a) Accept 4 Digit Hexadecimal Number.
b) Number=Number/10 & Number=Quotient
c) Push the remainder on the stack
d) If Number=0, Go to next step otherwise go to step b
e) Pop remainder , Convert into ascii & display untill all remainder are popped out
7 Bcd to hex Procedure:
a. RESULT=0
b. Accept BCD Digit
c. Check whether all BCD Digits are accepted. If YES then go to
step e otherwise go to next step
d. RESULT= RESULT*10 + BCD Digit accepted e. Display RESULT
FLOW CHART:
Program:
section .data
menumsg db 10,10,'###### Menu for Code Conversion ######'
db 10,'1: Hex to BCD'
db 10,'2: BCD to Hex'
section .bss
numascii resb 06 ;common buffer for choice, hex and bcd input
outputbuff resb 02
dispbuff resb 08
%macro display 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:
je exit
jmp _start
exit:
mov rax,60
mov rbx,0
syscall;
hex2bcd_proc:
display hexinmsg,hexinmsg_len
accept numascii,5
call packnum
mov ax,bx
mov rcx,0
mov bx,10;Base of Decimal No. system
h2bup1:mov dx,0
div bx
push rdx
inc rcx
cmp ax,0
jne h2bup1
mov rdi,outputbuff
h2bup2: pop rdx
add dl,30h
mov [rdi],dl
inc rdi
loop h2bup2
display bcdopmsg,bcdopmsg_len
display outputbuff,5
jmp menu;
bcd2hex_proc:
display bcdinmsg,bcdinmsg_len
accept numascii,6
display hexopmsg,hexopmsg_len
mov rsi,numascii
mov rcx,05
mov rax,0
mov ebx,0ah
b2hup1: mov rdx,0
mul ebx
mov dl,[rsi]
sub dl,30h
add rax,rdx
inc rsi
loop b2hup1
mov ebx,eax
call disp32_num
jmp menu;
packnum:
mov bx,0
mov ecx,04
mov esi,numascii
up1:
rol bx,04
mov al,[esi]
cmp al,39h
jbe skip1
sub al,07h
skip1: sub al,30h
add bl,al
inc esi
loop up1
ret;
disp32_num:
mov rdi,dispbuff; point esi to buffer
mov rcx,08; load number of digits to display
dispup1:
rol ebx,4; rotate number left by four bits
mov dl,bl; move lower byte in dl
and dl,0fh; mask upper digit of byte in dl
add dl,30h; add 30h to calculate ASCII code
cmp dl,39h; compare with 39h
jbe dispskip1; if less than 39h akip adding 07 more
add dl,07h; else add 07
dispskip1:
mov [rdi],dl; store ASCII code in buffer
inc rdi; point to next byte
loop dispup1; decrement the count of digits to display if not zero jump to repeat
display dispbuff+3,5; Dispays only lower 5 digits as upper three are '0'
ret
Output:
1: Hex to BCD;
2: BCD to Hex;
3: Exit;
BCD Equivalent::15;
1: Hex to BCD
2: BCD to Hex
3: Exit;
Hex Equivalent::0000F
Conclusion:
Experiment No. 7
Title: Write X86/64 ALP to switch from real mode to protected mode
and display the values of GDTR, LDTR, IDTR, TR and MSW Registers
Signature of Staff:
Experiment No. 7
Aim: Write X86/64 ALP to switch from real mode to protected mode and display the values of
GDTR, LDTR, IDTR, TR and MSW Registers.
Objectives:
To apply instruction set for implementing X86/64 bit assembly language programs
Environment:
Software Requirements :
2. OS:- Windows XP (16 bit Execution ), Fedora 18 (32 & 64 bit Execution)
THEORY:
Four registers of the 80386 locate the data structures that control
segmented memory management called as memory management registers:
GDTR :Global Descriptor Table Register
These register point to the segment descriptor tables GDT. Before any segment register
is changed in protected mode, the GDT register must point to a valid GDT. Initialization
of the GDT and GDTR may be done in real-address mode. The GDT (as well as LDTs)
should reside in RAM, because the processor modifies the accessed bit of descriptors.
The instructions LGDT
and SGDT give access to the GDTR.
LDTR :Local Descriptor Table Register
These register point to the segment descriptor tables LDT. The LLDT instruction loads
a linear base address and limit value from a six-byte data operand in memory into the
LDTR. The SLDT instruction always store into all 48 bits of the six-byte data operand.
IDTR Interrupt Descriptor Table Register
This register points to a table of entry points for interrupt handlers (the IDT). The
LIDT instruction loads a linear base address and limit value from a six-byte data
operand in memory into the IDTR. The SIDT instruction always store into all 48 bits
of the six-byte data operand.
TR Task Register
This register points to the information needed by the processor to define the
current task. These registers store the base addresses of the descriptor tables
(A descriptor table is simply a memory array of 8-byte entries that contain
Descriptors and descriptor stores all the information about segment) in the linear
address space and store the segment limits.
SLDT: Store Local Descriptor Table Register
Operation: DEST ← 48-bit BASE/LIMIT register
contents;
Description: SLDT stores the Local Descriptor Table Register (LDTR) in the two-byte
register or memory location indicated by the effective address operand. This register
is a selector that points into the Global Descriptor Table. SLDT is used only in
operating system software. It is not used in application programs.
Flags Affected: None
SGDT: Store Global Descriptor Table Register
Operation: DEST ← 48-bit BASE/LIMIT register
contents;
Description: SGDT copies the contents of the descriptor table register the six bytes of
memory indicated by the operand. The LIMIT field of the register is assigned to the
first word at the effective address. If the operand-size attribute is 32 bits, the next
three bytes are assigned the BASE field of the register, and the fourth byte is written
with zero. The last byte is undefined. Otherwise, if the operand-size attribute is 16
bits, the next 4 bytes are assigned the 32-bit BASE field of the register. SGDT and SIDT
are used only in operating system software; they are not used in application
programs.
Description: SIDT copies the contents of the descriptor table register the six bytes of
memory indicated by the operand. The LIMIT field of the register is assigned to the
first word at the effective address. If the operand-size attribute is 32 bits, the next
three bytes are assigned the BASE field of the register, and the fourth byte is written
with zero. The last byte is undefined. Otherwise, if the operand-size attribute is 16
bits, the next 4 bytes are assigned the 32-bit BASE field of the register. SGDT and SIDT
are used only in operating system software; they are not used in application
programs.
section .data
rmodemsg db 10,"Processor is in Real Mode"
rmsg_len equ $-rmodemsg
pmodemsg db 10,"Processor is in Protected Mode"
pmsg_len equ $-pmodemsg
gdtmsg db 10,"GDT Contents are::"
gdtmsg_len equ $-gdtmsg
ldtmsg db 10,"LDT Contents are::"
ldtmsg_len equ $-ldtmsg
idtmsg db 10,"IDT Contents are::"
idtmsg_len equ $-idtmsg
trmsg db 10,"Task Register Contents are::"
trmsg_len equ $-trmsg
mswmsg db 10,"Machine Status Word::"
mswmsg_len equ $-mswmsg
colmsg db ":"
nwline db 10;
section .bss
gdt resd 1
resw 1
ldt resw 1
idt resd 1
resw 1
tr resw 1
cr0_data resd 1
dispbuff resb 04
%macro display 2
mov rax,1 ;print
mov rdi,1 ;stdout/screen
mov rsi,%1 ;msg
mov rdx,%2 ;msg_len
syscall
%endmacro;
section .text
global _start
_start:
smsw eax ;Stores the machine status word (bits 0 ;through 15 of control register CR0) into eax.
mov [cr0_data],eax
bt eax,0 ;Checking PE(Protected Mode Enable) bit(LSB), ;
display16_proc:
dispskip1:
mov [rdi],dl ;store ASCII code in buffer
inc rdi ;point to next byte
loop dispup1 ;decrement the count of digits to display
display dispbuff,4 ;
ret
Output:
Conclusion:
Experiment No. 8 & 9
Title: Non-overlapped and overlapped block transfer.
Date of Performance: Roll No:
Signature of Staff:
Objectives:
To apply instruction set for implementing X86/64 bit assembly language programs
Environment:
Software Requirements :
2. OS:- Windows XP (16 bit Execution ), Fedora 18 (32 & 64 bit Execution)
AD
Theory:
9D
Non-overlapped blocks: 78
In memory, two blocks are known as non-overlapped when none of the element is common.
09
FF
28
F4
3F Memory Block 2
FD
BB
AA
0D
Memory Block 1
Memory Block 1
Instructions needed:
3. AND-AND each bit in a byte or word with corresponding bit in another byte or word
ALGORITHM:
i) Start
ii) Initialize data section
iii) Initialize RSI to point to source block
iv) Initialize RDI to point to destination block
v) Initialize the counter equal to length of block
vi) Get byte from source block & copy it into destination block
vii) Increment source & destination pointer
viii) Decrement counter
ix) If counter is not zero go to step vi
x) Display Destination Block
xi) Stop
i) Start
ii) Initialize data section
iii) Accept the overlapping position from the user
iv) Initialize RSI to point to the end of source block
v) Add RSI with overlapping Position & use it as pointer to point
to End of Destination Block.
vi) Initialize the counter equal to length of block
vii) Get byte from source block & copy it into destination block
viii) Decrement source & destination pointer
ix) Decrement counter
x) If counter is not zero go to step vii
xi) Display Destination Block
xii) Stop
Mathematical Model:
• To assemble
nasm –f elf 64 hello.nasm -o hello.o
• To link
ld –o hello hello.o
• To execute -
./hello
Program:
section .data
db 10,'3.Exit',10
srcblk db 01h,02h,03h,04h,05h
dstblk db 00,00,00,00,00
spacechar db 20h
section .bss
optionbuff resb 02
dispbuff resb 02
%macro display 2
mov rax,01
mov rdi,01
mov rsi,%1
mov rdx,%2
syscall
%endmacro
%macro accept 2
mov rax,00
mov rdi,00
mov rsi,%1
mov rdx,%2
syscall
%endmacro
section .text
global _start
_start:
display blk_bfrmsg,blk_bfrmsg_len
call dispsrc_blk_proc
call dispdest_blk_proc
menu: display menumsg,menumsg_len
accept optionbuff,02
je wos
je ws
mov rbx,00
syscall;
dispsrc_blk_proc:
display srcmsg,srcmsg_len
mov rsi,srcblk
mov rcx,05h
up1:push rcx
mov bl,[rsi]
push rsi
call disp8_proc
display spacechar,spchlength
pop rsi
inc rsi
pop rcx
loop up1
ret;
dispdest_blk_proc:
display dstmsg,dstmsg_len
mov rdi,dstblk
mov rcx,05
up2:push rcx
mov bl,[rdi]
push rdi
call disp8_proc
display spacechar,spchlength
pop rdi
inc rdi
pop rcx
loop up2
ret;
wos:
mov rsi,srcblk
mov rdi,dstblk
mov rcx,05
mov [rdi],bl
inc rsi
inc rdi
loop again
display blk_afrmsg,blk_afrmsg_len
call dispsrc_blk_proc
call dispdest_blk_proc
jmp menu;
ws:
mov rsi,srcblk
mov rdi,dstblk
mov rcx,05
cld
rep movsb
display blk_afrmsg,blk_afrmsg_len
call dispsrc_blk_proc
call dispdest_blk_proc
jmp menu;
disp8_proc:
mov rsi,dispbuff
mov rcx,02
dup1:
rol bl,4
mov dl,bl
and dl,0Fh
cmp dl,09H
jbe dskip
add dl,07h
dskip:add dl,30h
mov [rsi],dl
inc rsi
loop dup1
display dispbuff,02
ret
Output:
Conclusion:
Experiment No. 10
Signature of Staff:
Experiment 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 apply instruction set for implementing X86/64 bit assembly language programs
Environment:
Software Requirements :
2. OS:- Windows XP (16 bit Execution ), Fedora 18 (32 & 64 bit Execution)
Theory:
DD Define byte
DW Define word
DD Define doubleword
DQ Define quad word
DT Define Ten byte
RESB Reserve byte
RESW Reserve word
RESQ Reserve quad word
REST Reserve ten word
Instructions Needed:
MOV : Move or copy
word ROR : Rotate to
right AND : Logical
AND
INC :
Increment DEC
:
Decrement
JNZ : Jump if not
zero CMP : Compare
JNC : Jump if no
carry JBE : Jump if
below
The method taught in school for multiplying decimal no. is based on calculated partial
products, shifting it to the left & then adding them together. Shift & add multilplication
is similar to the multiplication performed by paper & pencil. This method adds the
multiplicand X to itself Y times where Y denotes the multiplier. To multiply two nos. by
paper & pencil placing the intermediate product in the appropriate positions to the left
of earlier product.
Step 1:
AX=11 + 11 = 22H
Step 2:
Decrement count =3
Check for carry, carry is not there So Add with itself
AX=22+22=44H
Rotate BL to left
BL=0 0000 0000 (00)
Step 3:
Decrement count=2
AX=44+44=88H
Rotate BL to left
Step 4:
Decrement count=0
AX=88+88=110H
Rotate BL to left
Step 5:
Add Ax, BX
0110+0000=0110H
i.e., 11H+10H=0110H
MATHEMATICAL MODEL:
Let x2= b7 b6 b5 b4 b3 b2 b1 b0
Y= b15 b14 b13 b12 bb11 b10 b09 b08 b07 b06 b05 b04 b03 b02 b01 b00
F3= display
{f1,f2,f3} Where
section .data
welmsg db 10,'Multiplication using successive addition',10
welmsg_len equ $-welmsg
nummsg db 10,'Enter two digits of Number::'
nummsg_len equ $-nummsg
resmsg db 10,'Multiplication of elements::'
resmsg_len equ $-resmsg
blankmsg db 10,'',10
blank_len equ $-blankmsg;
**********.bss Section**********************
section .bss
numascii resb 03
num1 resb 02
num2 resb 02
result resb 01
dispbuff resb 04
%macro display 2
mov rax,01
mov rdi,01
mov rsi,%1
mov rdx,%2
syscall
%endmacro
%macro accept 2
mov rax,00
mov rdi,00
mov rsi,%1
mov rdx,%2
syscall
%endmacro;
**********.text Section**********************
section .text
global _start
_start:
**********Packnum Procedure**********************
packnum:
mov bl,0
mov ecx,02
mov esi,numascii
up1:rol bl,04
mov al,[esi]
cmp al,39h
jbe skip1
sub al,07h
skip1: sub al,30h
add bl,al
inc esi
loop up1
ret;
**********Display Procedure**********************
disp16_proc:
mov ecx,4
mov edi,dispbuff
dup1: rol bx,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
display dispbuff,4
ret
Output
;Multiplication of elements::0014
;[root@localhost MIT2016]#
Conclusion:
Experiment No. 11
Signature of Staff:
Experiment 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 apply instruction set for implementing X86/64 bit assembly language programs
Environment:
Software Requirements :
2. OS:- Windows XP (16 bit Execution ), Fedora 18 (32 & 64 bit Execution)
Theory:
PSP-:
Whenever DOS loads a program for execution it creates a 256 byte data
structure called PSP.
It is available at 1st paragraph of true memory program itself is located and
load above psp.
Handles-
Use of file handles to file operation the file management function access the file
in fashion similar To that used under UNIX.
As FCB is a 37 file data structure allocated with the application program memory.
1. Start
2. Get source file name and destination file name from command tail.
3. If file is not present display error message as “File not found” and stop.
4. If present, open the file in read mode.
5. Read the contents of file and print the data on the screen
6. Stop
1. Start.
2. Get source file name and destination file name from command tail.
3. If file is not present display error message as “File not found” and stop.
4. If present, open the file in read mode.
5. Read name of destination file and open it in read mode.
6. Read the contents of file source and write it into destination file.
7. Stop.
Program:
%macro print 2
mov rax,1
mov rdi,1
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
%macro iomodule 4
mov rax,%1
mov rdi,%2
mov rsi,%3
mov rdx,%4
syscall
%endmacro
%macro exit 0
mov rax,60
syscall
%endmacro
section .data
cmd db 10,13, “command menu”
db 10,13,”1.TYPE”
db 10,13,”2.COPY”
db 10,13,”3.DELETE”
db 10,13,”4.EXIT”
db 10,13,”Enter choice:”
len equ $ – cmd
entercmd db 10,13,”Enter command:——: “
cmdlen equ $ – entercmd
msg0 db 10,13,”Failed to open the file!!!”
len0 equ $ – msg0
msg1 db 10,13,”File opened successfully!!!”
len1 equ $ – msg1
msg2 db 10,13,”Failed to open the file!!!”
len2 equ $ – msg2
msg3 db 10,13, “Command not found!!!!”
len3 equ $ – msg3
section .bss
buffer resb 200
bufferlen resb 8
choice resb 50
chlen equ $ – choice
fdis1 resb 200
fdis2 resb 200
cnt1 resb 2
ch1 resb 2
name1 resb 20
name2 resb 20
size resb 200
section .text
global _start
_start:
print cmd, len
accept ch1, 2
mov rsi, ch1
cmp byte[rsi], ‘1’
je TYPE
cmp byte[rsi], ‘2’
je COPY
cmp byte[rsi], ‘3’
exit
jmp error
TYPE:
print entercmd, cmdlen
accept choice, chlen
mov rsi, choice
mov byte[size], al
dec byte[size]
mov al, byte[rsi]
nd:
jmp success
error:
print msg3, len3
jmp _start
success:
inc rsi
dec byte[size]
mov rdi, name1
top:
mov al, byte[rsi]
mov [rdi], al
inc rdi
inc rsi
dec byte[size]
jnz top
mov rax, 3
mov rdi, name1
syscall
jmp _start
COPY:
print entercmd, cmdlen
accept choice, chlen
mov byte[size], al
dec byte[size]
mov al, byte[rsi]
nd2:
cmp al, ‘c’
jne error
inc rsi
dec byte[size]
mov al, byte[rsi]
cmp al, ‘o’
jne error
inc rsi
dec byte[size]
mov al, byte[rsi]
cmp al, ‘p’
jne error
inc rsi
dec byte[size]
mov al, byte[rsi]
cmp al, ‘y’
jne error
inc rsi
dec byte[size]
jmp success2
success2:
inc rsi
dec byte[size]
mov rdi, name1
mov rcx, 9
nsp:
mov al, [rsi]
mov [rdi], al
inc edi
inc rsi
dec byte[size]
loop nsp
inc rsi
dec byte[size]
xor rdi, rdi
mov rdi, name2
nnl:
mov al, [rsi]
mov [rdi], al
inc rdi
inc rsi
dec byte[size]
jnz nnl
mov rax, 3
mov rdi, name1
syscall
mov rax, 3
mov rdi, name2
syscall
jmp _start
exit
Output:
swlab@swlab-H81-M1:~$ cd Desktop/
swlab@swlab-H81-M1:~/Desktop$ cd cmd/
swlab@swlab-H81-M1:~/Desktop/cmd$ nasm -f elf64 cmd.asm
swlab@swlab-H81-M1:~/Desktop/cmd$ ld -o cmd cmd.o
swlab@swlab-H81-M1:~/Desktop/cmd$ ./cmd
command menu
1.TYPE
2.COPY
Enter command:——
command menu
1.TYPE
2.COPY
Conclusion:
Experiment No. 12
Signature of Staff:
Experiment No. 12
Title: Write x86 ALP to find the factorial of a given integer number on a command line by using recursion.
Explicit stack manipulation is expected in the code.
Objectives:
To apply instruction set for implementing X86/64 bit assembly language programs
Environment:
Software Requirements :
2. OS:- Windows XP (16 bit Execution ), Fedora 18 (32 & 64 bit Execution)
Theory:
A recursive procedure is one that calls itself. There are two kind of recursion: direct
and indirect. In direct recursion, the procedure calls itself and in indirect recursion,
the first procedure calls a second procedure, which in turn calls the first procedure.
Recursion could be observed in numerous mathematical algorithms. For example,
consider the case of calculating the factorial of a number. Factorial of a number is
Instructions needed:
1. AND-AND each bit in a byte or word with corresponding bit in another byte or word
8. ADD- ADD instructions are used for performing simple addition of binary data
in byte, word and doubleword size, i.e., for adding 8-bit, 16-bit or 32-bit
operands, respectively.
ALGORITHM:
MATHEMATICAL MODEL:
Let S = { s , e , X, Y, │Φs }
The factorial function is formally defined by the product
This notation works in a similar way to summation notation (Σ), but in this case we
multiply rather than add terms. For example, if n = 4, we would substitute k = 1, then k
= 2, then k = 3 and finally k = 4 and write:
expression: n!
Commands
• To assemble
nasm –f elf 64hello.nasm -o hello.o
• To link
ld –o hello hello.o
• To execute -
./hello
JSPM’s
Imperial College of Engineering and Research, Wagholi,
Pune.
(Approved by AICTE, Delhi & Govt. of Maharashtra, affiliated to Savitribai Phule Pune University)
Gat.No.720,Pune-Nagar road,Wagholi,Pune,412207
Department of Computer Engineering
_______________________________________________________________________________________________________
Program:
section .data
num db 00h
msg db “Factorial is : “
msglen equ $-msg
msg1 db “*****Program to find Factorial of a number***** “,0Ah
db “Enter the number : “,
msg1len equ $-msg1
zerofact db ” 00000001 “
zerofactlen equ $-zerofact
section .bss
dispnum resb 16
result resb 4
temp resb 3
section .text
global _start
_start:
scall 1,1,msg1,msg1len
scall 0,0,temp,3 ;accept number from user
call convert ;convert number from ascii to hex
mov [num],dl
scall 1,1,msg,msglen
JSPM’s
Imperial College of Engineering and Research, Wagholi,
Pune.
(Approved by AICTE, Delhi & Govt. of Maharashtra, affiliated to Savitribai Phule Pune University)
Gat.No.720,Pune-Nagar road,Wagholi,Pune,412207
Department of Computer Engineering
_______________________________________________________________________________________________________
mov rdx,0 ;RMD
mov rax,0 ;RMD
mov al,[num] ;store number in accumulator
cmp al,01h
jbe endfact
mov rbx,0 ;RMD
mov bl,01h
call factr ;call factorial procedure
call display
call exit
endfact:
scall 1,1,zerofact,zerofactlen
call exit
cmp rax,01h
je retcon1
push rax
dec rax
call factr
retcon:
pop rbx
mul ebx
jmp endpr
ret
cont:
mov rdx,0 ;RMD
mov rbx,0 ;RMD
mov bl,10h
div ebx
cmp dl,09h
jbe skip
add dl,07h
skip:
add dl,30h
mov [rsi],dl
dec rsi
loop cont
scall 1,1,dispnum,16
ret
mov rax,60
mov rdi,0
syscall
ret
OUTPUT:
swlab@swlab-Veriton-M200-H81:~/Desktop/dhokane$ nasm -f elf64 fact.asm
swlab@swlab-Veriton-M200-H81:~/Desktop/dhokane$ ld -o fact fact.o
swlab@swlab-Veriton-M200-H81:~/Desktop/dhokane$ ./fact
*****Program to find Factorial of a number*****
Enter the number : 03
Factorial is : 0000000000000006
swlab@swlab-Veriton-M200-H81:~/Desktop/dhokane$
Conclusion: