MP 1-8 Combine (Direct Double Side Print)
MP 1-8 Combine (Direct Double Side Print)
ASSIGNMENT 1
CODE:-
Section .data
m1 db 'enter hex number (max 16 digits)',10
lengthm1 equ $-m1
m2 db 'you have entered numbered:',10
lengthm2 equ $-m2
section .bss
array resb 200 ;reserve 200 bytes in memory
counter resb 1 ;reserve 1 byte to counter
section .text
global _start
_start:
mov rax,1 ;display message 'enter hex number'
mov rdi,1
mov rsi,m1
mov rdx, lengthm1
syscall
mov byte[counter],5 ;set counter 5
mov rsi,array ;point rsi to start of array
accept:mov rax,0 ;accept a 16 digit number
mov rdi,0
mov rdx,17
syscall
add rsi,17 ;add 17 to rsi to store next number of 16 digits
dec byte[counter]
jnz accept ;if counte is not zero,accept next number
mov rax,1 ;DISPLAY MESSAGE 'YOU HAVE ENTERED NUMBER'
mov rdi,1
mov rsi,m2
mov rdx,lengthm2
syscall
mov byte[counter],5 ;set counter to 5
mov rsi,array ;print rsi to start of array
display:mov rax,1 ;display a 16 digit number
mov rdi,1
mov rdx,17
syscall
add rsi,17 ;add 17 to rsi to pint to next number of 16 digits
dec byte[counter]
jnz display ;if counter is not zero,display next number
mov rax,60 ;terminate the program
syscall
OUTPUT:-
Name : Kunal Nalwade
Roll no. : COSB72
ASSIGNMENT 2
CODE:-
section .data
m1 db 'Enter string (max 255 characters): ', 10
lengthm1 equ $-m1
m2 db 'Length of string is: ', 10
lengthm2 equ $-m2
section .bss
array resb 200 ; Reserve 200 bytes for user input
lengthinascii resb 2 ; Reserve 2 bytes to store ASCII length
section .text
global _start
_start:
; Display "Enter string" message
mov rax, 1 ; sys_write
mov rdi, 1 ; stdout
mov rsi, m1 ; message
mov rdx, lengthm1 ; length of message
syscall
dec rax ; Length of string is stored in rax by syscall, decrement (ignoring Enter key)
mov rbx, rax ; Store length in rbx
mov rax, 1
mov rdi, 1
mov rsi, m2
mov rdx, lengthm2
syscall
mov rdi, lengthinascii
mov al, bl ; Move length into AL
rol al, 4 ; Rotate high nibble to low nibble
and al, 0Fh ; Mask high nibble
cmp al, 9h
ja add37h1
add al, 30h ; Convert to ASCII (0-9)
jmp skip1
add37h1:
add al, 37h ; Convert to ASCII (A-F)
skip1:
mov [rdi], al ; Store first digit
inc rdi ; Move to next byte
add37h2:
add al, 37h ; Convert to ASCII (A-F)
skip2:
mov [rdi], al ; Store second digit
; Exit program
mov rax, 60
xor rdi, rdi
syscall
OUTPUT:-
Name : Kunal Nalwade
Roll no. : COSB72
Title: Find the largest number
ASSIGNMENT 3
section .data
section .bss
maxnumascii resb 2 ; two digits of maximum number after converted to ASCII
; will be stored here
section .text
global _start
_start:
inc rsi ; point rsi to next location to save next ascii digit
mov al,bl ; again take max number which was saved in bl
and al,0fh ; mask upper digit
add al,30h ; convert digit in lower nibble to ASCII by adding 30h
mov byte[rsi],al
mov rax,1 ; display the two digits of max number saved at maxnumascii
mov rdi,1
mov rsi,maxnumascii
mov rdx,2
syscall
mov rax,60 ; exit the program
syscall
Output:
Name : Kunal Nalwade
Roll no. : COSB72
ASSIGNMENT 4
:INPUT CODE :
section .data
m1 db 'Total positive numbers: ',10
m1length equ $-m1
m2 db 'Total negative numbers: ',10
m2length equ $-m2
array db 1,-2,-3,4,5,6 ; Example array
section .bss
positive resb 1 ; Counter for positive numbers
negative resb 1 ; Counter for negative numbers
posinascii resb 3 ; ASCII storage (for up to 2-digit numbers + null)
neginascii resb 3 ; ASCII storage
section .text
global _start
_start:
mov byte [positive], 0 ; Initialize positive count to 0
mov byte [negative], 0 ; Initialize negative count to 0
mov rsi, array ; Point RSI to first element
mov cx, 6 ; Counter for 6 elements
back:
mov al, [rsi] ; Load value from array
movsx ax, al ; Sign extend to AX
test ax, ax ; Check if number is negative
js incneg ; Jump if negative
inc byte [positive] ; Increment positive count
jmp next
incneg:
inc byte [negative] ; Increment negative count
next:
inc rsi ; Move to next element
loop back ; Repeat until CX = 0
; Exit program
mov rax, 60
xor rdi, rdi
syscall
;OUTPUT :
Name : Kunal Nalwade
Roll no. : COSB72
ASSIGNMENT 5
:INPUT CODE :
section .data
rmodemsg db 10, 'Processor is in Real Mode', 0
rmsg_len equ $ - rmodemsg
newline db 10
section .bss
gdt resd 1 ; Reserve space for GDTR
ldt resw 1 ; Reserve space for LDTR
idt resd 1 ; Reserve space for IDTR
tr resw 1 ; Reserve space for TR
cr0_data resd 1 ; Store CR0 (MSW)
num_buff resb 4 ; Buffer for number to ASCII conversion
%macro print 2
mov rax, 1 ; syscall: write
mov rdi, 1 ; file descriptor: stdout
mov rsi, %1 ; message address
mov rdx, %2 ; message length
syscall
%endmacro
section .text
global _start
_start:
smsw eax ; Read CR0 (MSW) into EAX
mov [cr0_data], eax ; Store MSW in cr0_data
protected_mode:
print pmodemsg, pmsg_len ; Print protected mode message
exit_program:
mov rax, 60 ; syscall: exit
xor rdi, rdi ; status 0
syscall
convert_loop:
rol bx, 4 ; Rotate left to get next nibble
mov dl, bl
and dl, 0x0F ; Mask upper bits
add dl, '0' ; Convert to ASCII
cmp dl, '9'
jbe store_char
add dl, 7 ; Adjust for 'A'-'F'
store_char:
mov [rsi], dl ; Store character in buffer
inc rsi ; Move to next buffer position
loop convert_loop
;OUTPUT :
Name : Kunal Nalwade
Roll no. : COSB72
ASSIGNMENT 6
:INPUT CODE :
section .data
newline db 10,10 ; Two newline characters for formatting output
newline_length equ $-newline ; Length of newline sequence
space db ' ' ; Space character for separating hex values
spacelength equ $-space ; Length of the space character
section .bss
num_in_ascii resb 2 ; Buffer to store two ASCII characters representing hex
values
counter resb 1 ; Counter for looping through 5 elements
%macro print 2
; Macro to print a message using syscall
mov rax,1 ; syscall number for write
mov rdi,1 ; file descriptor 1: standard output
mov rsi,%1 ; pointer to message
mov rdx,%2 ; length of message
syscall ; invoke syscall to print
%endmacro
section .text
global _start
_start:
; ---------------------------------------------
; display_block: Displays 5 bytes from the address in RBP as hex values
; ---------------------------------------------
display_block:
mov byte[counter],5 ; Set loop counter to 5
repeat2:
mov rcx,num_in_ascii ; RCX points to ASCII buffer
;OUTPUT :
Name : Kunal Nalwade
Roll no. : COSB72
ASSIGNMENT 7
:INPUT CODE :
;----------------------------------
; Transfer without string operations
without:
mov rsi, src ; Point to source block
mov rdi, dst + 2 ; Point to destination block (offset by 2 bytes)
mov rcx, 3 ; Transfer 3 bytes
copy_loop:
mov al, [rsi] ; Load byte from source block into AL
mov [rdi], al ; Store byte into destination block
inc rsi ; Move to next byte in source block
inc rdi ; Move to next byte in destination block
loop copy_loop ; Repeat until RCX = 0
;-----------------------------------------------------
; Transfer with string operations
with:
mov rsi, src ; Point to source block
mov rdi, dst + 2 ; Point to destination block (offset by 2 bytes)
mov rcx, 3 ; Transfer 3 bytes
;----------------------------------------------------
exit:
mov rax, 60 ; syscall: exit
mov rdi, 0 ; exit code 0
syscall
;---------------------------------------------------
; Procedure to display block content in hex format
dispblk_proc:
mov rcx, 5 ; Loop counter: 5 bytes to display
display_loop:
mov bl, [rsi] ; Load byte from memory into BL
push rsi ; Save RSI on stack
push rcx ; Save RCX on stack
call disp_proc ; Call display procedure to format hex value
pop rcx ; Restore RCX from stack
pop rsi ; Restore RSI from stack
inc rsi ; Move to the next byte
loop display_loop ; Repeat for all 5 bytes
;---------------------------------------------------
; Procedure to format and display hex bytes
disp_proc:
mov rsi, dispbuff ; Point to display buffer
digit:
add al, '0' ; Convert to ASCII (0-9 or A-F)
mov [rsi], al ; Store the high nibble in buffer
inc rsi ; Move to the next buffer position
;OUTPUT :
Name :- Kunal Nalwade
Roll no:- COSB72
Assignment no.8
%macro dispmsg 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 .data
section .bss
num resb 03
num1 resb 01
result resb 04
cho resb 2
section .text
global _start
_start:
mov rax,00
mov rbx,00
mov rcx,00
mov rdx,00
mov byte[result],0
mov byte[num],0
mov byte[num1],0
dispmsg choice,choice_len
accept cho,2
cmp byte[cho],31h
je a
cmp byte[cho],32h
je b
jmp exit
a: call Succe_addition
jmp _start
b: call Add_shift
jmp _start
exit:
mov rax,60
mov rdi,0
syscall
convert:
mov rbx,00
mov rax,00
mov rcx,02
mov rsi,num
up1:
rol bl,04
mov al,[rsi]
cmp al,39h
jg p1
sub al,30h
jmp p2
p1: sub al,37h
p2: add bl,al
inc rsi
loop up1
ret
display:
mov rcx,4
mov rdi,result
dup1:
rol bx,4
mov al,bl
and al,0fh
cmp al,09h
jg p3
add al,30h
jmp p4
p3: add al,37h
p4: mov [rdi],al
inc rdi
loop dup1
dispmsg result,4
ret
Succe_addition:
dispmsg m1,m1_len
accept num,3
call convert
mov[num1],bl
dispmsg m1,m1_len
accept num,3
call convert
mov rcx,00
mov rax,[num1]
repet:
add rcx,rax
dec bl
jnz repet
mov[result],rcx
dispmsg m2,m2_len
mov rbx,[result]
call display
ret
Add_shift:
dispmsg m1,m1_len
accept num,3
call convert
mov [num1],bl
dispmsg m1,m1_len
accept num,3
call convert
mov [num],bl
mov rbx,00
mov rcx,00
mov rdx,00
mov rax,00
mov dl,08
mov al,[num1]
mov bl,[num]
p: shl ax,01
dec dl
jnz p11
mov[result],rcx
dispmsg m2,m2_len
mov rbx,[result]
call display
ret
OUTPUT :-