| | |
Converting Decimal Value to Binary (MIPS)
![]() |
•
•
Join Date: Mar 2007
Posts: 1
Reputation:
Solved Threads: 0
Hi,
I have a simple code written in MIPS assembly that reads an integer from a user and displays it to the screen. The code is as follows:
How can I convert this decimal value to a binary?
Thank you in advance.
I have a simple code written in MIPS assembly that reads an integer from a user and displays it to the screen. The code is as follows:
Assembly Syntax (Toggle Plain Text)
main: la $a0, in_msg li $v0, 4 syscall li $v0, 5 syscall move $t0, $v0 la $a0, out_msg li $v0, 4 syscall move $a0, $t0 li $v0, 1 syscall li $v0, 10 syscall .data in_msg: .asciiz "Input: " out_msg: .asciiz "\nOutput:\n-------\n"
How can I convert this decimal value to a binary?
Thank you in advance.
•
•
Join Date: Feb 2008
Posts: 3
Reputation:
Solved Threads: 0
I figured it out:
# Brent Wagner
.data
ask_str1: .asciiz "Enter a number PLEASE: "
result_str: .asciiz ""
.align 2
.text
.globl __start
__start:
# ask and store the first number
li $v0, 4
la $a0, ask_str1
syscall
li $v0, 5
syscall
move $a0, $v0
jal print_bin
# New Line
li $v0, 11
li $a0, 10
syscall
j __start
print_bin:
add $t0, $zero, $a0 # put our input ($a0) into $t0
add $t1, $zero, $zero # Zero out $t1
addi $t3, $zero, 1 # load 1 as a mask
sll $t3, $t3, 31 # move the mask to appropriate position
addi $t4, $zero, 32 # loop counter
loop:
and $t1, $t0, $t3 # and the input with the mask
beq $t1, $zero, print # Branch to print if its 0
add $t1, $zero, $zero # Zero out $t1
addi $t1, $zero, 1 # Put a 1 in $t1
j print
print: li $v0, 1
move $a0, $t1
syscall
srl $t3, $t3, 1
addi $t4, $t4, -1
bne $t4, $zero, loop
jr $ra
# Brent Wagner
.data
ask_str1: .asciiz "Enter a number PLEASE: "
result_str: .asciiz ""
.align 2
.text
.globl __start
__start:
# ask and store the first number
li $v0, 4
la $a0, ask_str1
syscall
li $v0, 5
syscall
move $a0, $v0
jal print_bin
# New Line
li $v0, 11
li $a0, 10
syscall
j __start
print_bin:
add $t0, $zero, $a0 # put our input ($a0) into $t0
add $t1, $zero, $zero # Zero out $t1
addi $t3, $zero, 1 # load 1 as a mask
sll $t3, $t3, 31 # move the mask to appropriate position
addi $t4, $zero, 32 # loop counter
loop:
and $t1, $t0, $t3 # and the input with the mask
beq $t1, $zero, print # Branch to print if its 0
add $t1, $zero, $zero # Zero out $t1
addi $t1, $zero, 1 # Put a 1 in $t1
j print
print: li $v0, 1
move $a0, $t1
syscall
srl $t3, $t3, 1
addi $t4, $t4, -1
bne $t4, $zero, loop
jr $ra
•
•
Join Date: Feb 2008
Posts: 3
Reputation:
Solved Threads: 0
I figured it out:
# Brent Wagner
.data
ask_str1: .asciiz "Enter a number PLEASE: "
result_str: .asciiz ""
.align 2
.text
.globl __start
__start:
# ask and store the first number
li $v0, 4
la $a0, ask_str1
syscall
li $v0, 5
syscall
move $a0, $v0
jal print_bin
# New Line
li $v0, 11
li $a0, 10
syscall
j __start
print_bin:
add $t0, $zero, $a0 # put our input ($a0) into $t0
add $t1, $zero, $zero # Zero out $t1
addi $t3, $zero, 1 # load 1 as a mask
sll $t3, $t3, 31 # move the mask to appropriate position
addi $t4, $zero, 32 # loop counter
loop:
and $t1, $t0, $t3 # and the input with the mask
beq $t1, $zero, print # Branch to print if its 0
add $t1, $zero, $zero # Zero out $t1
addi $t1, $zero, 1 # Put a 1 in $t1
j print
print: li $v0, 1
move $a0, $t1
syscall
srl $t3, $t3, 1
addi $t4, $t4, -1
bne $t4, $zero, loop
jr $ra
# Brent Wagner
.data
ask_str1: .asciiz "Enter a number PLEASE: "
result_str: .asciiz ""
.align 2
.text
.globl __start
__start:
# ask and store the first number
li $v0, 4
la $a0, ask_str1
syscall
li $v0, 5
syscall
move $a0, $v0
jal print_bin
# New Line
li $v0, 11
li $a0, 10
syscall
j __start
print_bin:
add $t0, $zero, $a0 # put our input ($a0) into $t0
add $t1, $zero, $zero # Zero out $t1
addi $t3, $zero, 1 # load 1 as a mask
sll $t3, $t3, 31 # move the mask to appropriate position
addi $t4, $zero, 32 # loop counter
loop:
and $t1, $t0, $t3 # and the input with the mask
beq $t1, $zero, print # Branch to print if its 0
add $t1, $zero, $zero # Zero out $t1
addi $t1, $zero, 1 # Put a 1 in $t1
j print
print: li $v0, 1
move $a0, $t1
syscall
srl $t3, $t3, 1
addi $t4, $t4, -1
bne $t4, $zero, loop
jr $ra
![]() |
Similar Threads
- Help With Decimal to Binary conversion (IT Professionals' Lounge)
- How to convert a decimal number into its binary equivalent (Python)
- decimal to binary conversion (C++)
- Stuck on a program I am working on, converting a ASCII charchter to 7 Bit (Assembly)
- How do I Take in Hexidecimal input out put binary, decimal, and octal (Java)
- i need code for binary to decimal (it should be written in 'C') (C)
Other Threads in the Assembly Forum
- Previous Thread: something about resident program ?
- Next Thread: Floating point in Assembly
| Thread Tools | Search this Thread |






