Microprocessor/Codes/Practical-4.1 (with string).asm
2024-06-10 11:23:57 +05:30

86 lines
3.2 KiB
NASM

; THIS CODE HAS BEEN TESTED AND IS FULLY OPERATIONAL.
; Problem Statement: Write X86/64 ALP to perform 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
mov rdi,1
mov rsi,%1
mov rdx,%2
syscall
%endmacro
section .data
msg1 db 10,"src blk",10,13 ; Initializing the display message
msg1len equ $-msg1 ; Initializing the length of message
msg2 db 10,"dest blk",10,13
msg2len equ $-msg2
srcblk db 10h,20h,30h,40h,50h ; Storing variables in source block
cnt equ 05h ; Count is equal to 5 as 5 variables declared
space db " " ; Space between the variables to be displayed
spacelen equ $-space ; Initializing length of space
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 msg1,msg1len ; 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,02h ; Move 02h into rcx register
mov rsi,srcblk ; Move contents of the source blk to the rsi
mov rdi,destblk ; Shift 2 positions in the source blk and move the contents into rdi
rep movsb ; Repeat move string byte
mov rcx,03h ; Move 02h into rcx register
mov rsi,srcblk ; Move contents of the source blk to the rsi
mov rdi,destblk+2
rep movsb
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 ; Initialize 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
; END OF CODE