?
Solved

Assembly Language program in MIPS

Posted on 2011-04-19
4
Medium Priority
?
589 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:oparebea
  • 3
4 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35425746
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
 

Author Comment

by:oparebea
ID: 35428169
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
 

Accepted Solution

by:
oparebea earned 0 total points
ID: 35430523
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
 

Author Closing Comment

by:oparebea
ID: 35775455
Thanks for your help
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
This article will show, step by step, how to integrate R code into a R Sweave document
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses
Course of the Month17 days, 3 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question