Assembly Programming (MIPS): Convert BCD to Decimal

In summary, the conversation discusses converting a decimal number to binary number and explains how the representation of 8762 in binary is 1000 0111 0110 0010, with each decimal digit converted to its 4-bit binary form. The code presented also shows how this conversion can be achieved.
  • #1
Margarita0076
5
0
Homework Statement
Write an assembly program to read a four character BCD number from the user. This will be read in as a standard integer. However, you are to convert this number to its BCD equivalent, and print out this decimal number. You can assume that the input is always 4 characters and is legal (i.e. only the digits from 0-9 will appear).
Relevant Equations
Here’s an example:
User types in 8762. This is stored in the computer as 100010001110102
We need to convert it to the representation 1000 0111 0110 0010, which is BCD for 8762 (Notice the first 4 bits has an 8, the next 4 bits has a 7, and so on)
Print out that BCD value (10000111011000102) as a decimal: 34658 (Your syscall print_int does this anyway).

Output:
BCD: 8762
Binary: 34658
I have this code which convert decimal number to binary number, but I do not understand how representation 8762 like 1000 0111 0110 0010.
8​
7​
6​
2​
1000​
0111​
0110​
0010​
Code:
     .data
strmsg: .asciiz "please Enter a decimal number: "
     .text
#main method
.globl main
#main function
main:
    #load the value
    li $v0, 4
    #load the string message
    la $a0, strmsg
    syscall
    # read the value 5
    li $v0, 5
    #syscall method
    syscall
    add $t0, $zero, $v0
    # loop
    li $t1, 31

Loop:
    blt $t1, 0, EndLoop
    srlv $t2, $t0, $t1
    and $t2, 1
    #load value 1
    li $v0, 1
    move $a0, $t2
    syscall

    #substract 1.
    sub $t1, $t1, 1
    b Loop

EndLoop:
    #load the value
    li $v0, 10
    #end
    syscall
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
Welcome to PF.
Margarita0076 said:
I have this code which convert decimal number to binary number, but I do not understand how representation 8762 like 1000 0111 0110 0010.
8762 base 10, convert to binary is 0010001000111010 base 2.
8192 + 512 + 32 + 16 + 8 + 2 = 8762
 
Last edited:
  • Like
Likes sysprog
  • #3
Margarita0076 said:
I have this code which convert decimal number to binary number, but I do not understand how representation 8762 like 1000 0111 0110 0010.
Each decimal digit in 8762 is converted to its 4-bit binary form.
8 = 1000
7 = 0111
6 = 0110
2 = 0010
Is that your question, or are you asking how to take an arbitrary decimal integer and represent it in BCD (binary coded decimal)? If this is really your question, you can do this by stripping off each decimal digit and converting it to a 4-bit binary value.
 
  • Like
Likes sysprog

1. How do I convert a BCD number to decimal in assembly programming?

To convert a BCD (Binary Coded Decimal) number to decimal in assembly programming, you will need to use a series of instructions that manipulate the binary digits and convert them into their decimal equivalents. This can be achieved by using bitwise operations, such as shifting and masking, to extract the individual digits and then combining them to form the decimal number.

2. What is the difference between BCD and decimal numbers?

BCD numbers are represented in binary form, with each digit of the number being represented by a 4-bit binary code. This means that a BCD number can only represent the digits 0-9. In contrast, decimal numbers are represented in base 10, with each digit being represented by a single decimal digit. This allows decimal numbers to represent a wider range of values, but they require more bits to store the same number compared to BCD.

3. Can I use a loop to convert multiple BCD numbers to decimal?

Yes, you can use a loop in assembly programming to convert multiple BCD numbers to decimal. The loop can be used to iterate through each BCD number and apply the conversion algorithm to each one. This can be useful when working with arrays or large sets of data that need to be converted.

4. How does the BCD to decimal conversion algorithm work?

The BCD to decimal conversion algorithm works by extracting the individual digits of the BCD number and then multiplying them by their respective place values (1, 10, 100, etc.). These products are then added together to get the final decimal value. For example, the BCD number 0101 would be converted to decimal as follows: (0 x 10^3) + (1 x 10^2) + (0 x 10^1) + (1 x 10^0) = 5.

5. Are there any built-in functions or libraries for BCD to decimal conversion in assembly programming?

No, there are no built-in functions or libraries for BCD to decimal conversion in assembly programming. This is because the conversion algorithm is relatively simple and can be implemented using basic instructions and operations. However, some assembly programming languages may have libraries or macros that provide shortcuts for BCD to decimal conversion.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
22
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
9K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
4K
  • Programming and Computer Science
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
4K
Back
Top