Program 3: Printing Binary Values
Program due: Wednesday, April 2nd, 8 p.m.
Write a function named binary that takes two arguments. The first argument will be an integer. The second argument will be 32, 16, or 8. binary will print the binary equivalent of the first argument. The second argument determines whether you are to use all 32 bits of the first argument, only the right-half of the bits, or only the right-most 8 bits of the number. Note that these correspond to a full word, a half word, and a byte.
We will provide main. main will call binary one or more times. Each time, binary will print the binary version. Suppress leading zeroes in the binary number. Instead, for each leading zero, print a blank space. When binary is done, return the number of blank spaces printed.
Here is an example:
.data
 
mainNumElements:
      .word  5
 
mainValues:
      .word  -257
      .word  16
      .word -53
      .word   0
      .word  0x777
 
mainEqualStr:
      .asciiz " = "
mainThereStr:
      .asciiz "; there are "
mainLeadingStr:
      .asciiz " leading blanks\n"
mainBitStr:
      .asciiz "-bit values:\n"
 
.text
main:
         # Function prologue -- even main has one
         subu  $sp, $sp, 24       # allocate stack space -- default of 24
         sw    $fp, 0($sp)        # save frame pointer of caller
         sw    $ra, 4($sp)        # save return address
         addiu $fp, $sp, 24       # setup frame pointer for main
 
         # for (numBits = 32; numBits >= 8; numBits /= 2)
         #    for (i = 0; i < mainNumElements; i++)
         #       binary(mainValues[i], numBits)
         addi  $a1, $zero, 32     # $a1 = numBits = 32 bits
        
mainOuterLoopBegin:
         addi  $s1, $zero, 0      # $s1 = i = 0
         la    $t0, mainNumElements
         lw    $s2, 0($t0)        # $s2 = mainNumElements
         la    $s3, mainValues    # $s3 = addr of mainValues[0]
 
         slti  $t0, $a1, 8        # $t0 = (numBits < 8)
         bne   $t0, $zero, mainDone
        
         # print numBits
         addi  $a0, $a1, 0
         li    $v0, 1
         syscall
         la    $a0, mainBitStr
         li    $v0, 4
         syscall
 
mainLoopBegin:
         slt   $t0, $s1, $s2      # $t0 = (i < mainNumElements)
         beq   $t0, $zero, mainLoopEnd
        
         # call: binary(mainValues[i], numBits)
         sll   $t0, $s1, 2        # $t0 = 4 * i
         add   $t0, $s3, $t0      # $t0 = addr of mainValues[i]
         lw    $a0, 0($t0)        # $a0 = mainValues[i]
         # addi  $a1, $zero, 8      # $a1 = numBits
         jal   binary
         addi  $s4, $v0, 0        # $t0 = number of leading blanks
 
         # print original value
         addi  $s5, $a0, 0
         la    $a0, mainEqualStr
         li    $v0, 4
         syscall
         addi  $a0, $s5, 0
         li    $v0, 1
         syscall
         la    $a0, mainThereStr
         li    $v0, 4
         syscall
         addi  $a0, $s4, 0
         li    $v0, 1
         syscall
         la    $a0, mainLeadingStr
         li    $v0, 4
         syscall
 
         addi  $s1, $s1, 1        # i++
         j     mainLoopBegin
 
mainLoopEnd:
         srl   $a1, $a1, 1        # $a1 = numBits = numBits div 2
         j     mainOuterLoopBegin
        
mainDone:
         # Epilogue for main -- restore stack & frame pointers and return
         lw    $ra, 4($sp)        # get return address from stack
         lw    $fp, 0($sp)        # restore frame pointer of caller
         addiu $sp, $sp, 24       # restore stack pointer of caller
         jr    $ra                # return to caller
 
# Your code goes below this line
The output of prog3.s for this case should be:
32-bit values:
11111111111111111111111011111111 = -257; there are 0 leading blanks
                           10000 = 16; there are 27 leading blanks
11111111111111111111111111001011 = -53; there are 0 leading blanks
                               0 = 0; there are 31 leading blanks
                     11101110111 = 1911; there are 21 leading blanks
16-bit values:
1111111011111111 = -257; there are 0 leading blanks
           10000 = 16; there are 11 leading blanks
1111111111001011 = -53; there are 0 leading blanks
               0 = 0; there are 15 leading blanks
     11101110111 = 1911; there are 5 leading blanks
8-bit values:
11111111 = -257; there are 0 leading blanks
   10000 = 16; there are 3 leading blanks
11001011 = -53; there are 0 leading blanks
       0 = 0; there are 7 leading blanks
 1110111 = 1911; there are 1 leading blanks
What will be provided:
Each test case will contain a main function. The main function will call binary zero or more times.
Output:
Your binary function will print the binary digits for the number. When the number has one or more leading 0’s, binary should replace those 0’s with one blank space each.
There is a special case: a 32-bit value that contains all 0’s will result in 31 blanks and one 0.
Do not print a newline character following the binary value. The provided main function will then print the rest of the line (as shown in the example above).
Label naming conventions:
Labels in MIPS programs are global. This presents a problem when you want to use a label that is already in use elsewhere in the program. To avoid conflicts in labels between your code and the test cases, we will adopt the following practice:
Labels will have the name of the procedure or function at the start of the label. For example, if I want to use the label LoopBegin: in main, then I will name the label mainLoopBegin:. If you also want to use the label LoopBegin: in your binary procedure, you will name it binaryLoopBegin:.
This convention applies to labels used in .text segments, and to labels used in .data segments. For example, if you need a label for a string that your function will print, then the label for the string will start with binary.
We will check that you are following these labeling conventions when we grade your program. Points will be deducted for not following this convention.
Programming points:
You are required to follow the calling conventions for functions and procedures that you write.
Put appropriate comments in your program to indicate what is happening in the program. Include comments at the beginning (below the required comment) that describe the purpose of the program.
The required comment line:
    # Your code goes below this line
must be present in the prog3.s file that you turnin! Everything that you provide (comments and code) must go below this required comment line.
The prog3.s that you turn in may contain one of the test cases above the required comment, or the required comment can be the very first line (no test case data and no comments above it). Either is acceptable.
Test cases:
We will provide some sample files for you to copy. They will be available to copy on the Unix machines (lectura and the Fedora machines, fd01 to fd08). They will also be on the Windows machines. On the Windows systems, look in the shared Rotis drive: Rotis->csc252->shared->prog3. On the Unix machines, look in the directory: ~cs252/spring08/prog3. In both cases, the files will be named: test01.s, test02.s, etc. Secure ftp can also be used to access the test cases on lectura from your home system. Please check with us if you have problems accessing these test cases.
The required comment line:
    # Your code goes below this line
must be present in the prog3.s file that you turnin! The prog3.s that you turnin may contain one of the test cases above the required comment, or the required comment can be the very first line (no test case data above it). Either is acceptable.
Turnin:
Note: We are asking for submission of programs via D2L only.
  1. Name your program: prog3.s
  2. Using a web browser, go to: http://d2l.arizona.edu/
  3. Login using the “UA NetID Login” (upper-left corner of the web page).
  4. You should now be at “My Home” on D2L. At the center bottom of the screen, under the heading “My Academic Courses”, you should find “C SC252 SP08 001-002H Homer” listed under 2008 Spring. Click on this link.
  5. You will now be at the CSc 252 page for Spring 08.
  6. There is a row of links just underneath the Wildcat. In this row, you will find the link “Dropbox”. Click on this link.
  7. You will be at a page that shows three Dropbox Folders: Program3, Program3-Late, Program3-Regrade. Only one of these will be active. The Program3 folder is for on-time submissions. The Program3-Late folder will be available for late submissions. The Program3-Regrade folder is used for requesting a re-grade of the program after the grades have been returned. Click on “Program3”. If you are doing a late turnin, click on “Program3-Late”.
  8. You should now be at the page: “Submit Files - Program3”. Click on the “Add a File” button. A pop-up window will appear. Click on the “Choose File” button. Use the file browser that will appear to select your prog3.s file. Click on the “Upload” button in the lower-right corner of the pop-up window.
  9. You are now back at the “Submit Files - Program3”. Click on “Upload” in the lower-right of this window. You should now be at the “File Upload Results” page, and should see the message File Submission Successful.
  10. You can repeat this process as often as necessary. Each submission will be time-stamped. When we grade programs, we will grade only the most recent submission.