Chapter loop: lw $v1, 0($a0) addi $v0, $v0, 1 sw $v1, 0($a1) addi $a0, $a0, 1 addi $a1, $a1, 1 bne $v1, $zero, loop

Similar documents
HW2 solutions You did this for Lab sbn temp, temp,.+1 # temp = 0; sbn temp, b,.+1 # temp = -b; sbn a, temp,.+1 # a = a (-b) = a + b;

CS152 Computer Architecture and Engineering

SPIM Instruction Set

MIPS Reference Guide

Patterson PII. Solutions

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont )

MIPS Instruction Reference

Examples of branch instructions

Solutions for Chapter 2 Exercises

MIPS Instruction Set

F. Appendix 6 MIPS Instruction Reference

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA MIPS ISA. In a CPU. (vonneumann) Processor Organization

MIPS%Assembly% E155%

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: MIPS Programming

Mips Code Examples Peter Rounce

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

Reduced Instruction Set Computer (RISC)

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

Course Administration

comp 180 Lecture 10 Outline of Lecture Procedure calls Saving and restoring registers Summary of MIPS instructions

Computer Architecture. The Language of the Machine

ENCM 369 Winter 2013: Reference Material for Midterm #2 page 1 of 5

Lecture 5: Procedure Calls

Flow of Control -- Conditional branch instructions

MIPS Instruction Format

101 Assembly. ENGR 3410 Computer Architecture Mark L. Chang Fall 2009

ELEC / Computer Architecture and Design Fall 2013 Instruction Set Architecture (Chapter 2)

Reduced Instruction Set Computer (RISC)

ECE232: Hardware Organization and Design. Computer Organization - Previously covered

EEM 486: Computer Architecture. Lecture 2. MIPS Instruction Set Architecture

5/17/2012. Recap from Last Time. CSE 2021: Computer Organization. The RISC Philosophy. Levels of Programming. Stored Program Computers

1/26/2014. Previously. CSE 2021: Computer Organization. The Load/Store Family (1) Memory Organization. The Load/Store Family (2)

Recap from Last Time. CSE 2021: Computer Organization. Levels of Programming. The RISC Philosophy 5/19/2011

ECE 30 Introduction to Computer Engineering

Points available Your marks Total 100

MIPS ISA and MIPS Assembly. CS301 Prof. Szajda

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

MIPS function continued

Chapter 2. Instructions: Language of the Computer. Adapted by Paulo Lopes

CS222: MIPS Instruction Set

EE 361 University of Hawaii Fall

CS 61c: Great Ideas in Computer Architecture

CSc 256 Midterm 2 Fall 2011

MIPS Functions and the Runtime Stack

MIPS Assembly Language. Today s Lecture

Today s Lecture. MIPS Assembly Language. Review: What Must be Specified? Review: A Program. Review: MIPS Instruction Formats

The MIPS Instruction Set Architecture

CS 61C: Great Ideas in Computer Architecture. MIPS Instruction Formats

ECE468 Computer Organization & Architecture. MIPS Instruction Set Architecture

1 5. Addressing Modes COMP2611 Fall 2015 Instruction: Language of the Computer

Mark Redekopp, All rights reserved. EE 357 Unit 11 MIPS ISA

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary

Control Instructions

MIPS ISA. 1. Data and Address Size 8-, 16-, 32-, 64-bit 2. Which instructions does the processor support

CS61c MIDTERM EXAM: 3/17/99

CS 4200/5200 Computer Architecture I

Lecture 2. Instructions: Language of the Computer (Chapter 2 of the textbook)

Previously. CSE 2021: Computer Organization. Memory Organization. The Load/Store Family (1) 5/26/2011. Used to transfer data to/from DRAM.

Lec 10: Assembler. Announcements

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

MIPS Instruction Set Architecture (2)

Review (1/2) IEEE 754 Floating Point Standard: Kahan pack as much in as could get away with. CS61C - Machine Structures


CS3350B Computer Architecture MIPS Introduction

CSc 256 Midterm (green) Fall 2018

The MIPS R2000 Instruction Set

Computer Science and Engineering 331. Midterm Examination #1. Fall Name: Solutions S.S.#:

MIPS R-format Instructions. Representing Instructions. Hexadecimal. R-format Example. MIPS I-format Example. MIPS I-format Instructions

CSE Lecture In Class Example Handout

Architecture II. Computer Systems Laboratory Sungkyunkwan University

We will study the MIPS assembly language as an exemplar of the concept.

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

ECE Exam I February 19 th, :00 pm 4:25pm

CSc 256 Midterm 2 Spring 2012

Lecture 9: Disassembly

EE108B Lecture 3. MIPS Assembly Language II

EEC 581 Computer Architecture Lecture 1 Review MIPS

ECE 2035 Programming HW/SW Systems Fall problems, 7 pages Exam Two 23 October 2013

Computer Architecture. MIPS Instruction Set Architecture

Chapter 2. lw $s1,100($s2) $s1 = Memory[$s2+100] sw $s1,100($s2) Memory[$s2+100] = $s1

MIPS Assembly Programming

CS3350B Computer Architecture MIPS Instruction Representation

INSTRUCTION SET COMPARISONS

Announcements. EE108B Lecture MIPS Assembly Language III. MIPS Machine Instruction Review: Instruction Format Summary

CENG3420 Lecture 03 Review

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

Computer Organization MIPS ISA

CS 110 Computer Architecture MIPS Instruction Formats

CMPE324 Computer Architecture Lecture 2

CS3350B Computer Architecture

COMPUTER ORGANIZATION AND DESIGN

CSCI 402: Computer Architectures. Instructions: Language of the Computer (3) Fengguang Song Department of Computer & Information Science IUPUI.

Recitation 5: Recitation 5: MIPS Interrupts and Floating Point

Thomas Polzer Institut für Technische Informatik

Computer Architecture

COMP2611: Computer Organization MIPS function and recursion

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

Computer Architecture. Chapter 2-2. Instructions: Language of the Computer

Overview. Introduction to the MIPS ISA. MIPS ISA Overview. Overview (2)

Transcription:

Chapter 3 3.7 loop: lw $v1, 0($a0) addi $v0, $v0, 1 sw $v1, 0($a1) addi $a0, $a0, 1 addi $a1, $a1, 1 bne $v1, $zero, loop Instructions Format OP rs rt Imm lw $v1, 0($a0) I 35 4 3 0 addi $v0, $v0, 1 I 8 2 2 1 sw $v1, 0($a1) I 43 5 3 0 addi $a0, $a0, 1 I 8 4 4 1 addi $a1, $a1, 1 I 8 5 5 1 bne $v1, $zero, loop I 5 3 0-6 3.9 loop: add $t1, $3, $3 add $t1, $t1, $t1 add $t1, $t1, $s6 ;$s6-> base address of save[] lw $t0, 0($t1) beq $t0, $s5, loop ;change from bne to beq add $s3, $s3,$s4 ;$s4->j, $s3->i, executed in delay slots 11*6=66 instructions are executed if save[i+j*l]=k for l=0,1,2,, 9 3.10 Pseudoinstructions What is accomplished Min sequence of Mips Move $t5, $t3 $t5=$t3 Add $t5, $t3,$zer0 Clear $t5 $t5=0 Xor $t5, $t5, $t5 Li $t5, small $t5=small Addi $t5, $zero, small Li $t5,big $t5=big Lui $t5, big[31:16] Ori $t5, $t5, big[15,0] Lw $t5, big($t3) $t5=mem[$t3+big] Lui $t5, big[31:16] Ori $t5, $t0, big[15,0] Add $t3, $t3,$t5 Lw $t5, 0($t3) Addi $t5, $t3,big $t5=$t3+big Lui $t5, big[31:16] ori $t5, big[15,0] addi $t5,$t5,$t3 Beq $t5, small, L If $t5=small, branch to L Addi $at,$zero, small Beq $t5, $at, L Beq $t5, big, L If $t5=big, branch to L Lui $at, big[31:16] ori $at, big[15,0] beq $at,$t5,l Ble $t5, $t3,l If $t5<=$t3, branch to L Slt $at, $t3, $t5 Beq $at,$zero, L Bge $5, $t3, L If $t5>=$t3, branch to L Slt $at, $t5, $t3 Beq $at,$zero, L Bgt $5, $t3, L If $t5>$t3, branch to L Slt $at, $t3, $t5 Bne $at,$zero, L

3.17 ( (100 - (x * 22)) / 100) ) * 110% = 1 Solving: x = (100 - (1/(110%) * 100)) / 22 = 41% 3.22 One MIPS implementation: Procedure written without pseudoinstructions, assuming delayed branches and loads: atoi: or $v0, $zero, $zero # int product = 0; ori $t2, $zero, 10 # const int ten = 10; _loop: lb $t0, 0($a0) # { char c = str[i] or $zero, $zero, $zero # nop beq $t0, $zero, _done # if (c invalid) error; slti $t1, $t0, '0' # bne $t1, $zero, _error # slti $t1, $t0, '9'+1 # beq $t1, $zero, _error # subi $t0, $t0, '0' # int digit = c 0 ; mult $v0, $t2 # product *= 10; mflo $v0 # j _loop # add $v0, $v0, $t0 # product += digit; } _error: ori $v0, $zero, -1 # if (error) product = -1; _done: jr $ra # return product; or $zero, $zero, $zero # nop 3.26 fib: addiu $v0, $zero, 0 #initialize the return value beq $a0, $zero, exit #if n= =0, exit ori $zero, $zero, 0 #nop addiu $v0, $zero, 1 beq $a0, $v0, exit #if n= =1, exit ori $zero, $zero,0 #nop addiu $sp, $sp, -12 sw $a0, 0($sp) sw $ra, 4($sp) sw $s0, 8($sp) addiu $a0, $a0, -1 jal fib addiu $s0, $v0, 0 #store fib(n-1) to $s0 lw $a0, 0($sp) #recover $a0 addiu $a0, $a0, -2 #$a0=n-2 jal fib addiu $v0, $v0, $s0 lw lw $ra, 4($sp) $s0, 8($sp)

addiu $sp, $sp, 12 exit: jr $ra ori $zero, $zero, 0 3.27 Notice that parameters for fib_iter are stored in $a0,$a1 and $a2. fib_iter: addiu $v0,$zero, $a1 #initialize the return value to be $a1 beq $a3, $zero, exit #if count= = 0, exit addiu $sp, $sp 16 #store parameter for recursive call sw $a0, 0($sp) sw $a1, 4($sp) sw $a2, 8($sp) sw $ra, 12($sp) addiu $t0, $a0,0 #store $a0 into $t0 temporally add $a0, $a0, $a1 #a0=$a0+$a1 addiu $a1, $t0, 0 #$a1=$a0 addiu $a2, $a2,-1 #count=count-1 jal fib_iter lw $a0, 0($sp) #pop out of stack lw $a1, 4($sp) lw $a2, 8($sp) lw $ra, 12($sp) addiu $sp, $sp, 16 exit: jr $ra #return ori $zero, $zero, 0 3.29 sbn temp, temp,.+1 # temp = 0; sbn temp, b,.+1 # temp = -b; sbn a, temp,.+1 # a = a (-b) = a + b; 3.30 sbn neg_a, neg_a,.+1 # neg_a = 0; sbn neg_a, a,.+1 # neg_a = -a; sbn c, c,.+1 # c = 0; loop: sbn b, one,.+1 # do { b = b 1; sbn c, neg_a,.+1 # c = c + a; sbn temp, temp,.+1 # temp = 0; sbn temp, b, loop # } while (b > 0); Note (1) This solution does not work if b = 0, because the problem description said to assume that a and b are greater than 0. Perfectionist students are likely to write solutions that do work for b = 0 though, so their answers would be an instruction or too long. A4.ktext 0x80000080 addiu $at, $at, -28 #assume that $at stores the kernel stack pointer swc0 $12, 0($at) #store status register swc0 $13, 4($at) #store cause register

swc0 $14, 8($at) #store EPC sw $a0, 12($at) sw $a1, 16($at) sw $k0, 20($at) sw $k1,24($at) mfc0 $k0, $12 ori $k0, $k0, 0xF001 #set interrupt enable and interrupt mask lwc0 $12, $k0 #turn on interrupt enable here mfc0 $k0, $13 mfc0 $k1, $14 mov $a0, $k0 #set parameters mov $a1, $k1 #set parameters sgt $v0, $k0, 0x44 #check if the exception is caused by interrupt bgtz $v0, interrupt jal print_excp #non interrupt exception j done #ready to return interrupt: jal interrupt_proc #if it is interrupt, call interrupt procedure done:.kdata lwc0 $12, 0($at) #restore status register lwc0 $13, 4($at) #restore cause register lwc0 $14, 8($at) #restore EPC lw $a0, 12($at) lw $a1, 16($at) lw $k0, 20($at) lw $k1,24($at) addiu $at, $at, 28 #restore the kernel stack pointer addiu $k1, $k1,4 rfe Save0:.word 0 save1:.word 0 save2:.word 0 save3:.word 0 save4:.word 0 Notice that the Cause register, Status reg, EPC and those registers used by exception handler should be stored in kernel stack, where $at stores the kernel stack pointer. In kernel mode, $sp could not be used for possible mem error exception. Also, the inerrupt priority is assumed to be handled by hardware. A5.ktext 0x80000080 sw $a0, save0 sw $a1, save1 mfc0 $k0, $13 # Move Cause into $k0 mfc0 $k1, $14 # Move EPC into $k1 addiu $v0, $zero, 0x44

slt $v0, $v0, $k0 # Ignore interrupts bgtz $v0, _restore mov $a0, $k0 # Move Cause into $a0 mov $a1, $k1 # EPC into $a1 jal print_excp # Print exception error msg _restore: lw $a0, save0 lw $a1, save1 lw $k0, -4($k1) # $k0 = previous instruction srl $k0, $k0, 26 # $k0 = opcode of prev instr ori $k1, $zero, 2 # opcode of j beq $k0, $k1, _delayslot # ori $k0, $zero, 4 # opcode of beq beq $k0, $k1, _delayslot # and so on for: jr, jal, bne, bltz, bgezal, bczt... _done: mfc0 $k1, $14 # reload EPC into $k1 addiu $k1, $k1, 4 # Do not reexecute fault instr rfe # done in delay-slot of jr _delayslot: mfc0 $k1, $14 # reload EPC into $k1 addiu $k0, $k1, -4 # $k0 = EPC - 4 addiu $k1, $k1, 4 # $k1 = EPC + 4 jr $k0 # poke at branching instr j _check _check: rfe or $zero,$zero,$zero.kdata Save0:.word 0 save1:.word 0 This problem is hard. The basic idea of this solution is to do everything possible in order not to touch the instruction that caused the exception. We need a way to poke the branching instruction, that is, execute the instruction without executing any instructions around it. This procedure works by calling the branching instruction with jr, but putting a j in the delay-slot of the jr, so that we will jump back after executing the branching instruction and not execute its regular delay-slot. If it turns out that the branch is not taken (which may happen with a bne or beq), then we jump back to EPC+4. Note: this solution assumes that j in branch delay slots will NOT executed if branch is taken! Other elegant solutions will be highly appreciated