MIPS coding.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Branches Two branch instructions:
The University of Adelaide, School of Computer Science
Lecture 9: MIPS Instruction Set
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.
SPRING 2015 QtSpim Demo & Tutorial. 2 By DGP Outline How to write your own MIPS assembly language program How to use QtSpim simulator.
The University of Adelaide, School of Computer Science
SPIM and MIPS programming
Syscall in MIPS Xinhui Hu Yuan Wang.
MIPS Function Continued
Assembly Language Working with the CPU.
1 COMS 361 Computer Organization Title: Instructions Date: 9/28/2004 Lecture Number: 10.
ECE 0142 Recitation #5.
Assembly Process. Machine Code Generation Assembling a program entails translating the assembly language into binary machine code This requires more than.
MIPS Coding. Exercise – the bubble sort 5/8/2015week04-3.ppt2.
MIPS Instruction Set Advantages
19/02/2009CA&O Lecture 05 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: PCSpim Tutorial Engr. Umbreen Sabir Computer Engineering.
First Programming Assignment For MIPS R3000 Processor Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki.
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.
Lecture # 1 SPIM & MIPS Programming. SPIM SPIM is a MIPS32 simulator that reads and executes assembly language program written for SPIM. Platform -Unix,
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.
MIPS I/O and Interrupt.
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.
Control Structures Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Conditional Control Structure if ( i < j ) goto A; else.
MIPS Assembly Language Programming
MIPS Assembly Language Programming
MIPS Instruction Set Advantages
Introduction to Lab #1 José Nelson Amaral.
MIPS I/O and Interrupt.
CS 286 Computer Organization and Architecture
MIPS Procedures.
MIPS Coding Continued.
Computer Architecture & Operations I
MIPS instructions.
Decision Making.
MIPS I/O and Interrupt.
Instructions for Making Decisions
The University of Adelaide, School of Computer Science
MIPS coding.
MIPS Procedures.
Lecture 4: MIPS Instruction Set
Assembler Directives Example program: .data # DATA segment city: .asciiz “Seattle” .align 2 num: .word 2210, 2341, 26, 0x022ec,
MIPS Functions.
MIPS Instruction Encoding
MIPS Functions.
MIPS Instruction Encoding
MIPS Functions.
MIPS coding.
MIPS Procedures.
Review.
MIPS Coding.
MIPS function continued
MIPS Functions.
MIPS Coding.
MIPS I/O and Interrupt.
QtSpim Demo & Tutorial SPRING 2011.
CS 286 Computer Organization and Architecture
Review.
MIPS Coding Continued.
MIPS coding.
MIPS assembly.
Program Assembly.
MIPS Functions.
MIPS function continued
MIPS Functions.
MIPS instructions.
Conditional Branching (beq)
Presentation transcript:

MIPS coding

slt, slti slt $t3, $t1, $t2 slti $t3, $t1, 100 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 1 if $t1 < 100; else clear $t3 to be 0.

Complete MIPS code The text segment in the source code usually starts with .text .globl main main: where ``main’’ is the label associated with the address of the first instruction of the code. And the code usually ends with li $v0,10 # telling the simulator to stop syscall Comment with `#’

SPIM Run codes with SPIM. SPIM is a simulator. Use any editor to write the source file, save it as an .asm file. Run SPIM, load the source file. F10 to step through the code. Monitor how the registers change. F5 to run the code Can set breakpoints for debugging SPIM can be downloaded at http://spimsimulator.sourceforge.net/ Lots of good references online, like https://www.cs.tcd.ie/~waldroj/itral/spim_ref.html

Working with the simulator Can check How the program runs How the instructions are encoded, addressed How to monitor the change of the registers Later, how the memory is used to store data

Some Comments Being able to write if-else, we can have all other fancy things like for loop, while loop…. That is why we do not have an instruction for the for loop or while loop, but we build it from the if-else.

Loop A loop is needed when we need to do something repeatedly It must have A loop body (what you should do in each iteration) A checking point to see if we still need to go on A mechanism to bring us from the end of the loop back to the beginning of the loop (therefore it is called a loop)

Loop example 1 How to count the number of 1s in a register?

Counting the number of 1s .text .globl main main: # counting the number of 1s in register $s0, no need to save it li $s0, 97 li $t0, 0 LOOP: andi $t1, $s0, 1 add $t0, $t0, $t1 srl $s0, $s0, 1 bne $s0, $zero, LOOP done: li $v0, 10 syscall

Loop example 2 How to translate the following to MIPS assembly? 12/2/2018 Loop example 2 How to translate the following to MIPS assembly? We first translate into a C program using if and goto 12/2/2018 week04-3.ppt CDA3100

Compiling a while loop in C 12/2/2018 Compiling a while loop in C Assume that i and k correspond to registers $s3 and $s5 and starting address of array save is in $s6 12/2/2018 week04-3.ppt CDA3100

Compiling a while loop in C 12/2/2018 Compiling a while loop in C Assume that i and k correspond to registers $s3 and $s5 and starting address of array save is in $s6 12/2/2018 week04-3.ppt CDA3100

12/2/2018 While Loop How many instructions will be executed for the following array save? Assume that k = 10 and i = 0 initially 12/2/2018 week04-3.ppt CDA3100

Optimized How many instructions now? Assume k = 10 and i = 0 initially 12/2/2018 Optimized How many instructions now? Assume k = 10 and i = 0 initially 12/2/2018 week04-3.ppt CDA3100

The loop code .data save:.word 10, 10, 10, 10, 10, 11, 12, .text .globl main main: li $s3, 0 li $s5, 10 la $s6, save Loop: sll $t1, $s3, 2 add $t1, $t1, $s6 lw $t0, 0($t1) bne $t0, $s5, Exit addi $s3, $s3, 1 j Loop Exit: done: li $v0, 10 # these two lines are to tell the simulator to stop syscall

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.