×
Samples Blogs Make Payment About Us Reviews 4.9/5 Order Now

Program in Flat Assembler to Convert a Number from Decimal to Binary in Assembly Language Assignment Solution

July 06, 2024
Rehana Magnus
Rehana Magnus
🇨🇦 Canada
Assembly Language
Rehana Magnus, PhD in Computer Science from the esteemed Acadia Institute of Technology, Canada. With 6 years of experience, specializes in assembly language programming. Proficient in low-level coding, optimizing performance, and enhancing system functionality.
Key Topics
  • Instructions
  • Requirements and Specifications
Tip of the day
Familiarize yourself with OCaml's pattern matching; it simplifies handling recursive data structures like lists and trees, making your code concise and easier to debug.
News
In 2024, Girls Who Code introduced a Data Science + AI track in their free summer programs for high school students, fostering skills in cybersecurity and creative coding​

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
  7. If Count < 8, jump to step (2)

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
program to Convert a number from decimal to binary Assembly language

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.