0% found this document useful (0 votes)
2 views

MP6

This document describes an X86/64 assembly language program that converts 4-digit hexadecimal numbers to their equivalent BCD representation and vice versa. The program is user-friendly, allowing users to choose between HEX to BCD, BCD to HEX, or exit the program, while providing prompts for input and displaying results. It includes sections for data, BSS, and text, with macros for printing and reading input.

Uploaded by

ximom71139
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

MP6

This document describes an X86/64 assembly language program that converts 4-digit hexadecimal numbers to their equivalent BCD representation and vice versa. The program is user-friendly, allowing users to choose between HEX to BCD, BCD to HEX, or exit the program, while providing prompts for input and displaying results. It includes sections for data, BSS, and text, with macros for printing and reading input.

Uploaded by

ximom71139
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Experiment 6

Program: Write an X86/64 ALP to convert 4-digit Hex number into its
equivalent BCD number and 5-digit BCD number into its equivalent HEX
number. Make your program user friendly to accept the choice from user for: (a)
HEX to BCD (b) BCD to HEX (c) EXIT. Display proper strings to prompt the
user while accepting the input and displaying the result. (Wherever necessary,
use 64-bit registers).

Section .data
msg: db 0x0A
len: equ $-msg

msg1: db "1. HEX to BCD",0x0A


db "2. BCD to HEX",0x0A
db "3. Exit",0x0A
db "Enter choice: "
len1: equ $-msg1

msg2: db "Enter 4-digit HEX number: "


len2: equ $-msg2

msg3: db "Equivalent BCD: "


len3: equ $-msg3

msg4: db "Enter 5-digit BCD number: "


len4: equ $-msg4

msg5: db "Equivalent HEX: "


len5: equ $-msg5

msg6: db "Invalid choice!",0x0A


len6: equ $-msg6

;****************************************************

Section .bss

chc: resb 0x01


temp: resb 0x06
data: resd 0x01
cnt_div: resb 0x01
result: resq 0x01
cnta: resb 0x01
cntd: resb 0x01
res: resb 0x01
cnt_mul: resb 0x01

;****************************************************
%macro print 2 ;macro for printing
mov rax,0x01
mov rdi,0x01
mov rsi,%1
mov rdx,%2
syscall
%endmacro

;****************************************************

%macro read 2 ;macro for reading


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

;****************************************************

Section .text
Global _start
_start:

menu:
print msg1,len1
read chc,0x02

cmp byte[chc],0x31
je hex

cmp byte[chc],0x32
je bcd

cmp byte[chc],0x33
je exit

print msg6,len6
jmp menu

hex:
print msg2,len2
read temp,0x06

mov rsi,temp
mov byte[cnta],0x04 ;4 digit Input
call ascii_hex

print msg3,len3
call hextobcd
print msg,len
jmp menu

;****************************************************

bcd:
print msg4,len4
read temp,0x06

mov rsi,temp
mov byte[cnta],0x05 ;5 digit input
call ascii_hex

print msg5,len5
call bcdtohex

print msg,len
jmp menu

;****************************************************

exit:
mov rax,0x3C
mov rdi,0x00
syscall

;****************************************************

hextobcd:
xor eax,eax
xor ecx,ecx

mov ax,word[data]
mov byte[cnt_div],0x05

loop1:
xor edx,edx
mov bx,0x0A
div bx ;Result in ax Remainder in dx

ror ecx,0x04
or cx,dx

dec byte[cnt_div]
jnz loop1

rol ecx,0x10 ;Result in proper form

mov dword[result+0x04],ecx
mov byte[cntd],0x08
call disp
ret
;****************************************************

bcdtohex:
xor eax,eax
xor rcx,rcx
xor rbx,rbx

mov byte[cnt_mul],0x05

mov r8,[data]
ror r8,0x10 ;Get MSB at lowest nibble

back2:
mov bx,0x0A
mul bx

mov rcx,r8
and rcx,0xF ;Seperate req digit

add eax,ecx

rol r8,0x04

dec byte[cnt_mul]
jnz back2

mov dword[result+0x04],eax
mov byte[cntd],0x08
call disp
ret

;****************************************************

ascii_hex:
xor ebx,ebx
xor eax,eax

digit2:
mov bl,byte[rsi]

cmp bl,0x39
jbe digit1
sub bl,0x07

digit1:
sub bl,0x30

sal eax,0x04
add al,bl

inc rsi
dec byte[cnta]
jnz digit2

mov dword[data],eax
ret

;****************************************************

disp:
xor rbx,rbx

back:
rol qword[result],0x04
mov bl,byte[result]
and bl,0FH

cmp bl,09H
jbe next
add bl,0x07

next:
add bl,0x30

mov byte[res],bl
print res,1

dec byte[cntd]
jnz back

ret

OUTPUT:

(base) kj-comp@kjcomp-OptiPlex-380:~$ nasm -f elf64 mp6_new.asm


(base) kj-comp@kjcomp-OptiPlex-380:~$ ld -o A mp6_new.o
(base) kj-comp@kjcomp-OptiPlex-380:~$ ./A
1. HEX to BCD
2. BCD to HEX
3. Exit
Enter choice: 1
Enter 4-digit HEX number: FFF0
Equivalent BCD: 00065520

1. HEX to BCD
2. BCD to HEX
3. Exit
Enter choice: 2
Enter 5-digit BCD number: 65520
Equivalent HEX: 0000FFF0

1. HEX to BCD
2. BCD to HEX
3. Exit
Enter choice: 2
Enter 5-digit BCD number: 12345
Equivalent HEX: 00003039

1. HEX to BCD
2. BCD to HEX
3. Exit
Enter choice: 1
Enter 4-digit HEX number: 3039
Equivalent BCD: 00012345
1. HEX to BCD
2. BCD to HEX
3. Exit
Enter choice: 3
(base) kj-comp@kjcomp-OptiPlex-380:~$

You might also like