# Convert a Number from Decimal to Binary in Assembly Language Assignment Solution.

## Instructions

Objective
Write an Assembly language Assignment program in Flat Assembler (not Visual Studio) to Convert a number from decimal to binary.

## Requirements and Specifications

Either submit the source code file (asm) on Ecampus under the "Submit Homework" menu option, or demo the programs to the instructor during lab.
Using x86 assembly language, write a program that converts an 8-bit integer to binary and hexadecimal using bitwise operators. Do not use external functions. You may use the algorithm below.
Algorithm to convert Value to binary:
1. Set Count to 0
2. AND Value with 128 (binary 10000000) and store into Temp
3. If result is zero, output "0"
4. If result is not zero, output "I"
5. Shift Temp left
6. Increment Count
Algorithm to convert Value to hexadecimal:
1. Shift Value right 4 digits and store into Temp
2. If Temp <=9, print Temp
3. If Temp >=10, add 55 to Temp and print the ASCII character
4. AND Value with 240 (binary 00001111) and store into Temp
5. If Temp <=9, print Value
6. If Temp >=10, add 55 to Temp and print the ASCII character
Screenshots of output

Source Code

format PE console

entry _start

include 'win32a.inc'

header: DB "This x86 assembly program converts an integer to binary and hex", 10, 13, 10, 13, 0

prompt: DB "Enter an integer from 0 - 255: ", 0

binmsg: DB "Binary: ", 0

hexmsg: DB "Hex: ", 0

intfmt: DB "%d", 0

chrfmt: DB "%c", 0

newline: DB 10, 13, 0

value: DD 0

_start:

push ebp

mov ebp, esp

call [printf] ; print the string

add esp, 4 ; restore stack

prompt user to enter number

call [printf] ; print the string

add esp, 4 ; restore stack

push value ; save result in value

push intfmt ; pass format

call [scanf] ; read the number

add esp, 8 ; restore stack

Print the binary result message:

call [printf] ; print the string

add esp, 4 ; restore stack

make binary conversion

mov bh, 0 ; set count to zero

mov bl, [value] ; load number in bl

binloop:

test bl, 128 ; and number with 128

jne print1 ; if and is not zero, print 1

else, print 0

mov eax, '0' ; load character 0

jmp binNext ; print next bin

print1:

mov eax, '1' ; load character 1

binNext:

push eax ; pass character

push chrfmt ; pass char format

call [printf] ; print char

add esp, 8 ; restore stack

shl bl, 1 ; shift left once

inc bh ; increment count

cmp bh, 8 ; compare count with 8

jl binloop ; repeat if count <8

call [printf] ; print newline

add esp, 4 ; restore stack

Print the hex result message:

call [printf] ; print the string

make hex conversion

mov al, [value] ; load number in al

shr al, 4 ; shift right 4 bits

cmp al, 9 ; compare with 10

jg hex1 ; if >10, print hex digit

add al, '0' ; convert to ascii

jmp printLow ; go to print low part

hex1:

add al, 55 ; convert to ascii

printLow:

push eax ; pass converted character

push chrfmt ; pass char format

call [printf] ; print char

add esp, 8 ; restore stack

mov al, [value] ; load number in eax

and al, 15 ; and to leave low nibble

cmp al, 9 ; compare with 10

jg hex2 ; if >10, print hex digit

add al, '0' ; convert to ascii

jmp done ; we are done

hex2:

add al, 55 ; convert to ascii

done:

push eax ; pass converted character

push chrfmt ; pass char format

call [printf] ; print char

add esp, 8 ; restore stack

call [printf] ; print newline

add esp, 4 ; restore stack

mov ebp, esp

pop ebp

call [ExitProcess] ; exit the program

Import section