This set of extra-credit problems covers material from the first part of the course. You can do as many as you like, but you can only receive a total of 50 extra points on this assignment and Homework X2 combined.
NOTE that the usual rules for collaboration do not apply to this assignment. More in the following section.
Please include with each part of the assignment the Honor Code pledge or just the word ``pledged'', and the statement ``This assignment is entirely my own work'' (where ``my own work'' means ``except for anything I got from the assignment itself, such as starter code, or from the course Web site or sample solutions to other assignments). For this assignment you should not work with or seek help from other students or from tutors, but you can consult other sources (other books, Web sites, etc.), as long as you identify them.
Answer the following questions. You may write out your answers by hand or using a word processor or other program, but please submit hard copy, either in class or in one of my mailboxes (outside my office or in the ASO).
.text .globl main main: # opening linkage addi $sp, $sp, -4 sw $ra, 0($sp) # prompt and get two integers from "console" la $a0, prompt li $v0, 4 # "print string" syscall syscall li $v0, 5 # "read int" syscall syscall la $t0, dataX sw $v0, 0($t0) # save result in dataX li $v0, 5 # "read int" syscall syscall la $t0, dataY sw $v0, 0($t0) # save result in dataY # call procedure to add and print la $a0, dataX la $a1, dataY jal foobar # closing linkage lw $ra, 0($sp) addi $sp, $sp, 4 jr $ra .end main # variables and constants .data prompt: .asciiz "Enter two integers, one per line:\n" # note that .word forces alignment -- i.e., causes assembler to insert # space if not on a word boundary (address a multiple of 4) dataX: .word 0 dataY: .word 0
.text .globl foobar foobar: # add two integers and print result # $a0, $a1 have addresses of two integrs # opening linkage addi $sp, $sp, -4 sw $ra, 0($sp) # compute result into $s0 lw $t0, 0($a0) lw $t1, 0($a1) add $s0, $t0, $t1 # print addi $a0, $s0, 0 li $v0, 1 # "print int" syscall syscall la $a0, foobar_nl li $v0, 4 # "print string" syscall syscall # closing linkage lw $ra, 0($sp) addi $sp, $sp, 4 jr $ra # variables and constants .data foobar_nl: .asciiz "\n"
Do the following programming problems. You will end up with at least one code file per problem. Submit your program source (and any other needed files) by sending mail to bmassing@cs.trinity.edu with each file as an attachment. Please use a subject line that mentions the course and the assignment (e.g., ``csci 2321 hw X1'' or ``computer design hw X1''). You can develop your programs on any system that provides the needed functionality, but I will test them on one of the department's Linux machines, so you should probably make sure they work in that environment before turning them in.
Starter program test-convert-int.s contains code to prompt the user for a text string, read it, call the convert procedure, and print the results. Your mission is to fill in the body of the convert procedure so it works as described.
Sample executions:
% spim -f test-convert-int.s Loaded: /usr/share/spim/exceptions.s Enter a line of text: 10 Input 10 Result 10 % spim -f test-convert-int.s Loaded: /usr/share/spim/exceptions.s Enter a line of text: -20 Input -20 Result -20 % spim -f test-convert-int.s Loaded: /usr/share/spim/exceptions.s Enter a line of text: abcd Input abcd Error -1 % spim -f test-convert-int.s Loaded: /usr/share/spim/exceptions.s Enter a line of text: 1000000000000 Input 1000000000000 Error -2
HINTS:
li $t0, '0'and the same thing works for other characters, such as the null character:
li $t0, '\0'I strongly advise that you do this rather than looking up ASCII values and putting them in your code: MIPS assembly code is hard enough to read already, and using the ASCII values directly just makes it worse.
Starter program
test-print-hexbytes.s
contains code to prompt the user for
a text string, read it, call the procedure to print the whole
buffer, and then prompt for an integer, read it, and call the
procedure to print the 4-byte result.
Your mission is to fill in the body of the print procedure
so it works as described.
Sample execution:
% spim -f test-print-hexbytes.s Loaded: /usr/share/spim/exceptions.s Enter a line of text: abcd Input abcd Result 61 62 63 64 0a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 Enter an integer: 20 Input 20 Result 14 00 00 00
HINTS:
/* put result of conversion in "work", ignoring errors */ int work = 0; while (*p != '\0') { work = work*10 + (*p - '0'); ++p; }