Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8653255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:29:59+00:00 2026-06-12T14:29:59+00:00

I made a program in which I try to display the binary form of

  • 0

I made a program in which I try to display the binary form of a number input by user. But the program isn’t doing the masking correctly. What should I do to solve it?

user input in $s0

Loop:

and $t0,$s0,2147483648  // anding it with this number because only its MSB is 1 all other bits are zero

sll $s0,$s0,1

move $a0,$t0

li $v0,1
syscall

beq $t1,31,Exit

addi $t1,$t1,1
j Loop

UPDATE:
I modified this code as suggested by dbrank0, but now it displays only one bit instead of 32 bits

Loop:

and $t0,$s0,2147483648

sll $s0,$s0,1

beq $t1,31,Exit

move $a0,$t0

addi $t1,$t1,1

bgtu $t0,0,Check

li $t0,0
j Loop

Disp:
li $v0,1
syscall

j Loop

Check:
li $t0,1

j Disp

It will be a great deal if someone can help me solve this problem.

Regards

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:30:00+00:00Added an answer on June 12, 2026 at 2:30 pm

    Here’s a problem:

    bgtu  $t0, 0, Check
    li    $t0, 0
    j     Loop
    

    If it’s a zero, it doesn’t get displayed because you’re jumping to Loop instead of Disp. Oh look, Disp is written immediately after this instruction anyhow! Solution: get rid of the jump altogether.

    Here’s another problem, as dbrank0 described:

    Disp:
    li $v0,1
    syscall
    

    This will display the contents of $a0 as an integer. But if the bit was a 1, the value of $a0 will be 0x80000000, not 1! When you try to print 0x80000000, it’ll treat it as a signed integer and print -2147483648 instead.

    Here’s another problem:

    beq $t1,31,Exit
    

    Firstly, this instruction is in an awkward place. Why are you checking for an exit condition after the and and shift? You should check for it either at the start or the end, not in the middle. Furthermore, you need to check against 32 because there are 32 bits and you check before printing each bit. Currently the last bit will get chopped off as a result.


    There’s a clever way to make your program do less work than it needs to. Take advantage of the fact that you’re displaying from left to right (ie, the most significant bit is being displayed first). When the MSB is set, it can be treated as a negative number in two’s complement!

    li     $t0, 32
    li     $v0, 1
    
    Loop:
    bltz   $s0, Bit1
    li     $a0, 0
    j      Disp
    
    Bit1:
    li     $a0, 1
    
    Disp:
    syscall
    
    Tail:
    subi   $t0, $t0, 1
    beqz   $t0, Exit
    sll    $s0, $s0, 1
    j      Loop
    
    Exit:
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made a front end for a program which test java (foo.class) binary
I have made a program in Python 3, which tests whether a number is
I made a program which i want to launch with some protocal for eg
I've made a small program which has 2 buttons and each does certain thing.
Problem: Have made a small mail program which works perfectly on my developer pc
How to tell python for example, I made a program, which opens a specific
I made a program in Idle that says: for trial in range(3): if input('Password:')
I made a program: import collections x = input(Enter in text: ) x_counter =
i have made a program in which i make a file in the data/data/mypackagename/
Recently I made a program in c, which only purpose was to overflow the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.