News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Representation of two's complement

Started by Herakles, June 08, 2010, 12:44:07 AM

Previous topic - Next topic

Herakles

Hi,
our new topic is the "arithmetic of computers" and in order to illustrate this new topic, they want us to write the following programme:

Quote
a. Write a MIPS-assembler-programme which is able to read in a positive or negative decimal digit and give out its binary representation under the use of two's complement representation. Use the system call read_int ($v0 := 5), in order to read in the decimal digit from shell. Check out your programme with various positive and negative inputs.

b. Which is the highest and lowest decimal digit your programme can correctly handle? Explain your answer.


This task sounds to me like "Design a space shuttle by your own, travel to Mars in 80 days, come safely back and report us!"
It would be very nice, if you could give me some support, because otherwise I'll have to send them a blank document...

Thank you very much! :U

Regards,
Herakles

mineiro

English is not my mother language, so, be patience.
two complement is one way to represent negative numbers. One calculator never subtract, only add . So, how can you represent negative numbers only using addition? The answer is two complement.
You need have in mind the number of digits; so you have 2 hexadecimal digits, going from 00h to 0FFh. In this example, the first half can represent the plus digits, and the rest the negative numbers.
The formula is: "not" the number and add one. The number +30h (0011 0000)b, will be (1100 1111)b plus one == (1101 0000) (0D0h). The D0h is the representation of -30h.
About your second question, try to do the two complement of the numbers: 00h and 80h(suppose 2 hexadecimal digits) and think about.

dedndave

the answer to part (b) is going to be limited by the size of the output buffer
i would use Ling Long Kai Fang for the base conversion (aka Horner's Rule)

FORTRANS

Hi,

   The rational (loosely) behind two's complement notation
is to use half the numeric range of a set of numbers as
negative numbers.  In two digit hexadecimal, if you subtract
one from zero you get 0FFH, so you call that "minus one".
And you end up with zero 000H, positive numbers from one
001H to 127 07FH, and negative numbers from minus one
0FFH to minus 128 080H.

   For your b, the size of the numbers determines the range
allowed.  Two hex digits, a byte, allow only the range -128
to 127.  A 16-bit word allows a range from -32768 to 32767.
For beginners the usual size is to use the register size of the
CPU.  Or to limit the range based on a limited input range.
A little harder to explain, but you could limit the input to be
five decimal digits as an example.

HTH,

Steve N.

Herakles

Thank you very much so far.
I have forgotten to mention (as always) that we are talking about MIPS R2000.

Subtask b:
I have read that a MIPS R2000 register's size is 32 bit, so I think this can be seen as the answer to subtask b (lowest binary digit: -2147483648, highest binary digit: 2147483647)...?

Subtask a:
Unfortunately I am still not able to understand how to implement the task, because I don't know how MIPS does handle digits... For example: the input is -4, so do I have to implement a conversion of the decimal digit 4 to the binary digit and then add 1 to the end or is there a better solution?

It would be quite helpful if you could post a pseudo code of the needed implementation...

Thanks.

clive

For a) I think the SPIM provides you with the integer as a 32-bit two's complement value. The task therefore seems to be to just print out those 32-bits as 0/1 binary. To do this you basically need to shift the register left 32 times, looking at the high order bit or carry to determine if the bit is high/low 1/0.

void outbinary(unsigned long x)
{
  int i;

  i = 32;

  while(i--)
  {
    putchar('0' + (char)(x >> 31));

    x = (x << 1);
  }

  putchar('\n');
}


or

void outbinary(unsigned long x)
{
  int i = 32;

  while(i--)
    putchar('0' + (char)((x >> i) & 1));

  putchar('\n');
}
It could be a random act of randomness. Those happen a lot as well.

Herakles

Thank you very much, clive.

Now I begin to understand but I have still some difficulties with your sentence "you basically need to shift the register left 32 times, looking at the high order bit or carry to determine if the bit is high/low 1/0."

I have to admit that I have difficulties to understand your code...
Lets's say I enter -4, what does the programme do?

clive

        -4
0xFFFFFFFC
11111111111111111111111111111100


Got a CASIO calculator that can do DEC/HEX/BIN/OCT? If not buy one, indispensable to an engineer doing hw/sw.

In DEC type 0 - 4, then switch to HEX, and to BIN

You could also try this in the Windows Calculator (Run -> Calc) and switch it to scientific mode. It supports 64-bits (switchable to 8/16/32/64 bits), my CASIO is limited to 10 digits.
It could be a random act of randomness. Those happen a lot as well.

Herakles

Certainly. Bought it eight years ago, but it still does work perfectly fine.

But my problem still is the implementation of conversion in MIPS...
Would it be also possible to make an implementation like "read in a decimal digit and just convert it to a binary digit" or is there only one solution (the one you mentioned)?

So let's say my programme (which still doesn't exist) reads in a decimal digit like -4 and saves it to register $t0. What do I have to do with this register then?

clive

Pretty sure "system call read_int" will return 0FFFFFFFCh if you enter -4.

I could implement it in MIPS, but I don't think you'd learn much if I did.


# '0' + (char)(x >> 31)
SRL $t1, $t0, 31
ADDIU $t1, $t1, 48 # ASCII '0'
It could be a random act of randomness. Those happen a lot as well.

dedndave

we are masm programmers, primarily
from a quick glance at MIPS, it shouldn't be hard to convert code that only uses simple instructions
but, if you google around a bit, there is plenty of material on Horner's Rule

Herakles

Quote from: dedndave on June 08, 2010, 11:09:10 PM
we are masm programmers, primarily
from a quick glance at MIPS, it shouldn't be hard to convert code that only uses simple instructions
but, if you google around a bit, there is plenty of material on Horner's Rule

Usually not, but it always depends on the task. The other thing is, that I have four more courses, so I can't invest too much time in one course.

I want to be honest with you: every Wednesday we get a new exercise (usually containing about 4 tasks) and we have to solve and upload it until next Wednesday at twelve noon (CET). In case every task is correctly, the exercise will be marked as "passed" and depending on how many of all 12 exercises are passed after 12 weeks, we can get a maximum of a 10% bonus on all points of the exam in July. On Friday they always give us a sample solution.

Herakles

Quote from: clive on June 08, 2010, 10:16:39 PM
Pretty sure "system call read_int" will return 0FFFFFFFCh if you enter -4.

I could implement it in MIPS, but I don't think you'd learn much if I did.


# '0' + (char)(x >> 31)
SRL $t1, $t0, 31
ADDIU $t1, $t1, 48 # ASCII '0'

Thank you very much so far, clive.

I've checked the instruction "srl" in my documentation, but I am not able to understand it:


instruction arguments effect description
srl Rd, Rs, Imm Rd:=Rs DIV 2^Imm Shift right logical



Anyway,... My problem is, that I am still a beginner, so my repertoire is too small, to understand a new task at once. I know that "Learning by doing" is the Alpha and Omega, but concerning this task, I have now reached a point, where nothing goes forward. I talked with some fellow students and we all share the same problems with this task.

If you have the time and the motivation to implement this task, I would really appreciate that.

oex

srl is rather simple

it's like this: imagine a 255 byte value 11111111
shift it right once and you get 01111111

(256/2)

I dont know mips or whatever but would guess the arguments mean Destination (01111111), Source (11111111), Number Of Shift Bytes (1)
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

Ghandi

Seeing as this is for an assignment, i would have thought the first port of call would be the professor/teacher/lecturer who is actually holding the course. They are the ones who know their innermost criteria and can explain exactly what they need from you, all you have to do is approach them and explain that you have difficulties. There is no shame in doing this, it is how people learn.

HR,
Ghandi