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
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.
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)
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.
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.
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');
}
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?
-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.
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?
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'
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
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.
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.
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)
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
# Assumes no Load Delays, and no Delay Slots for branches
.data
str1: .ascii "Please type in integer, then ENTER.\n"
.asciiz "The input is converted to binary.\n"
askint: .asciiz "\n?-> "
answstr:.asciiz "The binary value is: "
crlf: .asciiz "\n\n"
strbuf: .asciiz "1234567890123456789012345678901234567890" # 40 chars nof scratch
.text
main: li $v0, 4 # Print String
la $a0, str1 # load address str1
syscall
li $v0, 4 # Print String
la $a0, askint # load address askint
syscall
li $v0, 5 # Read Integer into $v0
syscall
move $t0, $v0
la $a0, strbuf # load address of string buffer
li $t2, 32 # Bit Count, i = 32
binary:
srl $t1, $t0, 31 # t1 = t0 >> 31
addiu $t1, $t1, 48 # t1 = t1 + '0'
sb $t1, 0($a0) # Store Byte [$a0] = $t1
addiu $a0, $a0, 1 # $a0++
sll $t0, $t0, 1 # t0 = t0 << 1
addiu $t2, $t2, -1 # i--
bne $t2, $zero, binary
sb $zero, 0($a0) # Terminate string with NUL
li $v0, 4 # Print String
la $a0, answstr # Answer Message
syscall
li $v0, 4 # Print String
la $a0, strbuf # string buffer with binary ascii
syscall
li $v0, 4 # Print String
la $a0, crlf # Carriage Return, Line Feed
syscall
li $v0, 10 # Exit
syscall
Quote from: Ghandi on June 09, 2010, 03:47:59 AM
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
They help us a bit, of course, but always after Wednesday, twelve noon (see my post above). :green
I don't know about the situation in other western countries, but concerning education, Germany is very underdeveloped (compared to countries like Norway, Sweden, England,...). From your start at elementary school until your finish at university you are expected not to make questions and to do all by your own. It's because Germany doesn't want to spend money on education, so the classes and courses are overfilled and there is always far too little personnel.
This country will be for the high jump one day...
Thank you very much, clive. :thumbu :U
You are maybe not going to believe this, but from your codes I am learning pretty much more than from the so-called "sample solutions".
I'll study your code and hopefully I'll understand everything.
Regards,
Herakles
EDIT:
Does this line strbuf: .asciiz "1234567890123456789012345678901234567890" # 40 chars nof scratch
have any influence on my statement that the lowest possible decimal digit is -232-1 = -2147483648 and the highest possible decimal digit is 232-1-1 = 2147483647 ?
Quote
I don't know about the situation in other western countries, but concerning education, Germany is very underdeveloped (compared to countries like Norway, Sweden, England,...). From your start at elementary school until your finish at university you are expected not to make questions and to do all by your own. It's because Germany doesn't want to spend money on education, so the classes and courses are overfilled and there is always far too little personnel.
Ouch, i'm sorry to hear that. I wasn't trying to disparage you in any way either, i hope you don't think that was the intention. :D I am unfamiliar with this subject, so i'lll just wish you the best of luck with your research.
HR,
Ghandi
Quote from: Ghandi on June 09, 2010, 09:07:30 AM
Ouch, i'm sorry to hear that. I wasn't trying to disparage you in any way either, i hope you don't think that was the intention. :D I am unfamiliar with this subject, so i'lll just wish you the best of luck with your research.
HR,
Ghandi
No, I didn't feel disparaged nor did I accuse you of that intention. :wink
Thank you very much.
Take care!
Quote
I don't know about the situation in other western countries, but concerning education, Germany is very underdeveloped (compared to countries like Norway, Sweden, England,...). From your start at elementary school until your finish at university you are expected not to make questions and to do all by your own. It's because Germany doesn't want to spend money on education, so the classes and courses are overfilled and there is always far too little personnel.
I left school at 16 but have found if there's a will there's a way, all this information will be online if you look for it, it just takes a little perseverance and commitment.... In the long run you will learn more this way than someone from university that gets given all the answers, there are countries far worse off in the world than Germany....
Quote from: Herakles
Does this line strbuf: .asciiz "1234567890123456789012345678901234567890" # 40 chars of scratch
have any influence on my statement that the lowest possible decimal digit is -232-1 = -2147483648 and the highest possible decimal digit is 232-1-1 = 2147483647 ?
No, I just need 32+1 bytes for the ASCII digits and terminating NUL character. This was just a quick way of allocating that, as I'm not familiar with SPIM's assembler.
To decode larger integers (say 64-bit), you'd have to use the "string read" function, and decode the decimal value into two registers. Then you could print out the binary for the high order 32-bits, followed by the low order 32-bits.
To oex:
I don't know how much you know about the German school system. After elementary school (class 1 to 4) there are three different types of school pupils can attend and it depends on their marks after the last year of elementary school. Because of several processions between Germany and Greece and finally staying in Germany, my marks after elementary school were bad, so I had to go to the worst school of the three different types. After two years I managed to go to the better school and after that I finally managed to go to the best school type and there I managed to take my A-levels. What I want to tell you is that I know what hard work means and I also know that life never ain't easy.
To clive:
Thanks for the very good explanation.
They told us that only few students have been able to create a solution to that task and I was the only one who used bitshift logical operators, which is a good sign for me, but not for them, because it shows that the task was too difficult for most of the students. Without your help clive, I would be a part of that students, who haven't been able to upload a solution, so once again I just wanted to say that I really appreciate your strong support! :U
Here is the sample solution they gave us on Friday and I have to say that your solution seems to be more consistent:
.data
prmpt: .asciiz "Please type in a number: "
erg2k: .asciiz "The input is converted to binary: "
nl: .asciiz "\n"
res: .space 33
.text
main:
li $v0, 4
la $a0, prmpt
syscall # print string
li $v0, 5
syscall # read into $v0
move $t0, $v0 # readed numberl in $t0
li $t2, 32 # Counter1 := 32 (descending Counter)
li $t3, 0 # Counter2 := 0 (ascending Counter)
li $t4, 1 # $t4 := 1 (constant)
li $t5, 48 # ASCII("0") = 48
li $t6, 49 # ASCII("1") = 49
li $t7, 00 # null termination
loop:
rol $t0, $t0, 1 # start with bit on the maximum left
and $t1, $t0, $t4 # bit by bit comparison
addi $t2, $t2, -1 # Counter1--
beqz $t1, null # Bit 0 or 1?
one: # Bit was 1
sb $t6, res($t3) # save bit as string
addi $t3, 1 # Counter2++
beqz $t2, end # Alle 32 Bits abgearbeitet?
j loop
null: # Bit war 0
sb $t5, res($t3) # Bit als String speichern
addi $t3, 1 # Counter2++
beqz $t2, end # processed all 32 bit?
j loop
end:
sb $t7, res+1($t3) # null termination at the end
la $a0, erg2k
li $v0, 4
syscall
la $a0, res # print result
li $v0, 4
syscall
la $a0, nl # print new line
li $v0, 4
syscall
li $v0, 10 # Exit
syscall
Quote from: Herakles
Here is the sample solution they gave us on Friday and I have to say that your solution seems to be more consistent:
Yes, I like mine better too. It's how I'd do it on an ARM, x86, or 68K.
Hello i need this program in 16 bits two's complement any help will be appreciated.
Quote from: clive on June 09, 2010, 04:12:48 AM
# Assumes no Load Delays, and no Delay Slots for branches
.data
str1: .ascii "Please type in integer, then ENTER.\n"
.asciiz "The input is converted to binary.\n"
askint: .asciiz "\n?-> "
answstr:.asciiz "The binary value is: "
crlf: .asciiz "\n\n"
strbuf: .asciiz "1234567890123456789012345678901234567890" # 40 chars nof scratch
.text
main: li $v0, 4 # Print String
la $a0, str1 # load address str1
syscall
li $v0, 4 # Print String
la $a0, askint # load address askint
syscall
li $v0, 5 # Read Integer into $v0
syscall
move $t0, $v0
la $a0, strbuf # load address of string buffer
li $t2, 32 # Bit Count, i = 32
binary:
srl $t1, $t0, 31 # t1 = t0 >> 31
addiu $t1, $t1, 48 # t1 = t1 + '0'
sb $t1, 0($a0) # Store Byte [$a0] = $t1
addiu $a0, $a0, 1 # $a0++
sll $t0, $t0, 1 # t0 = t0 << 1
addiu $t2, $t2, -1 # i--
bne $t2, $zero, binary
sb $zero, 0($a0) # Terminate string with NUL
li $v0, 4 # Print String
la $a0, answstr # Answer Message
syscall
li $v0, 4 # Print String
la $a0, strbuf # string buffer with binary ascii
syscall
li $v0, 4 # Print String
la $a0, crlf # Carriage Return, Line Feed
syscall
li $v0, 10 # Exit
syscall
is it possible i can get this in 16bits two's complement
that's for MIPS
it looks like Greek to me :P
Quote from: tadjei on April 19, 2011, 09:50:21 AM
Hello i need this program in 16 bits two's complement any help will be appreciated.
Hi,
With apologizes to Clive for what I crudely did to his
program... Here is a rather basic line by line translation
to 8088 code for DOS. The get integer routine was NOT
implemented and a dummy value is used instead.
HTH,
Steve
Quote from: FORTRANS on April 19, 2011, 01:36:47 PM
Quote from: tadjei on April 19, 2011, 09:50:21 AM
Hello i need this program in 16 bits two's complement any help will be appreciated.
Hi,
With apologizes to Clive for what I crudely did to his
program... Here is a rather basic line by line translation
to 8088 code for DOS. The get integer routine was NOT
implemented and a dummy value is used instead.
HTH,
Steve
Steve i need this in MIPS. Clive if you are there is possible i can also get it in MIPS