MP Code WTH Op
MP Code WTH Op
Aim: Write an X86/64 ALP to accept five 64 bit Hexadecimal numbers from user and store
Source code:
section .data
section .bss
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:
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:
add rsi,rbx
syscall
add rbx,17
dec byte[counter]
JNZ loop2
mov rdi,0
syscall
Output:
Practical No 2
Name: Gaikwad Pratiksha Vinayak
Aim: Write an X86/64 ALP to accept a string and to display its length.
Source Code:
section .data
section .bss
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
mov Rdi,0
syscall
%macro dispmsg 2
mov Rax,1
mov Rdi,1
mov rsi,%1
mov rdx,%2
syscall
%endmacro
display:
up1:
add al,30h
add_37:
add al,37h
skip:
ret
Output:
Practical No 3
Name: Gaikwad Pratiksha Vinayak
Aim: Write an X86/64 ALP to find the largest of given Byte/Word/Dword/64-bit numbers.
Source Code:
section .data
section .bss
cnt resb 1
result resb 16
%macro dispmsg 2
mov Rax,1
mov Rdi,1
mov rsi,%1
mov rdx,%2
syscall
%endmacro
section .text
global _start
_start:
mov byte[cnt],5
mov rsi,array
mov al,0
jg skip
xchg al ,[rsi]
dec byte[cnt]
jnz LP
;display al
call display
;display message
mov Rax,1
mov Rdi,1
mov Rsi,msg1
mov Rdx,len1
syscall
mov Rdi,0
syscall
display:
up1:
add al,30h
add_37:
add al,37h
skip1:
ret
Output:
Practical No 4
Name: Gaikwad Pratiksha Vinayak
Aim: Write a switch case driven X86/64 ALP to perform 64-bit hexadecimal arithmetic
operations (+,-,*, /) using suitable macros. Define procedure for each operation.
Source Code:
section .data
db '1: Addition', 10
db '2: Subtraction', 10
db '3: Multiplication', 10
db '4: Division', 10
no1 dq 8h
no2 dq 2h
result dq 0
nwmsg db 10
section .bss
choice resb 1
dispbuff resb 16
%macro scall 4
mov rax, %1
mov rdi, %2
mov rsi, %3
mov rdx, %4
syscall
%endmacro
section .text
global _start
_start:
main_loop:
je add_proc
je sub_proc
je mul_proc
je div_proc
jmp main_loop
add_proc:
call disp64num
jmp main_loop
sub_proc:
call disp64num
jmp main_loop
mul_proc:
mul rbx
call disp64num
jmp main_loop
div_proc:
div rbx
call disp64num
jmp main_loop
disp64num:
mov rcx, 16
lea rdi, [dispbuff]
disp_loop:
rol rbx, 4
mov al, bl
cmp al, 9
jbe digit
jmp write
digit:
write:
mov [rdi], al
inc rdi
loop disp_loop
scall 1, 1, dispbuff, 16
ret
Output:
Practical No 5
Name: Gaikwad Pratiksha Vinayak
Aim: Write an X86/64 ALP to count number of positive and negative numbers from the array.
Source Code:
section .data
nwline db 10
arrcnt equ 8
pcnt db 0
ncnt db 0
section .bss
dispbuff resb 2
%macro print 2
syscall
%endmacro
section .text
global _start
_start:
up1:
pnxt:
pskip:
call disp8num
call disp8num
syscall
disp8num:
dup1:
cmp al, 9
jbe dskip
dskip:
loop dup1
ret
Output:
Practical No 6
Name: Gaikwad Pratiksha Vinayak
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
Source Code:
section .data
newline db 10, 10
section .bss
section .text
global _start
_start:
menu:
syscall
syscall
je hex_to_bcd
je bcd_to_hex
je exit_program
; Invalid choice
mov rax, 1
mov rdi, 1
syscall
jmp menu
hex_to_bcd:
mov rax, 1
mov rdi, 1
syscall
mov rax, 0
mov rdi, 0
mov rdx, 5
syscall
convert_hex_to_bcd:
jbe hex_digit
hex_digit:
add bl, al
inc rsi
loop convert_hex_to_bcd
mov rax, 1
mov rdi, 1
syscall
div bx ; Divide ax by 10
inc rcx
test ax, ax
jnz convert_binary_to_ascii
display_bcd:
mov [dispbuff], dl
mov rax, 1
mov rdi, 1
mov rdx, 1
syscall
loop display_bcd
jmp menu
bcd_to_hex:
mov rax, 1
mov rdi, 1
syscall
mov rax, 0
mov rdi, 0
mov rdx, 6
syscall
convert_bcd_to_hex:
inc rsi
loop convert_bcd_to_hex
mov rax, 1
mov rdi, 1
syscall
convert_hex_to_ascii:
mov dl, bl
jbe hex_output_digit
hex_output_digit:
mov [dispbuff], dl
mov rax, 1
mov rdi, 1
syscall
loop convert_hex_to_ascii
jmp menu
exit_program:
syscall
Output:
Practical No 7
Name: Gaikwad Pratiksha Vinayak
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.
Source Code:
section .data
colonmsg db ':', 10
newline db 10
section .bss
section .text
global _start
_start:
jc protected_mode
call real_mode
protected_mode:
call display_message
jmp read_system_tables
real_mode:
call display_message
read_system_tables:
mov [tr], ax
; Display GDT
call display_message
call display_gdt
; Display LDT
mov rsi, ldtmsg
call display_message
call display_ldt
; Display IDT
call display_message
call display_idt
call display_message
call display_tr
call display_message
call display_cr0
; Exit
syscall
display_message:
syscall
ret
display_gdt:
call display_number
call display_number
mov rbx, [gdt] ; Lower 2 bytes of GDT base address
call display_number
ret
display_ldt:
call display_number
ret
display_idt:
call display_number
call display_number
call display_number
ret
display_tr:
call display_number
ret
display_cr0:
call display_number
call display_number
ret
display_number:
convert_to_ascii:
cmp al, 9
jbe digit
digit:
mov [rsi], al
inc rsi
loop convert_to_ascii
mov rdx, 8
syscall
ret
Output:
Practical No 8
Name: Gaikwad Pratiksha Vinayak
Aim: Write X86/64 ALP to perform non-overlapped block transfer without string specific
Source Code:
global _start
_start:
%macro disp 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
section .text
menu:
disp msg2,len2
disp msg,len
accept choice,02
mov al,byte[choice]
cmp al,31h
je without_string
cmp al,32h
je with_string
cmp al,33h
je exit
without_string:
mov rsi,sarr
mov rdi,darr
a1:
mov al,[rsi]
mov [rdi],al
inc rsi
inc rdi
dec byte[cnt]
jnz a1
disp msg1,len1
mov rsi,darr
a2:
mov rax,[rsi]
push rsi
call displayproc
pop rsi
inc rsi
dec byte[cnt1]
jnz a2
jmp menu
with_string:
mov rsi,sarr
mov rdi,darr
CLD
mov rcx,05
rep movsb
disp msg1,len1
mov rsi,darr
a3:
mov rax,[rsi]
push rsi
call displayproc
pop rsi
inc rsi
dec byte[cnt2]
jnz a3
jmp menu
exit:
mov rax,60
mov rdi,0
syscall
mov rsi,disparr+1
mov rcx,2
l4:mov rdx,0
mov rbx,10h
div rbx
cmp dl,09h
jle l5
add dl,07h
l5:add dl,30h
mov [rsi],dl
dec rsi
dec rcx
jnz l4
mov rax,1
mov rdi,1
mov rsi,disparr
mov rdx,2
syscall
ret
section .data
sarr db 01H,02H,03H,04H,05H
darr db 00H,00H,00H,00H,00H
cnt db 05
cnt1 db 05
cnt2 db 05
msg db "Menu",10
db "1.Without_string",10
db "2.with_string",10
db "3.Exit",10
section .bss
disparr resb 02
choice resb 02
%ifdef COMMENT
Menu
1.Without_string
2.with_string
3.Exit
Enter choice
Menu
1.Without_string
2.with_string
3.Exit
Enter choice
Menu
1.Without_string
2.with_string
3.Exit
Enter choice
%endif
Output:
Practical No 9
Name: Gaikwad Pratiksha Vinayak
Aim: Write X86/64 ALP to perform overlapped block transfer with string specific instructions
Source Code:
global _start
_start:
%macro disp 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
section .text
menu:
disp msg2,len2
disp msg,len
accept choice,02
mov al,byte[choice]
cmp al,31h
je without_string
cmp al,32h
je with_string
cmp al,33h
je exit
without_string:
mov rsi,arr1
mov rdi,arr1
add rsi,09
add rdi,13
a1:
mov al,[rsi]
mov [rdi],al
dec rsi
dec rdi
dec byte[cnt1]
jnz a1
disp msg1,len1
mov rsi,arr1
a2:
mov rax,[rsi]
push rsi
call displayproc
pop rsi
inc rsi
dec byte[cnt2]
jnz a2
jmp menu
with_string:
mov rsi,arr2
mov rdi,arr2
add rsi,09
add rdi,13
STD
mov rcx,10
rep movsb
disp msg1,len1
mov rsi,arr2
a3:
mov rax,[rsi]
push rsi
call displayproc
pop rsi
inc rsi
dec byte[cnt3]
jnz a3
jmp menu
exit:
mov rax,60
mov rdi,0
syscall
mov rsi,disparr+1
mov rcx,2
l4:mov rdx,0
mov rbx,10h
div rbx
cmp dl,09h
jle l5
add dl,07h
l5:add dl,30h
mov [rsi],dl
dec rsi
dec rcx
jnz l4
mov rax,1
mov rdi,1
mov rsi,disparr
mov rdx,2
syscall
ret
section .data
arr1 db 01H,02H,03H,04H,05H,06H,07H,08H,09H,0AH,0BH,0CH,0DH,0EH,0FH
arr2 db 01H,02H,03H,04H,05H,06H,07H,08H,09H,0AH,0BH,0CH,0DH,0EH,0FH
cnt1 db 10
cnt2 db 15
cnt3 db 15
msg db "Menu",10
db "1.Without_string",10
db "2.with_string",10
db "3.Exit",10
db "Enter choice",10
section .bss
disparr resb 02
choice resb 02
%ifdef COMMENT
Menu
1.Without_string
2.with_string
3.Exit
Enter choice
010203040102030405060708090A0F
Menu
1.Without_string
2.with_string
3.Exit
Enter choice
010203040102030405060708090A0F
Menu
1.Without_string
2.with_string
3.Exit
Enter choice
%endif
Output:
Practical No 10
Name: Gaikwad Pratiksha Vinayak
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).
Source Code:
section .data
newline db 10, 0
section .bss
section .text
global _start
_start:
call print_string
call print_string
successive_addition:
call print_string
add_shift_method:
shift_left:
call print_string
syscall
print_string:
syscall
ret
; Print newline
print_newline:
call print_string
ret
read_hex:
syscall
ret
; Print a hexadecimal number (32-bit)
print_hex:
print_hex_loop:
jbe store_digit
store_digit:
jnz print_hex_loop
ret
Output: