Converting Decimal Value to Binary (MIPS)

Reply

Join Date: Mar 2007
Posts: 1
Reputation: bastek is an unknown quantity at this point 
Solved Threads: 0
bastek bastek is offline Offline
Newbie Poster

Converting Decimal Value to Binary (MIPS)

 
0
  #1
Mar 21st, 2007
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:

  1.  
  2. main:
  3. la $a0, in_msg
  4. li $v0, 4
  5. syscall
  6.  
  7. li $v0, 5
  8. syscall
  9. move $t0, $v0
  10.  
  11. la $a0, out_msg
  12. li $v0, 4
  13. syscall
  14.  
  15. move $a0, $t0
  16. li $v0, 1
  17. syscall
  18.  
  19. li $v0, 10
  20. syscall
  21.  
  22. .data
  23. in_msg: .asciiz "Input: "
  24. out_msg: .asciiz "\nOutput:\n-------\n"

How can I convert this decimal value to a binary?

Thank you in advance.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 3
Reputation: RustyBEW is an unknown quantity at this point 
Solved Threads: 0
RustyBEW RustyBEW is offline Offline
Newbie Poster

Re: Converting Decimal Value to Binary (MIPS)

 
0
  #2
Feb 20th, 2008
I am currently trying to work on this. If anyone can help...it would be greatly appreciated.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Converting Decimal Value to Binary (MIPS)

 
0
  #3
Feb 20th, 2008
Post what you've got and what part of it is giving you trouble.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 3
Reputation: RustyBEW is an unknown quantity at this point 
Solved Threads: 0
RustyBEW RustyBEW is offline Offline
Newbie Poster

Re: Converting Decimal Value to Binary (MIPS)

 
0
  #4
Feb 20th, 2008
Originally Posted by Duoas View Post
Post what you've got and what part of it is giving you trouble.
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 3
Reputation: RustyBEW is an unknown quantity at this point 
Solved Threads: 0
RustyBEW RustyBEW is offline Offline
Newbie Poster

Re: Converting Decimal Value to Binary (MIPS)

 
0
  #5
Feb 20th, 2008
Originally Posted by Duoas View Post
Post what you've got and what part of it is giving you trouble.
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 3
Reputation: ombadboy is an unknown quantity at this point 
Solved Threads: 0
ombadboy ombadboy is offline Offline
Newbie Poster

Re: Converting Decimal Value to Binary (MIPS)

 
0
  #6
Nov 24th, 2008
could someone briefly describe how this MIPS code works please?

i get the syscalls, and the registers and all.. but how does it actually convert the decimal into binary? I cant seem to understand the logic behind it..
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC