converting decimal to hex

singage · updated May 08, 2022
%macro print 2  
      mov rax, 1
    mov rdi, 1
    mov rsi,%1
    mov rdx, %2
    syscall
%endmacro
 
%macro exit 0 
mov rax, 60
    mov rdi, 0
    syscall
%endmacro
section .bss  ;here we reserve the size
char_ans resb 2 

section .text
global _start 
_start:   ;we are gona write procedure to convert to hexadecimal no
  mov rax,20
  call display
  exit
display:    ; name of the procedure

mov rbx,16 ;we gonna divide by 16 so we will assign one register 16
mov rcx,2  ;how many digits we want our no. shud be ..ie..produce number in 2 
mov rsi,char_ans + 1;pointing to the last location of my whatever number im going to produce
           ;char_ans is a variable in which we r going to store the converted number 
           
;now we will use division so  we will make sure it is present in AX register  
;division takes two operand AX and BX ..and diviser we already taken as 16
;division store its output into diff register 
;RDX->stores remainder and RAX->stores quotient
back: ;becz we want to repeat 
mov rdx,0 ;to avoid any garbage value
div rbx  ;divison occured ,RDX->stores remainder and RAX->stores quotient
         ;now remainder is present into dx..which is 1 digit,so we can work on the lower part
         ;of DX...that is DL...
cmp dl,09h;we will compare with 09h because
         ;hexadecimal consist of 2 rangeees (0-9) and (A-F)
         ;we compare because the value could not be passed to the output console as it 
         ;it has to be passed in ASCII format .

;suppose it is not between 0-9
jbe add30      ;jump below equal...if it is below 9 or equal to 9  then jumb below to the lebel anad add 30h
add dl,07h
add30: ;we wre taking one label if it is between 0-9 and add 30h
add dl,30h


;now we will movw to the adress stored by rsi
mov[rsi],dl
;now we will decrement rsi
dec rsi
dec rcx
jnz back  ;jump to back level if not zer0


print char_ans ,2
ret
   
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
Output

Comments

Please sign up or log in to contribute to the discussion.