0% found this document useful (0 votes)
87 views7 pages

MP Ass3

The document describes a program that: 1) Accepts a 4-digit hexadecimal number as input and converts it to its equivalent binary coded decimal (BCD) number. 2) Accepts a 5-digit BCD number as input and converts it to its equivalent hexadecimal number. 3) Displays menus to prompt the user to choose the conversion and enter/view numbers. The program uses 64-bit registers for processing and input/output.

Uploaded by

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

MP Ass3

The document describes a program that: 1) Accepts a 4-digit hexadecimal number as input and converts it to its equivalent binary coded decimal (BCD) number. 2) Accepts a 5-digit BCD number as input and converts it to its equivalent hexadecimal number. 3) Displays menus to prompt the user to choose the conversion and enter/view numbers. The program uses 64-bit registers for processing and input/output.

Uploaded by

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

Title:

Write X86/64 ALP to convert 4-digit Hex number into its equivalent BCD number and
5-digitBCD 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)

**** HEX to BCD *****

section .data

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


db 10,'1: Hex to BCD'
db 10,'3: Exit'
db 10,10,'Please Enter Choice::'
menumsg_len equ $-menumsg

hexinmsg db 10,10,'Please enter 4 digit hex number::'


hexinmsg_len equ $-hexinmsg

bcdopmsg db 10,10,'BCD Equivalent::'


bcdopmsg_len equ $-bcdopmsg

bcdinmsg db 10,10,'Please enter 5 digit BCD number::'


bcdinmsg_len equ $-bcdinmsg

hexopmsg db 10,10,'Hex Equivalent::'


hexopmsg_len equ $-hexopmsg

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

section .bss

numascii resb 06 ;common buffer for choice, hex and bcd input
outputbuff resb 02
dispbuff resb 08

%macro display 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: display menumsg,menumsg_len


accept numascii,2

cmp byte [numascii],'1'


je hex2bcd_proc

cmp byte [numascii],'3'


je exit
jmp _start

exit:
mov rax,60
mov rbx,0
syscall

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

hex2bcd_proc:
display hexinmsg,hexinmsg_len
accept numascii,5
call packnum
mov ax,bx
mov rcx,0
mov bx,10 ;Base of Decimal No. system
h2bup1: mov dx,0
div bx
push rdx
inc rcx
cmp ax,0
jne h2bup1
mov rdi,outputbuff

h2bup2: pop rdx


add dl,30h
mov [rdi],dl
inc rdi
loop h2bup2
display bcdopmsg,bcdopmsg_len
display outputbuff,5
jmp menu

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

packnum:
mov bx,0
mov ecx,04
mov esi,numascii
up1:
rol bx,04
mov al,[esi]
cmp al,39h
jbe skip1
sub al,07h
skip1: sub al,30h
add bl,al
inc esi
loop up1
ret

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

disp32_num:
mov rdi,dispbuff ;point esi to buffer
mov rcx,08 ;load number of digits to display

dispup1:
rol ebx,4 ;rotate number left by four bits
mov dl,bl ;move lower byte in dl
and dl,0fh ;mask upper digit of byte in dl
add dl,30h ;add 30h to calculate ASCII code
cmp dl,39h ;compare with 39h
jbe dispskip1 ;if less than 39h akip adding 07 more
add dl,07h ;else add 07

dispskip1:
mov [rdi],dl ;store ASCII code in buffer
inc rdi ;point to next byte
loop dispup1 ;decrement the count of digits to display
;if not zero jump to repeat

display dispbuff+3,5 ;Dispays only lower 5 digits as upper three are '0'

ret

********** OUTPUT *********

Aadministrator@administrator-OptiPlex-3010:~$ nasm -f elf64 h.asm


administrator@administrator-OptiPlex-3010:~$ ld -o h h.o
administrator@administrator-OptiPlex-3010:~$ ./h

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


1: Hex to BCD
3: Exit

Please Enter Choice::1

Please enter 4 digit hex number::FFFF

BCD Equivalent::65535

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


1: Hex to BCD
3: Exit

Please Enter Choice::3


administrator@administrator-OptiPlex-3010:~$
**** BCD to HEX*****

section .data

menu db 10,"-----------Menu----------"
db 10,"2. BCD to Hex"
db 10,"3. Exit "
db 10
db 10,"Enter your choice: "
menu_len: equ $-menu

bhmsg db 10,"BCD to Hex "


db 10,"enter 5-digit BCD number: "
bhmsg_len: equ $-bhmsg

hmsg db 10,13,"Equivalent Hex number is: "


hmsg_len: equ $-hmsg

bmsg db 10,13,"Equivalent BCD number is: "


bmsg_len: equ $-bmsg

section .bss

buf resb 6
buf_len: equ $-buf

digitcount resb 1

ans resw 1
char_ans resb 4

%macro dispmsg 2 ;macro for display


mov rax,4 ;load system call for display
mov rbx,1 ;load standard output file descriptor
mov rcx,%1 ;load address of buffer
mov rdx,%2 ;load length of buffer
int 80h ;call to kernel
%endmacro

%macro accept 2 ;macro for accepting


mov rax,3 ;load system call for accept
mov rbx,1 ;load standard input file descriptor
mov rcx,%1 ;load address of buffer
mov rdx,%2 ;load length of buffer
int 80h
%endmacro

section .text
global _start
_start:
menum:
dispmsg menu, menu_len
accept buf,buf_len
mov al,[buf]

case2: cmp byte[buf],'2'


jne case3
call bcd_hex

case3: cmp byte[buf],'3'


je exit
jmp menum

exit:
mov rax,1
mov rbx,0
int 80h

bcd_hex:
dispmsg bhmsg, bhmsg_len
accept buf,buf_len ;buflen = 5 + 1
mov rsi,buf ;load bcd pointer
xor rax,rax ;sum
mov rbx,10
mov rcx,05 ;digit_count

back1:
xor rdx,rdx
mul ebx ;previous digit * 10 = ans (rax*rbx = rdx:rax)
xor rdx,rdx ;sum
mov dl,[rsi] ; Take current digit
sub dl,30h ; accepted digit is Decimal(0-9), so Sub 30H only
add rax,rdx
inc rsi
dec rcx
jnz back1
mov [ans],ax
dispmsg hmsg, hmsg_len
mov ax,[ans]
call display_16

ret
;--------------------------------------------------------------------
accept_16:
accept buf,5
xor bx,bx
mov rcx,4
mov rsi,buf

next_digit: shl bx,04


mov al,[rsi]
cmp al,39h
jbe sub30
sub al,7h

sub30: sub al,30h


add bx,ax
inc rsi
loop next_digit
ret
;----------------------------------------------------------------
display_16:
mov rsi,char_ans+3
mov rcx,4

cnt: mov rdx,0


mov rbx,16
div rbx
cmp dl, 09h
jbe add30
add dl, 07h
add30:
add dl,30h
mov [rsi],dl
dec rsi
dec rcx
jnz cnt
dispmsg char_ans,4
ret

************ OUTPUT*****************

proglabii@102proglabii25:~$ nasm -f elf64 BH.asm


proglabii@102proglabii25:~$ ld -o BH BH.o
proglabii@102proglabii25:~$ ./BH

-----------Menu----------
2. BCD to Hex
3. Exit

Enter your choice: 2

BCD to Hex
enter 5-digit BCD number: 00010

Equivalent Hex number is: 000A

You might also like