You are on page 1of 31

The University of Texas at Dallas Th U i it f T t D ll

Erik Jonsson School of Engineering and g g Computer Science

Developing a Program in SPIM


This lecture follows up the two development examples in Lecture 15 with a longer, more complex loop program. Lecture 18 will feature a final exercise in program development, and the following class period will include an extended class programming exercise. The purpose of these three extended how-to sessions is to encourage every student to dive into the programming process. While classroom lecture can familiarize students with assembly language instructions and simple examples of instruction sequences, keep in mind that the only way to learn programming is to program. One of the hardest things to do when learning programming is to get into the mindset of doing the programs. As we go through this lecture remember that the techniques included lecture, here are only valuable if you apply them immediately.
Lecture #16: SPIM Programming Example
N. B. Dodge 09/09

The University of Texas at Dallas Th U i it f T t D ll

Erik Jonsson School of Engineering and g g Computer Science

Programming Problem
The desired program is defined as follows:
Th program will take keyboard input for a specific d i l The ill t k k b d i tf ifi decimal number. It will convert the decimal number to a hexadecimal number. The program will then print out the result on the SPIM simulated console.

This is a slightly modified version of the p g g y program in Waldron (former textbook): Chapter 7, Section 7.4, AN EXAMPLE PROGRAM.
2 Lecture #16: SPIM Programming Example
N. B. Dodge 09/09

The University of Texas at Dallas Th U i it f T t D ll

Erik Jonsson School of Engineering and g g Computer Science

Analysis y
A program to convert a decimal number to the equivalent hexadecimal number and print out the result must do the following:
1. Input the number from the keyboard. 2. Convert the decimal number to a hexadecimal number. 3. Convert each digit of the hex number to the ASCII symbol for that number (since printout with syscall 12 is only decimal). ( p y y ) 4. Store the ASCII code for each hex digit in a byte string (could also print out character-by-character using syscall 11). 5. Output the hexadecimal number using syscall 4.
3 Lecture #16: SPIM Programming Example
N. B. Dodge 09/09

The University of Texas at Dallas Th U i it f T t D ll

Erik Jonsson School of Engineering and g g Computer Science

Analysis (2)
There is some good news:
SPIM routinely converts characters input from the keyboard into y p y the binary value of the number, which it stores in $v0. Furthermore, this binary number is immediately readable as hex ( (in the console readout). ) Thus the number in $v0 is partitionable into 4-bit nibbles, that represent hexadecimal digits.

The task is then reduced to:


Analyzing each 4-bit nibble separately (as a single hex digit). Converting each hex digit into an equivalent ASCII character, storing the string character at a time, and printing the result.
4 Lecture #16: SPIM Programming Example
N. B. Dodge 09/09

The University of Texas at Dallas Th U i it f T t D ll

Erik Jonsson School of Engineering and g g Computer Science

Analysis (3)
Other considerations:
For simplicity, only fixed-point, integral numbers, (floatingpoint programming not covered i EE 2310) i t i t d in 2310). System call 5 will take any number of digits as an input. However, it truncates after the decimal number input reaches a size bigger than can be stored in a 32-bit fi d i t i bi th b t di 32 bit fixed-point representation (that is 0x ffff ffff, or about 4,300,000,000). Worse yet, it truncates the most significant bits. In order to forestall this problem we will limit inputs to eight (8) decimal digits. The biggest 8-digit number is 99,999,999 = 0x 05f5e0ff, so we will always be able to represent the decimal number properly in the conversion. b l i th i
5 Lecture #16: SPIM Programming Example
N. B. Dodge 09/09

The University of Texas at Dallas Th U i it f T t D ll

Erik Jonsson School of Engineering and g g Computer Science

Analysis (4)
The decimal-hex conversion routine can be stated as follows:
1. After keyboard input, store contents of $v0 in another register (must reserve $v0 for input/output functions). The number will already be the binary equivalent of the decimal number. 2. Analyze th number nibble b nibble as a h number. 2 A l the b ibbl by ibbl hex b 3. Convert each nibble to the equivalent ASCII character for that hex number, storing the ASCII character in a reserved byte string location location. 4. When all eight nibbles (i.e., the eight hexadecimal numbers) of the 32-bit MIPS input data word are analyzed, output the resulting ASCII string as the hex number equivalent to the g g q decimal number that was input from the keyboard.
6 Lecture #16: SPIM Programming Example
N. B. Dodge 09/09

The University of Texas at Dallas Th U i it f T t D ll

Erik Jonsson School of Engineering and g g Computer Science

Analysis (5)
Syscall 4 prints out characters starting at the address in $a0, and moves upward in memory sequence, printing out characters until it reaches a null). This suggests that the storage of hex numbers must start with the most significant, which can then be stored in a reserved memory space, starting at the lowest address. The digits will then be printed out in correct order. Due to the restriction of 8 decimal digits (99,999,999 max. size), the left most left-most digit will always be 0. If the input number is much smaller, more digits of the result will be 0. Leading 0s can be eliminated by testing the value and removing them until a non zero number is found, then proceeding with the non-zero analysis (will want to keep 0s interior to the number!).
7 Lecture #16: SPIM Programming Example
N. B. Dodge 09/09

The University of Texas at Dallas Th U i it f T t D ll

Erik Jonsson School of Engineering and g g Computer Science

Analysis (6)
The eight-digit hex number can be analyzed digit-bydigit as follows:
Use logical AND to mask off or isolate each 4-bit hex number. The masked result will be a 32-bit number with all 0s except where the 4-bit mask preserved 4 bits. To simplify the loop, the mask should always be in the same place. Use a shift or rotate instruction to get each succeeding 4-bit nibble positioned under our mask.
8 Lecture #16: SPIM Programming Example
N. B. Dodge 09/09

The University of Texas at Dallas Th U i it f T t D ll

Erik Jonsson School of Engineering and g g Computer Science

Analysis (7)
Doing a rol 4 then an andi using a 0x 0000 000f mask will result in one hex number (nibble) in the four least significant bit positions, and further, allows analysis of the number most mostsignificant hex digit first. The number being analyzed must be placed in the four least significant bit locations. By doing this, the four bits can be analyzed as the single hex digit that they represent (since the other 28 bit positions are all zero).
0x 0000 d000 0x 0000 000d This number is NOT a hex d d This number is a hex d

Then we can store the ASCII equivalent of each digit, mostsignificant-digit first, so that the SPIM print routine will print first out the digits in the correct numeric order.
9 Lecture #16: SPIM Programming Example
N. B. Dodge 09/09

The University of Texas at Dallas Th U i it f T t D ll

Erik Jonsson School of Engineering and g g Computer Science

Program Sequence
With preliminary analysis complete, the basic task sequence is:

10

Input number (already in hex/binary form). Move number to another register. i Rotate left most significant 4 bits to right-most 4 bit position. Mask off those 4 bits and store in another register. (Result is one hex digit [4-bit binary number] in right 4 bit positions.) Preliminary loop eliminates leading zeroes until a non-zero character encountered. Convert bi C t binary numbers t equivalent hex ASCII character. b to i l th h t Store character in the reserved memory string for later print-out. Repeat for each 4-bit number until remaining digits are converted. When th l Wh the loop completes, print out th h d i l number. l t i t t the hexadecimal b
Lecture #16: SPIM Programming Example
N. B. Dodge 09/09

The University of Texas at Dallas Th U i it f T t D ll

Erik Jonsson School of Engineering and g g Computer Science

Basic Flow Chart


Input number, move from $v0 to $tx $ Rol 4 $tx, AND with 0xf, store in $ty y Leading 0? Yes No

Rol 4 $tx, AND with 0xf, store in $ty

Convert the 4-bit # in $ty to ASCII char. representing that #

Decrement counter ($tz)

No Count = 0? Yes Load address of byte string representing hex number in $a0 Print out ASCII representation of hex number
N. B. Dodge 09/09

11

Lecture #16: SPIM Programming Example

The University of Texas at Dallas Th U i it f T t D ll

Erik Jonsson School of Engineering and g g Computer Science

Loop Mechanics
With the basic program flow laid out, the hex digit analysis loop must be designed. designed
Since each 32-bit binary MIPS number is equivalently 8 hex digits, must go through the loop 8 times (remember that at least the first digit will always be a leading zero). This requires a counter. As the ASCII characters will be printed at the end of the program, need to reserve a print string. This requires a pointer to point to the p g q p p current character storage location so that characters are stored in the correct order (MSB first, and then in descending order). Will need to increment both counter and pointer.

12

Lecture #16: SPIM Programming Example

N. B. Dodge 09/09

The University of Texas at Dallas Th U i it f T t D ll

Erik Jonsson School of Engineering and g g Computer Science

Data Declarations
Data declarations:
Prompt command. Answer announcement. Reserved space for ASCII string being formed. p g g

Thus:

13

prompt: answer: result: return:

Enter decimal number (1-8 digits): Hexadecimal H d i l number is: b i .space 8 # this reserves 8 byte location .asciiz \n # we end with return, for neatness!
Lecture #16: SPIM Programming Example
N. B. Dodge 09/09

The University of Texas at Dallas Th U i it f T t D ll

Erik Jonsson School of Engineering and g g Computer Science

Register Assignment
A b f As before, use t registers, since they are the customary it i th th t safe registers. Register assignment:
Counter Use $t0 Register to hold hex digit being analyzed Use $t1 Register to hold original number input Use $t2 Pointer to the current byte storage location in the print buffer (holds ASCII representation of each hex digit) Use $t3

14

Lecture #16: SPIM Programming Example

N. B. Dodge 09/09

The University of Texas at Dallas Th U i it f T t D ll

Erik Jonsson School of Engineering and g g Computer Science

Loop Flow with Register Designations


Input number, move from $v0 to $t2 $ Rol 4 $t2, AND with 0xf, store in $t1 Leading 0? Yes No

Rol 4 $t2, AND with 0xf, store in $t1

Convert 4-bit # in $t1 to ASCII and store in print buffer

$t0 $t0+1, $t3 $t3+1

No Loop count = 8?
15

Yes

Load address of byte string representing hex number in $a0

Print out ASCII representation of hex number


N. B. Dodge 09/09

Lecture #16: SPIM Programming Example

The University of Texas at Dallas Th U i it f T t D ll

Erik Jonsson School of Engineering and g g Computer Science

Converting to Hex
The big question: How to convert a hexadecimal digit to the ASCII code for the character representing that digit? Consider hexadecimal C id h d i l numbers 0 9 (0000 1001) b 0-9 (0000-1001):
The ASCII code for 0 is 0x 30 (decimal 48) The ASCII code for 1 is 0x 31 (decimal 49) Etc., to 9, whose ASCII code is 0x 39 (decimal 57) The ASCII code for hex numbers 0 to 9 (binary 0000 to 1001) is exactly decimal 48 (hex 30) greater than the number.

Therefore we can define the formula: nASCII =n+48 for a decimal number n.

16

We can therefore convert any hex number 0 to 9 to the ASCII code for that f th t number b adding 48. b by ddi 48
Lecture #16: SPIM Programming Example
N. B. Dodge 09/09

The University of Texas at Dallas Th U i it f T t D ll

Erik Jonsson School of Engineering and g g Computer Science

Converting to Hex ( ) g (2)


What about hex numbers a-f?
Clearly there must be some value to add to hex numbers a f Clearly, a-f that results in the ASCII code for those same numbers. In fact, the ASCII code for A is 7 greater than the ASCII code for 9. Since A-F are closer numerically to 0-9, we use capital letters rather than small letters to represent hex A-F. The loop must test each hex number to see if it is 9 or less. p If hex 9 or less add 48 to get the ASCII code for the number. If hex A-F, add (48+7) to get the ASCII code for the number. We also need to make a provision for number = 0. p
17 Lecture #16: SPIM Programming Example
N. B. Dodge 09/09

The University of Texas at Dallas Th U i it f T t D ll

Erik Jonsson School of Engineering and g g Computer Science

Final Loop Flow Chart


Input number, move from $v0 to $t2 $ Rol 4 $t2, AND with 0xf, store in $t1 Leading 0? No Yes Eighth 0? No Yes

Yes Rol 4 $t2, AND with 0xf, store in $t1 # = 0-9? No Loop count = 8?
18

No

Add 7 to #

Add 48 to #

$t0 $t0+1, $t3 $t3+1

Store 0 as first buffer character

Yes

Load address of byte string representing hex number in $a0

Print out ASCII representation of hex number


N. B. Dodge 09/09

Lecture #16: SPIM Programming Example

The University of Texas at Dallas Th U i it f T t D ll

Erik Jonsson School of Engineering and g g Computer Science

Writing the Program


Although there are probably a few niceties that could be added to the program flow chart at this point, we have enough i f h h information at thi point t go ahead and ti t this i t to h d d write the program. The following program is written for PC SPIM. As noted before, the program is written in the Windows text editor Notepad, in order to avoid the formatting characters inserted by Word (could also use g y ( Word and store as text file or use Mipster, etc.).
First we write program header information (mainly for us). We then define our data with data declarations. Finally, we write the program segments, including the loop(s).
19 Lecture #16: SPIM Programming Example
N. B. Dodge 09/09

The University of Texas at Dallas Th U i it f T t D ll

Erik Jonsson School of Engineering and g g Computer Science

Program Prolog
## ## ## ## ## ## ## ## ## Lecture 16 Demo Program 1 Hex Number Conversion 1, Prints out hexadecimal value of a decimal number. $t0 - Loop counter $t1 - Holds each hex digit as analyzed $t2 - Holds number input from keyboard $t3 - Pointer to current address in print buffer Program name and d d description i ti Definition of register roles it l

20

Lecture #16: SPIM Programming Example

N. B. Dodge 09/09

The University of Texas at Dallas Th U i it f T t D ll

Erik Jonsson School of Engineering and g g Computer Science

Data Declarations
.data data result: .space 8 Reserves 8 spaces for output string * Carriage return needed after each output return: .asciiz "\n" Defines a byte prompt: .asciiz "Enter decimal number (1-8 string to be used decimal digits): " as the prompt to answer: .asciiz "Hexadecimal number is " solicit a decimal
Defines a byte string to be used to precede the answer number entry Defines what follows as data to be used in program p g

* We only need 7 spaces, but since .space clears the locations to 0, this ensures that the string is null-terminated.
21 Lecture #16: SPIM Programming Example
N. B. Dodge 09/09

The University of Texas at Dallas Th U i it f T t D ll

Erik Jonsson School of Engineering and g g Computer Science

Pre-Loop Software
.text main: nop p la $a0,prompt li $v0,4 syscall li $v0,5 syscall move $t2,$v0 la $a0,answer li $v0,4 $v0 4 syscall move $t0,$0 la $t3,result

Text declaration
# Load print out of prompt sequence # Output prompt to enter decimal number # Read integer to convert to hex Outputs p p prompt: p "Enter decimal number (1-8 decimal digits): " Sequence to input the 8-digit decimal number and store it as a binary number in $t2

# Output answer header

Outputs answer preceder as shown

# Clear counter Initiate counter and # Set pointer to first empty location in buffer buffer pointer

22

Lecture #16: SPIM Programming Example

N. B. Dodge 09/09

The University of Texas at Dallas Th U i it f T t D ll

Erik Jonsson School of Engineering and g g Computer Science

Loop Software
elim: rol $t2,$t2,4 position and $t1,$t2,0xf bgtz $t1,num addi $t0,$t0,1 $t0 $t0 1 beq $t0,8,zero j elim rol $t2,$t2,4 and $t1,$t2,0xf # Left rotate left digit to right-most # "Mask off" left-most digit # If a non-zero go to character conversion routine # Since character = 0 increment 0, # counter # If 8 zeroes, loop done; go to print # Get next character # Left rotate left digit to right-most # position # "Mask off" left-most digit

loop:

23

Lecture #16: SPIM Programming Example

N. B. Dodge 09/09

The University of Texas at Dallas Th U i it f T t D ll

Erik Jonsson School of Engineering and g g Computer Science

Loop Software (2)


num: conv: ble $t1,9,conv add $t1,$t1,7 add $t1,$t1,48 sb $t1,($t3) addi $t3,$t3,1 addi $t0,$t0,1 $t0 $t0 1 blt $t0,8,loop j print # Go to conv routine directly if # hex # 0-9 # Add 7 because hex number is a-f # Convert number to ASCII # Store ASCII representation of hex # number in buffer b i b ff # Increment pointer point to next # location in buffer # Increment counter # If analys not complete, do loop again # Analysis complete; go to print routine

24

Lecture #16: SPIM Programming Example

N. B. Dodge 09/09

The University of Texas at Dallas Th U i it f T t D ll

Erik Jonsson School of Engineering and g g Computer Science

Final Printout Software


zero: print: li $t0,0x30 sb $t0,0($t3) la $a0,result li $v0,4 syscall la $a0,return li $v0,4 syscall li $v0, 10 syscall # If number was 0, put 0 in # print buffer # Print out ASCII string of hex # numbers # Print out carriage return for # neatness # End program

The program listing follows


25 Lecture #16: SPIM Programming Example
N. B. Dodge 09/09

## ## ## ## ## ## ##

Lecture 16 Demo Program 1, Hex Number Conversion Prints out hexadecimal value of a decimal number. t0 - Loop counter t1 - Holds each hex digit as analyzed t2 - Holds number input from keyboard t3 - Pointer to current address in print buffer

.text main: nop i la $a0,prompt li $v0,4 syscall li $v0,5 $ 05 syscall move $t2,$v0 la $a0,answer li $v0,4 $ 04 syscall move $t0,$0 la $t3,result

# Load print out of prompt sequence # Output prompt to enter decimal number # R d integer to convert to hex Read i h # Output answer header # Clear counter # Set pointer to first empty location in buffer

elim: rol $t2,$t2,4 and $t1,$t2,0xf bgtz $t1,num addi $t0,$t0,1 $t0 $t0 1 beq $t0,8,zero j elim loop: rol $t2,$t2,4 and $t1,$t2,0xf num: ble $t1,9,conv add $t1,$t1,7 , , conv: add $t1,$t1,48 sb $t1,($t3) addi $t3,$t3,1 addi $t0,$t0,1 $t0 $t0 1 blt $t0,8,loop j print

# Left rotate left digit to right-most position # "Mask off" left-most digit # If a non-zero go to character conversion routine # Since character = 0, increment counter 0 # If 8 zeroes, loop done; go to print # Get next character # Left rotate left digit to right-most position # "Mask off" left-most digit # Go to conv routine directly if hex # 0-9 # Add 7 because hex number is a-f # Convert number to ASCII # Store ASCII representation of hex number in buffer # Increment pointer point to next location in buffer # Increment counter # If analys not complete, do loop again # Analysis complete; go to print routine

zero: li $t0,0x30 sb $t0,0($t3) print: la $ 0 i t l $a0,result lt li $v0,4 syscall la $a0,return li $v0,4 $ 04 syscall li $v0, 10 syscall .data result: return: prompt: t answer: #

# If number was 0, put 0 in print buffer # Print out ASCII string of h numbers P i t t t i f hex b # Print out carriage return for neatness # End program

.space 8 .asciiz "\n" .asciiz "Enter decimal number (1 8 d i l digits): " ii "E t d i l b (1-8 decimal di it ) .asciiz "Hexadecimal number is "

End of file Lecture 16 Demo Program 1

The University of Texas at Dallas Th U i it f T t D ll

Erik Jonsson School of Engineering and g g Computer Science

Summary
We have AGAIN gone through the thought process to define, flow-chart, code, and check out a MIPS assembly-language assembly language program involving a loop. loop In order to properly learn SPIM, each student MUST p do this process a number of times. It is the ONLY WAY to learn MIPS assembly language programming. My experience is that unless each student both runs i i classroom demo programs (single step) and codes and p g executes 10+ programs on his/her own , chances of passing EE 2310 are reduced dramatically!
29 Lecture #16: SPIM Programming Example
N. B. Dodge 09/09

The University of Texas at Dallas Th U i it f T t D ll

Erik Jonsson School of Engineering and g g Computer Science

Exercise 1
Now lets do one of those 20 programs!
1. 0x84c732d6 is loaded into $t0. 2. Construct a program to output each byte as a signed number. The text and data declarations are done. 3. Hints: b t l d d f 3 Hi t a byte loaded from memory with the msb = 1 is automatically sign-extended. $t1 will be used as a counter. 4. Which bytes above will be output as negative numbers? Dont worry about the value of the decimal numbers that are printed. b h i d Hex bytes 84, c7, and d6.
30

main:

.text li $t0,0x84c732d6 li $t1,4

data1:

li $v0,10 syscall .data .space 1 space


N. B. Dodge 09/09

Lecture #16: SPIM Programming Example

The University of Texas at Dallas Th U i it f T t D ll

Erik Jonsson School of Engineering and g g Computer Science

Homework
As usual:
Write down the two or three most important things learned today and add to your list. Write down two or three concepts or ideas that are not clear. After reviewing them, if you still have questions, see me during office hours.

Review this lecture carefully. Continue work on homework #6. #6 Also, copy or list Lecture 16 Demo Program 1 into Notepad, and run in PCSPIM (especially single-step!).
31 Lecture #16: SPIM Programming Example
N. B. Dodge 09/09

You might also like