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:
- Set Count to 0
- AND Value with 128 (binary 10000000) and store into Temp
- If result is zero, output "0"
- If result is not zero, output "I"
- Shift Temp left
- Increment Count
- If Count < 8, jump to step (2)
Algorithm to convert Value to hexadecimal:
- Shift Value right 4 digits and store into Temp
- If Temp < 9, print Temp
- If Temp > 10, add 55 to Temp and print the ASCII character
- AND Value with 240 (binary 00001111) and store into Temp
- If Temp < 9, print Value
- If Temp > 10, add 55 to Temp and print the ASCII character
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
section '.text' code readable executable
_start:
push ebp
mov ebp, esp
print header message
push header ; load address of message
call [printf] ; print the string
add esp, 4 ; restore stack
prompt user to enter number
push prompt ; load address of prompt
call [printf] ; print the string
add esp, 4 ; restore stack
Read number from user
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:
push binmsg ; load address of the binary 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
push newline ; load address of newline
call [printf] ; print newline
add esp, 4 ; restore stack
Print the hex result message:
push hexmsg ; load address of the binary 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
push newline ; load address of newline
call [printf] ; print newline
add esp, 4 ; restore stack
mov ebp, esp
pop ebp
call [ExitProcess] ; exit the program
Import section
section '.idata' import data readable
library kernel32, 'kernel32.dll', \
msvcrt,'msvcrt.dll'
import kernel32, \
ExitProcess,'ExitProcess'
import msvcrt, \
printf, 'printf', scanf, 'scanf'
Similar Samples
Explore our sample projects at ProgrammingHomeworkHelp.com to see our expertise in action. From complex algorithms to elegant code solutions, our samples showcase the high-quality work we deliver to help students excel in their programming courses.
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language