0

I'm struggling with this question:

Prompt the user to enter a number, print the number in 16-bit two's complement

To print the number you will print each bit as either the string "1" or the string "0", using a loop that will print one bit in each iteration. Start printing with bit 15. (Recall that we number the bits starting from 0 at the low order bit.) In your loop test bit 15, and then print either "1" or "0". Use a shift instruction to get the next bit into position 15 before repeating.

I unfortunately missed a lecture that was about shifts and using masks, so I don't really have much understanding of how I would go about doing this lab. How can I print a specific bit of a number? I understand that I just keep printing bit 15, and then doing a shift left, but I have no idea this would be done in MIPS. Any help would be very much appreciated.

EDIT:

I understand the shifting perfectly, it's just printing the bit thats confusing me.

For example, if the number I wanted to convert to two's complement was 25 and is in register $t0.

First I print the 15th bit. Then I do a shift left. And then I repeat 15 times.

It should look something like this:

# Print bit
sll     $t0, $t0, 1

I just don't get how to print the first bit at spot 15.

zaynv
  • 235
  • 2
  • 4
  • 15
  • In ascii the character '0' is represented with the value 0x30, a '1' 0x31. so if you were to take a number shift right N bits then and with a 1 you would get either a 0 or a 1 yes? then add or or 0x30 you get either 0x30 or 0x31 yes? repeat for each value of N... – old_timer Dec 02 '14 at 04:18

2 Answers2

1

break down the problem -- 1. how to print the bit, and 2. how to determine the bit's value

You already know how to determine the bit's value, you test bit 15 and keep shifting the other bits into position 15.

How are you expected to "prompt the user" to enter a value? Printing the bit is the same printing a prompt.

Andras
  • 2,995
  • 11
  • 17
  • I would just load the address of the prompt string into $a0, put the value 4 in $v0 and do a syscall to print the prompt. I'm confused on printing a bit because I don't know what register the bit I want to print is going to be in. – zaynv Dec 02 '14 at 04:14
  • don't think of it as printing _THE_ bit. Think of it as an if-the-else: if the bit is 1, load $a = "1" and print it. Else load $a = "0" and print that. The bit you're testing is in $t0, that's also the bit you're "printing", but note the quotes -- it's not the bit you're printing, it's a "1" or "0" prompt string that corresponds to the state of the bit. – Andras Dec 02 '14 at 04:19
  • Oh, that makes sense! I feel silly now, I was spending quite a bit of time trying to figure out how to print the actual bit of a register. I think I have most of it figured out now, but the last thing I'm confused about is accessing bit 15. How exactly do I test that specific bit? I'm assuming in my loop I would put the bit's value in a register and then check if it is 0 or 1, but I have no idea how to put the 15th bit into a register. – zaynv Dec 02 '14 at 04:28
  • look over the section on using masks. masks are bit patterns AND-ed with the data. You must have learned how to test a register for all zeroes; design a bitmask (binary value) that when AND-ed with your data in $t0 will result in a zero if bit 15 is zero, and will result in non-zero if bit 15 is not zero. You'll need another temp register to hold the results of the AND. – Andras Dec 02 '14 at 04:38
  • 1
    Just to make sure I understand - Let's say the number entered by the user was 45. That would be 0000 0000 0010 1101 in 16-bit two's complement. So does that mean the mask I should use should be 1000 0000 0000 0000? When these two are AND'ed, it will result in non-zero if bit 15 is not zero. And I suppose that this mask would work with any other number as well. – zaynv Dec 02 '14 at 05:20
  • Thanks for all your help! I just have one last question. I believe that a mask is supposed to be in a `0x##` format. How would I convert 1000 0000 0000 0000 to a form like that? – zaynv Dec 02 '14 at 18:09
  • each group of 4 bits is expressed as a hexadecimal digit [0-9a-f], most significant digits at the left. A 16-bit number would have 4 hexadecimal digits, like 0x8000; a 32-bit number would have 8 digits – Andras Dec 02 '14 at 18:12
  • Ah, I see. In this case, 0x8000 would be the mask right? Because the 1 is in the "8's column" and the rest are 0's? – zaynv Dec 02 '14 at 18:21
  • Just finished the program! Thank you very much for taking the time. I wish I could repay you, but all I can offer is my eternal gratitude :p thanks for being awesome. – zaynv Dec 02 '14 at 19:55
0

Masks are generally used with "bitwise and" or "bitwise or" instructions. To determine whether a particular single bit is set to 1 in a number, you take the "bitwise and" of that number with another number (the "mask") in which that particular bit is set to 1 and all other bits are zero. (There are other applications where you would want to set more than one bit in a mask; better get a copy of those lecture notes!)

There are many places to look up the MIPS instruction set. For example, here or here. Find a page like those, then search for "shift left" and "bitwise and". (And remember where you found that document so you can look up other things later!)

David K
  • 3,147
  • 2
  • 13
  • 19
  • I've edited my description, I'm okay with shifting now but I don't know how I would print a specific bit. And thanks for the links! – zaynv Dec 02 '14 at 04:14