Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.

Slides:



Advertisements
Similar presentations
1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Advertisements

The University of Adelaide, School of Computer Science
©UCB CS 161 Lecture 4 Prof. L.N. Bhuyan
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 5 MIPS ISA & Assembly Language Programming.
Computer Architecture CSCE 350
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
The University of Adelaide, School of Computer Science
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
The University of Adelaide, School of Computer Science
Lec 9Systems Architecture1 Systems Architecture Lecture 9: Assemblers, Linkers, and Loaders Jeremy R. Johnson Anatole D. Ruslanov William M. Mongan Some.
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
Informationsteknologi Saturday, September 29, 2007 Computer Architecture I - Class 41 Today’s class More assembly language programming.
Assembly Language II CPSC 321 Andreas Klappenecker.
1 Warning! Unlike the previous lessons, for today's lesson you will have to listen, think and even understand (for the exam, of course). Individuals with.
MIPS Assembly Language I Computer Architecture CPSC 321 Andreas Klappenecker.
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
Computer Architecture CPSC 321 E. J. Kim. Overview Logical Instructions Shifts.
Lecture 6: Procedures (cont.). Procedures Review Called with a jal instruction, returns with a jr $ra Accepts up to 4 arguments in $a0, $a1, $a2 and $a3.
Intro to Computer Architecture
S. Barua – CPSC 440 CHAPTER 2 INSTRUCTIONS: LANGUAGE OF THE COMPUTER Goals – To get familiar with.
RISC Concepts, MIPS ISA and the Mini–MIPS project
Computer Architecture - The Instruction Set The Course’s Goals  To be interesting and fun. An interested student learns more.  To answer questions that.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
Computing Systems Instructions: language of the computer.
CHAPTER 2 ISA Instructions (logical + procedure call)
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Computer Architecture Instruction Set Architecture Lynn Choi Korea University.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Computer Organization CS224 Fall 2012 Lessons 9 and 10.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
Lecture 4: MIPS Instruction Set
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
Chapter 2 CSF 2009 The MIPS Assembly Language. Stored Program Computers Instructions represented in binary, just like data Instructions and data stored.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
COMPUTER ORGANIZATION LECTURE 3: ISA YASSER MOHAMMAD.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José Nelson Amaral.
Lec 6Systems Architecture1 Systems Architecture Lecture 6: Branching and Procedures in MIPS Jeremy R. Johnson Anatole D. Ruslanov William M. Mongan Some.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
Computer Architecture & Operations I
Rocky K. C. Chang Version 0.1, 25 September 2017
Computer Architecture Instruction Set Architecture
Computer Architecture Instruction Set Architecture
Lecture 4: MIPS Instruction Set
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Procedures (Functions)
Procedures (Functions)
CSCI206 - Computer Organization & Programming
What's wrong with this procedure?
Instructions - Type and Format
Solutions Chapter 2.
MIPS Instructions.
ECE232: Hardware Organization and Design
The University of Adelaide, School of Computer Science
Instruction encoding The ISA defines Format = Encoding
Logical and Decision Operations
Chapter 2 Instructions: Language of the Computer part 2
Program and memory layout
Systems Architecture I
Computer Architecture
Presentation transcript:

Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow the next 6 steps: 1. Place parameters in a place the procedure can access them. 2. Jump to the procedure code. 3. Allocate storage needed for the procedure. 4. Perform the task. 5. Place the result(s) in a place the calling program can access. 6. Return to the point of origin. 1/17

Computer Structure - The Instruction Set (2) Registers for Procedures  MIPS software allocates the following of its 32 registers for procedure calling:  $a0 - $a3 : 4 argument registers  $v0 - $v1 : 2 return value registers  $ra : return address register to return to point of origin.  2 new instructions that support procedures are:  jal ProcedureAddress Jump and link, jump to the procedure and save the address of the following instruction in $ra.  jr register Jump register, jump to the address in the register. 2/17

Computer Structure - The Instruction Set (2) Using More Registers  If the compiler needs more than the 4 input and 2 output registers, it can use other registers.  But it must restore the values in those registers to the values before the procedure was called.  This is a case of register spill, register values are saved in memory.  The ideal structure for saving registers is the stack, values are pushed onto the stack before the procedure call and poped out after.  A register called the stack pointer ($sp ) points to the address in memory where the stack resides. 3/17

Computer Structure - The Instruction Set (2) Compiling a Leaf Procedure int leaf(int g,int h,int i,int j){ int f;// the arguments are in $a0-$a3, f=(g+h) - (i+j); //f is in $s0 return f; } Before continuing we will introduce a new instruction: addi $t0,$t0,10# $t0=$t0+10 add immediate (addi) has as one of its operands an immediate value. addi is of the I-type instructions and in fact gives the format its name. 4/17

Computer Structure - The Instruction Set (2) The Assembly Version subi $sp,$sp,12#make room for 3 items on stack sw $t1,8($sp) #save $t1 sw $t0,4($sp) #save $t0 sw $s0,0($sp) #save $s0 add $t0,$a0,$a1 # $t0=g+h add $t1,$a2,$a3 # $t1=i+j sub $s0,$t0,$t1 # f=$t0+$t1 add $v0,$s0,$zero # $v0=$s0 lw $s0,0($sp) #restore $s0 lw $t0,4($sp) #restore $t0 lw $t1,8($sp) #restore $t1 addi $sp,$sp,12#remove the room on the stack jr $ra #jump back to point of origin

Computer Structure - The Instruction Set (2) Picture of Stack  MIPS software offers 2 classes of registers: $t0-$t9 : 10 temporary registers, not saved by the procedure. $s0-$s7 : 8 saved registers, saved by the procedure. 6/17

Computer Structure - The Instruction Set (2) Nested Procedures  Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures.  Problems may arise now, for example the main program calls procedure A with the argument 3 in $a0 and the return address in $ra. Procedure A then calls procedure B with the argument 7 in $a0 and with its return address in $ra. The previous address saved in $ra is destroyed.  We must, somehow, preserve these values across calls.  Lets look at a translation of the recursive factorial function in C++.

Computer Structure - The Instruction Set (2) Factorial in C++ int fact(int n) { if(n < 1) return (1); else return (n * fact(n -1)); }  The function calls itself multiple times.  The argument n is in register $a0, which is saved on the stack along with $ra. 7/17

Computer Structure - The Instruction Set (2) Factorial in Assembly fact: subi $sp,$sp,8 # make room for 2 items sw $ra,4($sp)# push the return address sw $a0,0($sp)# push the argument n slt $t0,$a0,1 # test for n<1 beq $t0,$zero,L1 # if n>=1 goto L1 li $v0,1 # pseudoinstruction $v0=1 addi $sp,$sp,8 # pop 2 items off stack jr $ra The following is the recursive call to fact(n-1) L1: subi $a0,$a0,1 # n-- jal fact # call fact(n-1) lw $a0,0($sp) # return from fact(n-1) lw $ra,4($sp) # pop n and return address addi $sp,$sp,8 # pop 2 items off stack mult $v0,$a0,$v0 # return n * fact(n-1) jr $ra

Computer Structure - The Instruction Set (2) Procedure Frame  The stack above $sp is preserved ( נשמר ) by making sure that the callee ( פונקציה נקראת ) doesn't write above $sp.  $sp is preserved by adding exactly the amount subtracted from it.  All other registers are preserved by being saved on the stack.  The stack also contains local variables that don't fit into registers.  The segment of the stack containing the saved registers and local variables is called the procedure frame or activation record.

Computer Structure - The Instruction Set (2) Frame Pointer  Some MIPS software use the register $fp to point to the first word of the procedure frame or activation record. 9/17

Computer Structure - The Instruction Set (2) Static and Automatic Variables  C++ has two storage classes for variables automatic and static.  Automatic variables are local to a function and are deleted when the function exits.  Static variables exist across function calls. Global C++ variables are static, as well as any variables defined with the keyword static. All other variables are automatic.  MIPS software reserves a register called the global pointer or $gp. Static variables are accessed through this register. 10/17

Computer Structure - The Instruction Set (2) Immediate Operands  As mentioned before the I-format instruction contains a 16 bit constant called an immediate. Using I-type instructions avoids loading values from memory into a register. Examples of instructions which use immediate values are: multi $s0,$s1,4 # $s0=$s1*4 slti $t0,$s2,10 # $t0=1 if $s2<10  But if a constant is larger than 16 bits? MIPS provides an instruction lui that loads a 16 bit constant into the upper half of an register. In order to load the value: into $s0 we must perform: lui $s0,61 #61d = addi $s0,$s0,2304 # 2304d =

Computer Structure - The Instruction Set (2) Addressing in Branches and Jumps  j # go to location Jumps to the address in memory bits (opcode) 26 bits (address)  bne $s0,$s1,Exit # branch if $s0!=$s Exit 6 bits 5 bits 5 bits 16 bits (address)  Problem: Addresses have to fit into a 16-bit field, no program could be larger than 64KByte.  Solution: Specify a register which would be added to the branch address. But which register? 11/17

Computer Structure - The Instruction Set (2) PC-Relative Addressing  The Program Counter (PC) is a register that always contains the address of the current instruction being executed.  By using the PC as the register to add to the branch address we can always branch within a range of to bytes of the current instruction.  This is enough for most loops and if statements.  This is called PC-relative addressing.  Procedures are not usually within a short range of the current instruction and thus jal is a J-type instruction. 12/17

Computer Structure - The Instruction Set (2) Branch Offset in Machine Language  Lets look at a loop in assembly: Loop:.. bne $t0,$t1,Exit subi $t0,$t0,1 j Loop Exit:  The machine code of the bne instruction is:  The branch instruction adds 8 bytes to the PC and not 12 because the PC is automatically incremented by 4 when an instruction is executed.  In fact the branch offset is 2 not 8. All MIPS instructions are 4 bytes long thus the offset is in words not bytes. The range of a branch has been multiplied by 4. 13/17

Computer Structure - The Instruction Set (2) Branch Offset Example 48subi$t0,$t1, bne$zero,$t0,L L1: jr$ra  What is the machine code translation of bne ?  opcode: 5, the opcode of bne.  rs: 0, $zero is register #0.  rt: 9, $t0 is register #9.  address: (L1 - PC)/4 = ( )/4 = 20/4 = 5  And if L1 is at address 28?  address: (L1 - PC)/4 = ( )/4 = -28/4 = -7 14/17

Computer Structure - The Instruction Set (2) Pseudodirect Addressing  The 26-bit field in the jump instruction is also a word address. Thus it is a 28-bit address. But the PC holds 32-bits?  The MIPS jump instruction replaces only the lower 28 bits of the PC, leaving the 4 highest bits of the PC unchanged.  48jL L2: add …  What is the machine code translation of j ?  opcode: 2, the opcode of j.  address: PC || /17

Computer Structure - The Instruction Set (2) Addressing Mode Summary  Immediate addressing - the Operand is a constant  Register addressing - the Operand is a register  Base or displacement addressing - the operand is at the memory location whose address is the sum of a register and a constant in the instruction.  PC-relative addressing - the address is the sum of the PC and a constant in the instruction.  Pseudodirect addressing - the jump address is the 26 bits of the instruction concatenated ( מצורף ) to the upper bits of the PC. 16/17

 Immediate addressing - the Operand is a constant addi $t0,$t1,102 # $t0=$t andi $s0,$s0,16 # $s0=$s0 & 16  Register addressing - the Operand is a register addi $t0,$t1,102 andi $s0,$s0,16 jr $s4 # jump to the address in $s4  Base or displacement addressing - the operand is at the memory location whose address is the sum of a register and a constant in the instruction. lw $t0,20($gp) # $t0 = $gp[5] lb $t1,7($s5) # $t0 = $s5[7]  PC-relative addressing - the address is the sum of the PC and a constant in the instruction. beg $s0,$s1,Label # if $s0<$s1 jump to Label  Pseudodirect addressing - the jump address is the 26 bits of the instruction concatenated ( מצורף ) to the upper bits of the PC. j L37 jal strcmp

Computer Structure - The Instruction Set (2) Addressing Modes (picture) 17/17