CDA 3100 Recitation Week 8. Question 1.data A:.word 21,3,2,9,100,22,6,15,33,90.text.globl main main: la $a0, A li $a1, 17 li $a2, 10 jal funct li $v0,

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

Lecture 5: MIPS Instruction Set
CS/COE0447 Computer Organization & Assembly Language
Branches Two branch instructions:
CDA 3100 Recitation Week 15. What does the function f1 do:.data A:.word 10,21,45,8,100,15,29,12,3,19 B:.word 2,5,33,5,20,1,53,52,5,5 C:.word 6,8,5,4,5,22,53,12,33,89.text.globl.
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
Solution 2nd Exam.
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.
Assembly Code Example Selection Sort.
The University of Adelaide, School of Computer Science
MIPS Function Continued
MIPS Assembly Language Programming
Recitation Material of Engineering an Assembler June 11, 2013.
Solution to Problem Recitation #1 (Week 2) ECEN350 Prof. Choi.
MIPS Coding. Exercise – the bubble sort 5/8/2015week04-3.ppt2.
Comp Sci instruction encoding 1 Instruction Encoding MIPS machine language Binary encoding of instructions MIPS instruction = 32 bits Three instruction.
MIPS Assembly Language I Computer Architecture CPSC 321 Andreas Klappenecker.
Lecture 4: Loads, Logic, Loops. Review Memory is byte-addressable, but lw and sw access one word at a time. These instructions transfer the contents of.
Lecture 5: Procedures. Function call book-keeping in C main() { int i,j,k,m;... i = mult(j,k);... m = mult(i,i);... } /* really dumb mult function */
More decisions and logic (1) Fall 2010 Lecture 4: Loads, Logic, Loops.
9/29: Lecture Topics Memory –Addressing (naming) –Address space sizing Data transfer instructions –load/store on arrays on arrays with variable indices.
Foundation of Systems Xiang Lian The University of Texas-Pan American.
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.
Lecture 8. MIPS Instructions #3 – Branch Instructions #1 Prof. Taeweon Suh Computer Science Education Korea University 2010 R&E Computer System Education.
Lecture 4: MIPS Instruction Set
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.
9/29: Lecture Topics Conditional branch instructions
CPEG323 Homework Review I Long Chen October, 17 th, 2005.
MIPS Coding. Exercise – the bubble sort 7/9/2016week04-3.ppt2.
MIPS Assembly Language Programming
Switch Statement Pre-requisites: 1D array addressing Updated 7/11/2013.
MIPS Procedures.
MIPS Coding Continued.
Lecture 4: MIPS Instruction Set
MIPS instructions.
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Pseudo instructions.
MIPS coding.
SPIM Syscalls.
MIPS Procedures.
Addressing in Jumps jump j Label go to Label op address 2 address
MIPS Functions.
Logical and Decision Operations
MIPS Instructions.
MIPS coding.
MIPS Functions.
Pseudo instructions.
Instruction encoding The ISA defines Format = Encoding
MIPS Functions.
Lecture 5: Procedure Calls
MIPS coding.
MIPS Procedures.
Review.
MIPS Coding.
The University of Adelaide, School of Computer Science
MIPS function continued
MIPS Functions.
MIPS Coding.
Lecture 6: Assembly Programs
Review.
MIPS Coding Continued.
MIPS coding.
MIPS assembly.
9/27: Lecture Topics Memory Data transfer instructions
MIPS Functions.
MIPS function continued
MIPS Functions.
MIPS instructions.
Conditional Branching (beq)
Presentation transcript:

CDA 3100 Recitation Week 8

Question 1.data A:.word 21,3,2,9,100,22,6,15,33,90.text.globl main main: la $a0, A li $a1, 17 li $a2, 10 jal funct li $v0, 10 syscall

Question 1.data A:.word 21,3,2,9,100,22,6,15,33,90.text.globl main main: la $a0, A# address of array li $a1, 17# ??? li $a2, 10# length of array jal funct li $v0, 10 syscall

Question 1 funct: li $t0, 0 li $v1, funct_L0: sll $t1, $t0, 2 add $t1, $t1, $a0 lw $t1, 0($t1) sub $t2, $t1, $a1 bgt $t2, $0, funct_L1 sub $t2, $0, $t2 funct_L1: bgt $t2, $v1, funct_L2 ori $v0, $t0, 0 ori $v1, $t2, 0 funct_L2: addi $t0, $t0, 1 blt $t0, $a2, funct_L0 jr $ra

Question 1 funct: li $t0, 0# Counter li $v1, # ??? funct_L0: sll $t1, $t0, 2 add $t1, $t1, $a0 lw $t1, 0($t1) sub $t2, $t1, $a1 bgt $t2, $0, funct_L1 sub $t2, $0, $t2 funct_L1: bgt $t2, $v1, funct_L2 ori $v0, $t0, 0 ori $v1, $t2, 0 funct_L2: addi $t0, $t0, 1# Increment counter blt $t0, $a2, funct_L0# Loop tester jr $ra

Question 1 funct: li $t0, 0# Counter li $v1, # ??? funct_L0: sll $t1, $t0, 2 add $t1, $t1, $a0 lw $t1, 0($t1) sub $t2, $t1, $a1 bgt $t2, $0, funct_L1# do next if $t2 < 0 sub $t2, $0, $t2# $t2 = abs($t2) funct_L1: bgt $t2, $v1, funct_L2 ori $v0, $t0, 0 ori $v1, $t2, 0 funct_L2: addi $t0, $t0, 1# Increment counter blt $t0, $a2, funct_L0# Loop tester jr $ra

Question 1 funct: li $t0, 0# Counter li $v1, # ??? funct_L0: sll $t1, $t0, 2# add $t1, $t1, $a0# Load element from array at counter lw $t1, 0($t1)# sub $t2, $t1, $a1 bgt $t2, $0, funct_L1# do next if $t2 < 0 sub $t2, $0, $t2# $t2 = abs($t2) funct_L1: bgt $t2, $v1, funct_L2 ori $v0, $t0, 0 ori $v1, $t2, 0 funct_L2: addi $t0, $t0, 1# Increment counter blt $t0, $a2, funct_L0# Loop tester jr $ra

Question 1 funct: li $t0, 0# Counter li $v1, # ??? funct_L0: sll $t1, $t0, 2# add $t1, $t1, $a0# Load element from array at counter lw $t1, 0($t1)# sub $t2, $t1, $a1 bgt $t2, $0, funct_L1# do next if $t2 < 0 sub $t2, $0, $t2# $t2 = abs($t2) funct_L1: bgt $t2, $v1, funct_L2# Update returns when ??? ori $v0, $t0, 0# Return array index ori $v1, $t2, 0# Return ??? funct_L2: addi $t0, $t0, 1# Increment counter blt $t0, $a2, funct_L0# Loop tester jr $ra

Question 1 funct: li $t0, 0# Counter li $v1, # ??? funct_L0: sll $t1, $t0, 2# add $t1, $t1, $a0# Load element from array at counter lw $t1, 0($t1)# sub $t2, $t1, $a1# ??? bgt $t2, $0, funct_L1# do next if $t2 < 0 sub $t2, $0, $t2# $t2 = abs($t2) funct_L1: bgt $t2, $v1, funct_L2# Update returns when ??? ori $v0, $t0, 0# Return array index ori $v1, $t2, 0# Return ??? funct_L2: addi $t0, $t0, 1# Increment counter blt $t0, $a2, funct_L0# Loop tester jr $ra

Question 1 funct: li $t0, 0# Counter li $v1, # ??? funct_L0: sll $t1, $t0, 2# add $t1, $t1, $a0# Load element from array at counter lw $t1, 0($t1)# sub $t2, $t1, $a1# Finds distance between element and parameter bgt $t2, $0, funct_L1# do next if $t2 < 0 sub $t2, $0, $t2# $t2 = abs($t2) funct_L1: bgt $t2, $v1, funct_L2# Update returns when ??? ori $v0, $t0, 0# Return array index ori $v1, $t2, 0# Return ??? funct_L2: addi $t0, $t0, 1# Increment counter blt $t0, $a2, funct_L0# Loop tester jr $ra

Question 1 funct: li $t0, 0# Counter li $v1, # Arbitrarily large distance funct_L0: sll $t1, $t0, 2# add $t1, $t1, $a0# Load element from array at counter lw $t1, 0($t1)# sub $t2, $t1, $a1# Finds distance between element and parameter bgt $t2, $0, funct_L1# do next if $t2 < 0 sub $t2, $0, $t2# $t2 = abs($t2) funct_L1: bgt $t2, $v1, funct_L2# Update returns when ??? ori $v0, $t0, 0# Return array index ori $v1, $t2, 0# Return ??? funct_L2: addi $t0, $t0, 1# Increment counter blt $t0, $a2, funct_L0# Loop tester jr $ra

Question 1 funct: li $t0, 0# Counter li $v1, # Arbitrarily large distance funct_L0: sll $t1, $t0, 2# add $t1, $t1, $a0# Load element from array at counter lw $t1, 0($t1)# sub $t2, $t1, $a1# Finds distance between element and parameter bgt $t2, $0, funct_L1# do next if $t2 < 0 sub $t2, $0, $t2# $t2 = abs($t2) funct_L1: bgt $t2, $v1, funct_L2# Update returns when new distance is smallest ori $v0, $t0, 0# Return array index ori $v1, $t2, 0# Return distance funct_L2: addi $t0, $t0, 1# Increment counter blt $t0, $a2, funct_L0# Loop tester jr $ra

Question 1.data A:.word 21,3,2,9,100,22,6,15,33,90.text.globl main main: la $a0, A# address of array li $a1, 17# find element closest to this li $a2, 10# length of array jal funct li $v0, 10 syscall

Question 1.data A:.word 21,3,2,9,100,22,6,15,33,90.text.globl main main: la $a0, A# address of array li $a1, 17# find element closest to this li $a2, 10# length of array jal funct# returns index and element closest to $a1 li $v0, 10 syscall

Question 1.data A:.word 21,3,2,9,100,22,6,15,33,90.text.globl main main: la $a0, A# address of array li $a1, 17# find element closest to this li $a2, 10# length of array jal funct# returns index and element closest to $a1 # nothing changes the array, but there was # probably a swap here that should have li $v0, 10 syscall

Question 2 (10 points) Write a complete function called “F1” that takes in $a0 a number and return in $v0 the location of the most significant 1. In this problem, the number passed in $a0 will never be 0. For example, if the $a0 is 30, it should return 4 because 30 in binary is “ ” and we index the locations as bit 0 to bit 31. Please note that You do not have to save $a0 in your function. If more than 5 instructions are used for this function, 2 points will be deducted for each additional instruction until no more points are left. Keep in mind that some pseudo instructions may result in more than one real instruction and the number of real instructions will be counted.

Question 2 Add return instruction because function F1: jr $ra

Question 2 Use srl to be able to count bits F1: srl $a0, $a0, 1 jr $ra

Question 2 Set up post test loop; loop if $a0 has a 1 bit F1: srl $a0, $a0, 1 bne $a0, $0, F1 jr $ra

Question 2 Count index position F1:addi $v0, $v0, 1 srl $a0, $a0, 1 bne $a0, $0, F1 jr $ra

Question 2 Initialize index position F1:addi $v0, $0, -1 F1_L1:addi $v0, $v0, 1 srl $a0, $a0, 1 bne $a0, $0, F1_L1 jr $ra

Question 2 Hints: – Don’t forget about initialization – Use correct version of branch to limit use of unconditional jump instructions – Don’t forget pseudo instructions are expanded out when it’s being graded – Good chance it will use a post test loop – Good chance it will use either sll / srl – Typically, don’t worry about saving addresses, $ra, ect – It’s only a couple point deduction for each extra instruction; don’t worry about it too much if you go over by one or two instructions