CS/COE 447: Spring 2010 Lab 4 YOUR NAME: YOUR PARTNER'S NAME: Each of you should hand in a hardcopy of your own solution. This lab will give you practice with memory allocation; labels; loading and storing bytes and words; character data type; and printing. It will help with programming assignment 1. Resources for this lab: Appendix B ascii table on the course web page hex <--> decimal converter might be useful (on the course web page) http://www.cs.pitt.edu/~wiebe/courses/CS447/Sp10/lab4.asm PART 1: For printing, the information you need is on pages B-43 and B-44. It's important to note that the printing facility is provided by Mars.jar. Printing is not part of the assembly language or instruction set architecture. ==Assemble lab4.asm and look at the symbol table (select under "settings"), and also at what has been stored in memory. Let's figure out why the labels stand for those numbers. The .asciiz creates a null-terminated string in memory. Null is simply ascii 0x00. Each character is one byte (8 bits; 2 hex digits). To save you busy work, we looked up the ascii values of the characters in the strings and included them in comments in lab4.asm: msg1: .asciiz "First Message" # hex: 46 69 72 73 74 20 4D 65 73 73 61 67 65 00 space: .asciiz " " # hex: 20 00 return: .asciiz "\n" # hex: 0A 00 msg2: .asciiz "The value of $t0 is" # hex: 54 68 65 20 76 61 6C 75 65 20 6F 66 20 24 74 30 20 69 73 00 Q: In "First Message", what memory location: contains the 'F'? contains the final 'e'? contains the space? Q: How many bytes are required to store the string whose address is 'space'? Q: Where is that string stored? (i.e., 'space') Q: Where is the first character of 'msg2' stored in memory? Q: Where is the first number in 'nums' stored? Consider both ways to confirm your answer to this question: (1) look up the answer in the symbol table; (2) in the data segment display, what are the row and column numbers? Q: What did the .align do? Give a general description, and then a specific one which mentions particular addresses. == Now step through the first block of code: addi $t0,$zero,55 addi $v0,$zero,4 la $a0,msg2 syscall la $a0,space syscall addi $v0,$zero,1 addi $a0,$t0,0 syscall addi $v0,$zero,4 la $a0,return syscall This is an example of printing strings and integers. Read pages B-43 and B-44 and make sure you understand how to print strings and integers. Ask questions, if needed. There is nothing to write down for this section. == Starting on page B-50, Appendix B goes through each individual instruction, including its machine code. You can look up the instructions on the green card and on these pages, to figure out how they work. Load and store operations (lw, sw, lbu, sb in this code) work as follows (leaving out the details of sign extending the immediate value): load: rt, number(rs) rt <-- M[number + rs] That is, take the contents of register rs, add number to it, and you have the address of a memory location. Then, retrieve the value stored in that memory location, and put it into register rt. Before stepping through the next block of code write down what you think will be placed in what memory location or register by each of the instructions. Memory Location or Register Value la $t0,nums lw $t1,12($t0) lw $t2,4($t0) lw $t3,0($t0) addi $t3,$t3,8 sw $t3,0($t0) sw $t2,12($t0) lbu $t5,0($t0) lbu $t6,1($t0) sb $t5,24($t0) Now run the code, check and fix your answers, discuss and/or ask questions, as needed. ==Step through the next block of code, tracing through which values are stored in which registers and memory locations. Make sure both of you understand what you are seeing as the program is executed. la $t0,msg1 lbu $t3,0($t0) lbu $t4,1($t0) la $t5,nums sb $t3,12($t5) addi $t3,$t3,2 sb $t3,13($t5) addi $t3,$t3,2 sb $t3,14($t5) addi $t3,$t3,2 sb $t3,15($t5) sw $t3,15($t5) Q: There is one question for this section: This block of code will throw an exception. Why? What is the error? Remove the offending instruction and re-assemble the code, so you can execute the last section. == Consider these next blocks of code: addi $v0,$zero,4 la $a0,msg1 syscall addi $v0,$zero,4 la $a0,return syscall la $a0,msg1 addi $v0,$zero,1 syscall addi $v0,$zero,4 la $a0,return syscall Q: before running the above code: what do you think will be printed by the first syscall? what do you think will be printed by the third syscall? Now run the code, check and fix your answers, discuss and/or ask questions, as needed. == Before running the next block of code, trace through it, and predict what will be printed. Use a format for tracing the code that works well for you (i.e., don't confuse yourself). la $t0,msg1 lbu $t2,2($t0) addi $t2,$t2,2 sb $t2,2($t0) la $a0,msg1 addi $v0,$zero,4 syscall Now run the code, check and fix your answers, discuss and/or ask questions, as needed. ==Finally, the program ends here: .data chars: .byte 0x41,0x42,0x43,0x44,0x45,0 .text la $t0,chars la $a0,chars addi $v0,$zero,4 syscall addi $v0,$zero,4 la $a0,return syscall addi $t1,$zero,0x46 sb $t1,5($t0) sb $zero,6($t0) la $t0,chars addi $v0,$zero,4 syscall Q: Before running the code: what do you think the first syscall will print? What values are stored where by the addi and two sw instructions? What do you think the second syscall will print? Now run the code, check and fix your answers, discuss and/or ask questions, as needed. PART 2: The following data segment defines 2 different 50-byte long regions of data, which we will use as buffers to store strings: .data buf1: .space 50 buf2: .space 50 For Part 2, you will write a MIPS program that copies a string from one buffer to another, ignoring whitespaces. First, write MIPS code that prompts the user for a string and stores it in buf1. Then, write MIPS code that copies each of the characters in buf1 to buf2, but ignoring whitespaces. You will have to keep two separate pointers to each buffer, because one might be moving faster than the other one. Finally, write MIPS code that prints the contents of buf2. You can assume that the whitespaces are tab, space, and newline.