Microprocessor/Codes/Practical-3.1 (with string).asm

95 lines
3.5 KiB
NASM
Raw Permalink Normal View History

2024-06-10 11:10:30 +05:30
; THIS CODE HAS BEEN TESTED AND IS FULLY OPERATIONAL.
; Problem Statement: Write X86/64 ALP to perform non-overlapped block transfer (WITH string specific instructions). Block containing data can be defined in the data segment.
; Code from Microprocessor (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/Microprocessor/
; BEGINNING OF CODE
%macro print 2
mov rax,1 ; Standard output
mov rdi,1 ; Input write
mov rsi,%1 ; Display message address
mov rdx,%2 ; Message length
syscall ; Interrupt for kernel in 64-bit
%endmacro
section .data
msg db 10, 'Block contents before transfer',10,13 ; Initializing the display message
msglen equ $-msg ; Length of the message
msg2 db 10, 'Block contents after transfer',10,13 ; Initializing the display message
msg2len equ $-msg2 ; Length of the message
msg3 db 10,13, 'Source block'
msg3len equ $-msg3
msg4 db 10,13, 'Destination block'
msg4len equ $-msg4
space db ' ' ; Space between the variables to be displayed
spacelen equ $-space ; Initializing length of space
srcblk db 10h,20h,30h,40h,50h ; Contents of the src block
destblk db 0,0,0,0,0 ; Initial contents of dest block
cnt equ 5 ; Count is equal to 5 as 5 variables declared
section .bss ; Storing the array of data/reserving space for the data
ans resb 4 ; Reserve buffer ans of 4-bytes(8-bits each)
;destblk resb 5 ; Reserve buffer destblk of 5-bytes
section .text
global _start ; Starting of the main program
_start:
print msg,msglen ; Displaying the msg1
mov rsi,srcblk ; Move contents of source blk to rsi
call disp_block ; Calling procedure disp_block
cld ; Clear direction flag
mov rcx,05h ; Move 05h into rcx register
mov rsi,srcblk ; Move contents of src block into rsi
mov rdi,destblk ; Move contents of dest blok into rdi
rep movsb ; Repeat move string byte
print msg2,msg2len ; Displaying the msg2
mov rsi,destblk ; Move contents of destblk into rsi
call disp_block ; Calling procedure disp_block
mov rax,60 ; Exit system call
xor rdi,rdi ; Clearing rdi (by using xor) so that we get 0 in destblk
syscall ; Interrupt for kernel in 64-bit
disp_block: ; Procedure disp_block
mov rbp,cnt ; Base pointers
back:
mov al,[rsi] ; Move contents of rsi to al
push rsi ; Push contents of rsi
call disp_8 ; Calling the disp_8 procedure
print space,1 ; Intialize one byte for space
pop rsi ; Pop contents of rsi
inc rsi ; Increment rsi by 1
dec rbp ; Decrement cnt through rbp
jnz back ; Jump to loop back if not zero
ret ; Return
disp_8: ; Procedure disp_8
mov rsi,ans+1 ; Move the contents of ans buffer into rsi
mov rcx,2 ; Move 2 into the rcx register
back1: ; Loop back1
mov rdx,0 ; Clear rdx
mov rbx,16 ; Move 16 into rbx register
div rbx ; Divide rax register by rbx register
cmp dl,09h ; Compare the contents of dl register with 09h
jbe add_30 ; If compared value is below (less) than add 30h
add dl ,07h ; Add 07h to dl register
add_30: ; add_30 label
add dl,30h ; Add 30h to the contents of dl register
mov[rsi],dl ; Move contents of dl into rsi
dec rsi ; Decrement rsi
dec rcx ; Decrement rcx
jnz back1 ; Jump if not zero to loop back1
print ans,2 ; Print the result
ret ; Return
; END OF CODE