Assembly Language program in MIPS

Below is a MIPS assembly language program. I need to write the program to generate results in 16 bits twos complement. At the moment the program generate's in 32 bits complement, but i need it in 16 bit twos complement. Please help is very appreciated.

  .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
oparebeaAsked:
Who is Participating?
 
oparebeaAuthor Commented:
kaufmed. your change of 16 in the loop did not work can you take a second look at it. I mean the first sample program you tried to correct.
0
 
käµfm³d 👽Commented:
Auf Deutsch oder auf Englisch?

Just kidding! I probably butchered that simple statement anyways  = )

See if this corrects the issue. I made note of what I changed, but I didn't check your logic since you said it's working for 32-bit. I only restricted the number of loop iterations. My assumption is that you will only be entering numbers which fit into 16 bits.
.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, 16 # Counter1 := 32 (descending Counter)            CHANGED HERE
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, 16 # start with bit on the maximum left       AND HERE
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

Open in new window

0
 
oparebeaAuthor Commented:
No. it did not work. Actually I was asked to start printing with 15 bit and our loop test bit 15 and use shift instruction to get the next bit into position. So i came out with another program. But the difficulty i am facing is instead printing the whole 32 bits i need it to print from the 16th to the last bit
New code below.

  .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
0
 
oparebeaAuthor Commented:
Thanks for your help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.