Assembly Programming (MIPS): Convert BCD to Decimal

Click For Summary
SUMMARY

The discussion focuses on converting a decimal number (8762) to its Binary Coded Decimal (BCD) representation using MIPS assembly programming. The provided code reads a decimal number and outputs its binary equivalent, while the BCD representation is achieved by converting each decimal digit into its 4-bit binary form: 8 as 1000, 7 as 0111, 6 as 0110, and 2 as 0010. The key takeaway is the method of breaking down a decimal number into individual digits and converting each to binary for BCD representation.

PREREQUISITES
  • MIPS assembly language programming
  • Understanding of binary and BCD (Binary Coded Decimal) representation
  • Basic knowledge of MIPS system calls
  • Bitwise operations in assembly language
NEXT STEPS
  • Study MIPS assembly language syntax and conventions
  • Learn about binary and BCD conversion techniques
  • Explore MIPS system calls for input and output operations
  • Investigate bitwise operations and their applications in assembly programming
USEFUL FOR

Students and professionals in computer science, particularly those interested in low-level programming, embedded systems, and digital systems design, will benefit from this discussion.

Margarita0076
Messages
5
Reaction score
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
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   Reactions: sysprog
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   Reactions: sysprog

Similar threads

  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 22 ·
Replies
22
Views
5K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
10K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 4 ·
Replies
4
Views
21K
  • · Replies 4 ·
Replies
4
Views
1K