Lab 4: ASCII Conversion
Part A: Due Wednesday, 20 February 2019, 11:59 PM Part B: Due Sunday, 3 March 2019, 11:59 PM
Minimum Submission Requirements
● Ensure that your Lab4 folder contains the following files (note the capitalization convention):
○ Diagram.pdf ○ Lab4.asm
○ README.txt
● Commit and push your repository Lab Objective
In this lab, you will develop a more detailed understanding of how data is
represented, stored, and manipulated at the processor level. In addition to
strengthening your understanding of MIPS coding, you will read a string input and
learn how to convert ASCII characters into a base 4 number and print it to the
console.
Lab Preparation
Read chapters 1 and 9 from Introduction To MIPS Assembly Language Programming. Specification
Functionality
The functionality of your program will be as follows:
1.
2. 3.
4.
5. 6.
7.
Read two 8-bit 2SC program arguments. These numbers may be entered as hex
(using the “0x” prefix) or binary (using the “0b” prefix). The range of these
inputs are: [0x80, 0x7F] in hex or [0b10000000, 0b01111111] in binary. Note
that this range is [-128, 127] in decimal.
a. Hex inputs will be exactly 2 digits.
b. Binary inputs will be exactly 8 bits.
Print the user inputs.
Identify each number as hex or binary by looking at “0x” prefix for hex and “0b” prefix for 2SC binary.
Convert the ASCII strings into two sign-extended integer values.
a. Convert the first program argument to a 32-bit two’s complement number, stored in register $s1.
b. Convert the second program argument to a 32-bit two’s complement number, stored in register $s2.
Add the two integer values, store the sum in $s0.
Print the sum as a signed base 4 number to the console. Do not print any
leading 0s.
a. If the number is negative, print a negative sign and the magnitude.
b. If the number is positive, just print the magnitude.
(Extra credit) Read in the program arguments as signed decimal numbers and print the sum as a base 4 value.
Lab 4
© 2019, Computer Engineering Department, University of California – Santa Cruz
Page 1 of 11 Winter 2019
a. The program arguments can be any decimal number between -128 and 127. These arguments do not need a prefix, and will not have any leading zeros (e.g. the number 9 will entered as “9” not “09”).
b. Note that the ASCII code for a negative sign (“-”) is 0x2D.
Two Parts
Part A: Block Diagram & Pseudocode
Before coding, you will first create a top level block diagram or flowchart to show how the different portions of your program will work together. Use https://www.draw.io or a similar drafting program to create this document. This diagram will be contained in the file Diagram.pdf. This diagram must be computer generated to receive full credit.
Next, you will create pseudocode that outlines your program. Your pseudocode will
appear underneath your header comment in Lab4.asm. You may modify your pseudocode as
you develop your program. Your pseudocode must be present in your final submission.
Guidelines on developing pseudocode can be found here:
https://www.geeksforgeeks.org/how-to-write-a-pseudo-code/
Part B: Assembly Code
Write a program in MIPS to implement the functionality.
Output
An example of the expected output is given below. Your code’s output format should match this output format exactly. New line characters are printed after each number representation.
Here are several example program outputs:
You entered the numbers: 0x35 0xF4
The sum in base 4 is: 221
— program is finished running —
You entered the numbers: 0b00110101 0b11110100
The sum in base 4 is: 221
— program is finished running —
Lab 4 Page 2 of 11 Winter 2019 © 2019, Computer Engineering Department, University of California – Santa Cruz
You entered the numbers: 0xCE 0b11110100
The sum in base 4 is: -332
— program is finished running —
You entered the numbers: 53 -12
The sum in base 4 is: 221
— program is finished running —
You entered the numbers: 0xCE -12
The sum in base 4 is: -332
— program is finished running —
Files
Diagram.pdf
This file will contain a block diagram or flowchart of how the different components
of your code work together. To receive full credit, you must submit a computer
generated diagram. Hand drawn diagrams will be worth half credit.
Lab4.asm
This file contains your pseudocode and assembly code.
Comments
Your code should include a header comment with your name, CruzID, date, lab number,
course number, quarter, school, program description and notes. Every program you
write should include information like this. This is a good opportunity to start
developing effective code documentation skills. An example header comment is shown
below.
Lab 4 Page 3 of 11 Winter 2019 © 2019, Computer Engineering Department, University of California – Santa Cruz
##########################################################################
# Created by: #
#
#
# Assignment: #
#
#
Last Name, First Name CruzID
13 February 2019
Lab 64: Hello World
CMPE 012, Computer Systems and Assembly Language UC Santa Cruz, Winter 2019
This program prints ‘Hello world.’ to the screen. This program is intended to be run from the MARS IDE.
# Description: #
# Notes: ##########################################################################
Every block or section of code should have a comment describing what that block of
code is for. In-line comments should be lined up (using spaces) for ease of
readability.
Register Usage
Registers $s1 and $s2 shall contain the two 32-bit two’s complement integers entered
by the user. Register $s0 shall be used to store the 32-bit two’s complement sum.
You should try to use as few registers as possible. Try to only use $zero, $v0, $a0,
$s0-$s2, and the temporary registers, $t0-$t9. If you run out of registers, you may
use $s3-$s8. You may use $ra only if you are implementing subroutines (which is
beyond the scope of this lab).
At the beginning of your code, and optionally at the beginning of each block of code,
indicate the functionality of the registers used. For instance, if you are using $t0
and $t1 for the user input and a loop counter, respectively, your comments should
include something like the following:
White Space
Line up instructions, operands, and comments to increase readability. Code should be
indented from labels. Notice how the instructions, operands and comments are all
lined up in columns in the good example.
Bad Example
# REGISTER USAGE
# $t0: user input # $t1: loop counter
LOOP:
LI $t1 2 #initialize $t1
ADDI $t0 $t0 1 #increment $t0
BLT $t0 $t1 LOOP #determine if code should re-enter loop
Lab 4 Page 4 of 11 Winter 2019 © 2019, Computer Engineering Department, University of California – Santa Cruz
Good Example
A Note About Tabs
It is preferable to line up comments using spaces as opposed to tabs. Text editors
can have different standards for the width of one tab character. For this reason, it
is preferable to line up comments using spaces, not tabs, so that the code appears
the same regardless of text editor.
README.txt
This file must be a plain text (.txt) file. For full credit, it should contain your
first and last name (as it appears on Canvas) and your CruzID. Your answers to the
questions should total at least 150 words. Your README should adhere to the following
template:
LOOP:
LI $t1 2
ADDI $t0 $t0 1 BLT $t0 $t1 LOOP
# initialize $t1
# increment $t0
# determine if code should re-enter loop
————————
Lab 4: ASCII (HEX or 2SC) to Base 4 CMPE 012 Winter 2019
Last Name, First Name CruzID ————————-
What was your approach to converting each ASCII input to two’s complement form?
Write the answer here.
What did you learn in this lab? Write the answer here.
Did you encounter any issues? Were there parts of this lab you found enjoyable?
Write the answer here.
How would you redesign this lab to make it better? Write the answer here.
Did you collaborate with anyone on this lab? Please list who you collaborated with and the nature of your collaboration.
Write the answer here.
Syscalls
When printing the integer values, you may use syscall system services 4 (print string) and 11 (print character). You may not use the following syscalls:
1 (print integer)
5 (read integer)
12 (read character)
Lab 4 Page 5 of 11 Winter 2019 © 2019, Computer Engineering Department, University of California – Santa Cruz
34 (print integer as hexadecimal)
35 (print integer as binary)
36 (print integer as unsigned)
You will lose a significant number of points if you use any of these syscalls. See
the rubric for more details.
Input
In this lab you will obtain two user inputs, not using a syscall, but by using
program arguments. The user will enter two values separated by one space as indicated
above. These numbers will be sign-extended to 32 bits and stored in $s1 and $s2. Turn
on program arguments from the Settings menu as shown below.
When program arguments are entered and the code is run, $a0 initially contains the
number of program arguments entered (program arguments are separated by spaces). The
register $a1 initially contains an address that we will call Address 1. If we go to
Address 1 in memory, we will find another address, which we will call Address 2A. If
we go to Address 2A in memory, we will find the ASCII encoding for the first byte of
the first string entered in the program arguments.
Example Program Argument
In this example, there are four program arguments. We will call these Program
Arguments A, B, C, and D as follows:
Program Argument A: “Flux”
Program Argument B: “is”
Program Argument C: “a”
Lab 4 Page 6 of 11 Winter 2019 © 2019, Computer Engineering Department, University of California – Santa Cruz
Program Argument D: “bunny”
The value of $a0 has the value of 4 to match the 4 program arguments. We will call
the value in $a1 “Address 1.”
Address 1 should be found near the stack pointer. You can jump to the stack area of
the program by selecting “current $sp” from the drop down menu on the bottom of the
memory window.
If we go to the Address 1 in memory (0x7fffefec in this example), we will find the
first of 4 more addresses. We will call these addresses “Address 2A, 2B, 2C, and 2D.”
Address 2A is the location of the first character in Program Argument A, Address 2B
is the location of the first character in Program Argument B, and so on.
Lab 4 Page 7 of 11 Winter 2019 © 2019, Computer Engineering Department, University of California – Santa Cruz
If we go to Address 2A (0x7ffffff8 in this example), we will find the ASCII encoding
for first character of Program Argument A. If we go to Address 2B (0x7ffffff5 in this
example), we will find the ASCII encoding for first character of Program Argument B,
and so on. Check the ASCII box to display the data as ASCII characters. Note that
each program argument ends with a null character.
It is important that you do not hard-code the values for any of the addresses in your program.
Turn Off Delayed Branching
From the settings menu, make sure Delayed branching is unchecked.
Lab 4 Page 8 of 11 Winter 2019 © 2019, Computer Engineering Department, University of California – Santa Cruz
Checking this option will insert a “delay slot” which makes the next instruction
after a branch execute, no matter the outcome of the branch. To avoid having your
program behave in unpredictable ways, make sure Delayed branching is turned off. In
addition, add a nop instruction after each branch instruction. For example:
MIPS Memory Configuration
To find the program arguments more easily in memory, you may choose to develop your
program using a compact memory configuration (Settings -> Memory Configuration).
However, your program MUST function properly using the Default memory configuration. You should not run into issues as long as you do not hard-code any memory addresses in your program. Make sure to test your program thoroughly using the Default memory configuration.
Lab 4 Page 9 of 11 Winter 2019 © 2019, Computer Engineering Department, University of California – Santa Cruz
LI $t12 LOOP:
ADDI $t0 $t0 1
BLT $t0 $t1 LOOP
NOP # nop added after the branch instruction ADD $t3 $t5 $t6
Submission Instructions
This assignment will be submitted in two parts. Late hours will not be used for Part
A of the assignment. If you do not submit a diagram or pseudocode by the Part A
deadline, you can submit it with your Part B submission for less points.
Grading Rubric
Part A
2 pt pseudocode
1 pt pseudocode submitted by Part A deadline
2 pt block diagram (-1 for hand drawn diagram)
1 pt block diagram submitted by Part A deadline
Part B
2 pt assembles without errors
Note: No credit for prompts, program argument print, register values or
test cases if program doesn’t assemble
2 pt prompts match specification exactly (1 per prompt)
2 pt program argument print
6 pt register values
2 pt correct value stored in $s1
2 pt correct value stored in $s2
2 pt correct value stored in $s0
Note: -1 per register if not sign extended properly
16 pt test cases:
TEST CASE
PROGRAM ARGUMENT 1
PROGRAM ARGUMENT 2
TEST CASE
PROGRAM ARGUMENT 1
PROGRAM ARGUMENT 2
1
+
bin
+
bin
9
–
bin
+
bin
2
+
bin
+
hex
10
–
bin
+
hex
3
+
bin
–
bin
11
–
bin
–
bin
4
+
bin
–
hex
12
–
bin
–
hex
5
+
hex
+
bin
13
–
hex
+
bin
6
+
hex
+
hex
14
–
hex
+
hex
7
+
hex
–
bin
15
–
hex
–
bin
8
+
hex
–
hex
16
–
hex
–
hex
NOTE: No credit for this section if you use a forbidden syscall
(1, 5, 12, 34, 35, 36)
6 pt style and documentation
1 pt comment on register usage
1 pt useful and sufficient comments
1 pt labels, instructions, operands, comments lined up in columns
2 pt readme file complete (should total at least 150 words)
1 pt complete headers for code and README
Note: program header must include name, CruzID, date, lab name, course name, quarter, school, program description, note on
Lab 4
© 2019, Computer Engineering Department, University of California – Santa Cruz
Page 10 of 11 Winter 2019
program argument usage
README must include name, CruzID, lab name, course name
8 pt (extra credit) takes decimal input
Note: Test cases will include both positive and negative inputs with number
of digits varying from 1 to 3
-20 pt if your program only runs in a specific memory configuration
or memory addresses are hard-coded
Lab 4 Page 11 of 11 Winter 2019 © 2019, Computer Engineering Department, University of California – Santa Cruz