0% found this document useful (0 votes)
11 views24 pages

MP 1-8 Combine (Direct Double Side Print)

This document contains multiple assembly language assignments by Kunal Nalwade, each demonstrating different programming concepts. Assignments include accepting user input, displaying messages, counting positive and negative numbers, and handling processor modes. Each section is well-structured with data, BSS, and text segments, showcasing various functionalities such as ASCII conversion and system calls.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views24 pages

MP 1-8 Combine (Direct Double Side Print)

This document contains multiple assembly language assignments by Kunal Nalwade, each demonstrating different programming concepts. Assignments include accepting user input, displaying messages, counting positive and negative numbers, and handling processor modes. Each section is well-structured with data, BSS, and text segments, showcasing various functionalities such as ASCII conversion and system calls.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Name : Kunal Nalwade

Roll no. : COSB72

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

; Accept user input


mov rax, 0 ; sys_read
mov rdi, 0 ; stdin
mov rsi, array ; buffer
mov rdx, 255 ; max length
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

; Process lower nibble


mov al, bl
and al, 0Fh ; Mask lower nibble
cmp al, 9h
ja add37h2
add al, 30h ; Convert to ASCII (0-9)
jmp skip2

add37h2:
add al, 37h ; Convert to ASCII (A-F)

skip2:
mov [rdi], al ; Store second digit

; Display the length stored in lengthinascii


mov rax, 1
mov rdi, 1
mov rsi, lengthinascii
mov rdx, 2
syscall

; 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

m1 db'Maximum number is:',10


lengthm1 equ $-m1
array db 11h,90h,34h,56h,23h

section .bss
maxnumascii resb 2 ; two digits of maximum number after converted to ASCII
; will be stored here
section .text

global _start
_start:

mov rsi,array ; point rsi to array


mov cx,5 ; counter for 5 numbers
mov al,byte[rsi] ; take first number from array in al and assume it as max

next:cmp al,byte[rsi] ; compare it with each number in array


Ja nochange ; if al > number in array, keep al as max
mov al,byte[rsi] ; else set that number as max & store in al

nochange:inc rsi ; take next number in array


dec cx ; check if all 5 numbers are compared
jnz next
mov bl,al ; max number is in al, save it in bl
mov rax,1 ; display message ‘maximum number is:’
mov rdi,1
mov rsi,m1
mov rdx,lengthm1
syscall

mov rsi,maxnumascii ; point rsi to maxnumascii tosave digits of max after


; converting to ASCII
mov al,bl ; take max number which was saved in bl
rol al,4 ; take its first digit to lower nibble
and al,0fh ; mask upper nibble
add al,30h ; convert to ascii by adding 30h
mov byte[rsi],al ; & save it

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

; Display "Total positive numbers:"


mov rax, 1
mov rdi, 1
mov rsi, m1
mov rdx, m1length
syscall

; Convert positive count to ASCII


movzx rax, byte [positive] ; Load positive count
add al, '0' ; Convert to ASCII
mov [posinascii], al ; Store ASCII value

; Display positive count


mov rax, 1
mov rdi, 1
mov rsi, posinascii
mov rdx, 1 ; Display 1 character
syscall

; Display "Total negative numbers:"


mov rax, 1
mov rdi, 1
mov rsi, m2
mov rdx, m2length
syscall

; Convert negative count to ASCII


movzx rax, byte [negative]
add al, '0'
mov [neginascii], al

; Display negative count


mov rax, 1
mov rdi, 1
mov rsi, neginascii
mov rdx, 1
syscall

; 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

pmodemsg db 10, 'Processor is in Protected Mode', 0


pmsg_len equ $ - pmodemsg

gdtmsg db 10, 'GDT Contents are:', 0


gmsg_len equ $ - gdtmsg

ldtmsg db 10, 'LDT Contents are:', 0


lmsg_len equ $ - ldtmsg

idtmsg db 10, 'IDT Contents are:', 0


imsg_len equ $ - idtmsg

trmsg db 10, 'Task Register Contents are:', 0


tmsg_len equ $ - trmsg

mswmsg db 10, 'Machine Status Word:', 0


mmsg_len equ $ - mswmsg

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

bt eax, 0 ; Check PE bit (Protection Enable)


jc protected_mode ; If set, jump to protected mode
print rmodemsg, rmsg_len ; Print real mode message
jmp exit_program

protected_mode:
print pmodemsg, pmsg_len ; Print protected mode message

sgdt [gdt] ; Store GDTR


sldt [ldt] ; Store LDTR
sidt [idt] ; Store IDTR
str [tr] ; Store TR

; Display GDT contents


print gdtmsg, gmsg_len
mov bx, [gdt+4]
call print_num
mov bx, [gdt+2]
call print_num
mov bx, [gdt]
call print_num
print newline, 1

; Display LDT contents


print ldtmsg, lmsg_len
mov bx, [ldt]
call print_num
print newline, 1

; Display IDT contents


print idtmsg, imsg_len
mov bx, [idt+4]
call print_num
mov bx, [idt+2]
call print_num
mov bx, [idt]
call print_num
print newline, 1

; Display Task Register contents


print trmsg, tmsg_len
mov bx, [tr]
call print_num
print newline, 1

; Display Machine Status Word


print mswmsg, mmsg_len
mov bx, [cr0_data+2]
call print_num
mov bx, [cr0_data]
call print_num
print newline, 1

exit_program:
mov rax, 60 ; syscall: exit
xor rdi, rdi ; status 0
syscall

; Subroutine to print numbers in hexadecimal


print_num:
mov rsi, num_buff ; Point RSI to buffer
mov rcx, 4 ; Number of nibbles to process (16 bits)

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

print num_buff, 4 ; Print the 4 characters


ret ; Return to calling program

;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

m1 db 'before transfer',10 ; Message before data transfer


m1length equ $-m1 ; Length of m1 message
m2 db 'after transfer',10 ; Message after data transfer
m2length equ $-m2 ; Length of m2 message
m3 db 'source block',10 ; Label for source block
m3length equ $-m3 ; Length of m3 message
m4 db 'destination block',10 ; Label for destination block
m4length equ $-m4 ; Length of m4 message

sblock db 11h,22h,33h,44h,55h ; Source block: 5 bytes of data


dblock times 5 db 0 ; Destination block: 5 bytes initialized to 0

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:

; Print initial information and source/destination blocks before transfer


print m1, m1length ; Print "before transfer"
print m3, m3length ; Print "source block"
mov rbp,sblock ; Point RBP to the source block
call display_block ; Display the source block

print newline,newline_length ; Print newline for spacing

print m4, m4length ; Print "destination block"


mov rbp, dblock ; Point RBP to the destination block
call display_block ; Display the destination block

print newline,newline_length ; Print newline


; Copy data from source block to destination block
mov rsi, sblock ; RSI points to source block
mov rdi, dblock ; RDI points to destination block
mov cx, 5 ; Set counter to 5 elements
repeat1:
mov al, [rsi] ; Load byte from source into AL
mov [rdi],al ; Store byte into destination
inc rsi ; Move to next source byte
inc rdi ; Move to next destination byte
dec cx ; Decrement counter
jnz repeat1 ; Repeat until 5 bytes are copied

; Print the blocks after data transfer


print m2, m2length ; Print "after transfer"

print m3, m3length ; Print "source block"


mov rbp, sblock ; Point RBP to source block
call display_block ; Display source block

print newline,newline_length ; Print newline

print m4, m4length ; Print "destination block"


mov rbp, dblock ; Point RBP to destination block
call display_block ; Display destination block

print newline,newline_length ; Final newline

; Exit the program


mov rax,60 ; syscall number for exit
syscall ; invoke exit

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

mov al, [rbp] ; Load byte from block into AL


mov bl,al ; Save original byte in BL for later use

; Convert upper nibble to ASCII


rol al,4 ; Rotate upper nibble into lower nibble position
and al,0fh ; Mask to get lower 4 bits
add al,30h ; Convert to ASCII (0-9 range)
mov [rcx], al ; Store in ASCII buffer

; Convert lower nibble to ASCII


mov al,bl ; Restore original byte
and al,0fh ; Mask lower nibble
add al,30h ; Convert to ASCII
mov [rcx+1],al ; Store in ASCII buffer

print num_in_ascii, 2 ; Print two ASCII characters representing hex


print space,1 ; Print space between hex values

inc rbp ; Move to next byte in block


dec byte [counter] ; Decrement loop counter
jnz repeat2 ; Repeat until all 5 bytes are displayed

ret ; Return from subroutine

;OUTPUT :
Name : Kunal Nalwade
Roll no. : COSB72

ASSIGNMENT 7

:INPUT CODE :

section .data ; Data section to define variables and messages


before db 10,'Block contents before transfer:',10 ; Message displayed before transfer
beforelen equ $-before ; Length of 'before' message

after db 10,'Block contents after transfer:',10 ; Message displayed after transfer


afterlen equ $-after ; Length of 'after' message

srcblk db 10,'Source block:',10 ; Label for source block


srcblklen equ $-srcblk ; Length of source block label

dstblk db 10,'Destination block:',10 ; Label for destination block


dstblklen equ $-dstblk ; Length of destination block label

src db 10h,20h,30h,40h,50h ; Source block: bytes with hex values


dst db 10h,20h,30h,40h,50h ; Destination block: bytes with hex values

chmsg db 10,'Enter your choice: ',10 ; Message prompting user input


len equ $-chmsg ; Length of 'Enter your choice' message

menu db 10,'1: Without string transfer',10,'2: With string transfer',10


menulen equ $-menu ; Menu options for choosing transfer method

section .bss ; Uninitialized data section


dispbuff resb 3 ; Buffer for displaying hex values (2 digits + newline)
choice resb 2 ; Buffer to store user input (choice)

; Macros for I/O operations


%macro write 2 ; Macro to write output
mov rax, 1 ; syscall: write
mov rdi, 1 ; file descriptor: stdout
mov rsi, %1 ; message to display
mov rdx, %2 ; length of message
syscall
%endmacro

%macro read 2 ; Macro to read input


mov rax, 0 ; syscall: read
mov rdi, 0 ; file descriptor: stdin
mov rsi, %1 ; buffer to store input
mov rdx, %2 ; number of bytes to read
syscall
%endmacro

section .text ; Code section


global _start ; Entry point
_start:
; Display source and destination blocks before transfer
write srcblk, srcblklen ; Print "Source block:"
mov rsi, src ; Point to the source block
call dispblk_proc ; Display the source block content

write dstblk, dstblklen ; Print "Destination block:"


mov rsi, dst ; Point to the destination block
call dispblk_proc ; Display the destination block content

; Display menu and prompt


write menu, menulen ; Print the menu options
write chmsg, len ; Print "Enter your choice: "
read choice, 2 ; Read user input (1 or 2)

; Compare user input


cmp byte [choice], '1' ; If input is '1', jump to 'without' section
je without

cmp byte [choice], '2' ; If input is '2', jump to 'with' section


je with

jmp exit ; If invalid choice, exit the program

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

; Display results after transfer


write after, afterlen ; Print "Block contents after transfer"

write srcblk, srcblklen ; Print "Source block:"


mov rsi, src ; Point to the source block
call dispblk_proc ; Display the source block content

write dstblk, dstblklen ; Print "Destination block:"


mov rsi, dst ; Point to the destination block
call dispblk_proc ; Display the destination block content

jmp exit ; Exit the program

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

cld ; Clear direction flag (forward movement)


rep movsb ; Copy bytes using string instruction
; Display results after transfer
write after, afterlen ; Print "Block contents after transfer"

write srcblk, srcblklen ; Print "Source block:"


mov rsi, src ; Point to the source block
call dispblk_proc ; Display the source block content

write dstblk, dstblklen ; Print "Destination block:"


mov rsi, dst ; Point to the destination block
call dispblk_proc ; Display the destination block content

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

ret ; Return to caller

;---------------------------------------------------
; Procedure to format and display hex bytes
disp_proc:
mov rsi, dispbuff ; Point to display buffer

; High nibble (upper 4 bits)


mov al, bl ; Copy byte into AL
shr al, 4 ; Shift right to extract the high nibble
cmp al, 9 ; Check if nibble <= 9
jbe digit ; If <= 9, jump to digit label
add al, 7 ; If > 9, convert to A-F

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

; Low nibble (lower 4 bits)


mov al, bl ; Reload the byte
and al, 0x0F ; Mask to extract the low nibble
cmp al, 9 ; Check if nibble <= 9
jbe digit2 ; If <= 9, jump to digit2 label
add al, 7 ; If > 9, convert to A-F
digit2:
add al, '0' ; Convert to ASCII (0-9 or A-F)
mov [rsi], al ; Store the low nibble in buffer
inc rsi ; Move to the next buffer position

; Add newline character


mov byte [rsi], 10 ; Add newline (ASCII 10)
inc rsi ; Move to the next position

; Write the formatted hex value


write dispbuff, 3 ; Write 2-digit hex value + newline
ret ; Return to caller

;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

m1 db 10,'Enter two Digit NUmber::'


m1_len equ $-m1
m2 db 10,'Multipliaction of elements is::'
m2_len equ $-m2
choice db 10,13,'Enter your Choice:'
db 10,13,'1.Successive Adition'
db 10,13,'2.Ada and Shift method'
db 10,13,'3.Exit'
choice_len equ $-choice

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]

p11: shr bx,01


jnc p
add cx,ax

p: shl ax,01
dec dl
jnz p11

mov[result],rcx
dispmsg m2,m2_len

mov rbx,[result]
call display
ret
OUTPUT :-

You might also like