1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.

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
Lecture 20: 11/12/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
©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.
10/6: Lecture Topics Procedure call Calling conventions The stack
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
MIPS Calling Convention Chapter 2.7 Appendix A.6.
Lecture 8 Sept 23 Completion of Ch 2 translating procedure into MIPS role of compilers, assemblers, linking, loading etc. pitfalls, conclusions Chapter.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
The University of Adelaide, School of Computer Science
Computer Architecture CSCE 350
MIPS Function Continued
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
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
CS3350B Computer Architecture Winter 2015 Lecture 4
Procedure call frame: Hold values passed to a procedure as arguments
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.”
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
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
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/
Memory/Storage Architecture Lab Computer Architecture MIPS Instruction Set Architecture ( Supporting Procedures )
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
MIPS function continued. Recursive functions So far, we have seen how to write – A simple function – A simple function that have to use the stack to save.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Character Data and 32-bit Constants (Lecture #20) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying.
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.
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.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 8: MIPS Procedures and Recursion.
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.
Function Calls in Assembly MIPS R3000 Language (extensive use of stack) Updated 7/11/2013.
Rocky K. C. Chang Version 0.1, 25 September 2017
Computer structure: Procedure Calls
Lecture 5: Procedure Calls
Computer Architecture & Operations I
Lecture 6: Assembly Programs
Lecture 4: MIPS Instruction Set
Procedures (Functions)
Procedures (Functions)
CSCI206 - Computer Organization & Programming
Instructions - Type and Format
MIPS Instructions.
CSCI206 - Computer Organization & Programming
The University of Adelaide, School of Computer Science
10/4: Lecture Topics Overflow and underflow Logical operations
MIPS function continued
Program and memory layout
Lecture 6: Assembly Programs
Systems Architecture I
MIPS function continued
Computer Architecture
Where is all the knowledge we lost with information? T. S. Eliot
MIPS function continued
Presentation transcript:

1 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. We must preserve these values across calls. Lets look at a translation of the recursive factorial function in C.

2 Factorial in C int fact(int n) { if(n < 1) return (1); else return (n * fact -1); } As we can C the function calls itself multiple times. The argument n is sent to the function via the register $a0, which is saved on the stack along with $ra.

3 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

4 Procedure Frame The stack above $sp is preserved ( נשמר ) by making sure that the callee ( פונקציה נקראת ) is doesn't write above $sp. $sp is preserved by adding exactly the same 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.

5 Frame Pointer Some MIPS software use the register $fp to point to the first word of the frame of a procedure.

6 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.

7 Characters and Bytes Most computers today use 8-bit bytes to represent characters, with the American Standard Code for Information Interchange (ASCII) being the most common representation. MIPS provides special instruction to move bytes: lb $t0,0($sp) # read byte from memory sb $t0,0($gp) # write byte to memory lb loads a byte from memory into the rightmost 8 bits of the register. sb takes a byte from the 8 rightmost bits in the register and stores them in memory.

8 Strcpy in C++ void strcpy(char x[], char y[]) { int i; i=0; while((x[i]=y[i]) != 0) i++ } The base address for the arrays x and y are in registers $a0 and $a1.

9 Strcpy in Assembly strcpy: add $t0,$zero,$zero #$t0=0 L1: add $t1,$a1,$t0 #$t1=&y[i] lb $t2,0($t1) # $t2=y[i] add $t3,$a0,$t0 #$t3=&x[i] sb $t2,0($t3) # x[i]=y[i] addi $t0,$t0,1 # i++ bne $t2,$zero,L1 # if y[i]!=0 loop jr $ra # return

10 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 instruction 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 perfrom: lui $s0,61 #61d = addi $s0,$s0,2304 # 2304d =

11 Addressing in Branchs 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) If addresses of the program have to fit into a 16-bit field no program could be larger than 64KByte which is unrealistic. An alternative would to specify a register which would be added to the branch address. But which register?

12 PC-Relative Adressing The Program Counter (PC) is a register the software can't access directly, that always contains the address of the current instruction being executed. Thus if we use the PC as the register to add to the branch address we can always branch within a range of to 2 15 bytes of the current instruction. This is enough for most loops and if statements. This form of addressing 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

13 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.

14 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 jumps instruction replaces only the lower 28 bits of the PC, leaving the 4 highest bits of the PC unchanged. The loader and linker must avoid placing a program across an address boundary ( גבול ) of 256MB (64 million instructions). Otherwise a j must be replaced with a jr.

15 Addressing Mode Summary Immediate addressing - the Operand is a constant Register addressing - the Operand is a register Base or displacement addressing - the operand is 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 Addressing Modes (picture)

17 The Intel 80x86 MIPS was the vision of a single group in 1985, all pieces fit together nicely. Such is not the case with the 80x86. It is the product of several independent groups who evolved the architecture over 20 years: Here are important 80x86 milestones: a 16 bit microprocessor introduced floating-point coprocessor extends the memory to 24 bits is a true 32 bit processor , Pentium (92) and Pentium Pro (95) add more performance MMX expands instruction set for multi-media Pentium II and III add more power

18 80x86 Registers

19 Typical 80x86 Instructions

20 80x86 Instruction Formats