Recitation Session 3 CSE341 Computer Organization University at Buffalo radhakri@buffalo.edu February 19, 2016 CSE341 Computer Organization Recitation Session 3 1/21 Assembler code nrfact.asm Project 1 You need to get familiarized with several concepts before venturing into Project 1 CSE341 Computer Organization Recitation Session 3 2/21 Procedure Calls in MIPS You can think of a procedure like a spy who leaves with a secret plan, acquires resources, performs the task, covers his or her tracks, and then returns to the point of origin with the desired result 1 Put parameters in a place where the procedure can access them. 2 Transfer control to the procedure. 3 Acquire the storage resources needed for the procedure. 4 Perform the desired task. 5 Put the result value in a place where the calling program can access it. 6 Return control to the point of origin, since a procedure can be called from several points in a program. CSE341 Computer Organization Recitation Session 3 3/21 Procedure Calls in MIPS $a0-$a3: four argument registers in which to pass parameters $v0-$v1: two value registers in which to return values $ra: one return address register to return to the point of origin MIPS assembly language includes an instruction just for the procedures: it jumps to an address and simultaneously saves the address of the following instruction in register $ra. The jump-and-link instruction (jal) is simply written – jal Procedure What instruction do you execute return to original location after completing procedure? CSE341 Computer Organization Recitation Session 3 4/21 Stack Pointer in MIPS Useful when a compiler needs more registers for a procedure than the four argument and two return value registers The stack pointer is adjusted by one word for each register that is saved or restored. placing data onto the stack is called a push, and removing data from the stack is called a pop stacks grow from higher addresses to lower addresses. This convention means that you push values onto the stack by subtracting from the stack pointer. Adding to the stack pointer shrinks the stack, thereby popping values off the stack. CSE341 Computer Organization Recitation Session 3 5/21 Memory Layout - MIPS Assembly CSE341 Computer Organization Recitation Session 3 6/21 Addressing in Branches and Jumps The MIPS jump instructions have the simplest addressing. They use the final MIPS instruction format, called the J-type, which consists of 6 bits for the operation field and the rest of the bits for the address field. j 10000 # go to location 10000 CSE341 Computer Organization Recitation Session 3 7/21 Addressing in Branches and Jumps CSE341 Computer Organization Recitation Session 3 8/21 Addressing in Branches and Jumps - Further Reading Read through these concepts in the text and familiarize yourself with different addressing schemes Immediate addressing, where the operand is a constant within the instruction itself Register addressing, where the operand is a register Base or displacement addressing, where the operand is at the memory location whose address is the sum of a register and a constant in the instruction PC-relative addressing, where the branch address is the sum of the PC and a constant in the instruction Pseudodirect addressing, where the jump address is the 26 bits of the instruction concatenated with the upper bits of the PC CSE341 Computer Organization Recitation Session 3 9/21 Reading Assembly Programs Names that begin with a period like .data and .globl, are assembler directives that tell the assembler how to translate a program but do not produce machine instructions Names followed by a colon, such as str: or main:, are labels that name the next memory location If a line begins with a label, the assembler records in its symbol table the name of the label and the address of the memory word that the instruction occupies The .data directive tells the assembler to store the string in the programs data segment, and the .text directive tells the assembler to store the instructions in its text segment. CSE341 Computer Organization Recitation Session 3 10/21 Reading Assembly Programs Some assemblers also implement pseudoinstructions, which are instructions provided by an assembler but not implemented in hardware. Many pseudoinstructions could also be simulated with macros, but the MIPS assembler can generate better code for these instructions because it can use a dedicated register ($at) and is able to optimize the generated code. CSE341 Computer Organization Recitation Session 3 11/21 SPIM System Calls SPIM provides a small set of operating systemlike services through the system call (syscall) instruction. To request a service, a program loads the system call code into register $v0 and arguments into registers $a0$a3 (or $f12 for floating-point values) System calls that return values put their results in register $v0 (or $f0 for floating-point results). CSE341 Computer Organization Recitation Session 3 12/21 SPIM System Calls CSE341 Computer Organization Recitation Session 3 13/21 SPIM System Calls What does this Program print? CSE341 Computer Organization Recitation Session 3 14/21 String Copy Example CSE341 Computer Organization Recitation Session 3 15/21 String Copy Example CSE341 Computer Organization Recitation Session 3 16/21 String Copy Example CSE341 Computer Organization Recitation Session 3 17/21 String Copy Example CSE341 Computer Organization Recitation Session 3 18/21 String Copy Example CSE341 Computer Organization Recitation Session 3 19/21 Swap Integer Example CSE341 Computer Organization Recitation Session 3 20/21 Swap Integer Example CSE341 Computer Organization Recitation Session 3 21/21