MP
MP
%macro io 4
mov rax, %1 ; System call number (1 for write, 0 for read)
mov rdi, %2 ; File descriptor (1 for stdout, 0 for stdin)
mov rsi, %3 ; Buffer address
mov rdx, %4 ; Buffer size
syscall ; Invoke system call
%endmacro
%macro exit 0
mov rax, 60 ; System call number for exit
mov rdi, 0 ; Exit status code (0 for success)
syscall ; Invoke system call
%endmacro
section .data
msg1 db "Write an x86/64 ALP to accept 5 hexadecimal numbers from user and",\
" store them in an array and display the count of positive and negative numbers", 10
msg1len equ $-msg1
msg2 db "Enter 5 64-bit hexadecimal numbers (0-9, A-F only): ", 10
msg2len equ $-msg2
msg3 db "The count of positive numbers is: ", 10
msg3len equ $-msg3
msg4 db "The count of negative numbers is: ", 10
msg4len equ $-msg4
newline db 10
section .bss
asciinum resb 17 ; Buffer for user input (16 characters + 1 for null terminator)
hexnum resq 5 ; Array to store 5 64-bit hexadecimal numbers
pcount resb 1 ; Positive count
ncount resb 1 ; Negative count
section .text
global _start
_start:
; Display initial message
io 1, 1, msg1, msg1len
io 1, 1, msg2, msg2len
; Input 5 hexadecimal numbers
mov rcx, 5 ; Loop counter for 5 inputs
mov rsi, hexnum ; Address to store the converted numbers
xor byte [pcount], 0 ; Initialize positive count
xor byte [ncount], 0 ; Initialize negative count
next_input:
push rsi ; Save registers
push rcx
next_digit:
rol bl, 4
mov al, bl ; Isolate the nibble
and al, 0Fh ; Mask the lower 4 bits
cmp al, 9
jbe add_0 ; Convert to '0'-'9'
add al, 7h ; Convert to 'A'-'F'
add_0:
add al, 30h ; Convert to ASCII
mov [rsi], al ; Store in output buffer
inc rsi ; Move to next character
loop next_digit
next_char:
rol rbx, 4 ; Make space for the next nibble
mov al, [rsi] ; Load a character
cmp al, '9'
jbe convert_digit ; Convert '0'-'9'
sub al, 7h ; Adjust 'A'-'F'
convert_digit:
sub al, 30h ; Convert ASCII to numeric value
add bl, al ; Add to rbx
inc rsi ; Move to next character
loop next_char
ret
; 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.
;Assignment 5
;Name; Adarsh Chauhan
;Roll No: 7207
;Date: 18 March, 2025
section .data
msgname db "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.", 10
msg db "------------------MENU------------------", 10
db " 1. Hex to BCD ", 10
db " 2. BCD to Hex ", 10
db " 3. Exit ", 10
db "Enter your choice : "
msglen equ $-msg
msg1 db "Number : "
len equ $-msg1
endl db 10
section .bss
num resd 1
choice resb 2
a_hex resb 5
a_bcd resb 6
buffer resb 5
temp resb 4
section .text
global _start
_start :
write msgname,msgnamelen
main_menu :
write msg, msglen
read choice, 2
jmp main_menu
case1 :
write msg1, len
read a_hex, 5
call hex_bcd
jmp main_menu
case2 :
write msg1, len
read a_bcd, 6
call bcd_hex
jmp main_menu
case3 :
mov rax, 60
mov rdi, 0
syscall
mov rcx, 5
convert :
mov rdx, 0 ; initialize rdx with 0
loop convert
mov rcx, 5
mov rsi, buffer
print_bcd :
pop ax ; pop the remainder(reverse order)
add ax, 30h ; add 30h to it
mov [rsi], ax ; move that value in the buffer
inc rsi ; increment the pointer
loop print_bcd
write buffer, 5 ; print the answer (BCD number)
write endl, 1
ret
; Procedure to convert a bcd number to a hexadecimal number
bcd_hex :
mov rsi, a_bcd
mov rcx, 5
mov bx, 0AH
mov rax, 0 ; initialize rax with 0
b_convert :
mul bx ; multiply the rax with 0Ah(10 in decimal)
mov dl, [rsi] ; move the digit stored in buffer in dl
sub dl, 30h ; subtract 30h from the digit
add al, dl ; add the number obtained to al
inc rsi ; increment the pointer
loop b_convert
mov bx, ax
call display
ret
;Assignment 6
;Name: Adarsh Chauhan
;Roll No: 7207
;Date: 25 March, 2025
%macro io 4
mov rax,%1 ; System call number (1 for write, 0 for read)
mov rdi,%2 ; File descriptor (1 for stdout, 0 for stdin)
mov rsi,%3 ; Buffer address
mov rdx,%4 ; Buffer size
syscall ; Invoke system call
%endmacro
%macro exit 0
mov rax,60 ; System call number for exit
mov rdi,0 ; Exit status code (0 for success)
syscall ; Invoke system call
%endmacro
section .data
source dq 0x123456789ABCDEF0, \
0x0FEDCBA987654321, \
0xA1B2C3D4E5F60718, \
0xFFFFFFFF00000000, \
0x7F8E9DA1BC2D3E4F
dest dq 0,0,0,0,0
len db 5
msg1 db "Source: ",10
msg1len equ $-msg1
newline db 10
arrow db " ---> "
arrowlen equ $-arrow
section .bss
ascii64 resb 16
choice resb 2
section .text
global _start
_start:
io 1,1,menu,menulen
io 0,0,choice,2
opt1:
call print_src
mov rsi,source
mov rdi,dest
mov rcx,5
lp1:
mov rbx,[rsi]
mov [rdi],rbx
add rsi, 8
add rdi, 8
loop lp1
call print_dest
exit
opt2:
call print_src
mov rsi,source
mov rdi,dest
mov rcx,5
rep movsq
call print_dest
exit
print_src:
io 1,1,msg1,msg1len
mov rsi,source
mov rcx, 5
next:
mov rbx,rsi
push rcx
push rsi
call hex_ascii64
io 1,1,arrow,arrowlen
pop rsi
mov rbx,[rsi]
push rsi
call hex_ascii64
io 1,1,newline,1
pop rsi
pop rcx
add rsi, 8
loop next
ret
print_dest:
io 1,1,msg2,msg2len
mov rdi,dest
mov rcx,5
next2:
mov rbx,rdi
push rcx
push rdi
call hex_ascii64
io 1,1,arrow,arrowlen
pop rdi
mov rbx,[rdi]
push rdi
call hex_ascii64
io 1,1,newline,1
pop rdi
pop rcx
add rdi, 8
loop next2
ret
hex_ascii64:
mov rsi, ascii64 ; Address of output buffer
mov rcx, 16 ; Loop for 16 characters
next3:
rol rbx, 4 ; Get the most significant nibble
mov al, bl ; Isolate the nibble
and al, 0Fh ; Mask the lower 4 bits
cmp al, 9
jbe add30h ; Convert to '0'-'9'
add al, 7h ; Convert to 'A'-'F'
add30h:
add al, 30h ; Convert to ASCII
mov [rsi], al ; Store in output buffer
inc rsi ; Move to next character
loop next3
io 1,1, ascii64, 16 ; io 1,1, the converted number
ret
;Program for overlapped block transfer
;Assignment 7
;Name: Adarsh Chauhan
;Roll No: 7207
;Date: 25 March, 2025
%macro io 4
mov rax,%1 ; System call number (1 for write, 0 for read)
mov rdi,%2 ; File descriptor (1 for stdout, 0 for stdin)
mov rsi,%3 ; Buffer address
mov rdx,%4 ; Buffer size
syscall ; Invoke system call
%endmacro
%macro exit 0
mov rax,60 ; System call number for exit
mov rdi,0 ; Exit status code (0 for success)
syscall ; Invoke system call
%endmacro
section .data
source dq 0x123456789ABCDEF0, \
0x0FEDCBA987654321, \
0xA1B2C3D4E5F60718, \
0xFFFFFFFF00000000, \
0x7F8E9DA1BC2D3E4F
newline db 10
arrow db " ---> "
arrowlen equ $-arrow
section .bss
ascii64 resb 16
choice resb 2
section .text
global _start
_start:
io 1,1,menu,menulen
io 0,0,choice,2
opt1:
call print_src
io 1,1,msg2,msg2len
mov rsi,source
add rsi,20h
mov rdi,source
add rdi,30h
mov rcx,5
lp1:
mov rbx,[rsi]
mov [rdi],rbx
sub rsi, 8
sub rdi, 8
loop lp1
call print_dest
exit
opt2:
call print_src
io 1,1,msg2,msg2len
mov rsi,source
add rsi,20h
mov rdi,source
add rdi,30h
std
mov rcx,5
rep movsq
call print_dest
exit
print_src:
io 1,1,msg1,msg1len
mov rsi,source
add rsi,20h
mov rcx, 5
next:
mov rbx,rsi
push rcx
push rsi
call hex_ascii64
io 1,1,arrow,arrowlen
pop rsi
mov rbx,[rsi]
push rsi
call hex_ascii64
io 1,1,newline,1
pop rsi
pop rcx
sub rsi, 8
loop next
ret
print_dest:
mov rdi,source
add rdi,30h
mov rcx,5
next2:
mov rbx,rdi
push rcx
push rdi
call hex_ascii64
io 1,1,arrow,arrowlen
pop rdi
mov rbx,[rdi]
push rdi
call hex_ascii64
io 1,1,newline,1
pop rdi
pop rcx
sub rdi, 8
loop next2
ret
hex_ascii64:
mov rsi, ascii64 ; Address of output buffer
mov rcx, 16 ; Loop for 16 characters
next3:
rol rbx, 4 ; Get the most significant nibble
mov al, bl ; Isolate the nibble
and al, 0Fh ; Mask the lower 4 bits
cmp al, 9
jbe add30h ; Convert to '0'-'9'
add al, 7h ; Convert to 'A'-'F'
add30h:
add al, 30h ; Convert to ASCII
mov [rsi], al ; Store in output buffer
inc rsi ; Move to next character
loop next3
io 1,1, ascii64, 16 ; io 1,1, the converted number
ret
;Assignment 8
;Name: Adarsh Chauhan
;Roll No: 7207
;Date: 1 April, 2025
; Program to multiply two 8-bit hex numbers using Successive Addition and
; Shift-Add methods
; Uses NASM syntax for Linux environment
section .data
msg1 db "Enter first 8-bit hex number (2 digits): ", 10
msg1len equ $ - msg1
msg2 db "Enter second 8-bit hex number (2 digits): ", 10
msg2len equ $ - msg2
msg3 db "Result (Successive Addition): ", 10
msg3len equ $ - msg3
msg4 db "Result (Shift and Add): ", 10
msg4len equ $ - msg4
dispbuff db 4 dup(0) ; Buffer for displaying 4-digit hex result
newline db 10 ; Newline character
section .bss
ascii_num resb 3 ; Buffer for 2-digit hex input + newline
num1 resb 1 ; First hex number
num2 resb 1 ; Second hex number
call Succ_Add
PRINT msg3, msg3len ; Display result header
PRINT dispbuff, 4 ; Display result
PRINT newline, 1 ; Newline for formatting
call Shift_Add
PRINT msg4, msg4len ; Display result header
PRINT dispbuff, 4 ; Display result
PRINT newline, 1 ; Newline for formatting
; Exit program
mov rax, 60 ; sys_exit
mov rdi, 0 ; Return code 0
syscall
add_loop:
test rcx, rcx ; Check if counter is zero
jz done_succ ; If zero, exit loop
add bx, ax ; Add AX to BX (accumulate result)
dec rcx ; Decrease counter
jmp add_loop ; Repeat
done_succ:
call Hex_to_Ascii ; Convert result to ASCII
ret ; Return
shift_loop:
test dx, dx ; Check if counter is zero
jz done_shift ; If zero, exit loop
shr bl, 1 ; Shift BL right by 1 bit
jnc no_add ; If no carry, skip addition
add cx, ax ; Add AX to CX (accumulate result)
no_add:
shl ax, 1 ; Shift AX left by 1 (next partial product)
dec dx ; Decrease counter
jmp shift_loop ; Repeat
done_shift:
mov bx, cx ; Move result to BX for display
call Hex_to_Ascii ; Convert result to ASCII
ret ; Return
convert_hex:
rol bx, 4 ; Rotate BX left by 4 bits
mov al, bl ; Move lower byte to AL
and al, 0Fh ; Mask lower nibble
cmp al, 9 ; Compare with 9
jbe add_30_hex ; If <= 9, add 30h
add al, 7 ; If > 9, add 37h for A-F
add_30_hex:
add al, 30h ; Convert to ASCII
mov [rsi], al ; Store in buffer
inc rsi ; Move to next position
dec rcx ; Decrease counter
jnz convert_hex ; Repeat until done
ret ; Return
convert_ascii:
rol bl, 4 ; Shift BL left by 4 bits
mov al, [rsi] ; Load ASCII digit
cmp al, '9' ; Compare with '9' (39h)
jbe sub_30_ascii ; If <= '9', subtract 30h
sub al, 37h ; If > '9', subtract 37h (for A-F)
jmp combine
sub_30_ascii:
sub al, 30h ; Subtract 30h (for 0-9)
combine:
add bl, al ; Add to result
inc rsi ; Move to next digit
dec rcx ; Decrease counter
jnz convert_ascii ; Repeat until done
ret ; Return
Macro file
; macro.asm - Macros for Linux syscalls
%macro Print 2
mov rax, 1 ; syscall: write
mov rdi, 1 ; stdout
mov rsi, %1 ; buffer
mov rdx, %2 ; length
syscall
%endmacro
%macro Accept 2
mov rax, 0 ; syscall: read
mov rdi, 0 ; stdin
mov rsi, %1
mov rdx, %2
syscall
%endmacro
%macro fopen 1
mov rax, 2 ; syscall: open
mov rdi, %1 ; filename pointer
mov rsi, 0 ; read-only
syscall
%endmacro
%macro fread 3
mov rax, 0 ; syscall: read
mov rdi, %1 ; file descriptor
mov rsi, %2 ; buffer
mov rdx, %3 ; size
syscall
%endmacro
%macro fwrite 3
mov rax, 1 ; syscall: write
mov rdi, %1 ; file descriptor
mov rsi, %2 ; buffer
mov rdx, %3 ; size
syscall
%endmacro
%macro fclose 1
mov rax, 3 ; syscall: close
mov rdi, %1
syscall
%endmacro
%macro fcreate 1
mov rax, 2 ; syscall: open
mov rdi, %1 ; filename
mov rsi, 577o ; O_WRONLY | O_CREAT | O_TRUNC
mov rdx, 0644o ; permissions
syscall
%endmacro
%macro fdelete 1
mov rax, 87 ; syscall: unlink
mov rdi, %1
syscall
%endmacro
Assignment 9
;ass9.asm - Menu-driven TYPE, COPY, DELETE
;Assignment 9
;Name: Adarsh Chauhan
;Roll No: 7207
;Date: 8/04/2025
%include "macro.asm"
section .data
intro_msg db 10,"Write X86/64 ALP to implement TYPE, COPY, DELETE \
using file operations", 10, \
%include "macro.asm"
section .data
msg_space db "Number of spaces: ", 0
msg_space_len equ $-msg_space
msg_line db "Number of lines: ", 0
msg_line_len equ $-msg_line
msg_char db "Number of occurrences of character: ", 0
msg_char_len equ $-msg_char
dispbuff db 0, 0
nl db 10
section .bss
scount resb 1
ncount resb 1
ccount resb 1
section .text
global far_procedure
extern buffer, buf_len, character
far_procedure:
xor rcx, rcx
xor rbx, rbx
xor rdx, rdx
mov rsi, buffer
mov rcx, [buf_len]
mov bl, byte [character]
.count_loop:
cmp rcx, 0
je display_results
mov al, [rsi]
cmp al, 0x20
jne .check_line
inc byte [scount]
.check_line:
cmp al, 0x0A
jne .check_char
inc byte [ncount]
.check_char:
cmp al, bl
jne .next
inc byte [ccount]
.next:
inc rsi
dec rcx
jmp .count_loop
display_results:
; Display space count
Print msg_space, msg_space_len
mov bl, [scount]
call display8num
; Display line count
Print msg_line, msg_line_len
mov bl, [ncount]
call display8num
; Display character count
Print msg_char, msg_char_len
mov bl, [ccount]
call display8num
ret
display8num:
mov rsi, dispbuff
mov rcx, 2
.next_digit:
rol bl, 4
mov al, bl
and al, 0x0F
cmp al, 9
jbe .add30
add al, 0x37
jmp .store
.add30:
add al, 0x30
.store:
mov [rsi], al
inc rsi
loop .next_digit
Print dispbuff, 2
Print nl, 1
ret
Assignment 10
;Assignment 10
;Name: Adarsh Chauhan
;Roll No: 7207
;Date: 15 April, 2025
%include "macro.asm"
section .data
intro_msg db "Write X86 ALP to find,", 10, \
"a) Number of Blank spaces", 10, \
"b) Number of lines", 10, \
"c) Occurrence of a particular character.", 10
intro_len equ $-intro_msg
msg1 db "Enter file name: ", 0
msg1len equ $-msg1
msg2 db "Enter character to search: ", 0
msg2len equ $-msg2
error_msg db "Error in Opening File", 10
error_len equ $-error_msg
section .bss
global buffer
global buf_len
global character
filename resb 100
character resb 2
buffer resb 1024
buf_len resq 1
filehandle resq 1
section .text
global _start
extern far_procedure
_start:
; Show assignment intro
Print intro_msg, intro_len
; Prompt for file name
Print msg1, msg1len
Accept filename, 100
; Replace newline with null terminator
mov rsi, filename
.find_newline:
mov al, [rsi]
cmp al, 10
je .null_terminate
cmp al, 0
je .after_filename
inc rsi
jmp .find_newline
.null_terminate:
mov byte [rsi], 0
.after_filename:
; Prompt for character
Print msg2, msg2len
Accept character, 2
mov byte [character+1], 0 ; Ensure null termination
; Open file
mov rax, 2 ; syscall: open
mov rdi, filename ; file name
mov rsi, 0 ; 0-READDONLY
syscall
cmp rax, -1
je open_error
mov [filehandle], rax
; Read file into buffer
mov rdi, [filehandle]
mov rax, 0
mov rsi, buffer
mov rdx, 1024
syscall
mov [buf_len], rax
; Close file
mov rax, 3
mov rdi, [filehandle]
syscall
; Call FAR procedure
call far_procedure
; Exit
mov rax, 60
xor rdi, rdi
syscall
open_error:
Print error_msg, error_len
mov rax, 60
mov rdi, 1
syscall