section .data
msg db "welcome to code converter",10
len equ $ -msg
msg1 db "1.enter your choice",10,"1.hexa to bcd",10,"2.bcd to hex",10,"3.exit",10
len1 equ $ -msg1
msg2 db "enter 16 hex no",10
len2 equ $ -msg2
msg3 db "enter bcd to convert in hex no ",10
len3 equ $ -msg4
msg4 db "hex no for given bcd no is:",10
len4 equ $ -msg5
msg6 db "bcd to given hex no is",10
len6 equ $ -msg6
msg5 db "thank you",10
len5 equ $ -msg6
section .bss
n resb 2
hex resb 5
bcd resb 6
%macro disp 2
mov rax,01
mov rdi,01
mov rsi,%1
mov rdx,%2
syscall
%endmacro
%macro inn 2
mov rax,00
mov rdi,00
mov rsi,%1
mov rdx,%2
syscall
%endmacro
section .text
global _start
_start:
disp msg,len
again:
disp msg1,len1
inn n,02
cmp byte[n],31h ;choice for option 1,2 and 3 comapring with choice 1
jne BCD ;if not equal to 1 then go to BCD
disp msg2,len2
inn hex,05 ;Taking HEX input of 4 digit and one for enter FFFF
xor rax,rax
mov rsi,hex ;rsi is pointing to Hex input
mov cl,04 ;counter for left shift size 4
mov ch,04 ;Counter for no of digits 4
up:
cmp byte[rsi],39h ;compare first digit of hex input with 39h with 46H(F)
jng skip1 ;jump not grater to skip1 if number is smaller then 9(46>39)
sub byte[rsi],07h ;if number is greater then 39h then subtract from (46h-7h=3F)
skip1: ;
sub byte[rsi],30h ;subtract (3f-30h=F)
shl rax,cl ;shift left ax register by 4 bits it will be zero only
add al,[rsi] ;now F will be added to lower bits of AX
inc rsi ;now rsi will point to second F of FFFF
dec ch ;ch will be 3 now
jnz up ;jump not zero to up until all digit of FFFF are added in RAX
xor ebx,ebx ;make EBX zero
mov bl,0Ah ;Bl register holds 0Ah,0A is hex equvivalent of 16 decimal number
xor edx,edx ;make EDX empty
mov rsi,bcd ;RSI will point to the resultant
add rsi,04 ;rsi will point to the end of rsi (5th place of bcd array)
up1:
div ebx ;RAX=FFFF and EBX=0Ah after division quotient will be in EAX and remainder will be in ;EDX ,after first iteration EAX= 1999 and EDX= 5
mov [rsi],dl ;move dl to BCD where RSI is pointing
xor edx,edx
dec rsi ;RSI is pointing to previous locatioin
cmp eax,00 ;until quotient became zero ,quotient became zero in 5th iteration
jne up1
;l1st iteration FFFF/0A= 1999 and [FFFF-FFFA] =5 remainder -------EAX=1999 & EDX=5
;Second iteration 1999/A=28F and [1999-1996]=3 remainder ---------EAX=28F & EDX=3
;third iteration 28F/A=41H and [28F-28A]=5 remainder-----------EAX=41H & EDX=5
;Fourth iteration 41H/AH=06 and [41-3C]=5 remainder-------------EAX=06H & EDX=5
;Fifth iteration 06/A=0 and [6-0]=6 remainder -------------EAX=0H & EDX=6
mov rsi,bcd
mov cl,05
up2: ;this loop is converting Decimal to ASCII
add byte[rsi],30h ;First number of [rsi] is 6 so 6+30h=36H this loop conver all decimal to ascii
inc rsi
dec cl
jnz up2
disp msg6,len6
disp bcd,05 ;display output BCD number 65535
jmp again
BCD:
cmp byte[n],32h ;if choice is 2 then BCD to HEX conversion
jne exit
disp msg2,len2
inn bcd,06 ;if BCD=65535 is input number
xor rax,rax ;rax is empty
mov rsi,bcd ;rsi is pointing to BCD number 65535
mov ch,05
uphex:
sub byte[rsi],30h ;Number is stored in ascii so this loop is converting from ascii to decimal
inc rsi
dec ch
jnz uphex
mov cl,05h ;INITIALIZE COUNTER TO 5 since input is 5 digit
mov rsi,bcd ;rsi is pointing to BCD
j1:
add dx,0Ah ;add multiplicant 0Ah=16 decimal in DX register
mul dx ;MULTIPY ax*dx
xor bx,bx
mov bl,[rsi]
add al,bl ;l1st iteration 0*Oah=0 EAX=0 & EDX=0a
inc rsi ;Second iteration 6*0aH=3c EAX=3C EDX=0a
;tHIRD iteration 3c+5=41H 41H*0a=28A EAX=28a EDX=0a
;FOURTH ITERATION 28A+5=28f 28F*0A=1996 eax=1996 edx=0A
;fIFTH ITERATION 1996+3=1999 1999*A=FFFA EAX=FFFA EDX=0a
;sixth iteration FFFA+5=FFFF cl=0 EAX=FFFF
dec cl ;at the end cl become zero
jnz j1
mov rsi,hex ;Hex is blank currently
add rsi,03 ;rsi points to end of hex
mov ch,04 ;ch is 4 for no of digits
mov cl,04 ;cl is 04 for shifting
as:
mov bl,al ;Move the FF to BL register from AX
and bl,0fh ;AND FF with 0F so result is 0F
cmp bl,09h ;compare 0F>09
jng skip6 ;jump if not greater
add bl,07h ;add 0F+7=16H
skip6:
add bl,30h ;add 16+30h=46H(It is ascii value of F)
mov [rsi],bl
shr rax,cl
dec rsi
dec ch
jnz as
disp msg4,len4
disp hex,04
jmp again
exit:
cmp byte[n],33h
je sn
sn:
disp msg5,len5
mov rax,3ch
mov rsi,00
syscall
msg db "welcome to code converter",10
len equ $ -msg
msg1 db "1.enter your choice",10,"1.hexa to bcd",10,"2.bcd to hex",10,"3.exit",10
len1 equ $ -msg1
msg2 db "enter 16 hex no",10
len2 equ $ -msg2
msg3 db "enter bcd to convert in hex no ",10
len3 equ $ -msg4
msg4 db "hex no for given bcd no is:",10
len4 equ $ -msg5
msg6 db "bcd to given hex no is",10
len6 equ $ -msg6
msg5 db "thank you",10
len5 equ $ -msg6
section .bss
n resb 2
hex resb 5
bcd resb 6
%macro disp 2
mov rax,01
mov rdi,01
mov rsi,%1
mov rdx,%2
syscall
%endmacro
%macro inn 2
mov rax,00
mov rdi,00
mov rsi,%1
mov rdx,%2
syscall
%endmacro
section .text
global _start
_start:
disp msg,len
again:
disp msg1,len1
inn n,02
cmp byte[n],31h ;choice for option 1,2 and 3 comapring with choice 1
jne BCD ;if not equal to 1 then go to BCD
disp msg2,len2
inn hex,05 ;Taking HEX input of 4 digit and one for enter FFFF
xor rax,rax
mov rsi,hex ;rsi is pointing to Hex input
mov cl,04 ;counter for left shift size 4
mov ch,04 ;Counter for no of digits 4
up:
cmp byte[rsi],39h ;compare first digit of hex input with 39h with 46H(F)
jng skip1 ;jump not grater to skip1 if number is smaller then 9(46>39)
sub byte[rsi],07h ;if number is greater then 39h then subtract from (46h-7h=3F)
skip1: ;
sub byte[rsi],30h ;subtract (3f-30h=F)
shl rax,cl ;shift left ax register by 4 bits it will be zero only
add al,[rsi] ;now F will be added to lower bits of AX
inc rsi ;now rsi will point to second F of FFFF
dec ch ;ch will be 3 now
jnz up ;jump not zero to up until all digit of FFFF are added in RAX
xor ebx,ebx ;make EBX zero
mov bl,0Ah ;Bl register holds 0Ah,0A is hex equvivalent of 16 decimal number
xor edx,edx ;make EDX empty
mov rsi,bcd ;RSI will point to the resultant
add rsi,04 ;rsi will point to the end of rsi (5th place of bcd array)
up1:
div ebx ;RAX=FFFF and EBX=0Ah after division quotient will be in EAX and remainder will be in ;EDX ,after first iteration EAX= 1999 and EDX= 5
mov [rsi],dl ;move dl to BCD where RSI is pointing
xor edx,edx
dec rsi ;RSI is pointing to previous locatioin
cmp eax,00 ;until quotient became zero ,quotient became zero in 5th iteration
jne up1
;l1st iteration FFFF/0A= 1999 and [FFFF-FFFA] =5 remainder -------EAX=1999 & EDX=5
;Second iteration 1999/A=28F and [1999-1996]=3 remainder ---------EAX=28F & EDX=3
;third iteration 28F/A=41H and [28F-28A]=5 remainder-----------EAX=41H & EDX=5
;Fourth iteration 41H/AH=06 and [41-3C]=5 remainder-------------EAX=06H & EDX=5
;Fifth iteration 06/A=0 and [6-0]=6 remainder -------------EAX=0H & EDX=6
mov rsi,bcd
mov cl,05
up2: ;this loop is converting Decimal to ASCII
add byte[rsi],30h ;First number of [rsi] is 6 so 6+30h=36H this loop conver all decimal to ascii
inc rsi
dec cl
jnz up2
disp msg6,len6
disp bcd,05 ;display output BCD number 65535
jmp again
BCD:
cmp byte[n],32h ;if choice is 2 then BCD to HEX conversion
jne exit
disp msg2,len2
inn bcd,06 ;if BCD=65535 is input number
xor rax,rax ;rax is empty
mov rsi,bcd ;rsi is pointing to BCD number 65535
mov ch,05
uphex:
sub byte[rsi],30h ;Number is stored in ascii so this loop is converting from ascii to decimal
inc rsi
dec ch
jnz uphex
mov cl,05h ;INITIALIZE COUNTER TO 5 since input is 5 digit
mov rsi,bcd ;rsi is pointing to BCD
j1:
add dx,0Ah ;add multiplicant 0Ah=16 decimal in DX register
mul dx ;MULTIPY ax*dx
xor bx,bx
mov bl,[rsi]
add al,bl ;l1st iteration 0*Oah=0 EAX=0 & EDX=0a
inc rsi ;Second iteration 6*0aH=3c EAX=3C EDX=0a
;tHIRD iteration 3c+5=41H 41H*0a=28A EAX=28a EDX=0a
;FOURTH ITERATION 28A+5=28f 28F*0A=1996 eax=1996 edx=0A
;fIFTH ITERATION 1996+3=1999 1999*A=FFFA EAX=FFFA EDX=0a
;sixth iteration FFFA+5=FFFF cl=0 EAX=FFFF
dec cl ;at the end cl become zero
jnz j1
mov rsi,hex ;Hex is blank currently
add rsi,03 ;rsi points to end of hex
mov ch,04 ;ch is 4 for no of digits
mov cl,04 ;cl is 04 for shifting
as:
mov bl,al ;Move the FF to BL register from AX
and bl,0fh ;AND FF with 0F so result is 0F
cmp bl,09h ;compare 0F>09
jng skip6 ;jump if not greater
add bl,07h ;add 0F+7=16H
skip6:
add bl,30h ;add 16+30h=46H(It is ascii value of F)
mov [rsi],bl
shr rax,cl
dec rsi
dec ch
jnz as
disp msg4,len4
disp hex,04
jmp again
exit:
cmp byte[n],33h
je sn
sn:
disp msg5,len5
mov rax,3ch
mov rsi,00
syscall
No comments:
Post a Comment