0% found this document useful (0 votes)
8 views8 pages

MP 6

The document is an assembly language program that provides a menu for converting between hexadecimal and BCD (Binary-Coded Decimal) formats. It includes procedures for accepting user input, performing conversions, and displaying results. The program allows users to input a 4-digit hex number to get its BCD equivalent or a 5-digit BCD number to get its hex equivalent, with options to exit the program.

Uploaded by

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

MP 6

The document is an assembly language program that provides a menu for converting between hexadecimal and BCD (Binary-Coded Decimal) formats. It includes procedures for accepting user input, performing conversions, and displaying results. The program allows users to input a 4-digit hex number to get its BCD equivalent or a 5-digit BCD number to get its hex equivalent, with options to exit the program.

Uploaded by

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

Assignment 6

Name: Bhise Gayatri Rajendra Roll:308

Program

section .data

msg1 db 10,10, '###### Menu for Code Conversion ######'

db 10, '1: Hex to BCD'

db 10, '2: BCD to Hex'

db 10, '3: Exit'

db 10,10, 'Enter Choice:'

msg1length equ $-msg1

msg2 db 10,10, 'Enter 4 digit hex number::'

msg2length equ $-msg2

msg3 db 10,10, 'BCD Equivalent:'

msg3length equ $-msg3

msg4 db 10,10, 'Enter 5 digit BCD number::'

msg4length equ $-msg4

msg5 db 10,10, 'Wrong Choice Entered....Please try again!!!', 10,10

msg5length equ $-msg5

msg6 db 10,10, 'Hex Equivalent::'

msg6length equ $-msg6


cnt db 0 ; counter for BCD digits

section .bss

arr resb 6 ; Common buffer for choice, hex and BCD input

dispbuff resb 8 ; Buffer for displaying results

ans resb 1 ; Store single digit for display

%macro disp 2

mov rax, 01

mov rdi, 01

mov rsi, %1

mov rdx, %2

syscall

%endmacro

%macro accept 2

mov rax, 0

mov rdi, 0

mov rsi, %1

mov rdx, %2

syscall

%endmacro

section .text
global _start

_start:

menu:

disp msg1, msg1length

accept arr, 2 ; Accept choice (1, 2, or 3)

cmp byte [arr], '1'

jne l1

call hex2bcd_proc

jmp menu

l1:

cmp byte [arr], '2'

jne l2

call bcd2hex_proc

jmp menu

l2:

cmp byte [arr], '3'

je exit

disp msg5, msg5length

jmp menu

exit:
mov rax, 60

mov rbx, 0

syscall

hex2bcd_proc:

disp msg2, msg2length

accept arr, 5 ; 4 digits + enter

call conversion ; Convert Hex to BCD

mov rcx, 0

mov ax, bx

mov bx, 10 ; Base 10 for Decimal system

; Divide the hex value by 10 to get BCD representation

l33:

mov dx, 0

div bx ; Divide the number by 10

push rdx ; Push the remainder onto stack

inc rcx

inc byte [cnt] ; Increment digit counter

cmp ax, 0

jne l33

disp msg3, msg3length

l44:

pop rdx ; Pop the remainder from stack

add dl, 30h ; Convert to ASCII

mov [ans], dl
disp ans, 1 ; Display the digit

dec byte [cnt]

jnz l44

ret

bcd2hex_proc:

disp msg4, msg4length

accept arr, 6 ; 5 digits + enter

disp msg6, msg6length

mov rsi, arr

mov rcx, 5

mov rax, 0

mov ebx, 0ah ; Base 10 for BCD

l55:

mov rdx, 0

mul ebx ; ebx * eax = edx:eax

mov dl, [rsi]

sub dl, 30h ; Convert ASCII to number (0-9)

add rax, rdx

inc rsi

dec rcx

jnz l55
mov ebx, eax ; Store the result in ebx

call disp32_num ; Display the result

ret

conversion:

mov bx, 0

mov ecx, 4 ; 4 digits in hex input

mov esi, arr

up1:

rol bx, 4 ; Rotate left by 4 bits (1 hex digit per iteration)

mov al, [esi]

cmp al, 39h

jbe l22

sub al, 07h

l22:

sub al, 30h ; Convert ASCII to integer (0-9 or A-F)

add bl, al

inc esi

loop up1

ret

; Procedure to display a 32-bit number (result of BCD to Hex conversion)

disp32_num:

mov rdi, dispbuff


mov rcx, 8 ; Number of digits (since we are dealing with a 32-bit number)

l77:

rol ebx, 4 ; Rotate left by 4 bits

mov dl, bl

and dl, 0fh

add dl, 30h ; Convert to ASCII

cmp dl, 39h

jbe l66

add dl, 7 ; Adjust for 'A'-'F'

l66:

mov [rdi], dl

inc rdi

dec rcx

jnz l77

disp dispbuff + 3, 5 ; Display only lower 5 digits (upper 3 are zeros)

ret

output:

###### Menu for Code Conversion ######

1: Hex to BCD

2: BCD to Hex

3: Exit

Enter Choice:1
Enter 4 digit hex number::2345

BCD Equivalent:9029

###### Menu for Code Conversion ######

1: Hex to BCD

2: BCD to Hex

3: Exit

Enter Choice:2

Enter 5 digit BCD number::02345

Hex Equivalent::00929

[Execution complete with exit code 0]

You might also like