MIPS Coding.

Slides:



Advertisements
Similar presentations
CENG 311 Decisions in C/Assembly Language
Advertisements

Review of the MIPS Instruction Set Architecture. RISC Instruction Set Basics All operations on data apply to data in registers and typically change the.
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.
CS/COE0447 Computer Organization & Assembly Language
Branches Two branch instructions:
Arrays First step is to reserve sufficient space for the array.
The University of Adelaide, School of Computer Science
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,
Lecture 9: MIPS Instruction Set
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.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.
MIPS Assembly Language CPSC 321 Computer Architecture Andreas Klappenecker.
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.
Assembly Code Example Selection Sort.
The University of Adelaide, School of Computer Science
MIPS Function Continued
Assembly Language Working with the CPU.
Lecture 8: MIPS Instruction Set
MIPS Coding. Exercise – the bubble sort 5/8/2015week04-3.ppt2.
Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) MIPS encoding of instructions Spim simulator more examples of MIPS programming.
9/29: Lecture Topics Memory –Addressing (naming) –Address space sizing Data transfer instructions –load/store on arrays on arrays with variable indices.
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.
Chapter 2 Decision-Making Instructions (Instructions: Language of the Computer Part V)
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.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 11 Conditional Operations.
Control Structures Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Conditional Control Structure if ( i < j ) goto A; else.
MIPS Coding. Exercise – the bubble sort 7/9/2016week04-3.ppt2.
MIPS Instruction Set Advantages
CS2100 Computer Organisation
Lecture 7: MARS, Computer Arithmetic
MIPS Procedures.
MIPS Coding Continued.
Lecture 4: MIPS Instruction Set
Computer Architecture & Operations I
MIPS instructions.
Pseudo instructions.
The University of Adelaide, School of Computer Science
MIPS coding.
MIPS Procedures.
Assembler Directives Example program: .data # DATA segment city: .asciiz “Seattle” .align 2 num: .word 2210, 2341, 26, 0x022ec,
Addressing in Jumps jump j Label go to Label op address 2 address
MIPS coding.
MIPS Functions.
Pseudo instructions.
MIPS Functions.
MIPS coding.
MIPS Procedures.
Review.
The University of Adelaide, School of Computer Science
MIPS function continued
MIPS Functions.
3.
MIPS Coding.
Review.
MIPS Coding Continued.
MIPS coding.
MIPS assembly.
Program Assembly.
9/27: Lecture Topics Memory Data transfer instructions
MIPS Functions.
MIPS function continued
Conditional Control Structure
MIPS Functions.
MIPS instructions.
Conditional Branching (beq)
MIPS Assembly.
Presentation transcript:

MIPS Coding

Loop example 4 Suppose we have three arrays, A, B, C, all of size 10. Now we want to set C[i] = min(A[i], B[i]) for all 0<= i <= 9. 1/12/2019 week04-3.ppt

Loop example 4 Suppose we have three arrays, A, B, C, all of size 10. Now we want to set C[i] = min(A[i], B[i]) for all 0<= i <= 9. First, we need a loop to walk through the elements (done before) Second, we need to be able to read the elements (done before) Third, we need to be able to compare two numbers (done before) Fourth, we need to write back to the memory (easy) So, first write the loop. Then, write codes to read the elements. Then, compare with if else. Then, write back. 1/12/2019 week04-3.ppt

.data A: .word 12, 34, 67, 1, 45, 90, 11, 33, 67, 19 B: .word 90, 2, 93, 66, 8, 120, 121,11, 33, 9 C: .word 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 .text .globl main main: la $s0, A la $s1, B la $s2, C li $s3, 10 done:li $v0,10 syscall

.data A: .word 12, 34, 67, 1, 45, 90, 11, 33, 67, 19 B: .word 90, 2, 93, 66, 8, 120, 121,11, 33, 9 C: .word 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 .text .globl main main: la $s0, A la $s1, B la $s2, C li $s3, 10 li $t0, 0 # using $t0 as i LOOP:addi $t0, $t0, 1 # i ++ bne $t0, $s3, LOOP # go back if not yet 10 times done:li $v0,10 syscall

.data A: .word 12, 34, 67, 1, 45, 90, 11, 33, 67, 19 B: .word 90, 2, 93, 66, 8, 120, 121,11, 33, 9 C: .word 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 .text .globl main main: la $s0, A la $s1, B la $s2, C li $s3, 10 li $t0, 0 # using $t0 as i LOOP:sll $t4, $t0, 2 # $t4 = i * 4 add $t5, $t4,$s0 # $t5 will have the address of A[i] lw $t1, 0($t5) # $t1 has A[i] add $t6, $t4,$s1 # $t6 will have the address of B[i] lw $t2, 0($t6) # $t2 has B[i] addi $t0, $t0, 1 # i ++ bne $t0, $s3, LOOP # go back if not yet 10 times done:li $v0,10 syscall

.data A: .word 12, 34, 67, 1, 45, 90, 11, 33, 67, 19 B: .word 90, 2, 93, 66, 8, 120, 121,11, 33, 9 C: .word 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 .text .globl main main: la $s0, A la $s1, B la $s2, C li $s3, 10 li $t0, 0 # using $t0 as i LOOP:sll $t4, $t0, 2 # $t4 = i * 4 add $t5, $t4,$s0 # $t5 will have the address of A[i] lw $t1, 0($t5) # $t1 has A[i] add $t6, $t4,$s1 # $t6 will have the address of B[i] lw $t2, 0($t6) # $t2 has B[i] slt $t5, $t1, $t2 # set $t5 to be 1 if A[i] < B[i] beq $t5, $0, L1 # if $t5 == 0, goto L1. in this case, A[i] >= B[i] ori $t8, $t1, 0 # setting $t8 to be A[i] j L2 # always remember to jump in an if else! L1: ori $t8, $t2, 0 # setting $t8 to be B[i] L2: addi $t0, $t0, 1 # i ++ bne $t0, $s3, LOOP # go back if not yet 10 times done:li $v0,10 syscall

.data A: .word 12, 34, 67, 1, 45, 90, 11, 33, 67, 19 B: .word 90, 2, 93, 66, 8, 120, 121,11, 33, 9 C: .word 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 .text .globl main main: la $s0, A la $s1, B la $s2, C li $s3, 10 li $t0, 0 # using $t0 as i LOOP:sll $t4, $t0, 2 # $t4 = i * 4 add $t5, $t4,$s0 # $t5 will have the address of A[i] lw $t1, 0($t5) # $t1 has A[i] add $t6, $t4,$s1 # $t6 will have the address of B[i] lw $t2, 0($t6) # $t2 has B[i] slt $t5, $t1, $t2 # set $t5 to be 1 if A[i] < B[i] beq $t5, $0, L1 # if $t5 == 0, goto L1. in this case, A[i] >= B[i] ori $t8, $t1, 0 # setting $t8 to be A[i] j L2 # always remember to jump in an if else! L1: ori $t8, $t2, 0 # setting $t8 to be B[i] L2: add $t6, $t4, $s2 # now $t6 has the address of C[i] sw $t8, 0($t6) # now C[i] has the minimum of A[i] and B[i] addi $t0, $t0, 1 # i ++ bne $t0, $s3, LOOP # go back if not yet 10 times done:li $v0,10 syscall

Loop Example 5 The bubble sort 1/12/2019 week04-3.ppt

The bubble sort Need two loops – just encapsulate one in the other Need to read the elements – done before. Need to compare two numbers – done before Need to swap – not that hard

Getting the first loop done .data A: .word 12, 34, 67, 1, 45, 90, 11, 33, 67, 19 .text .globl main main: la $s7, A # getting the address li $s6, 9 # N-1 li $s0, 0 # i = 0 LOOP1: addi $s0, $s0, 1 # i = i + 1 bne $s0, $s6, LOOP1 # if i != N-1, outer loop again done: li $v0,10 syscall Getting the first loop done

Getting both loop done .data A: .word 12, 34, 67, 1, 45, 90, 11, 33, 67, 19 .text .globl main main: la $s7, A # getting the address li $s6, 9 # N-1 li $s0, 0 # i = 0 LOOP1: li $s1, 0 # j = 0 LOOP2: addi $s1, $s1, 1 # j = j + 1 sub $t7, $s6, $s0 # $t7 will get N-1-i bne $s1, $t7, LOOP2 # if j != N-1-i, inner loop again addi $s0, $s0, 1 # i = i + 1 bne $s0, $s6, LOOP1 # if i != N-1, outer loop again done: li $v0,10 syscall Getting both loop done

Adding the code to read the elements A[j] and A[j+1] .data A: .word 12, 34, 67, 1, 45, 90, 11, 33, 67, 19 .text .globl main main: la $s7, A # getting the address li $s6, 9 # N-1 li $s0, 0 # i = 0 LOOP1: li $s1, 0 # j = 0 LOOP2: sll $t0, $s1, 2 # $t0 = j * 4 add $t0, $t0, $s7 # $t0 is the address of A[j] lw $t1, 0($t0) # $t1 = A[j] lw $t2, 4($t0) # $t2 = A[j+1] addi $s1, $s1, 1 # j = j + 1 sub $t7, $s6, $s0 # $t7 will get N-1-i bne $s1, $t7, LOOP2 # if j != N-1-i, inner loop again addi $s0, $s0, 1 # i = i + 1 bne $s0, $s6, LOOP1 # if i != N-1, outer loop again done: li $v0,10 syscall Adding the code to read the elements A[j] and A[j+1]

Adding the comparison and swapping .data A: .word 12, 34, 67, 1, 45, 90, 11, 33, 67, 19 .text .globl main main: la $s7, A # getting the address li $s6, 9 # N-1 li $s0, 0 # i = 0 LOOP1: li $s1, 0 # j = 0 LOOP2: sll $t0, $s1, 2 # $t0 = j * 4 add $t0, $t0, $s7 # $t0 is the address of A[j] lw $t1, 0($t0) # $t1 = A[j] lw $t2, 4($t0) # $t2 = A[j+1] bgt $t1, $t2, L1 # if A[j] > A[j+1] goto L1, bypass the swapping sw $t1, 4($t0) # do the swap sw $t2, 0($t0) # do the swap L1: addi $s1, $s1, 1 # j = j + 1 sub $t7, $s6, $s0 # $t7 will get N-1-i bne $s1, $t7, LOOP2 # if j != N-1-i, inner loop again addi $s0, $s0, 1 # i = i + 1 bne $s0, $s6, LOOP1 # if i != N-1, outer loop again done: li $v0,10 syscall Adding the comparison and swapping

Pseudo instruction A pseudo instruction is not a real instruction supported by the hardware. It is created to make the coding easier. It is mapped to a unique sequence of real instructions by the assembler. We have seen some: li $s0, 0 # load immediate number, often mapped to ori. la $s7, A # load the address of label A into $s7 bgt $t1, $t2, L1 # branch if $t1 is greater than $t2. ``blt’’ also exits.

In-class exercise -- Loop

Data segment and code segment The code has a data segment and a code (text) segment. The beginning of the data segment in the assembly source code is indicated as .data and followed by several declarations such as A: .word 0,1,2,3,4,5,6,7,8,9 meaning an array of words whose starting address is associated with label ``A.’’ Several notes: It will allocate continuous spaces in the memory for the data .word means everything is 4 bytes save: is a label associated with the address of the first byte allocated. Like the label for the instructions, label for an address is also an address.