1 CDA 3101 Discussion Section 03.  Problem 2.6.1 The following problems deal with translating from C to MIPS. Assume that the variables f, g, h, i and.

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

Goal: Write Programs in Assembly
4.
Lecture 5: MIPS Instruction Set
MIPS assembly. Review  Lat lecture, we learnt  addi,  and, andi, or, ori, xor, xori, nor,  beq, j, bne  An array is stored sequentially in the memory.
The University of Adelaide, School of Computer Science
CS 136 Lab 2 MIPS ISA SPIM. Prob 2.30 sll $a2, $a2, 2 sll $a3, $a3, 2 add $v0, $zero, $zero add $t0, $zero, $zero outer:add $t4, $a0, $t0 lw $t4, 0($t4)
Lecture 20: 11/12/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
Solution 2nd Exam.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 5: Data Transfer Instructions / Control Flow Instructions Partially adapted from Computer.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
Assembly Code Example Selection Sort.
The University of Adelaide, School of Computer Science
Chapter 2 Instructions: Language of the Computer
1 Today’s lecture  Last lecture we started talking about control flow in MIPS (branches)  Finish up control-flow (branches) in MIPS —if/then —loops —case/switch.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patternson and Hennessy Text.
1 Review Chpt 2 and 3 Five components of a computer Input Output Memory Arithmetic & Logic Unit (ALU) Control Two types of information Data and instruction.
Data Transfer & Decisions I (1) Fall 2005 Lecture 3: MIPS Assembly language Decisions I.
MIPS Instruction Set Advantages
1 MIPS Assembly Language Programming CDA 3101 Discussion Section 03.
Lecture 8. MIPS Instructions #3 – Branch Instructions #1 Prof. Taeweon Suh Computer Science Education Korea University 2010 R&E Computer System Education.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Lecture 15: 10/24/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
1 Branches and Procedure Calls Lecture 14 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
MIPS coding. Review Shifting – Shift Left Logical (sll) – Shift Right Logical (srl) – Moves all of the bits to the left/right and fills in gap with 0’s.
Computer Architecture CSE 3322 Lecture 3 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/3/09 Read 2.8.
Computer Organization Instructions Language of The Computer (MIPS) 2.
MIPS coding. slt, slti slt $t3, $t1, $t2 – set $t3 to be 1 if $t1 < $t2 ; else clear $t3 to be 0. – “Set Less Than.” slti $t3, $t1, 100 – set $t3 to be.
Erik Jonsson School of Engineering and Computer Science FEARLESS Engineeringwww.utdallas.edu/~pervin EE/CE 2310 – HON/002 Introduction to Digital Systems.
Computer Organization Instructions Language of The Computer (MIPS) 2.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 11 Conditional Operations.
Computer Organization Instructions Language of The Computer (MIPS) 2.
Control Structures Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Conditional Control Structure if ( i < j ) goto A; else.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic3: Instructions, The Language of the Machine José Nelson Amaral.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
CPEG323 Homework Review I Long Chen October, 17 th, 2005.
Computer Architecture & Operations I
MIPS Assembly Language Programming
MIPS Instruction Set Advantages
CS2100 Computer Organisation
MIPS Coding Continued.
Lecture 4: MIPS Instruction Set
Computer Architecture & Operations I
Parallel Shared Memory
Decision Making.
Instructions for Making Decisions
The University of Adelaide, School of Computer Science
MIPS coding.
Pick up the handout on your way in!!
MIPS Procedures.
MISP Assembly.
Solutions Chapter 2.
MIPS Instruction Encoding
MIPS coding.
MIPS Instruction Encoding
MIPS coding.
MIPS Coding.
3.
MIPS Coding.
MIPS Assembly.
MIPS Coding Continued.
MIPS coding.
MIPS assembly.
MIPS instructions.
Conditional Branching (beq)
MIPS Assembly.
Presentation transcript:

1 CDA 3101 Discussion Section 03

 Problem The following problems deal with translating from C to MIPS. Assume that the variables f, g, h, i and j are assigned to registers $s0, $s1, $s2, $s3, and $s4, respectively. Assume that the base address of the integer array A and B are in registers $s6 and $s7, respectively. a. f=-g + h + B[1]; b. f= A[ B[g] + 1 ]; For the C statements above, what is the corresponding MIPS assembly code? 2

a) lw $s0, 4($s7) sub $s0, $s0, $s1 add $s0, $s0, $s2 b) sll $t0, $s1, 2 add $t0, $s7, $t0 lw $t0, 0($t0) addi $t0, $t0, 1 sll $t0, $t0, 2 add $t0, $s6, $t0 lw $s0, 0($t0) 3

 Problem The following problems deal with translating from MIPS to C. Assume that the variables f, g, h, i and j are assigned to registers $s0, $s1, $s2, $s3, and $s4, respectively. Assume that the base address fo the array A and B are in registers $s6 and $s7, respectively. add $s0, $s0, $s1 add $s0, $s3, $s2 add $s0, $s0, $s3 For the MIPS assembly code above, what is the corresponding C statement? 4

add $s0, $s0, $s1 f = f+g; add $s0, $s3, $s2 f = i+h; add $s0, $s0, $s3 f = f+i; f = 2i + h; 5

 Problem A= B & c[0]; For the C statement above, write a minimal sequence of MIPS assembly instructions that does the identical operation? 6

Assuming $t1 = A, $t2 = B, $s1 = base of Array C lw $t3, 0($s1) and $t1, $t2, $t3 7

 Problem Suppose that register $t0 contains a value two and $t1 has the value two What is the value of $t2 after the following instructions? slt $t2, $t0, $t1 beq $t2, $zero, ELSE j DONE ELSE : addi $t2, $zero, 2 DONE: 8

$t2 = 1 9

 Problem 2.29 Add comments to the following MIPS code and describe in one sentence what it computes. Assume $a0, $a1 are input, initial value a, b; $v0 is output. $a0: input a; $a1: input b; $v0: output; add$t0, $zero, $zero loop:beq $a1, $zero, finish add $t0, $t0, $a0 sub $a1, $a1, 1 jloop finish:addi $t0, $t0, 100 add $v0, $t0, $zero 10

 Problem 2.29 Add comments to the following MIPS code and describe in one sentence what it computes. Assume $a0, $a1 are input, initial value a, b; $v0 is output. $a0: input a; $a1: input b; $v0: output; add$t0, $zero, $zero loop:beq $a1, $zero, finish add $t0, $t0, $a0 sub $a1, $a1, 1 jloop finish:addi $t0, $t0, 100 add $v0, $t0, $zero 11 # t=0; # if(b==0) go to finish # t = t+a, increase t by a # b = b-1, # go to loop # t = t+100 # $v0 = t, output

 Problem 2.29 Add comments to the following MIPS code and describe in one sentence what it computes. Assume $a0, $a1 are input, initial value a, b; $v0 is output. add$t0, $zero, $zero loop:beq $a1, $zero, finish add $t0, $t0, $a0 sub $a1, $a1, 1 jloop finish:addi $t0, $t0, 100 add $v0, $t0, $zero 12 t=0; while(b!=0) { t = t + a; b = b – 1; } t = t + 100; v = t; So, v = a*b+100

 Problem 2.30 Add comments to the following MIPS code and describe in one sentence what it computes. The code processes 2 arrays and produces a value in $v0. Each array consists of 2500 words indexed 0 through 2499, the base addresses of the arrays are stored in $a0, $a1, and their sizes are stored in $a2, $a3. What will be returned in $v0? 13

 Problem 2.30 sll$a2, $a2, 2 sll$a3, $a3, 2 add$v0, $zero, $zero add$t0, $zero, $zero outer:add $t4, $a0, $t0 lw $t4, 0($t4) add $t1, $zero, $zero inner:add$t3, $a1, $t1 lw$t3, 0($t3) bne$t3, $t4, skip addi$v0, $v0, 1 skip:addi $t1, $t1, 4 bne$t1, $a3, inner addi $t0, $t0, 4 bne $t0, $a2, outer 14

 Problem 2.30 sll$a2, $a2, 2 sll$a3, $a3, 2 add$v0, $zero, $zero add$t0, $zero, $zero outer:add $t4, $a0, $t0 lw $t4, 0($t4) add $t1, $zero, $zero inner:add$t3, $a1, $t1 lw$t3, 0($t3) bne$t3, $t4, skip addi$v0, $v0, 1 skip:addi $t1, $t1, 4 bne$t1, $a3, inner addi $t0, $t0, 4 bne $t0, $a2, outer 15 #a2=a2*4, size in byte; #a3=a3*4, # $v0=0, counter; # $t0=0, index of $a0 (x) i; # $t4 = x[i]; start of outer loop; # $t1=0, index of $a1 (y) j; # $t3 = y[j], inner loop # if x[i]==y[j], counter increment; # else compare x[i] with y[j+1], until j reach the end of y # go back to start point of outer loop. Compare x[i+1] with y, and count the number

 Problem 2.30 sll$a2, $a2, 2 sll$a3, $a3, 2 add$v0, $zero, $zero add$t0, $zero, $zero outer:add $t4, $a0, $t0 lw $t4, 0($t4) add $t1, $zero, $zero inner:add$t3, $a1, $t1 lw$t3, 0($t3) bne$t3, $t4, skip addi$v0, $v0, 1 skip:addi $t1, $t1, 4 bne$t1, $a3, inner addi $t0, $t0, 4 bne $t0, $a2, outer 16 Assume $a0->x, $a1->y, it equals to a nested loop in C code: count = 0; for(i=0; i<2500; i++) { for(j=0; j<2500; j++) { if(x[i] == y[j]) count ++; } }