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

clive

# 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
It could be a random act of randomness. Those happen a lot as well.

Herakles

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...

Herakles

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 ?

Ghandi

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

Herakles

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!

oex

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....
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

clive

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.
It could be a random act of randomness. Those happen a lot as well.

Herakles

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


clive

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.
It could be a random act of randomness. Those happen a lot as well.

tadjei

Hello i need this program in 16 bits two's complement any help will be appreciated.

tadjei

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

dedndave

that's for MIPS
it looks like Greek to me   :P

FORTRANS

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

tadjei

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