;for non-overlapping
section .bss
ans resb 2 ; Reserve 2 bytes for storing the ASCII representation of a single byte in hexadecimal
%macro print 2
mov rax, 1 ; Syscall number for sys_write
mov rdi, 1 ; File descriptor 1 (stdout)
mov rsi, %1 ; Address of the message to print
mov rdx, %2 ; Length of the message
syscall ; Invoke the syscall to print the message
%endmacro
%macro exit 0
mov rax, 60 ; Syscall number for sys_exit
mov rdi, 0 ; Exit status 0
syscall ; Invoke the syscall to exit the program
%endmacro
section .data
sblock db 07h, 64h, 10h, 20h, 30h ; Source block: array of 5 bytes initialized with values
dblock times 5 db 00h ; Destination block: array of 5 bytes initialized to 0
smsg db 0Ah, "Source block is:", 0Ah ; Message to display before showing source block contents
smsg_len equ $ - smsg ; Calculate the length of the smsg string
dmsg db 0Ah, "Destination block is:", 0Ah ; Message to display before showing destination block contents
dmsg_len equ $ - dmsg ; Calculate the length of the dmsg string
space db " " ; Single space character for formatting the output
section .text
global _start
_start:
print smsg, smsg_len ; Print the initial message for the source block
mov rsi, sblock ; Load the address of the source block into RSI
call block_display ; Call the block_display function to display the source block
print dmsg, dmsg_len ; Print the initial message for the destination block
mov rsi, dblock ; Load the address of the destination block into RSI
call block_display ; Call the block_display function to display the destination block
call block_transfer ; Call the block_transfer function to copy data from sblock to dblock
print smsg, smsg_len ; Print the message again for the source block after transfer
mov rsi, sblock ; Load the address of the source block into RSI
call block_display ; Display the source block again
print dmsg, dmsg_len ; Print the message again for the destination block after transfer
mov rsi, dblock ; Load the address of the destination block into RSI
call block_display ; Display the destination block again to show updated values
exit ; Exit the program
display :
mov rbx, 16 ; Set RBX to 16 for hexadecimal conversion
mov rcx, 2 ; We expect a maximum of 2 digits in the output
mov rsi, ans+1 ; Set RSI to point to the last byte of the ans buffer
back1:
mov rdx, 0 ; Clear RDX before division
div rbx ; Divide RAX by 16, quotient in RAX, remainder in RDX
cmp dl, 09h ; Compare the remainder with 9
jbe add30 ; If the remainder is <= 9, jump to add30
add dl, 07h ; If remainder > 9, adjust for ASCII letters 'A'-'F'
add30:
add dl, 30h ; Convert remainder to ASCII '0'-'9' or 'A'-'F'
mov [rsi], dl ; Store the ASCII character in the ans buffer
dec rsi ; Move to the previous position in the buffer
dec rcx ; Decrease the digit counter
jnz back1 ; If more digits need processing, repeat the loop
print ans, 2 ; Print the 2-character hexadecimal value
ret ; Return from the display subroutine
block_transfer:
mov rsi, sblock ; Load the address of the source block into RSI
mov rdi, dblock ; Load the address of the destination block into RDI
mov rcx, 5 ; Set RCX to 5, the number of bytes to copy
next:
mov al, [rsi] ; Load the current byte from the source block into AL
mov [rdi], al ; Store the byte into the destination block
inc rsi ; Move to the next byte in the source block
inc rdi ; Move to the next byte in the destination block
dec rcx ; Decrement the loop counter
jnz next ; If RCX is not zero, repeat the loop for the next byte
ret ; Return from the block_transfer function
block_display:
mov rbp, 5 ; Set RBP to 5, the number of bytes to display
next1:
mov al, [rsi] ; Load the current byte into AL
push rsi ; Save the current position in RSI on the stack
call display ; Call the display subroutine to print the byte in hexadecimal
print space, 1 ; Print a space for formatting
pop rsi ; Restore the previous position of RSI from the stack
inc rsi ; Move to the next byte in the block
dec rbp ; Decrement the loop counter
jnz next1 ; If RBP is not zero, repeat the loop for the next byte
ret ; Return from the block_display function
To embed this project on your website, copy the following code and paste it into your website's HTML: