lab03.Handout

docx

School

University of California, Berkeley *

*We aren’t endorsed by this school

Course

MISC

Subject

Computer Science

Date

Feb 20, 2024

Type

docx

Pages

7

Uploaded by CaptainThunderGoldfish14

Report
Kettering University Computer Architecture and Organization Lab Exercise 3 Design of MIPS Programs Nested Procedures Winter 2023 Note Tests/final may contain questions about lab exercises. Prelab : Go over the handout rigorously. Lab Report: Upload one handout (report) per group (in .pdf ) to Blackboard before 11:59 pm on the coming Sunday and after you have done all the assignments, answered all the questions, and shown your lab work to the lab instructor individually . A demo sign-up sheet will be posted if necessary. Name : Lab Partner’s Name : Purpose of this lab exercise (please use your own words)
CE-422/622 Lab Exercise 3 Objectives Examine how MIPS supports nested procedures. More specifically, How called and caller programs may communicate with each other. How to jump to a procedure and save the return address. How to use a stack. How/when to save/restore registers. How to return to the calling program. Introduction You will implement nested procedures in the MIPS assembly language: The system program uses the { jal main } instruction to call your first-level main program. Your main program then calls procedure xcount, and procedure xcount calls procedure xfind , as explained in this handout. What to hand in Upload this handout in the .pdf format after completing all the assignments and answering all the questions. Show your functional nested procedures in Assignment 2. to the lab instructor. Also, be prepared to individually answer the lab instructor’s questions rearguing today’s lab exercise and what your group did. You should work closely with your lab partner. You are also urged to talk to other students, teach them, or learn from them, as this will enhance your performance; however, do NOT copy from them! Please note: We can enter a procedure ONLY with a jal instruction. We can exit a producer ONLY with a jr instruction. Otherwise, it would be a MAJOR mistake. Students making these mistakes will not get credit for this lab. Page 2 of 7
CE-422/622 Lab Exercise 3 Assignments Create a lab03 folder in your Architecture directory. Then make a text file called nested.txt in lab03 . Place all three codes to be created today in nested.txt . 1. Procedure xfind Write procedure xfind in MIPS assembly language that takes two arguments, $a0 and $a1 . Register $a0 is a pointer to (the base of) a null-terminated ASCII character string, and $a1 has an ASCII character in its least significant byte. The other three bytes of $a1 are reset to zero. xfind should locate the first matching character in the string, return its address in $v0 , and return the character itself in $v1 . If the search is unsuccessful, xfind should return a pointer ( $v0 ) to the null character and reset register $v1 . Use the following main program to test your xfind . Place both codes in the same file and call it nested.txt . Note : If you copy and paste a .doc program into your text file (source code), delete all the single and double quotes (‘ ”), if any, and type them again. . data string : . asciiz “This is a null-terminated ascii string.” . text main : addi $sp, $sp, -4 # update $sp and then sw $ra, ($sp) # push $ra (return address to system Prog.); this is the first push on the stack la $a0, string # $a0 becomes a pointer to the base of string li $a1, ’i’ # look for ‘i’. ‘i’ sits in LSByte of $a1, the other 3 bytes are reset to zero. jal xfind # call xfind lw $ra, 0($sp) # pop $ra ($ra becomes return address to system program) addi $sp, $sp, 4 # restore $sp return : jr $ra # return to system program Once you have tested your xfind successfully, copy your commented and indented xfind to the space reserved for Figure 3.. 2. Procedure xcount Before you proceed with this section, test your xfind and make sure it works. Write procedure xcount in MIPS assembly language that takes two arguments, $a0 and $a1. Register $a0 is a pointer to (the base of) a null-terminated ASCII character string, and $a1 has an ASCII character in its least significant byte; the other three bytes of $a1 are cleared. This procedure should return in $v0 the total number of instances of the matching character in the string. You should use your xfind in your xcount . Add xcount to file nested.txt . To test the whole system, use the same main code you already have in nested.txt (and used in Assignment 1.) after you replace jal xfind with jal xcount . Figure 1. shows the communication between the caller/callee pairs graphically. What is the best place to put a breakpoint when you use the RUN command (F5)? Note that xcount is a non-leaf procedure; therefore, you need to save the return address ( $ra ) before xfind is called; otherwise, the return address to the main program will be lost. Also, use $s0 as the “hit-counter” in xcount . So, you need to save $s0 as well. These registers must be restored when xcount ends and before it returns to its calling ( main ) program. Page 3 of 7
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Total number of instances of the character character pointer Sys jal main nop a0 a1 a2 a3 main Call Return $a1 0 i 0 0 pointer character pointer character a0 a1 a2 a3 xcount v0 v0 v1 xfind v1 CE-422/622 Lab Exercise 3 Once you have tested the nested procedures successfully, copy your commented and indented xcount to the space reserved for Figure 3. and right after your xfind . Show your functional nested procedures to the lab instructor. Figure 1. Communication between caller/callee pairs. 3. System stack Remember that stack is part of the memory, and every memory location has four attributes: The  value, content, or number sitting in that location. The value changes over time. The  meaning  of that value, e.g., instruction, data (such as an addition operand), address of a memory location.  The  address  of the location, which is fixed and unique, i.e., the address does not change over time; also, no two locations may have the same address. The size of the location. Take the following steps to obtain the first three attributes of each stack word that the nested procedures use and show them in Figure 2.. For example, you may find out that the   value  in a stack location is   0x8000 001C , its meaning is the return address to the main, and its  address is 0x7FFF 0000 . Look at main , xfind, and xcount to see which registers and in what order are pushed on the stack at the deepest point, which happens inside xfind . Then fill out the meaning column in Figure 2. (e.g., return address to system program ). Load nested.txt , all three programs ( main , xcount, and xfind ) into spim , and then step through the instructions (using F10) until you reach the first instruction in the main . Read the content of $sp (shown in the register pane of the main window of spim ) and type it in the $sp , shown in Figure 2.. Remember, the first item will be written in the stack at { this address – 4 }. Based on this address, fill out the address column in Figure 2.. Continue to use the step key (F10) and stop right after all the push and the corresponding update $sp instructions in xfind . spim shows the system stack in the Data Segment under the Data tab in the main window. Check this out, and then type the content of each stack location (used by your procedures) in the Content column in Figure 2.. Page 4 of 7
0x $sp Address grows Stack grows The is passed by reference from to xfind via register The is passed by value from main to via register ... CE-422/622 Lab Exercise 3 Figure 2. Stack to be filled out. Question 1 What would you do if you needed more than 4 output (or 2 input) communication registers? Question 2 Calling and called programs may pass information by value and by reference . Fill in the blanks to give one example of each technique that you used in today’s assignments: Question 3 Two (out of …) common communication methods are through the stack and registers . Let us say that when an interrupt happens, the ISR is supposed to increment a counter. So, starting with 0, if the interrupt occurs five times, the counter will reach 5. Here is the question: Can the ISR use one of these communication methods to let the interrupted program know what the content of the counter is? Highlight Yes or No . If your answer is Yes , give an example. If your answer is No , explain your reason and then propose a solution: Page 5 of 7
CE-422/622 Lab Exercise 3 Question 4 R28 of MIPS is called Pointer to Global Area ( gp ). Briefly but clearly explain what it is for: Question 5 In this lab exercise, did you use the stack for communication purposes? Highlight Yes or No . If you did, give an example: Question 6 Think Critically : Consider the following two pairs of instructions that a subroutine may use to save register $s0 : addi $sp, $sp, -4 sw $s0, ($sp) or sw $s0, -4($sp) addi $sp, $sp, -4 Does it matter which one is used? Highlight Yes or No . If it does, briefly but clearly explain your reason: Page 6 of 7
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
CE-422/622 Lab Exercise 3 Figure 3. Commented and indented xfind and xcount to be placed after this line Page 7 of 7