Create an assembly program function that will convert an int

Create an assembly program (function) that will convert an integer to binary and display a 16-bit representation of 0\'s and 1\'s.

Your C++ program should pass a char array and the integer to be converted to an assembly function and then display the char array. Assembly function return value is irrelevant since the function is modifying directly the memory where the char array is stored.

Solution

uint16_t* DecToBin(char *input){

unsigned int intInput, Holder;
uint16_t charReturn[7];    //variable to store 16 bit binary numbers
uint16_t* ptr;
   val = atoi(input);        //buffer to intiger convertion
Holder = intInput % 2; //holding the reminder for each digit
charReturn[0] = Holder;
Holder = intInput / 2; //eleminating the digit
charReturn[1] = Holder % 2;
ptr = charReturn;
return ptr;
}

title decimal_to_binary
.model small
.stack 100h
.data
msg db \'Enter a decimal number:$\'
msg1 db 0dh,0ah,\'Invalid entry $\'
msg2 db 0dh,0ah,\'Its equiavlent binary is:$\'
.code
main proc
mov ax,@data
mov ds,ax

lea dx,msg
mov ah,9 ;print message
int 21h

mov ah,1
int 21h ;read data from user

cmp al,30h ;check whether user enter number or something else
jnge invalid ;jump if any invalid entry

cmp al,39h
jnle invalid

lea dx,msg2 ;print message
mov ah,9
int 21h

and al,0fh ;clear upper four bits of al register
mov cl,3 ;cl used as counter in shifting bits

mov bl,al ;save value in bl register
mov bh,bl ;move contents of bl into bh

shr bh,cl ;right shift bh register three times by using cl as a counter
add bh,30h ;make binary value visible as 0 or 1

mov ah,2 ;print binary value
mov dl,bh
int 21h

xor bh,bh ;clear bh register
mov bh,bl

mov cl,2 ;make cl counter value equals to two
and bh,04h ;clear all bits except third last bit

shr bh,cl
add bh,30h

mov ah,2 ;print binary value of third last bit
mov dl,bh
int 21h

xor bh,bh
mov bh,bl

and bh,02h ;clear all bits except second last bit
shr bh,1

add bh,30h

mov ah,2 ;print second last bit
mov dl,bh
int 21h

xor bh,bh
mov bh,bl   

and bh,01h ;clear all bits except the last bit
add bh,30h

mov ah,2 ;print last bit in binary
mov dl,bh
int 21h

jmp exit

invalid:

lea dx,msg1 ;used to print message of invalid entry
mov ah,9
int 21h

exit:

mov ah,4ch
int 21h

main endp
end main

Create an assembly program (function) that will convert an integer to binary and display a 16-bit representation of 0\'s and 1\'s. Your C++ program should pass
Create an assembly program (function) that will convert an integer to binary and display a 16-bit representation of 0\'s and 1\'s. Your C++ program should pass

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site