Monday, September 8, 2014

Hex to BCD BCD to Hex Conversion NASM

%macro print 2
mov rax,1
mov rdi,1
mov rsi,%1
mov rdx,%2
syscall
%endmacro

%macro read 2
mov rax,0
mov rdi,0
mov rsi,%1
mov rdx,%2
syscall
%endmacro

SECTION .data
msg: db"Enter the four digit hex number::",0xA
len: equ $-msg

msg1: db"Enter the five digit BCD number::",0xA
len1: equ $-msg1

msg2: db"BCD number of given hex number::"
len2: equ $-msg2

msg3: db"HEX number of given BCD number::"
len3: equ $-msg3

menu: db 0xA,"1.HEX TO BCD",0xA,"2.BCD TO HEX",0xA,"3.EXIT",0xA
lmenu: equ $-menu

SECTION .bss
choice: resb 2
hexNum: resb 10
bcdNum:resb 10
count: resb 1
result: resb 6
result1: resb 5

SECTION .text

global _start:
_start:

menuprint:
    print menu,lmenu
    read choice,2

cmp byte[choice],31h
je HextoBCD
cmp byte[choice],32h
je BCDtoHex
cmp byte[choice],33h
je exit

HextoBCD:
    ;accepting the number
    print msg,len
    read hexNum,10
        ;converting the ascii to hex
        convert:
                mov esi,hexNum
                mov bx,0
                mov ax,0
                mov ecx,4
                loop1:
                    rol bx,04h
                    mov al,[rsi]
                    cmp al,39h
                    jbe loop2
                    sub al,07h
                loop2:
                    sub al,30h
                    add bx,ax
                    inc rsi
        loop loop1   
mov ax,bx
mov edx,0 ; mov 00 in edx
mov ecx,10 ; mov counter as 10
mov byte[count],5
mov rsi,result ;pointing source as result

Div:
      div cx
    mov byte[rsi],dl
    add rsi,1
    dec byte[count]
    mov dl,0
jnz Div

sub rsi,1
mov byte[count],5
mov rdi,result1

Transfer:
        mov al,byte[rsi]
        mov byte[rdi],al
        dec rsi
        inc rdi
        dec byte[count]
jnz Transfer

mov byte[count],5
mov rsi,result1

convertResult:
        add byte[rsi],30h
        inc rsi
        dec byte[count]
jnz convertResult

print msg2,len2
print result1,5
jmp menuprint
   
BCDtoHex:
print msg1,len1
read bcdNum,10

convert_bcd:
        mov ecx,5
        mov ebx,0
        mov eax,0
        mov rsi,bcdNum
        loop31:
            rol ebx,04h
            mov al,byte[rsi]
            cmp al,39h
            jbe loop41
            sub al,07h
        loop41:
            sub al,30h
            add ebx,eax
            inc esi
        loop loop31


mov eax,0h

mov [result],ebx
mov esi,result

add esi,2 ;addin two mean?
mov dl,byte[esi]

mov byte[count],dl
cmp byte[count],0

jz loop4

loop5:
    add ax,10000
    dec byte[count]
jnz loop5

loop4:
    dec esi
    mov dl,byte[esi]
    rol dl,04h
    and dl,0Fh
    mov byte[count],dl
    cmp byte[count],0
jz loop6
loop7:
    add ax,1000
    dec byte[count]
jnz loop7

loop6:
    mov dl,byte[rsi]
    and dl,0Fh
    mov byte[count],dl
    cmp byte[count],0
jz loop8
loop9:
    add ax,100
    dec byte[count]
jnz loop9

loop8:
    mov esi,result
    mov dl,byte[esi]
    rol dl,04h
    and dl,0Fh
    mov byte[count],dl
    cmp byte[count],0
jz loop10

loop11:
    add ax,10
    dec byte[count]
jnz loop11

loop10:
    mov dl,byte[esi]
    and dl,0Fh
    mov byte[count],dl
    cmp byte[count],0
jz loop12
loop13:
    add ax,1
    dec byte[count]
jnz loop13
loop12:
    mov dx,ax

convert_result_to_ascii:
            mov ax,0
            mov ecx,4
            mov esi,result1
        loop14:
            rol dx,04
            mov al,dl
            and al,0Fh
            cmp al,09h
            jbe loop15
            add al,07h
        loop15:
            add al,30h
            mov [esi],al
            inc rsi
           
            loop loop14
print msg3,len3
print result1,4
jmp menuprint

exit:
mov rax,60
mov rdi,0
syscall


   

No comments:

Post a Comment

Home Java Python PHP