WORD HEXADECIMAL to ASCII conversion


section .data
message db "Stored value in array : "                     ;simple message declartion
length equ $-message                                           ; this statement stores value of length of message
nl db "",10
nll equ $-nl
reference_table : db "0123456789ABCDEF"         ;reference table is needed for XLAT instruction
array : dw 0xFACD,0x0E12,0x5432                      ;This is the array we wish to print

;=======================================================================
section .bss
converted_value resw 1 ;this will store the ASCII content of array
counter resb 1 ;this will store numbers to be converted
counter_word resb 1 ;to specify that we are converting a word
%macro PRINT 2 ;macro decalration for print vaule on screen
mov rax,1
mov rdi,1
mov rsi,%1
mov rdx,%2
syscall
%endmacro

;=======================================================================
section .text
global _start
_start:

PRINT message,length ;Print the message
mov byte[counter],3 ;initialize counter (number of words to print)
mov r8,array                                                  ;Give the source address by storing in RSI register
loop_number:
mov byte[counter_word],2 ;as word contains 2 bytes (for double word 4 bytes)

;this loop is for word conversion
loop_word:
rol word[r8],8 ;To take values from MSB to LSB
mov r9,converted_value                                ;Give the Destination address by storing in RDI register
CALL HEX_ASCII ;Call the conversion Routine
PRINT converted_value,2

dec byte[counter_word]
jnz loop_word

PRINT nl,nll
add r8,2 ;point to next word to convert
         
dec byte[counter]
jnz loop_number
;=======================================================================
;exit                                                        ;EXITING the program
mov rax,60
mov rbx,0
syscall

;=======================================================================
HEX_ASCII:
mov al,byte[r8] ;Get the byte content pointed by RSI
mov ah,al                                            ;Make another copy of same byte
shr al,4                                                ;Shift by 4 bits to right to get the MSB
mov ebx,reference_table ;This is necessary for XLAT to work
XLAT ;This will perform conversion
mov byte[r9],al ;Move the result(ASCII of MSB) in Destination
inc r9 ;Point to next byte of destination

mov al,ah                                            ;Retrive the copy of byte to be converted
and al,0Fh ;Mask the MSB (We consider only LSB nibble now)
mov ebx,reference_table ;This is necessary for XLAT to work
XLAT ;This will perform conversion
mov byte[r9],al ;Move the result(ASCII of LSB) in Destination

RET

;This is how we convert and PRINT stored values

No comments:

Post a Comment